From b611f8facb1a035c5978fbb00762679c8b691791 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Fri, 9 Aug 2019 22:16:34 -0500 Subject: [PATCH] Rewrite remapper --- .../geysermc/connector/GeyserConnector.java | 2 +- .../network/translators/item/DyeColor.java | 30 --- .../translators/item/ItemTranslator.java | 63 ++--- .../network/translators/item/StoneType.java | 21 -- .../network/translators/item/WoodType.java | 20 -- .../translators/item/block/ColoredBlock.java | 41 +++ .../translators/item/block/WoodBlock.java | 38 +++ .../translators/item/type/DyeColor.java | 55 ++++ .../translators/item/type/StoneType.java | 46 ++++ .../translators/item/type/WoodType.java | 45 ++++ .../org/geysermc/connector/utils/Chunks.java | 7 +- .../geysermc/connector/utils/RemapUtils.java | 102 -------- .../geysermc/connector/utils/Remapper.java | 236 +++++++----------- .../org/geysermc/connector/utils/Toolbox.java | 28 ++- 14 files changed, 358 insertions(+), 376 deletions(-) delete mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/DyeColor.java delete mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/StoneType.java delete mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/WoodType.java create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/block/ColoredBlock.java create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/block/WoodBlock.java create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/type/DyeColor.java create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/type/StoneType.java create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/item/type/WoodType.java delete mode 100644 connector/src/main/java/org/geysermc/connector/utils/RemapUtils.java diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 9a55c650..7bfe745e 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -148,7 +148,7 @@ public class GeyserConnector implements Connector { } }).join(); - metrics = new Metrics("GeyserMC", instance.getConfig().getUUID(), true, java.util.logging.Logger.getLogger("")); + metrics = new Metrics("GeyserMC", config.getUUID(), true, java.util.logging.Logger.getLogger("")); metrics.addCustomChart(new Metrics.SingleLineChart("servers", () -> 1)); metrics.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount)); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/DyeColor.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/DyeColor.java deleted file mode 100644 index bd277c9c..00000000 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/DyeColor.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.geysermc.connector.network.translators.item; - -import lombok.Getter; - -public enum DyeColor { - - WHITE, - ORANGE, - MAGENTA, - LIGHT_BLUE, - YELLOW, - LIME, - PINK, - GRAY, - LIGHT_GRAY, - CYAN, - PURPLE, - BLUE, - BROWN, - GREEN, - RED, - BLACK; - - @Getter - private final int id = ordinal(); - - public String getName() { - return name().toLowerCase(); - } -} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java index 7fcb72ec..a4e07f44 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java @@ -43,11 +43,9 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.nukkitx.protocol.bedrock.data.ItemData; import org.geysermc.connector.console.GeyserLogger; -import org.geysermc.connector.utils.MessageUtils; import org.geysermc.connector.utils.Remapper; -import org.geysermc.connector.utils.Toolbox; +import org.geysermc.connector.utils.MessageUtils; -import java.rmi.MarshalException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -55,7 +53,7 @@ import java.util.Map; public class ItemTranslator { - public static ItemStack translateToJava(ItemData data) { + public ItemStack translateToJava(ItemData data) { JavaItem javaItem = getJavaItem(data); if (data.getTag() == null) { @@ -64,7 +62,7 @@ public class ItemTranslator { return new ItemStack(javaItem.getId(), data.getCount(), translateToJavaNBT(data.getTag())); } - public static ItemData translateToBedrock(ItemStack stack) { + public ItemData translateToBedrock(ItemStack stack) { // Most likely air if null if (stack == null) { return ItemData.AIR; @@ -77,51 +75,38 @@ public class ItemTranslator { return ItemData.of(bedrockItem.getId(), (short) bedrockItem.getData(), stack.getAmount(), translateToBedrockNBT(stack.getNBT())); } - public static BedrockItem getBedrockItem(ItemStack stack) { - Map m = Remapper.JAVA_TO_BEDROCK.get(stack.getId()); - if (m == null) { + public BedrockItem getBedrockItem(ItemStack stack) { + BedrockItem bedrockItem = Remapper.ITEM_REMAPPER.convertToBedrock(stack); + if (bedrockItem == null) { GeyserLogger.DEFAULT.debug("Missing mapping for java item " + stack.getId()); return BedrockItem.AIR; } - return new BedrockItem((String) m.get("name"), (Integer) m.get("id"), (Integer) m.get("data")); + + return bedrockItem; } - public static JavaItem getJavaItem(ItemData data) { - Map m = Remapper.BEDROCK_TO_JAVA.get(data.getId()).get(data.getDamage()); - if (m == null) { - GeyserLogger.DEFAULT.debug("Missing mapping for bedrock item " + data.getId() + ":" + data.getDamage()); + public JavaItem getJavaItem(ItemData data) { + JavaItem javaItem = Remapper.ITEM_REMAPPER.convertToJava(data); + if (javaItem == null) { + GeyserLogger.DEFAULT.debug("Missing mapping for bedrock item " + data.getId() + ":" + data.getDamage()); return JavaItem.AIR; } - return new JavaItem((String) m.get("name"), (Integer) m.get("id")); + + return javaItem; } - public static BedrockItem getBedrockBlock(BlockState stack) { - Map m = Remapper.JAVA_TO_BEDROCK_BLOCKS.get(stack.getId()); - return new BedrockItem((String) m.get("name"), (Integer) m.get("id"), (Integer) m.get("data")); - } - - public static String getBedrockIdentifier(String javaIdentifier) { - if (!Remapper.JAVA_TO_BEDROCK.containsKey(javaIdentifier)) { - return javaIdentifier; + public BedrockItem getBedrockBlock(BlockState state) { + BedrockItem bedrockItem = Remapper.BLOCK_REMAPPER.convertToBedrock(new ItemStack(state.getId())); + if (bedrockItem == null) { + //GeyserLogger.DEFAULT.debug("Missing mapping for java item " + state.getId()); + return BedrockItem.AIR; } - if ((int) Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("data") > 0) { - return Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("name") + ":" + Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("data"); - } - - return (String) Remapper.JAVA_TO_BEDROCK.get(javaIdentifier).get("name"); + return bedrockItem; } - public static String getJavaIdentifier(String bedrockIdentifier, int data) { - if (!Remapper.BEDROCK_TO_JAVA.containsKey(bedrockIdentifier)) { - return bedrockIdentifier; - } - - return (String) Remapper.BEDROCK_TO_JAVA.get(bedrockIdentifier).get(data).get("name"); - } - - private static CompoundTag translateToJavaNBT(com.nukkitx.nbt.tag.CompoundTag tag) { + private CompoundTag translateToJavaNBT(com.nukkitx.nbt.tag.CompoundTag tag) { CompoundTag javaTag = new CompoundTag(tag.getName()); Map javaValue = javaTag.getValue(); if (tag.getValue() != null && !tag.getValue().isEmpty()) { @@ -138,7 +123,7 @@ public class ItemTranslator { return javaTag; } - private static Tag translateToJavaNBT(com.nukkitx.nbt.tag.Tag tag) { + private Tag translateToJavaNBT(com.nukkitx.nbt.tag.Tag tag) { if (tag instanceof com.nukkitx.nbt.tag.ByteArrayTag) { com.nukkitx.nbt.tag.ByteArrayTag byteArrayTag = (com.nukkitx.nbt.tag.ByteArrayTag) tag; return new ByteArrayTag(byteArrayTag.getName(), byteArrayTag.getValue()); @@ -212,7 +197,7 @@ public class ItemTranslator { return null; } - private static com.nukkitx.nbt.tag.CompoundTag translateToBedrockNBT(CompoundTag tag) { + private com.nukkitx.nbt.tag.CompoundTag translateToBedrockNBT(CompoundTag tag) { Map> javaValue = new HashMap>(); if (tag.getValue() != null && !tag.getValue().isEmpty()) { for (String str : tag.getValue().keySet()) { @@ -229,7 +214,7 @@ public class ItemTranslator { return bedrockTag; } - private static com.nukkitx.nbt.tag.Tag translateToBedrockNBT(Tag tag) { + private com.nukkitx.nbt.tag.Tag translateToBedrockNBT(Tag tag) { if (tag instanceof ByteArrayTag) { ByteArrayTag byteArrayTag = (ByteArrayTag) tag; return new com.nukkitx.nbt.tag.ByteArrayTag(byteArrayTag.getName(), byteArrayTag.getValue()); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/StoneType.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/StoneType.java deleted file mode 100644 index 0fc7e122..00000000 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/StoneType.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.geysermc.connector.network.translators.item; - -import lombok.Getter; - -public enum StoneType { - - STONE, - GRANITE, - POLISHED_GRANITE, - DIORITE, - POLISHED_DIORITE, - ANDESITE, - POLISHED_ANDESITE; - - @Getter - private final int id = ordinal(); - - public String getName() { - return name().toLowerCase(); - } -} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/WoodType.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/WoodType.java deleted file mode 100644 index 5bfe38b2..00000000 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/WoodType.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.geysermc.connector.network.translators.item; - -import lombok.Getter; - -public enum WoodType { - - OAK, - SPRUCE, - BIRCH, - JUNGLE, - ACACIA, - DARK_OAK; - - @Getter - private final int id = ordinal(); - - public String getName() { - return name().toLowerCase(); - } -} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/block/ColoredBlock.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/block/ColoredBlock.java new file mode 100644 index 00000000..c1686eae --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/block/ColoredBlock.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 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.network.translators.item.block; + +public enum ColoredBlock { + + BANNER, + CARPET, + CONCRETE, + CONCRETE_POWDER, + DYE, + SHULKER_BOX, + STAINED_GLASS, + STAINED_GLASS_PANE, + TERRACOTTA, + WOOL, + +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/block/WoodBlock.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/block/WoodBlock.java new file mode 100644 index 00000000..0684cf3f --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/block/WoodBlock.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 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.network.translators.item.block; + +public enum WoodBlock { + + BOAT, + FENCE, + LEAVES, + LOG, + PLANKS, + SAPLING, + SLAB, + +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/type/DyeColor.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/type/DyeColor.java new file mode 100644 index 00000000..1d794d71 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/type/DyeColor.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019 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.network.translators.item.type; + +import lombok.Getter; + +public enum DyeColor { + + WHITE, + ORANGE, + MAGENTA, + LIGHT_BLUE, + YELLOW, + LIME, + PINK, + GRAY, + LIGHT_GRAY, + CYAN, + PURPLE, + BLUE, + BROWN, + GREEN, + RED, + BLACK; + + @Getter + private final int id = ordinal(); + + public String getName() { + return name().toLowerCase(); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/type/StoneType.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/type/StoneType.java new file mode 100644 index 00000000..7888bd60 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/type/StoneType.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 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.network.translators.item.type; + +import lombok.Getter; + +public enum StoneType { + + STONE, + GRANITE, + POLISHED_GRANITE, + DIORITE, + POLISHED_DIORITE, + ANDESITE, + POLISHED_ANDESITE; + + @Getter + private final int id = ordinal(); + + public String getName() { + return name().toLowerCase(); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/type/WoodType.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/type/WoodType.java new file mode 100644 index 00000000..d490f3f5 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/type/WoodType.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 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.network.translators.item.type; + +import lombok.Getter; + +public enum WoodType { + + OAK, + SPRUCE, + BIRCH, + JUNGLE, + ACACIA, + DARK_OAK; + + @Getter + private final int id = ordinal(); + + public String getName() { + return name().toLowerCase(); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/utils/Chunks.java b/connector/src/main/java/org/geysermc/connector/utils/Chunks.java index 4a7cbeca..cfdfadc1 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/Chunks.java +++ b/connector/src/main/java/org/geysermc/connector/utils/Chunks.java @@ -6,9 +6,8 @@ import gnu.trove.list.TByteList; import gnu.trove.list.array.TByteArrayList; import lombok.AccessLevel; import lombok.AllArgsConstructor; -import org.geysermc.connector.network.translators.item.ItemTranslator; +import org.geysermc.connector.network.translators.TranslatorsInit; -import java.util.List; import java.util.Objects; public class Chunks { @@ -39,7 +38,7 @@ public class Chunks { for (int y = 0; x < 16; x++) { for (int z = 0; x < 16; x++) { try { - list.add((byte) ItemTranslator.getBedrockBlock(chunk.getBlocks().get(x, y, z)).getId()); + list.add((byte) TranslatorsInit.getItemTranslator().getBedrockBlock(chunk.getBlocks().get(x, y, z)).getId()); } catch (NullPointerException e) { list.add((byte) 0); } @@ -53,7 +52,7 @@ public class Chunks { for (int y = 0; x < 16; x++) { for (int z = 0; x < 16; x++) { try { - list.add((byte) ItemTranslator.getBedrockBlock(chunk.getBlocks().get(x, y, z)).getData()); + list.add((byte) TranslatorsInit.getItemTranslator().getBedrockBlock(chunk.getBlocks().get(x, y, z)).getData()); } catch (NullPointerException e) { list.add((byte) 0); } diff --git a/connector/src/main/java/org/geysermc/connector/utils/RemapUtils.java b/connector/src/main/java/org/geysermc/connector/utils/RemapUtils.java deleted file mode 100644 index 57b348ea..00000000 --- a/connector/src/main/java/org/geysermc/connector/utils/RemapUtils.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.geysermc.connector.utils; - -import org.geysermc.connector.network.translators.item.*; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.jar.JarEntry; - -class RemapUtils { - private static final String MINECRAFT = "minecraft:"; - - static void start() { - //colors - Remapper.predicates.put((x) -> x.getF().contains("white"), (x, y) -> { - //System.out.println(x.getIdentifier()); - if(customColorIfNeeded(y)) return; - - if (y.getIdentifier().replaceAll("terracotta", "stained_hardened_clay") - .replaceAll("white_", "") - .equalsIgnoreCase(x.getIdentifier()) && x.getData() == 0) { - - for (DyeColor dyeColor : DyeColor.values()) { - JavaItem j = new JavaItem(y.getIdentifier().replaceAll("white", dyeColor.getName()), y.getId() + dyeColor.getId()); - Remapper.convertions.computeIfAbsent(j, (q) -> new ArrayList<>()); - Remapper.convertions.get(j).add(new BedrockItem(x.getIdentifier(), x.getId(), dyeColor.getId())); - } - } - - }); - //stone - Remapper.predicates.put((x) -> x.getF().contains("stone"), (x, y) -> { - //System.out.println(x.getIdentifier()); - if(customStoneIfNeeded(y)) return; - - if (y.getIdentifier().replaceAll("stone_", "") - .equalsIgnoreCase(x.getIdentifier()) && x.getData() == 0) { - - /*for (WoodType woodType : WoodType.values()) { - JavaItem j = new JavaItem(y.getIdentifier().replaceAll("oak", woodType.getName()), y.getId()); - Remapper.convertions.computeIfAbsent(j, (q) -> new ArrayList<>()); - Remapper.convertions.get(j).add(new BedrockItem(x.getIdentifier(), x.getId(), woodType.getId())); - }*/ - } - - }); - //shared name - Remapper.predicates.put((x) -> x.getF().equalsIgnoreCase(x.getS()), (x, y) -> { - try { - Remapper.convertions.computeIfAbsent(y, (q) -> new ArrayList<>()); - Remapper.convertions.get(y).add(x); - } catch (Exception e) { - // - } - }); - //wood - Remapper.predicates.put((x) -> x.getF().contains("oak"), (x, y) -> { - //System.out.println(x.getIdentifier()); - if(customWoodIfNeeded(y)) return; - - if (y.getIdentifier().replaceAll("oak_", "") - .equalsIgnoreCase(x.getIdentifier()) && x.getData() == 0) { - - for (WoodType woodType : WoodType.values()) { - JavaItem j = new JavaItem(y.getIdentifier().replaceAll("oak", woodType.getName()), y.getId() + woodType.getId()); - Remapper.convertions.computeIfAbsent(j, (q) -> new ArrayList<>()); - Remapper.convertions.get(j).add(new BedrockItem(x.getIdentifier(), x.getId(), woodType.getId())); - } - } - - }); - } - - private static boolean customColorIfNeeded(JavaItem j) { - if(j.getIdentifier().equalsIgnoreCase(MINECRAFT + "shulker_box")) { - Remapper.convertions.put(j, Arrays.asList(new BedrockItem(MINECRAFT + "undyed_shulker_box", 205, 0))); - return true; - } - return false; - } - - private static boolean customWoodIfNeeded(JavaItem j) { - if(j.getIdentifier().contains("fence_gate")) return true; - for(WoodType t : WoodType.values()) { - if (j.getIdentifier().equalsIgnoreCase(MINECRAFT + "stripped_" + t.getName() +"_wood")) { - Remapper.convertions.put(j, Arrays.asList(new BedrockItem(MINECRAFT + "wood", 467, t.getId() + 8))); - return false; - } - } - return false; - } - - private static boolean customStoneIfNeeded(JavaItem j) { - if (j.getIdentifier().equalsIgnoreCase(MINECRAFT + "stone")) { - for (StoneType type : StoneType.values()) { - Remapper.convertions.put(new JavaItem(MINECRAFT + type.getName(), type.getId() + 1), Arrays.asList(new BedrockItem(MINECRAFT + "stone", 1, type.getId()))); - } - return true; - } - return false; - } -} diff --git a/connector/src/main/java/org/geysermc/connector/utils/Remapper.java b/connector/src/main/java/org/geysermc/connector/utils/Remapper.java index 9e0ebf0a..4676dec2 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/Remapper.java +++ b/connector/src/main/java/org/geysermc/connector/utils/Remapper.java @@ -1,177 +1,121 @@ package org.geysermc.connector.utils; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; +import com.nukkitx.protocol.bedrock.data.ItemData; import org.geysermc.connector.network.translators.item.BedrockItem; -import org.geysermc.connector.network.translators.item.DyeColor; import org.geysermc.connector.network.translators.item.JavaItem; -import org.geysermc.connector.network.translators.item.StoneType; -import org.geysermc.connector.network.translators.item.WoodType; +import org.geysermc.connector.network.translators.item.block.ColoredBlock; +import org.geysermc.connector.network.translators.item.type.DyeColor; +import org.geysermc.connector.network.translators.item.type.StoneType; +import org.geysermc.connector.network.translators.item.block.WoodBlock; +import org.geysermc.connector.network.translators.item.type.WoodType; -import java.io.File; -import java.util.*; -import java.util.function.BiConsumer; -import java.util.function.Predicate; +import java.util.HashMap; +import java.util.Map; public class Remapper { - static final Map> convertions = new HashMap<>(); - static final Map>, BiConsumer> predicates = new LinkedHashMap<>(); + public static final String MINECRAFT = "minecraft:"; - private static List specials = new ArrayList<>(); + public static final Remapper ITEM_REMAPPER = new Remapper(); + public static final Remapper BLOCK_REMAPPER = new Remapper(); - public static Map>> BEDROCK_TO_JAVA = new HashMap<>(); - public static Map> JAVA_TO_BEDROCK = new HashMap<>(); + private final Map bedrockToJava; + private final Map javaToBedrock; - public static Map>> BEDROCK_TO_JAVA_BLOCKS = new HashMap<>(); - public static Map> JAVA_TO_BEDROCK_BLOCKS = new HashMap<>(); + public Remapper() { + bedrockToJava = new HashMap<>(); + javaToBedrock = new HashMap<>(); + } - // Method to convert java to bedrock - public static void addConversions(Map items1, Map java) { - for (StoneType type : StoneType.values()) { - specials.add(type.getName()); - } + // Registers the conversions for bedrock <-> java + public void registerConversions(Map bedrockItems, Map javaItems) { + for (Map.Entry bedrockItemEntry : bedrockItems.entrySet()) { + BedrockItem bedrockItem = bedrockItemEntry.getValue(); + String identifier = bedrockItem.getIdentifier(); - RemapUtils.start(); + for (ColoredBlock coloredBlock : ColoredBlock.values()) { + if (!getBedrockIdentifier(coloredBlock.name()).equalsIgnoreCase(bedrockItem.getIdentifier().replace(MINECRAFT, ""))) + continue; - for (Map.Entry javaItem : java.entrySet()) { - for (Map.Entry bedrockItem : items1.entrySet()) { - for(Predicate> predicate : predicates.keySet()) { - BiValue b = new BiValue<>(javaItem.getKey(), bedrockItem.getKey()); - BiValue b2 = new BiValue<>(javaItem.getKey().replaceAll("_", ""), bedrockItem.getKey().replaceAll("_", "")); - if(predicate.test(b) || predicate.test(b2)) { - predicates.get(predicate).accept(bedrockItem.getValue(), javaItem.getValue()); - } + // The item must be colorable + for (DyeColor color : DyeColor.values()) { + if (color.getId() != bedrockItem.getData()) + continue; + + // Add the color to the identifier + identifier = MINECRAFT + color.name().toLowerCase() + "_" + coloredBlock.name().toLowerCase(); + } + } + + for (WoodBlock woodBlock : WoodBlock.values()) { + if (!getBedrockIdentifier(woodBlock.name()).equalsIgnoreCase(bedrockItem.getIdentifier().replace(MINECRAFT, ""))) + continue; + + if (isTool(bedrockItem.getIdentifier())) + continue; + + if (woodBlock == WoodBlock.SLAB && !bedrockItem.getIdentifier().contains("wooden")) + continue; + + for (WoodType woodType : WoodType.values()) { + if (woodType.getId() != bedrockItem.getData()) + continue; + + identifier = MINECRAFT + woodType.name().toLowerCase() + "_" + woodBlock.name().toLowerCase(); + } + } + + if (bedrockItem.getIdentifier().replace(MINECRAFT, "").equalsIgnoreCase("stone") && !isTool(bedrockItem.getIdentifier())) { + for (StoneType stoneType : StoneType.values()) { + if (stoneType.getId() != bedrockItem.getData()) + continue; + + // Set the identifier to stone + identifier = MINECRAFT + stoneType.name().toLowerCase(); + } + } + + for (Map.Entry javaItemEntry : javaItems.entrySet()) { + if (identifier.equalsIgnoreCase(javaItemEntry.getKey())) { + bedrockToJava.put(bedrockItemEntry.getValue(), javaItemEntry.getValue()); + javaToBedrock.put(javaItemEntry.getValue(), bedrockItemEntry.getValue()); } } } - - //for(BedrockItem item : ) - - for (DyeColor dyeColor : DyeColor.values()) { - JavaItem j = java.get("minecraft:white_shulker_box".replaceAll("white_", dyeColor.getName() + "_")); - // System.out.println(j.getIdentifier() + " " + convertions.get(j).get(0).getIdentifier() + ":" + convertions.get(j).get(0).getData()); - } - - for (Map.Entry> entry : convertions.entrySet()) { - for (BedrockItem item : entry.getValue()) { - JAVA_TO_BEDROCK.computeIfAbsent(entry.getKey().getIdentifier(), (x) -> new HashMap<>()); - BEDROCK_TO_JAVA.computeIfAbsent(item.getIdentifier(), (x) -> new HashMap<>()); - JAVA_TO_BEDROCK.computeIfAbsent(entry.getKey().getId(), (x) -> new HashMap<>()); - BEDROCK_TO_JAVA.computeIfAbsent(item.getId(), (x) -> new HashMap<>()); - Map map = JAVA_TO_BEDROCK.get(entry.getKey().getIdentifier()); - Map map2 = JAVA_TO_BEDROCK.get(entry.getKey().getId()); - - map.put("name", item.getIdentifier()); - map2.put("name", item.getIdentifier()); - map.put("id", item.getId()); - map2.put("id", item.getId()); - map.put("data", item.getData()); - map2.put("data", item.getData()); - - BEDROCK_TO_JAVA.get(item.getIdentifier()).computeIfAbsent(item.getData(), (x) -> new HashMap<>()); - BEDROCK_TO_JAVA.get(item.getId()).computeIfAbsent(item.getData(), (x) -> new HashMap<>()); - - BEDROCK_TO_JAVA.get(item.getIdentifier()).get(item.getData()).put("name", entry.getKey().getIdentifier()); - BEDROCK_TO_JAVA.get(item.getIdentifier()).get(item.getData()).put("id", entry.getKey().getId()); - - BEDROCK_TO_JAVA.get(item.getId()).get(item.getData()).put("name", entry.getKey().getIdentifier()); - BEDROCK_TO_JAVA.get(item.getId()).get(item.getData()).put("id", entry.getKey().getId()); - } - } - - // Uncomment this for updated mappings - writeMappings(); } - public static void addConversions2(Map items1, Map java) { - convertions.clear(); - for (StoneType type : StoneType.values()) { - specials.add(type.getName()); + public JavaItem convertToJava(ItemData item) { + for (Map.Entry bedrockItem : Toolbox.BEDROCK_ITEMS.entrySet()) { + if (bedrockItem.getValue().getId() != item.getId() || bedrockItem.getValue().getData() != item.getDamage()) + continue; + + return bedrockToJava.get(bedrockItem.getValue()); } - for (Map.Entry javaItem : java.entrySet()) { - for (Map.Entry bedrockItem : items1.entrySet()) { - for (Predicate> predicate : predicates.keySet()) { - BiValue b = new BiValue<>(javaItem.getKey(), bedrockItem.getKey()); - BiValue b2 = new BiValue<>(javaItem.getKey().replaceAll("_", ""), bedrockItem.getKey().replaceAll("_", "")); - if (predicate.test(b) || predicate.test(b2)) { - predicates.get(predicate).accept(bedrockItem.getValue(), javaItem.getValue()); - } - } - } - } - - //for(BedrockItem item : ) - - for (DyeColor dyeColor : DyeColor.values()) { - JavaItem j = java.get("minecraft:white_shulker_box".replaceAll("white_", dyeColor.getName() + "_")); - // System.out.println(j.getIdentifier() + " " + convertions.get(j).get(0).getIdentifier() + ":" + convertions.get(j).get(0).getData()); - } - - for (Map.Entry> entry : convertions.entrySet()) { - for (BedrockItem item : entry.getValue()) { - JAVA_TO_BEDROCK_BLOCKS.computeIfAbsent(entry.getKey().getIdentifier(), (x) -> new HashMap<>()); - BEDROCK_TO_JAVA_BLOCKS.computeIfAbsent(item.getIdentifier(), (x) -> new HashMap<>()); - JAVA_TO_BEDROCK_BLOCKS.computeIfAbsent(entry.getKey().getId(), (x) -> new HashMap<>()); - BEDROCK_TO_JAVA_BLOCKS.computeIfAbsent(item.getId(), (x) -> new HashMap<>()); - Map map = JAVA_TO_BEDROCK_BLOCKS.get(entry.getKey().getIdentifier()); - Map map2 = JAVA_TO_BEDROCK_BLOCKS.get(entry.getKey().getId()); - - map.put("name", item.getIdentifier()); - map2.put("name", item.getIdentifier()); - map.put("id", item.getId()); - map2.put("id", item.getId()); - map.put("data", item.getData()); - map2.put("data", item.getData()); - - BEDROCK_TO_JAVA_BLOCKS.get(item.getIdentifier()).computeIfAbsent(item.getData(), (x) -> new HashMap<>()); - BEDROCK_TO_JAVA_BLOCKS.get(item.getId()).computeIfAbsent(item.getData(), (x) -> new HashMap<>()); - - BEDROCK_TO_JAVA_BLOCKS.get(item.getIdentifier()).get(item.getData()).put("name", entry.getKey().getIdentifier()); - BEDROCK_TO_JAVA_BLOCKS.get(item.getIdentifier()).get(item.getData()).put("id", entry.getKey().getId()); - - BEDROCK_TO_JAVA_BLOCKS.get(item.getId()).get(item.getData()).put("name", entry.getKey().getIdentifier()); - BEDROCK_TO_JAVA_BLOCKS.get(item.getId()).get(item.getData()).put("id", entry.getKey().getId()); - } - } - - writeMappings2(); + return null; } - private static void writeMappings() { - ObjectMapper mapper = new ObjectMapper(); - ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); - try { - writer.writeValue(new File("java_to_bedrock.json"), JAVA_TO_BEDROCK); - writer.writeValue(new File("bedrock_to_java.json"), BEDROCK_TO_JAVA); - } catch (Exception e) { - e.printStackTrace(); + public BedrockItem convertToBedrock(ItemStack item) { + for (Map.Entry javaItem : Toolbox.JAVA_ITEMS.entrySet()) { + if (javaItem.getValue().getId() != item.getId()) + continue; + + return javaToBedrock.get(javaItem.getValue()); } + + return null; } - private static void writeMappings2() { - ObjectMapper mapper = new ObjectMapper(); - ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); - try { - writer.writeValue(new File("java_to_bedrock_blocks.json"), JAVA_TO_BEDROCK_BLOCKS); - writer.writeValue(new File("bedrock_to_java_blocks.json"), BEDROCK_TO_JAVA_BLOCKS); - } catch (Exception e) { - e.printStackTrace(); - } + private static String getBedrockIdentifier(String javaIdentifier) { + javaIdentifier = javaIdentifier.toLowerCase(); + javaIdentifier = javaIdentifier.replace("terracotta", "stained_hardened_clay"); + javaIdentifier = javaIdentifier.replace("slab", "wooden_slab"); + javaIdentifier = javaIdentifier.replace("concrete_powder", "concretePowder"); + return javaIdentifier; } - private static boolean notSpecial(String key) { - for (String spec : specials) { - if (key.contains(spec)) { - return false; - } - } - return true; - } - - private static boolean tool(String s) { + private static boolean isTool(String s) { return s.contains("shovel") || s.contains("sword") || s.contains("axe") || s.contains("pickaxe") || s.contains("spade") || s.contains("hoe"); } } \ No newline at end of file diff --git a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java index 5369dac6..c8889915 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java +++ b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java @@ -7,6 +7,7 @@ import com.nukkitx.protocol.bedrock.packet.StartGamePacket; import com.nukkitx.protocol.bedrock.v361.BedrockUtils; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.network.translators.item.BedrockItem; import org.geysermc.connector.network.translators.item.JavaItem; @@ -26,15 +27,15 @@ public class Toolbox { e.printStackTrace(); } - Map m = new HashMap<>(); - + Map bedrockBlocks = new HashMap<>(); Map bedrockItems = new HashMap<>(); + for (Map e : entries) { BedrockItem bedrockItem = new BedrockItem((String) e.get("name"), (int) e.get("id"), (int) e.get("data")); - m.put(bedrockItem.getIdentifier(), bedrockItem); + bedrockBlocks.put(bedrockItem.getIdentifier(), bedrockItem); + bedrockItems.put(bedrockItem.getIdentifier() + ":" + bedrockItem.getData(), bedrockItem); } - ByteBuf b = Unpooled.buffer(); VarInts.writeUnsignedInt(b, entries.size()); for (Map e : entries) { @@ -86,8 +87,6 @@ public class Toolbox { javaItems.put(str, new JavaItem(str, (int) javaItemList.get(str).get("protocol_id"))); } - Remapper.addConversions(bedrockItems, javaItems); - JAVA_ITEMS = javaItems; InputStream javaItemStream2 = Toolbox.class.getClassLoader().getResourceAsStream("java/java_blocks.json"); @@ -99,19 +98,22 @@ public class Toolbox { ex.printStackTrace(); } - Map javaItems2 = new HashMap(); + Map javaBlocks = new HashMap(); for (String str : javaItemList2.keySet()) { - javaItems2.put(str, new JavaItem(str, (int) javaItemList2.get(str).get("protocol_id"))); + javaBlocks.put(str, new JavaItem(str, (int) javaItemList2.get(str).get("protocol_id"))); } - JAVA_BLOCKS = javaItems2; + JAVA_BLOCKS = javaBlocks; + BEDROCK_BLOCKS = bedrockBlocks; - BEDROCK_BLOCKS = m; + GeyserLogger.DEFAULT.debug("Remapping items..."); + Remapper.ITEM_REMAPPER.registerConversions(bedrockItems, javaItems); + GeyserLogger.DEFAULT.debug("Item remap complete!"); - Remapper.addConversions(bedrockItems, javaItems); - - Remapper.addConversions2(m, javaItems2); + GeyserLogger.DEFAULT.debug("Remapping blocks..."); + Remapper.BLOCK_REMAPPER.registerConversions(bedrockBlocks, javaBlocks); + GeyserLogger.DEFAULT.debug("Block remap complete!"); } public static final Collection ITEMS;