-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/kasuga/lib/core/menu/locator/AbstractChunkBasedLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package kasuga.lib.core.menu.locator; | ||
|
||
import net.minecraft.core.Registry; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.server.level.ServerChunkCache; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.level.ChunkPos; | ||
import net.minecraft.world.level.Level; | ||
|
||
import java.util.List; | ||
|
||
public abstract class AbstractChunkBasedLocator extends MenuLocator implements IChunkBasedLocator { | ||
protected Level level; | ||
protected ResourceKey<Level> levelResourceKey; | ||
protected final ChunkPos chunkPos; | ||
|
||
protected AbstractChunkBasedLocator(MenuLocatorType<?> type, ChunkPos chunkPos) { | ||
super(type); | ||
this.chunkPos = chunkPos; | ||
} | ||
|
||
protected AbstractChunkBasedLocator(MenuLocatorType<?> type, FriendlyByteBuf byteBuf) { | ||
super(type); | ||
this.chunkPos = byteBuf.readChunkPos(); | ||
this.levelResourceKey = byteBuf.readResourceKey(Registry.DIMENSION_REGISTRY); | ||
} | ||
|
||
@Override | ||
public void write(FriendlyByteBuf byteBuf) { | ||
byteBuf.writeChunkPos(chunkPos); | ||
byteBuf.writeResourceKey(levelResourceKey); | ||
} | ||
|
||
public void withLevel(Level level) { | ||
if (this.levelResourceKey != null) { | ||
return; | ||
} | ||
this.level = level; | ||
this.levelResourceKey = level.dimension(); | ||
} | ||
|
||
@Override | ||
public void enable(LocatedMenuManager manager) { | ||
super.enable(manager); | ||
this.listen(); | ||
if (!level.hasChunk(chunkPos.x, chunkPos.z)) { | ||
return; | ||
} | ||
this.broadcastEnable(); | ||
} | ||
|
||
protected void broadcastEnable() { | ||
List<ServerPlayer> players = | ||
((ServerChunkCache) level | ||
.getChunkSource() | ||
) | ||
.chunkMap | ||
.getPlayers(chunkPos, false); | ||
if (players == null) | ||
return; | ||
|
||
for (ServerPlayer player : players) { | ||
sendUpTo(player.connection.getConnection()); | ||
} | ||
} | ||
|
||
protected void listen() { | ||
ServerChunkMenuLocatorManager.register(this); | ||
} | ||
|
||
protected void unlisten() { | ||
ServerChunkMenuLocatorManager.unregister(this); | ||
} | ||
|
||
@Override | ||
public void disable(LocatedMenuManager manager) { | ||
this.broadcastDisable(); | ||
super.disable(manager); | ||
this.unlisten(); | ||
} | ||
|
||
@Override | ||
public ChunkPos getPosition() { | ||
return chunkPos; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/kasuga/lib/core/menu/locator/AbstractDynamicChunkBasedLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package kasuga.lib.core.menu.locator; | ||
|
||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.world.level.ChunkPos; | ||
|
||
public abstract class AbstractDynamicChunkBasedLocator extends AbstractChunkBasedLocator{ | ||
ChunkPos latestPos; | ||
|
||
protected AbstractDynamicChunkBasedLocator(MenuLocatorType<?> type, ChunkPos chunkPos) { | ||
super(type, chunkPos); | ||
latestPos = chunkPos; | ||
} | ||
|
||
protected AbstractDynamicChunkBasedLocator(MenuLocatorType<?> type, FriendlyByteBuf byteBuf) { | ||
super(type, byteBuf); | ||
} | ||
|
||
@Override | ||
public void write(FriendlyByteBuf byteBuf) { | ||
super.write(byteBuf); | ||
} | ||
|
||
@Override | ||
protected void listen() { | ||
ServerChunkMenuLocatorManager.register(this, latestPos); | ||
} | ||
|
||
@Override | ||
protected void unlisten() { | ||
ServerChunkMenuLocatorManager.unregister(this, latestPos); | ||
} | ||
|
||
protected void transfer(ChunkPos newChunk){ | ||
if(this.latestPos == newChunk) | ||
return; | ||
ServerChunkMenuLocatorManager.transfer(this, latestPos, newChunk); | ||
latestPos = newChunk; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/kasuga/lib/core/menu/locator/EntityMenuLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package kasuga.lib.core.menu.locator; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.level.ChunkPos; | ||
|
||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
public class EntityMenuLocator extends AbstractDynamicChunkBasedLocator { | ||
protected final UUID entityId; | ||
protected Entity entity; | ||
protected EntityMenuLocator(MenuLocatorType<?> type, Entity entity) { | ||
super(type, entity.chunkPosition()); | ||
this.entityId = entity.getUUID(); | ||
} | ||
|
||
public EntityMenuLocator(Entity entity) { | ||
super(MenuLocatorTypes.ENTITY, entity.chunkPosition()); | ||
this.entityId = entity.getUUID(); | ||
} | ||
|
||
protected EntityMenuLocator(MenuLocatorType<?> type, FriendlyByteBuf byteBuf) { | ||
super(type, byteBuf); | ||
this.entityId = byteBuf.readUUID(); | ||
} | ||
|
||
public EntityMenuLocator(FriendlyByteBuf byteBuf) { | ||
super(MenuLocatorTypes.ENTITY, byteBuf); | ||
this.entityId = byteBuf.readUUID(); | ||
} | ||
|
||
@Override | ||
public void write(FriendlyByteBuf byteBuf) { | ||
super.write(byteBuf); | ||
byteBuf.writeUUID(entityId); | ||
} | ||
|
||
public void tick(Entity entity){ | ||
transfer(entity.chunkPosition()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (!(object instanceof EntityMenuLocator)) return false; | ||
EntityMenuLocator that = (EntityMenuLocator) object; | ||
return Objects.equals(entityId, that.entityId); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(entityId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/main/java/kasuga/lib/core/menu/locator/MenuLocatorTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package kasuga.lib.core.menu.locator; | ||
|
||
public class MenuLocatorTypes { | ||
public static MenuLocatorType<BlockMenuLocator> CHUNK_MENU = new MenuLocatorType<>(BlockMenuLocator::new); | ||
public static MenuLocatorType<BlockMenuLocator> BLOCK = new MenuLocatorType<>(BlockMenuLocator::new); | ||
public static MenuLocatorType<EntityMenuLocator> ENTITY = new MenuLocatorType<>(EntityMenuLocator::new); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters