From 3858a839723e815ed47a1824c530a08449631d5d Mon Sep 17 00:00:00 2001 From: RK_01 <50594595+RaphiMC@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:50:36 +0100 Subject: [PATCH] Small code cleanup (#4213) --- .../blockentities/FlowerPotHandler.java | 4 ++-- .../storage/ChunkLightStorage.java | 17 +++++++---------- .../rewriter/WorldPacketRewriter1_9.java | 12 ++++++------ .../v1_8to1_9/storage/ClientWorld1_9.java | 7 ++----- .../v1_8to1_9/storage/CommandBlockStorage.java | 6 +++--- 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/FlowerPotHandler.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/FlowerPotHandler.java index 97bff3d33..5c95f2a89 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/FlowerPotHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_12_2to1_13/provider/blockentities/FlowerPotHandler.java @@ -22,12 +22,12 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.BlockEntityProvider; import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Pair; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler { // Object -> string (id without namespace) or byte (numeric id) - private static final Map, Integer> flowers = new ConcurrentHashMap<>(); + private static final Map, Integer> flowers = new HashMap<>(); static { register("air", (byte) 0, (byte) 0, 5265); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/storage/ChunkLightStorage.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/storage/ChunkLightStorage.java index 4681e67bb..672744728 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/storage/ChunkLightStorage.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_17_1to1_18/storage/ChunkLightStorage.java @@ -18,6 +18,7 @@ package com.viaversion.viaversion.protocols.v1_17_1to1_18.storage; import com.viaversion.viaversion.api.connection.StorableObject; +import com.viaversion.viaversion.api.minecraft.ChunkPosition; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -30,27 +31,27 @@ public final class ChunkLightStorage implements StorableObject { private final Set loadedChunks = new HashSet<>(); public void storeLight(final int x, final int z, final ChunkLight chunkLight) { - lightPackets.put(getChunkSectionIndex(x, z), chunkLight); + lightPackets.put(ChunkPosition.chunkKey(x, z), chunkLight); } public @Nullable ChunkLight removeLight(final int x, final int z) { - return lightPackets.remove(getChunkSectionIndex(x, z)); + return lightPackets.remove(ChunkPosition.chunkKey(x, z)); } public @Nullable ChunkLight getLight(final int x, final int z) { - return lightPackets.get(getChunkSectionIndex(x, z)); + return lightPackets.get(ChunkPosition.chunkKey(x, z)); } public boolean addLoadedChunk(final int x, final int z) { - return loadedChunks.add(getChunkSectionIndex(x, z)); + return loadedChunks.add(ChunkPosition.chunkKey(x, z)); } public boolean isLoaded(final int x, final int z) { - return loadedChunks.contains(getChunkSectionIndex(x, z)); + return loadedChunks.contains(ChunkPosition.chunkKey(x, z)); } public void clear(final int x, final int z) { - final long index = getChunkSectionIndex(x, z); + final long index = ChunkPosition.chunkKey(x, z); lightPackets.remove(index); loadedChunks.remove(index); } @@ -60,10 +61,6 @@ public final class ChunkLightStorage implements StorableObject { lightPackets.clear(); } - private long getChunkSectionIndex(final int x, final int z) { - return ((x & 0x3FFFFFFL) << 38) | (z & 0x3FFFFFFL); - } - public record ChunkLight(boolean trustEdges, long[] skyLightMask, long[] blockLightMask, long[] emptySkyLightMask, long[] emptyBlockLightMask, byte[][] skyLight, byte[][] blockLight) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java index d0bb3c3a8..88395f357 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/rewriter/WorldPacketRewriter1_9.java @@ -21,8 +21,8 @@ import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.StringTag; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.minecraft.BlockFace; -import com.viaversion.viaversion.api.minecraft.ClientWorld; import com.viaversion.viaversion.api.minecraft.BlockPosition; +import com.viaversion.viaversion.api.minecraft.ChunkPosition; import com.viaversion.viaversion.api.minecraft.chunks.BaseChunk; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; @@ -133,7 +133,7 @@ public class WorldPacketRewriter1_9 { ClientWorld1_9 clientWorld = wrapper.user().getClientWorld(Protocol1_8To1_9.class); Chunk chunk = wrapper.read(ChunkType1_8.forEnvironment(clientWorld.getEnvironment())); - long chunkHash = ClientWorld1_9.toLong(chunk.getX(), chunk.getZ()); + long chunkHash = ChunkPosition.chunkKey(chunk.getX(), chunk.getZ()); // Check if the chunk should be handled as an unload packet if (chunk.isFullChunk() && chunk.getBitmask() == 0) { @@ -152,7 +152,7 @@ public class WorldPacketRewriter1_9 { for (BlockFace face : BlockFace.HORIZONTAL) { int chunkX = chunk.getX() + face.modX(); int chunkZ = chunk.getZ() + face.modZ(); - if (!clientWorld.getLoadedChunks().contains(ClientWorld1_9.toLong(chunkX, chunkZ))) { + if (!clientWorld.getLoadedChunks().contains(ChunkPosition.chunkKey(chunkX, chunkZ))) { PacketWrapper unloadChunk = wrapper.create(ClientboundPackets1_9.FORGET_LEVEL_CHUNK); unloadChunk.write(Types.INT, chunkX); unloadChunk.write(Types.INT, chunkZ); @@ -171,7 +171,7 @@ public class WorldPacketRewriter1_9 { for (BlockFace face : BlockFace.HORIZONTAL) { int chunkX = chunk.getX() + face.modX(); int chunkZ = chunk.getZ() + face.modZ(); - if (!clientWorld.getLoadedChunks().contains(ClientWorld1_9.toLong(chunkX, chunkZ))) { + if (!clientWorld.getLoadedChunks().contains(ChunkPosition.chunkKey(chunkX, chunkZ))) { PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.LEVEL_CHUNK); Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>()); emptyChunk.write(chunkType, c); @@ -194,14 +194,14 @@ public class WorldPacketRewriter1_9 { chunkData.write(chunkType, chunk); chunkData.send(Protocol1_8To1_9.class); - clientWorld.getLoadedChunks().add(ClientWorld1_9.toLong(chunk.getX(), chunk.getZ())); + clientWorld.getLoadedChunks().add(ChunkPosition.chunkKey(chunk.getX(), chunk.getZ())); // Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk if (Via.getConfig().isChunkBorderFix()) { for (BlockFace face : BlockFace.HORIZONTAL) { int chunkX = chunk.getX() + face.modX(); int chunkZ = chunk.getZ() + face.modZ(); - if (!clientWorld.getLoadedChunks().contains(ClientWorld1_9.toLong(chunkX, chunkZ))) { + if (!clientWorld.getLoadedChunks().contains(ChunkPosition.chunkKey(chunkX, chunkZ))) { PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.LEVEL_CHUNK); Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>()); emptyChunk.write(chunkType, c); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/ClientWorld1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/ClientWorld1_9.java index 7defcd00e..953976af7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/ClientWorld1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/ClientWorld1_9.java @@ -17,16 +17,13 @@ */ package com.viaversion.viaversion.protocols.v1_8to1_9.storage; -import com.google.common.collect.Sets; import com.viaversion.viaversion.api.minecraft.ClientWorld; +import java.util.HashSet; import java.util.Set; public class ClientWorld1_9 extends ClientWorld { - private final Set loadedChunks = Sets.newConcurrentHashSet(); - public static long toLong(int msw, int lsw) { - return ((long) msw << 32) + lsw + 2147483648L; - } + private final Set loadedChunks = new HashSet<>(); public Set getLoadedChunks() { return loadedChunks; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/CommandBlockStorage.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/CommandBlockStorage.java index 994d69d48..7ab925850 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/CommandBlockStorage.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_8to1_9/storage/CommandBlockStorage.java @@ -22,12 +22,12 @@ import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.minecraft.BlockPosition; import com.viaversion.viaversion.util.Pair; +import java.util.HashMap; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; public class CommandBlockStorage implements StorableObject { - private final Map, Map> storedCommandBlocks = new ConcurrentHashMap<>(); + private final Map, Map> storedCommandBlocks = new HashMap<>(); private boolean permissions; public void unloadChunk(int x, int z) { @@ -39,7 +39,7 @@ public class CommandBlockStorage implements StorableObject { Pair chunkPos = getChunkCoords(position); if (!storedCommandBlocks.containsKey(chunkPos)) { - storedCommandBlocks.put(chunkPos, new ConcurrentHashMap<>()); + storedCommandBlocks.put(chunkPos, new HashMap<>()); } Map blocks = storedCommandBlocks.get(chunkPos);