From 30e10bb6450fc9e72d23feedf1e4ba26e12c5bc5 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Mon, 8 Jun 2020 12:27:34 +0200 Subject: [PATCH] Create soft wrapper for FU IntSet and Int2IntMap --- common/pom.xml | 8 + .../api/rewriters/MetadataRewriter.java | 8 +- .../blockconnections/ConnectionData.java | 4 +- .../FlowerConnectionHandler.java | 10 +- .../RedstoneConnectionHandler.java | 13 +- .../data/EntityTypeRewriter.java | 107 ----------- .../MetadataRewriter1_13To1_12_2.java | 8 +- .../packets/WorldPackets.java | 24 +-- .../storage/BlockConnectionStorage.java | 23 ++- .../storage/BlockStorage.java | 29 +-- .../data/EntityTypeRewriter.java | 109 ----------- .../data/MappingData.java | 14 +- .../MetadataRewriter1_14To1_13_2.java | 8 +- .../packets/EntityPackets.java | 3 +- .../protocol1_9to1_8/ItemRewriter.java | 10 +- .../protocol1_9to1_8/sounds/Effect.java | 15 +- .../util/fastutil/CollectionUtil.java | 173 ++++++++++++++++++ .../ViaVersion/util/fastutil/IntMap.java | 35 ++++ .../ViaVersion/util/fastutil/IntSet.java | 22 +++ 19 files changed, 324 insertions(+), 299 deletions(-) delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityTypeRewriter.java delete mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/EntityTypeRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/util/fastutil/CollectionUtil.java create mode 100644 common/src/main/java/us/myles/ViaVersion/util/fastutil/IntMap.java create mode 100644 common/src/main/java/us/myles/ViaVersion/util/fastutil/IntSet.java diff --git a/common/pom.xml b/common/pom.xml index 4874d16c3..4d79ce92a 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -19,5 +19,13 @@ 1.18 compile + + + + it.unimi.dsi + fastutil + 8.3.1 + provided + diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java index de6db2ebd..07b3c5599 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java @@ -12,6 +12,8 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.storage.EntityTracker; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntMap; import java.util.ArrayList; import java.util.Collections; @@ -23,7 +25,7 @@ import java.util.logging.Logger; public abstract class MetadataRewriter { private final Class entityTrackerClass; private final Protocol protocol; - private Map typeMapping; + private IntMap typeMapping; protected MetadataRewriter(Protocol protocol, Class entityTrackerClass) { this.protocol = protocol; @@ -182,7 +184,7 @@ public abstract class MetadataRewriter { } public & EntityType> void mapTypes(EntityType[] oldTypes, Class newTypeClass) { - if (typeMapping == null) typeMapping = new HashMap<>(oldTypes.length); + if (typeMapping == null) typeMapping = CollectionUtil.createIntMap(oldTypes.length); for (EntityType oldType : oldTypes) { try { T newType = Enum.valueOf(newTypeClass, oldType.name()); @@ -197,7 +199,7 @@ public abstract class MetadataRewriter { } public void mapType(EntityType oldType, EntityType newType) { - if (typeMapping == null) typeMapping = new HashMap<>(); + if (typeMapping == null) typeMapping = CollectionUtil.createIntMap(); typeMapping.put(oldType.getId(), newType.getId()); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java index 54932df94..4716d3dd4 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java @@ -16,6 +16,8 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntSet; import java.util.*; import java.util.Map.Entry; @@ -27,7 +29,7 @@ public class ConnectionData { static Map keyToId = new HashMap<>(); static Map connectionHandlerMap = new HashMap<>(); static Map blockConnectionData = new HashMap<>(); - static Set occludingStates = new HashSet<>(); + static IntSet occludingStates = CollectionUtil.createIntSet(377); public static void update(UserConnection user, Position position) { for (BlockFace face : BlockFace.values()) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/FlowerConnectionHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/FlowerConnectionHandler.java index 2f181262e..14eac24c9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/FlowerConnectionHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/FlowerConnectionHandler.java @@ -4,15 +4,15 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.BlockFace; import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntMap; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Set; public class FlowerConnectionHandler extends ConnectionHandler { - private static final Map flowers = new HashMap<>(); + private static final IntMap flowers = CollectionUtil.createIntMap(); static ConnectionData.ConnectorInitAction init() { final Set baseFlower = new HashSet<>(); @@ -38,8 +38,8 @@ public class FlowerConnectionHandler extends ConnectionHandler { @Override public int connect(UserConnection user, Position position, int blockState) { int blockBelowId = getBlockData(user, position.getRelative(BlockFace.BOTTOM)); - Integer connectBelow = flowers.get(blockBelowId); - if (connectBelow != null) { + int connectBelow = flowers.get(blockBelowId); + if (connectBelow != -1) { int blockAboveId = getBlockData(user, position.getRelative(BlockFace.TOP)); if (Via.getConfig().isStemWhenBlockAbove()) { if (blockAboveId == 0) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/RedstoneConnectionHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/RedstoneConnectionHandler.java index bfb3ef5ac..a09528880 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/RedstoneConnectionHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/RedstoneConnectionHandler.java @@ -3,16 +3,16 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.BlockFace; import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntMap; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Set; public class RedstoneConnectionHandler extends ConnectionHandler { private static final Set redstone = new HashSet<>(); - private static final Map connectedBlockStates = new HashMap<>(); - private static final Map powerMappings = new HashMap<>(); + private static final IntMap connectedBlockStates = CollectionUtil.createIntMap(1296); + private static final IntMap powerMappings = CollectionUtil.createIntMap(1296); static ConnectionData.ConnectorInitAction init() { final RedstoneConnectionHandler connectionHandler = new RedstoneConnectionHandler(); @@ -22,7 +22,7 @@ public class RedstoneConnectionHandler extends ConnectionHandler { redstone.add(blockData.getSavedBlockStateId()); ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler); connectedBlockStates.put(getStates(blockData), blockData.getSavedBlockStateId()); - powerMappings.put(blockData.getSavedBlockStateId(), Integer.valueOf(blockData.getValue("power"))); + powerMappings.put(blockData.getSavedBlockStateId(), Integer.parseInt(blockData.getValue("power"))); }; } @@ -57,8 +57,7 @@ public class RedstoneConnectionHandler extends ConnectionHandler { b |= connects(user, position, BlockFace.SOUTH) << 4; b |= connects(user, position, BlockFace.WEST) << 6; b |= powerMappings.get(blockState) << 8; - final Integer newBlockState = connectedBlockStates.get(b); - return newBlockState == null ? blockState : newBlockState; + return connectedBlockStates.getOrDefault(b, blockState); } private int connects(UserConnection user, Position position, BlockFace side) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityTypeRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityTypeRewriter.java deleted file mode 100644 index 0573c2c72..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityTypeRewriter.java +++ /dev/null @@ -1,107 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data; - - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class EntityTypeRewriter { - private static final Map entityTypes = new HashMap<>(); - - static { - registerEntity(1, 32); // item - ajl - registerEntity(2, 22); // xp_orb - abx - registerEntity(3, 0); // area_effect_cloud - abp - registerEntity(4, 15); // elder_guardian - aju - registerEntity(5, 84); // wither_skeleton - aku - registerEntity(6, 71); // stray - akq - registerEntity(7, 74); // egg - alz - registerEntity(8, 35); // leash_knot - ajb - registerEntity(9, 49); // painting - ajd - registerEntity(10, 2); // arrow - all - registerEntity(11, 67); // snowball - alw - registerEntity(12, 34); // fireball - alq - registerEntity(13, 65); // small_fireball - alv - registerEntity(14, 75); // ender_pearl - ama - registerEntity(15, 23); // eye_of_ender_signal - alo - registerEntity(16, 77); // potion - amc - registerEntity(17, 76); // xp_bottle - amb - registerEntity(18, 33); // item_frame - aja - registerEntity(19, 85); // wither_skull - ame - registerEntity(20, 55); // tnt - ajm - registerEntity(21, 24); // falling_block - ajk - registerEntity(22, 25); // fireworks_rocket - alp - registerEntity(23, 30); // husk - akc - registerEntity(24, 68); // spectral_arrow - alx - registerEntity(25, 60); // shulker_bullet - alu - registerEntity(26, 13); // dragon_fireball - alm - registerEntity(27, 89); // zombie_villager - akw - registerEntity(28, 63); // skeleton_horse - aht - registerEntity(29, 88); // zombie_horse - ahv - registerEntity(30, 1); // armor_stand - aiy - registerEntity(31, 11); // donkey - aho - registerEntity(32, 46); // mule - ahs - registerEntity(33, 20); // evocation_fangs - aln - registerEntity(34, 21); // evocation_illager - ajy - registerEntity(35, 78); // vex - akr - registerEntity(36, 81); // vindication_illager - aks - registerEntity(37, 31); // illusion_illager - akd - registerEntity(40, 41); // commandblock_minecart - aml - registerEntity(41, 5); // boat - ami - registerEntity(42, 39); // minecart - amj - registerEntity(43, 40); // chest_minecart - amk - registerEntity(44, 42); // furnace_minecart - amm - registerEntity(45, 45); // tnt_minecart - amp - registerEntity(46, 43); // hopper_minecart - amn - registerEntity(47, 44); // spawner_minecart - amo - registerEntity(50, 10); // creeper - ajs - registerEntity(51, 62); // skeleton - akm - registerEntity(52, 69); // spider - akp - registerEntity(53, 27); // giant - aka - registerEntity(54, 87); // zombie - akv - registerEntity(55, 64); // slime - akn - registerEntity(56, 26); // ghast - ajz - registerEntity(57, 53); // zombie_pigman - akh - registerEntity(58, 18); // enderman - ajv - registerEntity(59, 6); // cave_spider - ajr - registerEntity(60, 61); // silverfish - akl - registerEntity(61, 4); // blaze - ajq - registerEntity(62, 38); // magma_cube - ake - registerEntity(63, 17); // ender_dragon - aic - registerEntity(64, 83); // wither - aiw - registerEntity(65, 3); // bat - agl - registerEntity(66, 82); // witch - akt - registerEntity(67, 19); // endermite - ajw - registerEntity(68, 28); // guardian - akb - registerEntity(69, 59); // shulker - akk - registerEntity(200, 16); // ender_crystal - aib - registerEntity(90, 51); // pig - agy - registerEntity(91, 58); // sheep - ahd - registerEntity(92, 9); // cow - ags - registerEntity(93, 7); // chicken - agq - registerEntity(94, 70); // squid - ahg - registerEntity(95, 86); // wolf - ahl - registerEntity(96, 47); // mooshroom - agv - registerEntity(97, 66); // snowman - ahf - registerEntity(98, 48); // ocelot - agw - registerEntity(99, 80); // villager_golem - ahj - registerEntity(100, 29); // horse - ahp - registerEntity(101, 56); // rabbit - ahb - registerEntity(102, 54); // polar_bear - agz - registerEntity(103, 36); // llama - ahr - registerEntity(104, 37); // llama_spit - alr - registerEntity(105, 50); // parrot - agx - registerEntity(120, 79); // villager - ala - - // OBJECTS - // Couldn't find any object id change with mapped values - } - - private static void registerEntity(int type1_12, int type1_13) { - entityTypes.put(type1_12, type1_13); - } - - public static Optional getNewId(int type1_12) { - return Optional.ofNullable(entityTypes.get(type1_12)); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java index db9709547..78ef07c8d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/metadata/MetadataRewriter1_13To1_12_2.java @@ -1,6 +1,7 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_12Types; import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -10,7 +11,6 @@ import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.type.types.Particle; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; @@ -23,6 +23,7 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) { super(protocol, EntityTracker1_13.class); + mapTypes(Entity1_12Types.EntityType.values(), Entity1_13Types.EntityType.class); } @Override @@ -100,11 +101,6 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { // TODO: Boat has changed } - @Override - public int getNewEntityId(final int oldId) { - return EntityTypeRewriter.getNewId(oldId).orElse(oldId); - } - @Override protected EntityType getTypeFromId(int type) { return Entity1_13Types.getTypeFromId(type, false); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java index 4e33f4abc..1ffc37a6b 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java @@ -26,32 +26,32 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.BlockStorage; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntSet; -import java.util.HashSet; import java.util.List; import java.util.Optional; -import java.util.Set; public class WorldPackets { - private static final Set validBiomes = new HashSet<>(); + private static final IntSet VALID_BIOMES = CollectionUtil.createIntSet(70); static { // Client will crash if it receives a invalid biome id for (int i = 0; i < 50; i++) { - validBiomes.add(i); + VALID_BIOMES.add(i); } - validBiomes.add(127); + VALID_BIOMES.add(127); for (int i = 129; i <= 134; i++) { - validBiomes.add(i); + VALID_BIOMES.add(i); } - validBiomes.add(140); - validBiomes.add(149); - validBiomes.add(151); + VALID_BIOMES.add(140); + VALID_BIOMES.add(149); + VALID_BIOMES.add(151); for (int i = 155; i <= 158; i++) { - validBiomes.add(i); + VALID_BIOMES.add(i); } for (int i = 160; i <= 167; i++) { - validBiomes.add(i); + VALID_BIOMES.add(i); } } @@ -407,7 +407,7 @@ public class WorldPackets { int latestBiomeWarn = Integer.MIN_VALUE; for (int i = 0; i < 256; i++) { int biome = chunk.getBiomeData()[i]; - if (!validBiomes.contains(biome)) { + if (!VALID_BIOMES.contains(biome)) { if (biome != 255 // is it generated naturally? *shrug* && latestBiomeWarn != biome) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java index 56ccd68fd..85de84490 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java @@ -11,25 +11,29 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.WorldPackets; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class BlockConnectionStorage extends StoredObject { - private final Map> blockStorage = createLongObjectMap(); - - private static final Map reverseBlockMappings; + private static final short[] REVERSE_BLOCK_MAPPINGS = new short[8581]; private static Constructor fastUtilLongObjectHashMap; + private final Map> blockStorage = createLongObjectMap(); + static { try { fastUtilLongObjectHashMap = Class.forName("it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap").getConstructor(); Via.getPlatform().getLogger().info("Using FastUtil Long2ObjectOpenHashMap for block connections"); } catch (ClassNotFoundException | NoSuchMethodException ignored) { } - reverseBlockMappings = new HashMap<>(); + + Arrays.fill(REVERSE_BLOCK_MAPPINGS, (short) -1); for (int i = 0; i < 4096; i++) { int newBlock = MappingData.blockMappings.getNewId(i); - if (newBlock != -1) reverseBlockMappings.put((short) newBlock, (short) i); + if (newBlock != -1) { + REVERSE_BLOCK_MAPPINGS[newBlock] = (short) i; + } } } @@ -38,15 +42,18 @@ public class BlockConnectionStorage extends StoredObject { } public void store(int x, int y, int z, int blockState) { - Short mapping = reverseBlockMappings.get((short) blockState); - if (mapping == null) return; + short mapping = REVERSE_BLOCK_MAPPINGS[blockState]; + if (mapping == -1) return; + blockState = mapping; long pair = getChunkSectionIndex(x, y, z); Pair map = getChunkSection(pair, (blockState & 0xF) != 0); int blockIndex = encodeBlockPos(x, y, z); map.getKey()[blockIndex] = (byte) (blockState >> 4); NibbleArray nibbleArray = map.getValue(); - if (nibbleArray != null) nibbleArray.set(blockIndex, blockState); + if (nibbleArray != null) { + nibbleArray.set(blockIndex, blockState); + } } public int get(int x, int y, int z) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockStorage.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockStorage.java index dfdbf1b7c..94e9fcaa9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockStorage.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/storage/BlockStorage.java @@ -3,36 +3,39 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntSet; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; public class BlockStorage extends StoredObject { - private static final Set whitelist = new HashSet<>(); + private static final IntSet WHITELIST = CollectionUtil.createIntSet(46); private final Map blocks = new ConcurrentHashMap<>(); static { // Flower pots - whitelist.add(5266); + WHITELIST.add(5266); // Add those red beds - for (int i = 0; i < 16; i++) - whitelist.add(972 + i); + for (int i = 0; i < 16; i++) { + WHITELIST.add(972 + i); + } // Add the white banners - for (int i = 0; i < 20; i++) - whitelist.add(6854 + i); + for (int i = 0; i < 20; i++) { + WHITELIST.add(6854 + i); + } // Add the white wall banners for (int i = 0; i < 4; i++) { - whitelist.add(7110 + i); + WHITELIST.add(7110 + i); } // Skeleton skulls - for (int i = 0; i < 5; i++) - whitelist.add(5447 + i); + for (int i = 0; i < 5; i++) { + WHITELIST.add(5447 + i); + } } public BlockStorage(UserConnection user) { @@ -44,14 +47,14 @@ public class BlockStorage extends StoredObject { } public void store(Position position, int block, int replacementId) { - if (!whitelist.contains(block)) + if (!WHITELIST.contains(block)) return; blocks.put(position, new ReplacementData(block, replacementId)); } public boolean isWelcome(int block) { - return whitelist.contains(block); + return WHITELIST.contains(block); } public boolean contains(Position position) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/EntityTypeRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/EntityTypeRewriter.java deleted file mode 100644 index 32529384a..000000000 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/EntityTypeRewriter.java +++ /dev/null @@ -1,109 +0,0 @@ -package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class EntityTypeRewriter { - private static final Map entityTypes = new HashMap<>(); - - static { - regEnt(6, 7); // cave_spider - regEnt(7, 8); // chicken - regEnt(8, 9); // cod - regEnt(9, 10); // cow - regEnt(10, 11); // creeper - regEnt(11, 12); // donkey - regEnt(12, 13); // dolphin - regEnt(13, 14); // dragon_fireball - regEnt(14, 15); // drowned - regEnt(15, 16); // elder_guardian - regEnt(16, 17); // end_crystal - regEnt(17, 18); // ender_dragon - regEnt(18, 19); // enderman - regEnt(19, 20); // endermite - regEnt(20, 21); // evoker_fangs - regEnt(21, 22); // evoker - regEnt(22, 23); // experience_orb - regEnt(23, 24); // eye_of_ender - regEnt(24, 25); // falling_block - regEnt(25, 26); // firework_rocket - regEnt(26, 28); // ghast - regEnt(27, 29); // giant - regEnt(28, 30); // guardian - regEnt(29, 31); // horse - regEnt(30, 32); // husk - regEnt(31, 33); // illusioner - regEnt(32, 34); // item - regEnt(33, 35); // item_frame - regEnt(34, 36); // fireball - regEnt(35, 37); // leash_knot - regEnt(36, 38); // llama - regEnt(37, 39); // llama_spit - regEnt(38, 40); // magma_cube - regEnt(39, 41); // minecart - regEnt(40, 42); // chest_minecart - regEnt(41, 43); // command_block_minecart - regEnt(42, 44); // furnace_minecart - regEnt(43, 45); // hopper_minecart - regEnt(44, 46); // spawner_minecart - regEnt(45, 47); // tnt_minecart - regEnt(46, 48); // mule - regEnt(47, 49); // mooshroom - regEnt(48, 6); // ocelot -> cat TODO Remap untamed ocelot to ocelot? - regEnt(49, 51); // painting - regEnt(50, 53); // parrot - regEnt(51, 54); // pig - regEnt(52, 55); // pufferfish - regEnt(53, 56); // zombie_pigman - regEnt(54, 57); // polar_bear - regEnt(55, 58); // tnt - regEnt(56, 59); // rabbit - regEnt(57, 60); // salmon - regEnt(58, 61); // sheep - regEnt(59, 62); // shulker - regEnt(60, 63); // shulker_bullet - regEnt(61, 64); // silverfish - regEnt(62, 65); // skeleton - regEnt(63, 66); // skeleton_horse - regEnt(64, 67); // slime - regEnt(65, 68); // small_fireball - regEnt(66, 69); // snowgolem - regEnt(67, 70); // snowball - regEnt(68, 71); // spectral_arrow - regEnt(69, 72); // spider - regEnt(70, 73); // squid - regEnt(71, 74); // stray - regEnt(72, 76); // tropical_fish - regEnt(73, 77); // turtle - regEnt(74, 78); // egg - regEnt(75, 79); // ender_pearl - regEnt(76, 80); // experience_bottle - regEnt(77, 81); // potion - regEnt(78, 83); // vex - regEnt(79, 84); // villager - regEnt(80, 85); // iron_golem - regEnt(81, 86); // vindicator - regEnt(82, 89); // witch - regEnt(83, 90); // wither - regEnt(84, 91); // wither_skeleton - regEnt(85, 92); // wither_skull - regEnt(86, 93); // wolf - regEnt(87, 94); // zombie - regEnt(88, 95); // zombie_horse - regEnt(89, 96); // zombie_villager - regEnt(90, 97); // phantom - regEnt(91, 99); // lightning_bolt - regEnt(92, 100); // player - regEnt(93, 101); // fishing_bobber - regEnt(94, 82); // trident - } - - private static void regEnt(int type1_13, int type1_14) { - entityTypes.put(type1_13, type1_14); - } - - public static Optional getNewId(int type1_13) { - return Optional.ofNullable(entityTypes.get(type1_13)); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java index 261793a45..eec379699 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/data/MappingData.java @@ -8,19 +8,19 @@ import com.google.gson.JsonObject; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.Mappings; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntSet; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; public class MappingData { public static final BiMap oldToNewItems = HashBiMap.create(); public static Mappings blockStateMappings; public static Mappings blockMappings; public static Mappings soundMappings; - public static Set motionBlocking; - public static Set nonFullBlocks; + public static IntSet motionBlocking; + public static IntSet nonFullBlocks; public static void init() { Via.getPlatform().getLogger().info("Loading 1.13.2 -> 1.14 mappings..."); @@ -40,19 +40,19 @@ public class MappingData { JsonObject heightMapData = MappingDataLoader.loadData("heightMapData-1.14.json"); JsonArray motionBlocking = heightMapData.getAsJsonArray("MOTION_BLOCKING"); - us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData.motionBlocking = new HashSet<>(motionBlocking.size()); + MappingData.motionBlocking = CollectionUtil.createIntSet(motionBlocking.size()); for (JsonElement blockState : motionBlocking) { String key = blockState.getAsString(); Integer id = blockStateMap.get(key); if (id == null) { Via.getPlatform().getLogger().warning("Unknown blockstate " + key + " :("); } else { - us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData.motionBlocking.add(id); + MappingData.motionBlocking.add(id); } } if (Via.getConfig().isNonFullBlockLightFix()) { - nonFullBlocks = new HashSet<>(); + nonFullBlocks = CollectionUtil.createIntSet(1611); for (Map.Entry blockstates : mapping1_13_2.getAsJsonObject("blockstates").entrySet()) { final String state = blockstates.getValue().getAsString(); if (state.contains("_slab") || state.contains("_stairs") || state.contains("_wall[")) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java index a4a334f67..fe3e69975 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/metadata/MetadataRewriter1_14To1_13_2.java @@ -3,6 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.Entity1_13Types; import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.VillagerData; @@ -13,7 +14,6 @@ import us.myles.ViaVersion.api.rewriters.MetadataRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.Particle; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.EntityTypeRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.packets.InventoryPackets; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; @@ -23,6 +23,7 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { public MetadataRewriter1_14To1_13_2(Protocol1_14To1_13_2 protocol) { super(protocol, EntityTracker1_14.class); + mapTypes(Entity1_13Types.EntityType.values(), Entity1_14Types.EntityType.class); } @Override @@ -168,11 +169,6 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { } } - @Override - public int getNewEntityId(final int oldId) { - return EntityTypeRewriter.getNewId(oldId).orElse(oldId); - } - @Override protected EntityType getTypeFromId(int type) { return Entity1_14Types.getTypeFromId(type); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index b3de6fe43..e81c7e5f8 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -14,7 +14,6 @@ import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; -import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.EntityTypeRewriter; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; @@ -50,7 +49,7 @@ public class EntityPackets { int typeId = wrapper.get(Type.VAR_INT, 1); Entity1_13Types.EntityType type1_13 = Entity1_13Types.getTypeFromId(typeId, true); - typeId = EntityTypeRewriter.getNewId(type1_13.getId()).orElse(type1_13.getId()); + typeId = metadataRewriter.getNewEntityId(type1_13.getId()); Entity1_14Types.EntityType type1_14 = Entity1_14Types.getTypeFromId(typeId); if (type1_14 != null) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java index d69e1407e..659a3ac02 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/ItemRewriter.java @@ -5,6 +5,8 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntMap; import java.util.Collections; import java.util.HashMap; @@ -17,7 +19,7 @@ public class ItemRewriter { private static final Map POTION_NAME_TO_ID = new HashMap<>(); private static final Map POTION_ID_TO_NAME = new HashMap<>(); - private static final Map POTION_INDEX = new HashMap<>(); + private static final IntMap POTION_INDEX = CollectionUtil.createIntMap(36); static { /* Entities */ @@ -381,13 +383,13 @@ public class ItemRewriter { oldID -= 8192; } - Integer index = POTION_INDEX.get(oldID); - if (index != null) { + int index = POTION_INDEX.get(oldID); + if (index != -1) { return index; } oldID = POTION_NAME_TO_ID.get(potionNameFromDamage((short) oldID)); - return (index = POTION_INDEX.get(oldID)) != null ? index : 0; + return (index = POTION_INDEX.get(oldID)) != -1 ? index : 0; } private static void registerEntity(Integer id, String name) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/Effect.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/Effect.java index 632e7e298..dd0a5a115 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/Effect.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/sounds/Effect.java @@ -1,14 +1,13 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.sounds; -import java.util.HashMap; -import java.util.Map; +import us.myles.ViaVersion.util.fastutil.CollectionUtil; +import us.myles.ViaVersion.util.fastutil.IntMap; public class Effect { - private static final Map effects; + private static final IntMap EFFECTS = CollectionUtil.createIntMap(17); static { - effects = new HashMap<>(); addRewrite(1005, 1010); //Play music disc addRewrite(1003, 1005); //Iron door open addRewrite(1006, 1011); //Iron door close @@ -26,19 +25,17 @@ public class Effect { addRewrite(1020, 1029); //Anvil break addRewrite(1021, 1030); //Anvil use addRewrite(1022, 1031); //Anvil land - } public static int getNewId(int id) { - Integer newId = effects.get(id); - return newId != null ? newId : id; + return EFFECTS.getOrDefault(id, id); } public static boolean contains(int oldId) { - return effects.containsKey(oldId); + return EFFECTS.containsKey(oldId); } private static void addRewrite(int oldId, int newId) { - effects.put(oldId, newId); + EFFECTS.put(oldId, newId); } } diff --git a/common/src/main/java/us/myles/ViaVersion/util/fastutil/CollectionUtil.java b/common/src/main/java/us/myles/ViaVersion/util/fastutil/CollectionUtil.java new file mode 100644 index 000000000..fa5e7605e --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/util/fastutil/CollectionUtil.java @@ -0,0 +1,173 @@ +package us.myles.ViaVersion.util.fastutil; + +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Utility class to possibly wrap FastUtil collections for faster access. + * These should only be used for high access and low/no change collections, since resizing FastUtil collections is expensive. + */ +public class CollectionUtil { + private static final boolean FAST_UTIL = hasFastUtil(); + + private static boolean hasFastUtil() { + try { + Class.forName("Int2IntMap"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + + /** + * Creates a new FastUtil collection from the given map if present, else simply wraps the original. + * + * @param originalMap map to be reflected + * @return wrapped int map + */ + public static IntMap createIntMap(Map originalMap) { + return FAST_UTIL ? new WrappedFUIntMap(new Int2IntOpenHashMap(originalMap)) : new WrappedIntMap(originalMap); + } + + /** + * Creates a new FastUtil collection if present, else simply wraps a normal HashMap. + * + * @param size expected size of the collection + * @return wrapped int map + */ + public static IntMap createIntMap(int size) { + return FAST_UTIL ? new WrappedFUIntMap(new Int2IntOpenHashMap(size)) : new WrappedIntMap(new HashMap<>(size)); + } + + public static IntMap createIntMap() { + return FAST_UTIL ? new WrappedFUIntMap(new Int2IntOpenHashMap()) : new WrappedIntMap(new HashMap<>()); + } + + /** + * Creates a new FastUtil collection from the given set if present, else simply wraps the original. + * + * @param originalSet set to be reflected + * @return wrapped int set + */ + public static IntSet createIntSet(Set originalSet) { + return FAST_UTIL ? new WrappedFUIntSet(new IntOpenHashSet(originalSet)) : new WrappedIntSet(originalSet); + } + + /** + * Creates a new FastUtil collection if present, else simply wraps a normal HashSet. + * + * @param size expected size of the collection + * @return wrapped int set + */ + public static IntSet createIntSet(int size) { + return FAST_UTIL ? new WrappedFUIntSet(new IntOpenHashSet(size)) : new WrappedIntSet(new HashSet<>(size)); + } + + private static final class WrappedFUIntMap implements IntMap { + private final Int2IntMap map; + + private WrappedFUIntMap(Int2IntMap map) { + this.map = map; + } + + @Override + public int getOrDefault(int key, int def) { + return map.getOrDefault(key, def); + } + + @Override + public boolean containsKey(int key) { + return map.containsKey(key); + } + + @Override + public int put(int key, int value) { + return map.put(key, value); + } + + @Override + public int remove(int key) { + return map.remove(key); + } + } + + private static final class WrappedIntMap implements IntMap { + private final Map map; + + private WrappedIntMap(Map map) { + this.map = map; + } + + @Override + public int getOrDefault(int key, int def) { + return map.getOrDefault(key, def); + } + + @Override + public boolean containsKey(int key) { + return map.containsKey(key); + } + + @Override + public int put(int key, int value) { + return map.put(key, value); + } + + @Override + public int remove(int key) { + return map.remove(key); + } + } + + private static final class WrappedFUIntSet implements IntSet { + private final it.unimi.dsi.fastutil.ints.IntSet set; + + private WrappedFUIntSet(it.unimi.dsi.fastutil.ints.IntSet set) { + this.set = set; + } + + @Override + public boolean contains(int key) { + return set.contains(key); + } + + @Override + public boolean add(int key) { + return set.add(key); + } + + @Override + public boolean remove(int key) { + return set.remove(key); + } + } + + private static final class WrappedIntSet implements IntSet { + private final Set set; + + private WrappedIntSet(Set set) { + this.set = set; + } + + @Override + public boolean contains(int key) { + return set.contains(key); + } + + @Override + public boolean add(int key) { + return set.add(key); + } + + @Override + public boolean remove(int key) { + return set.remove(key); + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/util/fastutil/IntMap.java b/common/src/main/java/us/myles/ViaVersion/util/fastutil/IntMap.java new file mode 100644 index 000000000..060b25c8b --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/util/fastutil/IntMap.java @@ -0,0 +1,35 @@ +package us.myles.ViaVersion.util.fastutil; + +/** + * Very simple wrapping interface to either be implemented by a HashMap or FastUtil's OpenHashMap. + */ +public interface IntMap { + + /** + * @return value if present, -1 otherwise + * @see java.util.HashMap#get(Object) + */ + default int get(int key) { + return getOrDefault(key, -1); + } + + /** + * @see java.util.HashMap#getOrDefault(Object, Object) (Object) + */ + int getOrDefault(int key, int def); + + /** + * @see java.util.HashMap#containsKey(Object) + */ + boolean containsKey(int key); + + /** + * @see java.util.HashMap#put(Object, Object) + */ + int put(int key, int value); + + /** + * @see java.util.HashMap#remove(Object) + */ + int remove(int key); +} diff --git a/common/src/main/java/us/myles/ViaVersion/util/fastutil/IntSet.java b/common/src/main/java/us/myles/ViaVersion/util/fastutil/IntSet.java new file mode 100644 index 000000000..74ec51cb9 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/util/fastutil/IntSet.java @@ -0,0 +1,22 @@ +package us.myles.ViaVersion.util.fastutil; + +/** + * Very simple wrapping interface to either be implemented by a HashSet or FastUtil's OpenHashSet. + */ +public interface IntSet { + + /** + * @see java.util.HashSet#contains(Object) + */ + boolean contains(int key); + + /** + * @see java.util.HashSet#add(Object) + */ + boolean add(int key); + + /** + * @see java.util.HashSet#remove(Object) + */ + boolean remove(int key); +}