From 70c10bf44e19372d44a0b0c55ca827828dee99a3 Mon Sep 17 00:00:00 2001 From: Rutger Kok Date: Mon, 23 Jul 2018 21:12:04 +0200 Subject: [PATCH] Fix conversion of some items Items should be converted from Minecraft 1.12 to Minecraft 1.13. However, passing an "old" version of -1 seems to make Minecraft upgrade the item as if it were an item from Minecraft 1.7. This leads to garbage results for shulker boxes. A fix has been included to un-garbage already converted shulker boxes. --- pom.xml | 2 +- .../nms/SimpleNMSHandler.java | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 628ebca..5601d6d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 nl.rutgerkok.betterenderchest BetterEnderChest - 2.5.1-SNAPSHOT + 2.5.1 BetterEnderChest CraftBukkit plugin that adds functionality for both players and admins to the Ender Chest. https://github.com/rutgerkok/BetterEnderChest diff --git a/src/main/java/nl/rutgerkok/betterenderchest/nms/SimpleNMSHandler.java b/src/main/java/nl/rutgerkok/betterenderchest/nms/SimpleNMSHandler.java index f7ee4cf..93c1557 100644 --- a/src/main/java/nl/rutgerkok/betterenderchest/nms/SimpleNMSHandler.java +++ b/src/main/java/nl/rutgerkok/betterenderchest/nms/SimpleNMSHandler.java @@ -38,7 +38,6 @@ import net.minecraft.server.v1_13_R1.NBTTagString; import net.minecraft.server.v1_13_R1.TileEntity; import net.minecraft.server.v1_13_R1.TileEntityEnderChest; - import nl.rutgerkok.betterenderchest.BetterEnderChest; import nl.rutgerkok.betterenderchest.BetterEnderInventoryHolder; import nl.rutgerkok.betterenderchest.ChestRestrictions; @@ -198,7 +197,9 @@ private static class TagType { private static final int COMPOUND = 10; } - private BetterEnderChest plugin; + private static final int DATA_VERSION_MC_1_12_2 = 1343; + + private final BetterEnderChest plugin; public SimpleNMSHandler(BetterEnderChest plugin) { this.plugin = plugin; @@ -244,7 +245,7 @@ private int getRows(ChestOwner chestOwner, NBTTagCompound baseTag, NBTTagList in private int getStoredDataVersion(NBTTagCompound baseTag) { if (!baseTag.hasKey("DataVersion")) { - return -1; + return DATA_VERSION_MC_1_12_2; } return baseTag.getInt("DataVersion"); } @@ -326,6 +327,33 @@ public void openEnderChest(Location loc) { } } + private NBTTagCompound repairShulkerBoxes(NBTTagCompound item) { + NBTTagCompound itemTag = item.getCompound("tag"); + if (!itemTag.hasKey("BlockEntityTag")) { + return item; + } + NBTTagCompound blockEntityTag = itemTag.getCompound("BlockEntityTag"); + if (!blockEntityTag.hasKey("Items")) { + return item; + } + NBTTagList items = blockEntityTag.getList("Items", TagType.COMPOUND); + if (items.isEmpty()) { + return item; + } + NBTTagCompound firstItem = items.getCompound(0); + if (!firstItem.hasKey("Damage")) { + return item; + } + + // Ok, conversion failed. Downgrade item to 1.12.2 and try again + item.setByte("Damage", (byte) 0); + if (itemTag.hasKey("display")) { + NBTTagCompound displayTag = itemTag.getCompound("display"); + displayTag.setString("Name", blockEntityTag.getString("CustomName")); + } + return this.updateToLatestMinecraft(item, DATA_VERSION_MC_1_12_2); + } + @Override public void saveInventoryToFile(File file, SaveEntry saveEntry) throws IOException { FileOutputStream stream = null; @@ -387,11 +415,17 @@ private NBTTagCompound updateToLatestMinecraft(NBTTagCompound item, int oldVersi @SuppressWarnings("deprecation") int newVersion = Bukkit.getUnsafe().getDataVersion(); if (newVersion == oldVersion) { + // Check if update was correct (there used to be a bug) + if (item.hasKey("tag") && item.getString("id").endsWith("shulker_box")) { + return repairShulkerBoxes(item); + } + return item; } - Dynamic input = new Dynamic(DynamicOpsNBT.a, item); - Dynamic result = DataConverterRegistry.a().update(DataConverterTypes.ITEM_STACK, input, oldVersion, newVersion); + Dynamic input = new Dynamic<>(DynamicOpsNBT.a, item); + Dynamic result = DataConverterRegistry.a().update(DataConverterTypes.ITEM_STACK, input, oldVersion, + newVersion); return (NBTTagCompound) result.getValue(); }