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;
}
/**
* 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;

View File

@ -132,13 +132,13 @@ public class Protocol1_10To1_11 extends AbstractProtocol<ClientboundPackets1_9_3
if (effectID == 2002) {
int data = packetWrapper.get(Types.INT, 1);
boolean isInstant = false;
Pair<Integer, Boolean> 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);

View File

@ -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 {
//<oldData> to <newData, isInstant> mapping
private static final Int2ObjectMap<Pair<Integer, Boolean>> POTIONS = new Int2ObjectOpenHashMap<>(37);
private static final Int2ObjectMap<PotionData> POTIONS = new Int2ObjectOpenHashMap<>(37);
static {
addRewrite(0, 3694022, false);
@ -66,12 +65,14 @@ public class PotionColorMappings1_11 {
addRewrite(36, 3381504, false);
}
public static Pair<Integer, Boolean> 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) {
}
}

View File

@ -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<Pair<?, Byte>, Integer> flowers = new HashMap<>();
private static final int EMPTY_POT = 5265;
private static final Map<String, Byte> STRING_TO_BYTE_ID = new HashMap<>();
private static final Map<IntIdPair, Integer> 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) {
}
}

View File

@ -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<Integer, Integer> 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);
}
}

View File

@ -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<Pair<Integer, Integer>, Map<BlockPosition, CompoundTag>> storedCommandBlocks = new HashMap<>();
private final Map<Long, Map<BlockPosition, CompoundTag>> storedCommandBlocks = new HashMap<>();
private boolean permissions;
public void unloadChunk(int x, int z) {
Pair<Integer, Integer> chunkPos = new Pair<>(x, z);
storedCommandBlocks.remove(chunkPos);
storedCommandBlocks.remove(ChunkPosition.chunkKey(x, z));
}
public void addOrUpdateBlock(BlockPosition position, CompoundTag tag) {
Pair<Integer, Integer> chunkPos = getChunkCoords(position);
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;
}
long chunkKey = ChunkPosition.chunkKeyForBlock(position.x(), position.z());
Map<BlockPosition, CompoundTag> blocks = storedCommandBlocks.computeIfAbsent(chunkKey, k -> new HashMap<>());
blocks.put(position, tag);
}
private Pair<Integer, Integer> getChunkCoords(BlockPosition position) {
int chunkX = Math.floorDiv(position.x(), 16);
int chunkZ = Math.floorDiv(position.z(), 16);
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)
Map<BlockPosition, CompoundTag> 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));