From b4921132e170d818cb549608016ea1e90273be1e Mon Sep 17 00:00:00 2001 From: Redned Date: Sun, 18 Jul 2021 14:43:17 -0500 Subject: [PATCH] Replace BiMap with an Object2IntBiMap --- .../living/merchant/VillagerEntity.java | 2 +- ...BedrockInventoryTransactionTranslator.java | 2 +- .../player/BedrockActionTranslator.java | 2 +- .../holder/BlockInventoryHolder.java | 2 +- .../chest/DoubleChestInventoryTranslator.java | 2 +- .../java/world/JavaBlockChangeTranslator.java | 2 +- .../connector/registry/BlockRegistries.java | 5 +- .../loader/CollisionRegistryLoader.java | 11 +- .../connector/utils/Object2IntBiMap.java | 191 ++++++++++++++++++ 9 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/utils/Object2IntBiMap.java diff --git a/connector/src/main/java/org/geysermc/connector/entity/living/merchant/VillagerEntity.java b/connector/src/main/java/org/geysermc/connector/entity/living/merchant/VillagerEntity.java index 3c94f9de2..9aca12ba5 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/living/merchant/VillagerEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/living/merchant/VillagerEntity.java @@ -114,7 +114,7 @@ public class VillagerEntity extends AbstractMerchantEntity { if (bedPosition != null) { bedId = session.getConnector().getWorldManager().getBlockAt(session, bedPosition); } - String bedRotationZ = BlockRegistries.JAVA_IDENTIFIERS.get().inverse().get(bedId); + String bedRotationZ = BlockRegistries.JAVA_IDENTIFIERS.get().get(bedId); setRotation(rotation); setOnGround(isOnGround); this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java index 6e02d4c6b..589fa49d0 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java @@ -232,7 +232,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator= 2 && session.getGameMode() == GameMode.CREATIVE) { // Otherwise insufficient permissions int blockState = session.getBlockMappings().getJavaBlockState(packet.getBlockRuntimeId()); - String blockName = BlockRegistries.JAVA_IDENTIFIERS.get().inverse().getOrDefault(blockState, ""); + String blockName = BlockRegistries.JAVA_IDENTIFIERS.get().getOrDefault(blockState, ""); // In the future this can be used for structure blocks too, however not all elements // are available in each GUI if (blockName.contains("jigsaw")) { diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java index c116045cb..2603f2a0f 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java @@ -178,7 +178,7 @@ public class BedrockActionTranslator extends PacketTranslator 1 && (javaBlockString[0].equals("minecraft:chest") || javaBlockString[0].equals("minecraft:trapped_chest")) && !javaBlockString[1].contains("type=single")) { inventory.setHolderPosition(session.getLastInteractionBlockPosition()); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java index 5abe088a9..d144e99b8 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaBlockChangeTranslator.java @@ -101,7 +101,7 @@ public class JavaBlockChangeTranslator extends PacketTranslator BLOCKS = VersionedRegistry.create(RegistryLoaders.empty(Int2ObjectOpenHashMap::new)); @@ -43,7 +42,7 @@ public class BlockRegistries { public static final SimpleMappedRegistry JAVA_BLOCKS = SimpleMappedRegistry.create(RegistryLoaders.empty(Int2ObjectOpenHashMap::new)); - public static final MappedRegistry> JAVA_IDENTIFIERS = MappedRegistry.create(RegistryLoaders.empty(HashBiMap::create)); + public static final MappedRegistry> JAVA_IDENTIFIERS = MappedRegistry.create(RegistryLoaders.empty(Object2IntBiMap::new)); public static final SimpleMappedRegistry JAVA_CLEAN_IDENTIFIERS = SimpleMappedRegistry.create(RegistryLoaders.empty(Int2ObjectOpenHashMap::new)); diff --git a/connector/src/main/java/org/geysermc/connector/registry/loader/CollisionRegistryLoader.java b/connector/src/main/java/org/geysermc/connector/registry/loader/CollisionRegistryLoader.java index b20e1fe0e..fadd1e0d2 100644 --- a/connector/src/main/java/org/geysermc/connector/registry/loader/CollisionRegistryLoader.java +++ b/connector/src/main/java/org/geysermc/connector/registry/loader/CollisionRegistryLoader.java @@ -26,10 +26,10 @@ package org.geysermc.connector.registry.loader; import com.fasterxml.jackson.databind.node.ArrayNode; -import com.google.common.collect.BiMap; import it.unimi.dsi.fastutil.Pair; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2IntMap; import lombok.AllArgsConstructor; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.translators.collision.BoundingBox; @@ -40,6 +40,7 @@ import org.geysermc.connector.network.translators.collision.translators.OtherCol import org.geysermc.connector.network.translators.collision.translators.SolidCollision; import org.geysermc.connector.registry.BlockRegistries; import org.geysermc.connector.utils.FileUtils; +import org.geysermc.connector.utils.Object2IntBiMap; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; @@ -71,16 +72,16 @@ public class CollisionRegistryLoader extends MultiResourceRegistryLoader javaIdBlockMap = BlockRegistries.JAVA_IDENTIFIERS.get(); + Object2IntBiMap javaIdBlockMap = BlockRegistries.JAVA_IDENTIFIERS.get(); // Map of classes that don't change based on parameters that have already been created Map, BlockCollision> instantiatedCollision = new HashMap<>(); - for (Map.Entry entry : javaIdBlockMap.entrySet()) { - BlockCollision newCollision = instantiateCollision(entry.getKey(), entry.getValue(), annotationMap, instantiatedCollision, collisionList); + for (Object2IntMap.Entry entry : javaIdBlockMap.object2IntEntrySet()) { + BlockCollision newCollision = instantiateCollision(entry.getKey(), entry.getIntValue(), annotationMap, instantiatedCollision, collisionList); if (newCollision != null) { instantiatedCollision.put(newCollision.getClass(), newCollision); } - collisions.put(entry.getValue().intValue(), newCollision); + collisions.put(entry.getIntValue(), newCollision); } return collisions; } diff --git a/connector/src/main/java/org/geysermc/connector/utils/Object2IntBiMap.java b/connector/src/main/java/org/geysermc/connector/utils/Object2IntBiMap.java new file mode 100644 index 000000000..915ce5ac3 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/utils/Object2IntBiMap.java @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2019-2021 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.utils; + +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.ints.IntCollection; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectSet; +import it.unimi.dsi.fastutil.objects.ObjectSets; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.Objects; + +public class Object2IntBiMap implements Object2IntMap { + private final Object2IntMap forwards; + private final Int2ObjectMap backwards; + + public Object2IntBiMap() { + this(16); + } + + public Object2IntBiMap(int expected) { + this(expected, 0.75F); + } + + public Object2IntBiMap(T defaultForwardsValue) { + this(16, 0.75F, defaultForwardsValue, -1); + } + + public Object2IntBiMap(int expected, float loadFactor) { + this(expected, loadFactor, -1); + } + + public Object2IntBiMap(int expected, float loadFactor, int defaultBackwardsValue) { + this(expected, loadFactor, null, defaultBackwardsValue); + } + + public Object2IntBiMap(int expected, float loadFactor, T defaultForwardsValue, int defaultBackwardsValue) { + this.forwards = new Object2IntOpenHashMap<>(expected, loadFactor); + this.backwards = new Int2ObjectOpenHashMap<>(expected, loadFactor); + this.forwards.defaultReturnValue(defaultBackwardsValue); + this.backwards.defaultReturnValue(defaultForwardsValue); + } + + @Override + public int size() { + return this.forwards.size(); + } + + @Override + public boolean isEmpty() { + return this.forwards.isEmpty(); + } + + @Override + public int getInt(Object o) { + return this.forwards.getInt(o); + } + + public T get(int key) { + return this.backwards.get(key); + } + + @Override + public int getOrDefault(Object key, int defaultValue) { + return this.forwards.getOrDefault(key, defaultValue); + } + + public T getOrDefault(int key, T defaultValue) { + return this.backwards.getOrDefault(key, defaultValue); + } + + @Override + public void defaultReturnValue(int i) { + this.forwards.defaultReturnValue(i); + } + + public void defaultReturnValue(T v) { + this.backwards.defaultReturnValue(v); + } + + @Override + public int defaultReturnValue() { + return this.forwards.defaultReturnValue(); + } + + public T backwardsDefaultReturnValue() { + return this.backwards.defaultReturnValue(); + } + + @Override + public ObjectSet> object2IntEntrySet() { + return ObjectSets.unmodifiable(this.forwards.object2IntEntrySet()); + } + + public ObjectSet> int2ObjectEntrySet() { + return ObjectSets.unmodifiable(this.backwards.int2ObjectEntrySet()); + } + + @Override + public ObjectSet keySet() { + return this.forwards.keySet(); + } + + @Override + public IntCollection values() { + return this.forwards.values(); + } + + @Override + public boolean containsKey(Object o) { + return this.forwards.containsKey(o); + } + + @Override + public boolean containsValue(int i) { + return this.backwards.containsKey(i); + } + + @Override + public int put(T key, int value) { + this.backwards.put(value, key); + return this.forwards.put(key, value); + } + + @Override + public void putAll(@NotNull Map m) { + this.forwards.putAll(m); + for (Map.Entry entry : m.entrySet()) { + this.backwards.put((int) entry.getValue(), entry.getKey()); + } + } + + @Override + public int removeInt(Object key) { + if (!this.forwards.containsKey(key)) { + return this.defaultReturnValue(); + } + + int value = this.forwards.getInt(key); + if (!this.backwards.containsKey(value)) { + return this.defaultReturnValue(); + }; + this.backwards.remove(value); + return this.forwards.removeInt(key); + } + + @Override + public int hashCode() { + return this.forwards.hashCode(); + } + + @Override + public String toString() { + return this.forwards.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Object2IntBiMap that = (Object2IntBiMap) o; + return Objects.equals(this.forwards, that.forwards) && Objects.equals(this.backwards, that.backwards); + } +}