From 65db4a48b449a674fdef6190764c0ba0349f1d8d Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 19 Aug 2020 16:19:18 +0200 Subject: [PATCH] Cleanup --- .../net/minestom/server/instance/Chunk.java | 31 ++++++++++++++++--- .../server/play/MultiBlockChangePacket.java | 5 +-- .../net/minestom/server/utils/MathUtils.java | 4 +-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 6b58f4434..5f005c46a 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -18,6 +18,7 @@ import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.network.packet.server.play.UpdateLightPacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.BlockPosition; +import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.player.PlayerUtils; import net.minestom.server.utils.time.CooldownUtils; @@ -40,6 +41,8 @@ public abstract class Chunk implements Viewable { public static final int CHUNK_SIZE_Z = 16; public static final int CHUNK_SECTION_SIZE = 16; + public static final int CHUNK_SECTION_COUNT = CHUNK_SIZE_Y / CHUNK_SECTION_SIZE; + public static final int BIOME_COUNT = 1024; // 4x4x4 blocks protected Biome[] biomes; @@ -176,7 +179,7 @@ public abstract class Chunk implements Viewable { *

* Use {@link #retrieveDataBuffer(Consumer)} to be sure to get the updated version * - * @return the current cached data packet, can be null or outdated + * @return the last cached data packet, can be null or outdated */ public ByteBuf getFullDataPacket() { return fullDataPacket; @@ -271,7 +274,13 @@ public abstract class Chunk implements Viewable { return "Chunk[" + chunkX + ":" + chunkZ + "]"; } - // UNSAFE + /** + * Send the chunk to {@code player} and add it to the player viewing chunks collection + * and send a {@link PlayerChunkLoadEvent} + * + * @param player the viewer to add + * @return true if the player has just been added to the viewer collection + */ @Override public boolean addViewer(Player player) { final boolean result = this.viewers.add(player); @@ -286,7 +295,13 @@ public abstract class Chunk implements Viewable { return result; } - // UNSAFE + /** + * Remove the chunk to the player viewing chunks collection + * and send a {@link PlayerChunkUnloadEvent} + * + * @param player the viewer to remove + * @return true if the player has just been removed to the viewer collection + */ @Override public boolean removeViewer(Player player) { final boolean result = this.viewers.remove(player); @@ -382,14 +397,22 @@ public abstract class Chunk implements Viewable { public void sendChunkSectionUpdate(int section, Player player) { if (!PlayerUtils.isNettyClient(player)) return; + Check.argCondition(!MathUtils.isBetween(section, 0, CHUNK_SECTION_COUNT), + "The chunk section " + section + " does not exist"); PacketWriterUtils.writeAndSend(player, getChunkSectionUpdatePacket(section)); } + /** + * Get the {@link ChunkDataPacket} to update a single chunk section + * + * @param section the chunk section to update + * @return the {@link ChunkDataPacket} to update a single chunk sectionl + */ protected ChunkDataPacket getChunkSectionUpdatePacket(int section) { ChunkDataPacket chunkDataPacket = getFreshPartialDataPacket(); chunkDataPacket.fullChunk = false; - int[] sections = new int[16]; + int[] sections = new int[CHUNK_SECTION_COUNT]; sections[section] = 1; chunkDataPacket.sections = sections; return chunkDataPacket; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java index 7c7fe7307..77f353ecb 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java @@ -1,4 +1,5 @@ package net.minestom.server.network.packet.server.play; + import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; @@ -18,10 +19,10 @@ public class MultiBlockChangePacket implements ServerPacket { writer.writeLong(ChunkUtils.getChunkIndexWithSection(chunkX, chunkZ, section)); writer.writeBoolean(suppressLightUpdates); if (blockChanges != null) { - int length = blockChanges.length; + final int length = blockChanges.length; writer.writeVarInt(length); for (int i = 0; i < length; i++) { - BlockChange blockChange = blockChanges[i]; + final BlockChange blockChange = blockChanges[i]; writer.writeVarInt(blockChange.newBlockId << 12 | ChunkUtils.getLocalBlockPosAsShort(blockChange.positionX, blockChange.positionY, blockChange.positionZ)); } } else { diff --git a/src/main/java/net/minestom/server/utils/MathUtils.java b/src/main/java/net/minestom/server/utils/MathUtils.java index dfb1e7ce2..5d86994c8 100644 --- a/src/main/java/net/minestom/server/utils/MathUtils.java +++ b/src/main/java/net/minestom/server/utils/MathUtils.java @@ -17,7 +17,7 @@ public final class MathUtils { public static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); - long factor = (long) Math.pow(10, places); + final long factor = (long) Math.pow(10, places); value = value * factor; long tmp = Math.round(value); return (double) tmp / factor; @@ -26,7 +26,7 @@ public final class MathUtils { public static float round(float value, int places) { if (places < 0) throw new IllegalArgumentException(); - long factor = (long) Math.pow(10, places); + final long factor = (long) Math.pow(10, places); value = value * factor; long tmp = Math.round(value); return (float) tmp / factor;