Small cleanup here and there

This commit is contained in:
Nassim Jahnke 2024-11-03 17:12:29 +01:00
parent d48d0a21db
commit 157a1fe249
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
6 changed files with 92 additions and 92 deletions

View File

@ -66,6 +66,17 @@ public final class ChunkPosition {
return (long) chunkX & 0xffffffffL | ((long) chunkZ & 0xffffffffL) << 32; 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 @Override
public boolean equals(final Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -132,13 +132,13 @@ public class Protocol1_10To1_11 extends AbstractProtocol<ClientboundPackets1_9_3
if (effectID == 2002) { if (effectID == 2002) {
int data = packetWrapper.get(Types.INT, 1); int data = packetWrapper.get(Types.INT, 1);
boolean isInstant = false; boolean isInstant = false;
Pair<Integer, Boolean> newData = PotionColorMappings1_11.getNewData(data); PotionColorMappings1_11.PotionData newData = PotionColorMappings1_11.getNewData(data);
if (newData == null) { if (newData == null) {
getLogger().warning("Received unknown potion data: " + data); getLogger().warning("Received unknown potion data: " + data);
data = 0; data = 0;
} else { } else {
data = newData.key(); data = newData.data();
isInstant = newData.value(); isInstant = newData.instant();
} }
if (isInstant) { if (isInstant) {
packetWrapper.set(Types.INT, 0, 2007); packetWrapper.set(Types.INT, 0, 2007);

View File

@ -17,14 +17,13 @@
*/ */
package com.viaversion.viaversion.protocols.v1_10to1_11.data; 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.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
public class PotionColorMappings1_11 { public class PotionColorMappings1_11 {
//<oldData> to <newData, isInstant> mapping private static final Int2ObjectMap<PotionData> POTIONS = new Int2ObjectOpenHashMap<>(37);
private static final Int2ObjectMap<Pair<Integer, Boolean>> POTIONS = new Int2ObjectOpenHashMap<>(37);
static { static {
addRewrite(0, 3694022, false); addRewrite(0, 3694022, false);
@ -66,12 +65,14 @@ public class PotionColorMappings1_11 {
addRewrite(36, 3381504, false); addRewrite(36, 3381504, false);
} }
public static Pair<Integer, Boolean> getNewData(int oldData) { public static @Nullable PotionData getNewData(int oldData) {
return POTIONS.get(oldData); return POTIONS.get(oldData);
} }
private static void addRewrite(int oldData, int newData, boolean isInstant) { 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) {
}
} }

View File

@ -18,16 +18,19 @@
package com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.blockentities; package com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.blockentities;
import com.viaversion.nbt.tag.CompoundTag; 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.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.BlockEntityProvider; import com.viaversion.viaversion.protocols.v1_12_2to1_13.provider.BlockEntityProvider;
import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.Pair;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler { public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler {
// Object -> string (id without namespace) or byte (numeric id) private static final int EMPTY_POT = 5265;
private static final Map<Pair<?, Byte>, Integer> flowers = new HashMap<>(); private static final Map<String, Byte> STRING_TO_BYTE_ID = new HashMap<>();
private static final Map<IntIdPair, Integer> FLOWERS = new HashMap<>();
static { static {
register("air", (byte) 0, (byte) 0, 5265); 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) { private static void register(String identifier, byte blockId, byte blockData, int newId) {
flowers.put(new Pair<>(identifier, blockData), newId); STRING_TO_BYTE_ID.put(identifier, blockId);
flowers.put(new Pair<>(numbericBlockId, blockData), newId); FLOWERS.put(new IntIdPair(blockId, blockData), newId);
} }
@Override @Override
public int transform(UserConnection user, CompoundTag tag) { 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 // Convert item to String without namespace or to Byte
if (item instanceof String) { Tag itemTag = tag.get("Item");
item = Key.stripMinecraftNamespace((String) item); byte item = 0;
} else if (item instanceof Number) { if (itemTag instanceof StringTag stringTag) {
item = ((Number) item).byteValue(); item = STRING_TO_BYTE_ID.getOrDefault(Key.stripMinecraftNamespace(stringTag.getValue()), (byte) 0);
} else { } else if (itemTag instanceof NumberTag numberTag) {
item = (byte) 0; item = numberTag.asByte();
} }
// Convert data to Byte byte data = 0;
if (data instanceof Number) { if (tag.get("Data") instanceof NumberTag dataTag) {
data = ((Number) data).byteValue(); data = dataTag.asByte();
} else {
data = (byte) 0;
} }
Integer flower = flowers.get(new Pair<>(item, (byte) data)); Integer flower = FLOWERS.get(new IntIdPair(item, data));
if (flower != null) return flower; if (flower != null) {
flower = flowers.get(new Pair<>(item, (byte) 0)); return flower;
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) {
} }
} }

View File

@ -17,62 +17,62 @@
*/ */
package com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data; package com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data;
import java.util.HashMap; import it.unimi.dsi.fastutil.ints.Int2IntMap;
import java.util.Map; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
public final class MaxStackSize1_20_3 { public final class MaxStackSize1_20_3 {
private static final Map<Integer, Integer> mapping = new HashMap<>(); private static final Int2IntMap MAPPING = new Int2IntOpenHashMap();
static { static {
fill(521, 537, 1); fill(521, 537, 1);
fill(764, 790, 1); fill(764, 790, 1);
mapping.put(793, 1); MAPPING.put(793, 1);
mapping.put(795, 1); MAPPING.put(795, 1);
mapping.put(797, 1); MAPPING.put(797, 1);
fill(814, 843, 1); fill(814, 843, 1);
mapping.put(846, 1); MAPPING.put(846, 1);
mapping.put(853, 1); MAPPING.put(853, 1);
fill(854, 876, 1); fill(854, 876, 1);
fill(883, 905, 16); fill(883, 905, 16);
fill(906, 908, 1); fill(906, 908, 1);
mapping.put(909, 16); MAPPING.put(909, 16);
fill(911, 917, 1); fill(911, 917, 1);
mapping.put(924, 16); MAPPING.put(924, 16);
mapping.put(927, 1); MAPPING.put(927, 1);
mapping.put(928, 1); MAPPING.put(928, 1);
mapping.put(930, 1); MAPPING.put(930, 1);
fill(960, 976, 1); fill(960, 976, 1);
mapping.put(980, 1); MAPPING.put(980, 1);
mapping.put(990, 16); MAPPING.put(990, 16);
mapping.put(995, 1); MAPPING.put(995, 1);
mapping.put(1085, 1); MAPPING.put(1085, 1);
mapping.put(1086, 16); MAPPING.put(1086, 16);
mapping.put(1107, 1); MAPPING.put(1107, 1);
mapping.put(1113, 1); MAPPING.put(1113, 1);
mapping.put(1116, 16); MAPPING.put(1116, 16);
fill(1117, 1120, 1); fill(1117, 1120, 1);
mapping.put(1123, 1); MAPPING.put(1123, 1);
fill(1126, 1141, 16); fill(1126, 1141, 16);
mapping.put(1149, 1); MAPPING.put(1149, 1);
mapping.put(1151, 1); MAPPING.put(1151, 1);
fill(1154, 1156, 1); fill(1154, 1156, 1);
fill(1159, 1176, 1); fill(1159, 1176, 1);
mapping.put(1178, 1); MAPPING.put(1178, 1);
mapping.put(1182, 1); MAPPING.put(1182, 1);
mapping.put(1183, 1); MAPPING.put(1183, 1);
fill(1185, 1191, 1); fill(1185, 1191, 1);
mapping.put(1212, 16); MAPPING.put(1212, 16);
mapping.put(1256, 1); MAPPING.put(1256, 1);
} }
public static int getMaxStackSize(final int identifier) { 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) { private static void fill(final int start, final int end, final int value) {
for (int i = start; i <= end; i++) { for (int i = start; i <= end; i++) {
mapping.put(i, value); MAPPING.put(i, value);
} }
} }

View File

@ -21,53 +21,35 @@ import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.api.minecraft.BlockPosition; 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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class CommandBlockStorage implements StorableObject { public class CommandBlockStorage implements StorableObject {
private final Map<Pair<Integer, Integer>, Map<BlockPosition, CompoundTag>> storedCommandBlocks = new HashMap<>(); private final Map<Long, Map<BlockPosition, CompoundTag>> storedCommandBlocks = new HashMap<>();
private boolean permissions; private boolean permissions;
public void unloadChunk(int x, int z) { public void unloadChunk(int x, int z) {
Pair<Integer, Integer> chunkPos = new Pair<>(x, z); storedCommandBlocks.remove(ChunkPosition.chunkKey(x, z));
storedCommandBlocks.remove(chunkPos);
} }
public void addOrUpdateBlock(BlockPosition position, CompoundTag tag) { public void addOrUpdateBlock(BlockPosition position, CompoundTag tag) {
Pair<Integer, Integer> chunkPos = getChunkCoords(position); long chunkKey = ChunkPosition.chunkKeyForBlock(position.x(), position.z());
Map<BlockPosition, CompoundTag> blocks = storedCommandBlocks.computeIfAbsent(chunkKey, k -> new HashMap<>());
if (!storedCommandBlocks.containsKey(chunkPos)) {
storedCommandBlocks.put(chunkPos, new HashMap<>());
}
Map<BlockPosition, CompoundTag> blocks = storedCommandBlocks.get(chunkPos);
if (blocks.containsKey(position) && blocks.get(position).equals(tag)) {
return;
}
blocks.put(position, tag); blocks.put(position, tag);
} }
private Pair<Integer, Integer> getChunkCoords(BlockPosition position) { public Optional<CompoundTag> getCommandBlock(BlockPosition position) {
int chunkX = Math.floorDiv(position.x(), 16); Map<BlockPosition, CompoundTag> blocks = storedCommandBlocks.get(ChunkPosition.chunkKeyForBlock(position.x(), position.z()));
int chunkZ = Math.floorDiv(position.z(), 16); if (blocks == null) {
return Optional.empty();
return new Pair<>(chunkX, chunkZ);
} }
public Optional<CompoundTag> getCommandBlock(BlockPosition position) {
Pair<Integer, Integer> chunkCoords = getChunkCoords(position);
Map<BlockPosition, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords);
if (blocks == null)
return Optional.empty();
CompoundTag tag = blocks.get(position); CompoundTag tag = blocks.get(position);
if (tag == null) if (tag == null) {
return Optional.empty(); return Optional.empty();
}
tag = tag.copy(); tag = tag.copy();
tag.put("powered", new ByteTag((byte) 0)); tag.put("powered", new ByteTag((byte) 0));