Skip to content

Commit

Permalink
Added ChunkStatuses
Browse files Browse the repository at this point in the history
  • Loading branch information
senseiwells committed Apr 15, 2022
1 parent 6f4e651 commit ac835e0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/chunkdebug/feature/ChunkServerNetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -26,7 +29,7 @@ public class ChunkServerNetworkHandler {
HELLO = 0,
RELOAD = 15,
DATA = 16,
VERSION = 1_0_1;
VERSION = 1_0_3;

private final Map<ServerPlayerEntity, ServerWorld> validPlayersEnabled = new HashMap<>();
private final Map<ServerWorld, Set<ChunkData>> serverWorldChunks = new HashMap<>();
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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())
));
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/chunkdebug/mixins/ChunkHolderMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
37 changes: 22 additions & 15 deletions src/main/java/chunkdebug/utils/ChunkData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ac835e0

Please sign in to comment.