diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/ChunkPosition.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/ChunkPosition.java index 400dd6136..5ff063f15 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/ChunkPosition.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/ChunkPosition.java @@ -66,6 +66,17 @@ public final class ChunkPosition { return (long) chunkX & 0xffffffffL | ((long) chunkZ & 0xffffffffL) << 32; } + /** + * Returns a long key for the given block coordinates. + * + * @param x the block X coordinate + * @param z the block Z coordinate + * @return the chunk key + */ + public static long chunkKeyForBlock(final int x, final int z) { + return chunkKey(x >> 4, z >> 4); + } + @Override public boolean equals(final Object o) { if (this == o) return true; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java index b2c2b779f..6e11dd1ba 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/Protocol1_10To1_11.java @@ -132,13 +132,13 @@ public class Protocol1_10To1_11 extends AbstractProtocol newData = PotionColorMappings1_11.getNewData(data); + PotionColorMappings1_11.PotionData newData = PotionColorMappings1_11.getNewData(data); if (newData == null) { getLogger().warning("Received unknown potion data: " + data); data = 0; } else { - data = newData.key(); - isInstant = newData.value(); + data = newData.data(); + isInstant = newData.instant(); } if (isInstant) { packetWrapper.set(Types.INT, 0, 2007); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/data/PotionColorMappings1_11.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/data/PotionColorMappings1_11.java index 184974eb3..d504be5e8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/data/PotionColorMappings1_11.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_10to1_11/data/PotionColorMappings1_11.java @@ -17,14 +17,13 @@ */ package com.viaversion.viaversion.protocols.v1_10to1_11.data; -import com.viaversion.viaversion.util.Pair; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; +import org.checkerframework.checker.nullness.qual.Nullable; public class PotionColorMappings1_11 { - // to mapping - private static final Int2ObjectMap> POTIONS = new Int2ObjectOpenHashMap<>(37); + private static final Int2ObjectMap POTIONS = new Int2ObjectOpenHashMap<>(37); static { addRewrite(0, 3694022, false); @@ -66,12 +65,14 @@ public class PotionColorMappings1_11 { addRewrite(36, 3381504, false); } - public static Pair getNewData(int oldData) { + public static @Nullable PotionData getNewData(int oldData) { return POTIONS.get(oldData); } private static void addRewrite(int oldData, int newData, boolean isInstant) { - POTIONS.put(oldData, new Pair<>(newData, isInstant)); + POTIONS.put(oldData, new PotionData(newData, isInstant)); } + public record PotionData(int data, boolean instant) { + } } 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 5c95f2a89..afc16d189 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 @@ -18,16 +18,19 @@ package com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.blockentities; import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.nbt.tag.NumberTag; +import com.viaversion.nbt.tag.StringTag; +import com.viaversion.nbt.tag.Tag; 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; public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler { - // Object -> string (id without namespace) or byte (numeric id) - private static final Map, Integer> flowers = new HashMap<>(); + private static final int EMPTY_POT = 5265; + private static final Map STRING_TO_BYTE_ID = new HashMap<>(); + private static final Map FLOWERS = new HashMap<>(); static { register("air", (byte) 0, (byte) 0, 5265); @@ -55,37 +58,40 @@ public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler } - public static void register(String identifier, byte numbericBlockId, byte blockData, int newId) { - flowers.put(new Pair<>(identifier, blockData), newId); - flowers.put(new Pair<>(numbericBlockId, blockData), newId); + private static void register(String identifier, byte blockId, byte blockData, int newId) { + STRING_TO_BYTE_ID.put(identifier, blockId); + FLOWERS.put(new IntIdPair(blockId, blockData), newId); } @Override public int transform(UserConnection user, CompoundTag tag) { - Object item = tag.contains("Item") ? tag.get("Item").getValue() : null; - Object data = tag.contains("Data") ? tag.get("Data").getValue() : null; - // Convert item to String without namespace or to Byte - if (item instanceof String) { - item = Key.stripMinecraftNamespace((String) item); - } else if (item instanceof Number) { - item = ((Number) item).byteValue(); - } else { - item = (byte) 0; + Tag itemTag = tag.get("Item"); + byte item = 0; + if (itemTag instanceof StringTag stringTag) { + item = STRING_TO_BYTE_ID.getOrDefault(Key.stripMinecraftNamespace(stringTag.getValue()), (byte) 0); + } else if (itemTag instanceof NumberTag numberTag) { + item = numberTag.asByte(); } - // Convert data to Byte - if (data instanceof Number) { - data = ((Number) data).byteValue(); - } else { - data = (byte) 0; + byte data = 0; + if (tag.get("Data") instanceof NumberTag dataTag) { + data = dataTag.asByte(); } - Integer flower = flowers.get(new Pair<>(item, (byte) data)); - if (flower != null) return flower; - flower = flowers.get(new Pair<>(item, (byte) 0)); - if (flower != null) return flower; + Integer flower = FLOWERS.get(new IntIdPair(item, data)); + if (flower != null) { + return flower; + } - return 5265; // Fallback to empty pot + flower = FLOWERS.get(new IntIdPair(item, (byte) 0)); + if (flower != null) { + return flower; + } + + return EMPTY_POT; // Fallback to empty pot + } + + private record IntIdPair(int id, byte data) { } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/data/MaxStackSize1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/data/MaxStackSize1_20_3.java index f98228853..53557b817 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/data/MaxStackSize1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/data/MaxStackSize1_20_3.java @@ -17,62 +17,62 @@ */ package com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data; -import java.util.HashMap; -import java.util.Map; +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; public final class MaxStackSize1_20_3 { - private static final Map mapping = new HashMap<>(); + private static final Int2IntMap MAPPING = new Int2IntOpenHashMap(); static { fill(521, 537, 1); fill(764, 790, 1); - mapping.put(793, 1); - mapping.put(795, 1); - mapping.put(797, 1); + MAPPING.put(793, 1); + MAPPING.put(795, 1); + MAPPING.put(797, 1); fill(814, 843, 1); - mapping.put(846, 1); - mapping.put(853, 1); + MAPPING.put(846, 1); + MAPPING.put(853, 1); fill(854, 876, 1); fill(883, 905, 16); fill(906, 908, 1); - mapping.put(909, 16); + MAPPING.put(909, 16); fill(911, 917, 1); - mapping.put(924, 16); - mapping.put(927, 1); - mapping.put(928, 1); - mapping.put(930, 1); + MAPPING.put(924, 16); + MAPPING.put(927, 1); + MAPPING.put(928, 1); + MAPPING.put(930, 1); fill(960, 976, 1); - mapping.put(980, 1); - mapping.put(990, 16); - mapping.put(995, 1); - mapping.put(1085, 1); - mapping.put(1086, 16); - mapping.put(1107, 1); - mapping.put(1113, 1); - mapping.put(1116, 16); + MAPPING.put(980, 1); + MAPPING.put(990, 16); + MAPPING.put(995, 1); + MAPPING.put(1085, 1); + MAPPING.put(1086, 16); + MAPPING.put(1107, 1); + MAPPING.put(1113, 1); + MAPPING.put(1116, 16); fill(1117, 1120, 1); - mapping.put(1123, 1); + MAPPING.put(1123, 1); fill(1126, 1141, 16); - mapping.put(1149, 1); - mapping.put(1151, 1); + MAPPING.put(1149, 1); + MAPPING.put(1151, 1); fill(1154, 1156, 1); fill(1159, 1176, 1); - mapping.put(1178, 1); - mapping.put(1182, 1); - mapping.put(1183, 1); + MAPPING.put(1178, 1); + MAPPING.put(1182, 1); + MAPPING.put(1183, 1); fill(1185, 1191, 1); - mapping.put(1212, 16); - mapping.put(1256, 1); + MAPPING.put(1212, 16); + MAPPING.put(1256, 1); } public static int getMaxStackSize(final int identifier) { - return mapping.getOrDefault(identifier, 64); + return MAPPING.getOrDefault(identifier, 64); } private static void fill(final int start, final int end, final int value) { for (int i = start; i <= end; i++) { - mapping.put(i, value); + MAPPING.put(i, value); } } 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 7ab925850..b629092e3 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 @@ -21,53 +21,35 @@ import com.viaversion.nbt.tag.ByteTag; 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 com.viaversion.viaversion.api.minecraft.ChunkPosition; import java.util.HashMap; import java.util.Map; import java.util.Optional; public class CommandBlockStorage implements StorableObject { - private final Map, Map> storedCommandBlocks = new HashMap<>(); + private final Map> storedCommandBlocks = new HashMap<>(); private boolean permissions; public void unloadChunk(int x, int z) { - Pair chunkPos = new Pair<>(x, z); - storedCommandBlocks.remove(chunkPos); + storedCommandBlocks.remove(ChunkPosition.chunkKey(x, z)); } public void addOrUpdateBlock(BlockPosition position, CompoundTag tag) { - Pair chunkPos = getChunkCoords(position); - - if (!storedCommandBlocks.containsKey(chunkPos)) { - storedCommandBlocks.put(chunkPos, new HashMap<>()); - } - - Map blocks = storedCommandBlocks.get(chunkPos); - - if (blocks.containsKey(position) && blocks.get(position).equals(tag)) { - return; - } - + long chunkKey = ChunkPosition.chunkKeyForBlock(position.x(), position.z()); + Map blocks = storedCommandBlocks.computeIfAbsent(chunkKey, k -> new HashMap<>()); blocks.put(position, tag); } - private Pair getChunkCoords(BlockPosition position) { - int chunkX = Math.floorDiv(position.x(), 16); - int chunkZ = Math.floorDiv(position.z(), 16); - - return new Pair<>(chunkX, chunkZ); - } - public Optional getCommandBlock(BlockPosition position) { - Pair chunkCoords = getChunkCoords(position); - - Map blocks = storedCommandBlocks.get(chunkCoords); - if (blocks == null) + Map blocks = storedCommandBlocks.get(ChunkPosition.chunkKeyForBlock(position.x(), position.z())); + if (blocks == null) { return Optional.empty(); + } CompoundTag tag = blocks.get(position); - if (tag == null) + if (tag == null) { return Optional.empty(); + } tag = tag.copy(); tag.put("powered", new ByteTag((byte) 0));