--- a/net/minecraft/server/TileEntity.java +++ b/net/minecraft/server/TileEntity.java @@ -4,9 +4,18 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.util.Supplier; +// CraftBukkit start +import org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer; +import org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry; +import org.bukkit.inventory.InventoryHolder; +// CraftBukkit end public abstract class TileEntity { + // CraftBukkit start - data containers + private static final CraftPersistentDataTypeRegistry DATA_TYPE_REGISTRY = new CraftPersistentDataTypeRegistry(); + public CraftPersistentDataContainer persistentDataContainer; + // CraftBukkit end private static final Logger LOGGER = LogManager.getLogger(); private final TileEntityTypes b; @Nullable @@ -37,6 +46,14 @@ public void load(NBTTagCompound nbttagcompound) { this.position = new BlockPosition(nbttagcompound.getInt("x"), nbttagcompound.getInt("y"), nbttagcompound.getInt("z")); + // CraftBukkit start - read container + this.persistentDataContainer = new CraftPersistentDataContainer(DATA_TYPE_REGISTRY); + + NBTTagCompound persistentDataTag = nbttagcompound.getCompound("PublicBukkitValues"); + if (persistentDataTag != null) { + this.persistentDataContainer.putAll(persistentDataTag); + } + // CraftBukkit end } public NBTTagCompound save(NBTTagCompound nbttagcompound) { @@ -53,12 +70,24 @@ nbttagcompound.setInt("x", this.position.getX()); nbttagcompound.setInt("y", this.position.getY()); nbttagcompound.setInt("z", this.position.getZ()); + // CraftBukkit start - store container + if (this.persistentDataContainer != null && !this.persistentDataContainer.isEmpty()) { + nbttagcompound.set("PublicBukkitValues", this.persistentDataContainer.toTagCompound()); + } + // CraftBukkit end return nbttagcompound; } } + // CraftBukkit start @Nullable public static TileEntity create(NBTTagCompound nbttagcompound) { + return create(nbttagcompound, null); + } + + @Nullable + public static TileEntity create(NBTTagCompound nbttagcompound, @Nullable World world) { + // CraftBukkit end String s = nbttagcompound.getString("id"); return (TileEntity) IRegistry.BLOCK_ENTITY_TYPE.getOptional(new MinecraftKey(s)).map((tileentitytypes) -> { @@ -70,6 +99,7 @@ } }).map((tileentity) -> { try { + tileentity.setWorld(world); // CraftBukkit tileentity.load(nbttagcompound); return tileentity; } catch (Throwable throwable) { @@ -168,4 +198,13 @@ }, this::getPosition}); } } + + // CraftBukkit start - add method + public InventoryHolder getOwner() { + if (world == null) return null; + org.bukkit.block.BlockState state = world.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()).getState(); + if (state instanceof InventoryHolder) return (InventoryHolder) state; + return null; + } + // CraftBukkit end }