From ac835e09984f5626901cf180f0120bc0f8f6a009 Mon Sep 17 00:00:00 2001 From: Sensei Date: Fri, 15 Apr 2022 01:08:08 +0100 Subject: [PATCH] Added ChunkStatuses --- gradle.properties | 2 +- .../feature/ChunkServerNetworkHandler.java | 17 +++++++-- .../chunkdebug/mixins/ChunkHolderMixin.java | 9 ++++- src/main/java/chunkdebug/utils/ChunkData.java | 37 +++++++++++-------- src/main/resources/fabric.mod.json | 2 +- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/gradle.properties b/gradle.properties index 07ef5a3..88d4e16 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G # check available versions on maven for the given minecraft version you are using # Mod Properties - mod_version = 1.0.2 + mod_version = 1.0.3 maven_group = chunk-debug archives_base_name = chunk-debug diff --git a/src/main/java/chunkdebug/feature/ChunkServerNetworkHandler.java b/src/main/java/chunkdebug/feature/ChunkServerNetworkHandler.java index 2e9fda1..5c3454f 100644 --- a/src/main/java/chunkdebug/feature/ChunkServerNetworkHandler.java +++ b/src/main/java/chunkdebug/feature/ChunkServerNetworkHandler.java @@ -15,8 +15,11 @@ import net.minecraft.server.world.ThreadedAnvilChunkStorage; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; +import net.minecraft.util.math.ChunkPos; import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.RegistryKey; +import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.ChunkStatus; import java.util.*; @@ -26,7 +29,7 @@ public class ChunkServerNetworkHandler { HELLO = 0, RELOAD = 15, DATA = 16, - VERSION = 1_0_1; + VERSION = 1_0_3; private final Map validPlayersEnabled = new HashMap<>(); private final Map> serverWorldChunks = new HashMap<>(); @@ -86,9 +89,12 @@ public synchronized void forceReloadChunks() { ThreadedAnvilChunkStorage storage = world.getChunkManager().threadedAnvilChunkStorage; ThreadedAnvilChunkStorage.TicketManager ticketManager = ((ThreadedAnvilChunkStorageAccessor) storage).getTicketManager(); ((ThreadedAnvilChunkStorageAccessor) storage).getChunkHolderMap().values().forEach(chunkHolder -> { + ChunkPos pos = chunkHolder.getPos(); ChunkHolder.LevelType levelType = ChunkHolder.getLevelType(chunkHolder.getLevel()); - ChunkTicketType ticketType = ((IChunkTicketManager) ticketManager).getTicketType(chunkHolder.getPos().toLong()); - chunkDataSet.add(new ChunkData(chunkHolder.getPos(), levelType, ticketType)); + ChunkTicketType ticketType = ((IChunkTicketManager) ticketManager).getTicketType(pos.toLong()); + Chunk chunk = chunkHolder.getCurrentChunk(); + ChunkStatus status = chunk == null ? ChunkStatus.EMPTY : chunk.getStatus(); + chunkDataSet.add(new ChunkData(pos, levelType, status, ticketType)); }); this.updatesInLastTick.get(world).addAll(chunkDataSet); }); @@ -116,18 +122,21 @@ private void sendClientChunkData(ServerPlayerEntity player, ServerWorld world, C int size = chunkDataCollection.size(); long[] chunkPositions = new long[size]; byte[] levelTypes = new byte[size]; + byte[] statusTypes = new byte[size]; byte[] ticketTypes = new byte[size]; int i = 0; for (ChunkData chunkData : chunkDataCollection) { chunkPositions[i] = chunkData.getLongPos(); levelTypes[i] = chunkData.getLevelByte(); + statusTypes[i] = chunkData.getStatusByte(); ticketTypes[i] = chunkData.getTicketByte(); i++; } player.networkHandler.sendPacket(new CustomPayloadS2CPacket( ESSENTIAL_CHANNEL, new PacketByteBuf(Unpooled.buffer()).writeVarInt(DATA) - .writeVarInt(size).writeLongArray(chunkPositions).writeByteArray(levelTypes).writeByteArray(ticketTypes) + .writeVarInt(size).writeLongArray(chunkPositions).writeByteArray(levelTypes) + .writeByteArray(statusTypes).writeByteArray(ticketTypes) .writeString(world.getRegistryKey().getValue().getPath()) )); } diff --git a/src/main/java/chunkdebug/mixins/ChunkHolderMixin.java b/src/main/java/chunkdebug/mixins/ChunkHolderMixin.java index 5a9438d..9dc7a1f 100644 --- a/src/main/java/chunkdebug/mixins/ChunkHolderMixin.java +++ b/src/main/java/chunkdebug/mixins/ChunkHolderMixin.java @@ -8,6 +8,9 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ThreadedAnvilChunkStorage; import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.ChunkStatus; +import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -28,12 +31,16 @@ public static ChunkHolder.LevelType getLevelType(int distance) { throw new AssertionError(); } + @Shadow public abstract @Nullable Chunk getCurrentChunk(); + @Inject(method = "tick", at = @At("RETURN")) private void onTick(ThreadedAnvilChunkStorage chunkStorage, CallbackInfo ci) { ChunkHolder.LevelType levelType = getLevelType(this.level); ServerWorld world = ((ThreadedAnvilChunkStorageAccessor) chunkStorage).getWorld(); ThreadedAnvilChunkStorage.TicketManager ticketManager = ((ThreadedAnvilChunkStorageAccessor) chunkStorage).getTicketManager(); ChunkTicketType ticketType = ((IChunkTicketManager) ticketManager).getTicketType(this.pos.toLong()); - ChunkDebugServer.chunkNetHandler.updateChunkMap(world, new ChunkData(this.pos, levelType, ticketType)); + Chunk chunk = this.getCurrentChunk(); + ChunkStatus status = chunk == null ? ChunkStatus.EMPTY : chunk.getStatus(); + ChunkDebugServer.chunkNetHandler.updateChunkMap(world, new ChunkData(this.pos, levelType, status, ticketType)); } } diff --git a/src/main/java/chunkdebug/utils/ChunkData.java b/src/main/java/chunkdebug/utils/ChunkData.java index 76e9dcd..e9b2b23 100644 --- a/src/main/java/chunkdebug/utils/ChunkData.java +++ b/src/main/java/chunkdebug/utils/ChunkData.java @@ -3,15 +3,18 @@ import net.minecraft.server.world.ChunkHolder; import net.minecraft.server.world.ChunkTicketType; import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.chunk.ChunkStatus; public class ChunkData { private final ChunkPos chunkPos; private final ChunkHolder.LevelType levelType; + private final byte statusCode; private final byte ticketCode; - public ChunkData(ChunkPos chunkPos, ChunkHolder.LevelType levelType, ChunkTicketType ticketType) { + public ChunkData(ChunkPos chunkPos, ChunkHolder.LevelType levelType, ChunkStatus status, ChunkTicketType ticketType) { this.chunkPos = chunkPos; this.levelType = levelType; + this.statusCode = (byte) status.getIndex(); this.ticketCode = getTicketCode(ticketType); } @@ -27,26 +30,30 @@ public byte getLevelByte() { return (byte) this.levelType.ordinal(); } + public byte getStatusByte() { + return this.statusCode; + } + public byte getTicketByte() { return this.ticketCode; } private static byte getTicketCode(ChunkTicketType chunkTicketType) { - if (chunkTicketType == null) { - return 0; + if (chunkTicketType != null) { + return (byte) switch (chunkTicketType.toString()) { + default -> 0; + case "start" -> 1; + case "dragon" -> 2; + case "player" -> 3; + case "forced" -> 4; + case "light" -> 5; + case "portal" -> 6; + case "post_teleport" -> 7; + case "chonk" -> 8; + case "unknown" -> 9; + }; } - return (byte) switch (chunkTicketType.toString()) { - default -> 0; - case "start" -> 1; - case "dragon" -> 2; - case "player" -> 3; - case "forced" -> 4; - case "light" -> 5; - case "portal" -> 6; - case "post_teleport" -> 7; - case "chonk" -> 8; - case "unknown" -> 9; - }; + return 0; } @Override diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 53bd188..a444463 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "id": "chunkdebug", - "version": "1.0.2", + "version": "1.0.3", "name": "Chunk Debug", "description": "Chunk Debug server side support",