From 7dae4cbd0f1999482a352d3cf4ef0e8caecae253 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Fri, 9 Aug 2019 16:09:47 -0500 Subject: [PATCH] Fix NPE for items with missing mappings (Closes #24) --- .../org/geysermc/connector/GeyserConnector.java | 13 ++++--------- .../network/translators/item/BedrockItem.java | 2 ++ .../network/translators/item/ItemTranslator.java | 10 ++++++++++ .../network/translators/item/JavaItem.java | 2 ++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 0007497c..9a55c650 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -119,10 +119,6 @@ public class GeyserConnector implements Connector { shutdown(); } - metrics = new Metrics("GeyserMC", instance.getConfig().getUUID(), true, java.util.logging.Logger.getLogger("")); - - addMetrics(metrics); - logger.setDebug(config.isDebugMode()); Toolbox.CACHED_PALLETE.array(); @@ -151,6 +147,10 @@ public class GeyserConnector implements Connector { throwable.printStackTrace(); } }).join(); + + metrics = new Metrics("GeyserMC", instance.getConfig().getUUID(), true, java.util.logging.Logger.getLogger("")); + metrics.addCustomChart(new Metrics.SingleLineChart("servers", () -> 1)); + metrics.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount)); } public Collection getConnectedPlayers() { @@ -179,9 +179,4 @@ public class GeyserConnector implements Connector { players.remove(player.getAuthenticationData().getName()); players.remove(player.getAuthenticationData().getUUID()); } - - private static void addMetrics(Metrics m) { - m.addCustomChart(new Metrics.SingleLineChart("servers", () -> 1)); - m.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount)); - } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/BedrockItem.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/BedrockItem.java index 2537e564..dbfa8672 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/BedrockItem.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/BedrockItem.java @@ -32,6 +32,8 @@ import lombok.Getter; @AllArgsConstructor public class BedrockItem { + public static BedrockItem AIR = new BedrockItem("minecraft:air", 0, 0); + private String identifier; private int id; private int data; 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 dfc01970..7fcb72ec 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 @@ -42,6 +42,7 @@ import com.github.steveice10.opennbt.tag.builtin.ShortTag; 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; @@ -78,11 +79,20 @@ public class ItemTranslator { public static BedrockItem getBedrockItem(ItemStack stack) { Map m = Remapper.JAVA_TO_BEDROCK.get(stack.getId()); + if (m == 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")); } 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()); + + return JavaItem.AIR; + } return new JavaItem((String) m.get("name"), (Integer) m.get("id")); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/JavaItem.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/JavaItem.java index 1d22ff7a..288420b4 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/JavaItem.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/JavaItem.java @@ -32,6 +32,8 @@ import lombok.Getter; @AllArgsConstructor public class JavaItem { + public static JavaItem AIR = new JavaItem("minecraft:air", 0); + private String identifier; private int id;