Fix inventory tiles not saving inventory
This commit is contained in:
parent
bd564ee7cf
commit
6a5f5356ce
|
@ -4,6 +4,8 @@ import net.minecraft.entity.item.EntityItem;
|
|||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ChatComponentTranslation;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
@ -21,9 +23,41 @@ public class TileInventory extends TileEntity implements IInventory {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound tagCompound) {
|
||||
super.readFromNBT(tagCompound);
|
||||
NBTTagList tags = tagCompound.getTagList("Items", 10);
|
||||
inventory = new ItemStack[getSizeInventory()];
|
||||
|
||||
for (int i = 0; i < tags.tagCount(); i++) {
|
||||
NBTTagCompound data = tags.getCompoundTagAt(i);
|
||||
int j = data.getByte("Slot") & 255;
|
||||
|
||||
if (j >= 0 && j < inventory.length) {
|
||||
inventory[j] = ItemStack.loadItemStackFromNBT(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound tagCompound) {
|
||||
super.writeToNBT(tagCompound);
|
||||
NBTTagList tags = new NBTTagList();
|
||||
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
if (inventory[i] != null) {
|
||||
NBTTagCompound data = new NBTTagCompound();
|
||||
data.setByte("Slot", (byte) i);
|
||||
inventory[i].writeToNBT(data);
|
||||
tags.appendTag(data);
|
||||
}
|
||||
}
|
||||
|
||||
tagCompound.setTag("Items", tags);
|
||||
}
|
||||
|
||||
public void dropItems() {
|
||||
if (inventory != null)
|
||||
{
|
||||
if (inventory != null) {
|
||||
for (ItemStack stack : inventory)
|
||||
getWorld().spawnEntityInWorld(new EntityItem(getWorld(), getPos().getX(), pos.getY(), pos.getZ(), stack));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue