Properly copy NBT data in containers

This commit is contained in:
dordsor21 2020-05-14 14:19:33 +01:00
parent 501fd9c8e6
commit 5f896dd39a

View File

@ -26,7 +26,6 @@
package com.plotsquared.bukkit.schematic; package com.plotsquared.bukkit.schematic;
import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.sk89q.jnbt.ByteTag; import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.CompoundTag;
@ -34,7 +33,9 @@ import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.ShortTag; import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag; import com.sk89q.jnbt.Tag;
import org.bukkit.Material; import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.item.ItemType;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.Container; import org.bukkit.block.Container;
@ -171,17 +172,13 @@ public class StateWrapper {
if (this.tag == null) { if (this.tag == null) {
return false; return false;
} }
String tileid = this.tag.getString("id").toLowerCase();
if (tileid.startsWith("minecraft:")) {
tileid = tileid.replace("minecraft:", "");
}
World world = BukkitUtil.getWorld(worldName); World world = BukkitUtil.getWorld(worldName);
Block block = world.getBlockAt(x, y, z); Block block = world.getBlockAt(x, y, z);
if (block == null) { if (block == null) {
return false; return false;
} }
org.bukkit.block.BlockState state = block.getState(); org.bukkit.block.BlockState state = block.getState();
switch (tileid) { switch (getId()) {
case "chest": case "chest":
case "beacon": case "beacon":
case "brewingstand": case "brewingstand":
@ -190,36 +187,27 @@ public class StateWrapper {
case "furnace": case "furnace":
case "hopper": case "hopper":
case "shulkerbox": case "shulkerbox":
if (!(state instanceof Container)) {
return false;
}
List<Tag> itemsTag = this.tag.getListTag("Items").getValue(); List<Tag> itemsTag = this.tag.getListTag("Items").getValue();
int length = itemsTag.size(); Container container = (Container) state;
String[] ids = new String[length]; Inventory inv = container.getSnapshotInventory();
byte[] amounts = new byte[length]; for (Tag itemTag : itemsTag) {
byte[] slots = new byte[length];
for (int i = 0; i < length; i++) {
Tag itemTag = itemsTag.get(i);
CompoundTag itemComp = (CompoundTag) itemTag; CompoundTag itemComp = (CompoundTag) itemTag;
String id = itemComp.getString("id"); ItemType type = ItemType.REGISTRY.get(itemComp.getString("id").toLowerCase());
if (id.startsWith("minecraft:")) { if (type == null) {
id = id.replace("minecraft:", ""); continue;
} }
ids[i] = id; int count = itemComp.getByte("Count");
amounts[i] = itemComp.getByte("Count"); int slot = itemComp.getByte("Slot");
slots[i] = itemComp.getByte("Slot"); CompoundTag tag = (CompoundTag) itemComp.getValue().get("tag");
BaseItemStack baseItemStack = new BaseItemStack(type, tag, count);
ItemStack itemStack = BukkitAdapter.adapt(baseItemStack);
inv.setItem(slot, itemStack);
} }
if (state instanceof Container) { container.update(true, false);
Container container = (Container) state; return true;
Inventory inv = container.getSnapshotInventory();
for (int i = 0; i < ids.length; i++) {
Material mat = Material.getMaterial(ids[i].toUpperCase());
if (mat != null) {
ItemStack item = new ItemStack(mat, (int) amounts[i]);
inv.setItem(slots[i], item);
}
}
container.update(true, true);
return true;
}
return false;
case "sign": case "sign":
if (state instanceof Sign) { if (state instanceof Sign) {
Sign sign = (Sign) state; Sign sign = (Sign) state;
@ -250,7 +238,11 @@ public class StateWrapper {
} }
public String getId() { public String getId() {
return "Chest"; String tileid = this.tag.getString("id").toLowerCase();
if (tileid.startsWith("minecraft:")) {
tileid = tileid.replace("minecraft:", "");
}
return tileid;
} }
public List<CompoundTag> serializeInventory(ItemStack[] items) { public List<CompoundTag> serializeInventory(ItemStack[] items) {