Skip to content

Commit

Permalink
Fix conversion of some items
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rutgerkok committed Jul 23, 2018
1 parent dc11447 commit 70c10bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>nl.rutgerkok.betterenderchest</groupId>
<artifactId>BetterEnderChest</artifactId>
<version>2.5.1-SNAPSHOT</version>
<version>2.5.1</version>
<name>BetterEnderChest</name>
<description>CraftBukkit plugin that adds functionality for both players and admins to the Ender Chest.</description>
<url>https://github.com/rutgerkok/BetterEnderChest</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<NBTBase> input = new Dynamic<NBTBase>(DynamicOpsNBT.a, item);
Dynamic<NBTBase> result = DataConverterRegistry.a().update(DataConverterTypes.ITEM_STACK, input, oldVersion, newVersion);
Dynamic<NBTBase> input = new Dynamic<>(DynamicOpsNBT.a, item);
Dynamic<NBTBase> result = DataConverterRegistry.a().update(DataConverterTypes.ITEM_STACK, input, oldVersion,
newVersion);
return (NBTTagCompound) result.getValue();
}

Expand Down

0 comments on commit 70c10bf

Please sign in to comment.