Change TileEntityBookshelf to extend TileEntityLockableLoot, allowing it to generate loot from a loot table when opened

This commit is contained in:
Electroblob77
2020-07-06 17:46:14 +01:00
parent a78a4147db
commit e04e1e70af
@@ -5,13 +5,14 @@ import electroblob.wizardry.block.BlockBookshelf;
import electroblob.wizardry.inventory.ContainerBookshelf; import electroblob.wizardry.inventory.ContainerBookshelf;
import electroblob.wizardry.util.NBTExtras; import electroblob.wizardry.util.NBTExtras;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager; import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityLockableLoot;
import net.minecraft.util.ITickable; import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
@@ -19,7 +20,9 @@ import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.common.util.Constants.NBT;
public class TileEntityBookshelf extends TileEntity implements IInventory, ITickable { import javax.annotation.Nullable;
public class TileEntityBookshelf extends TileEntityLockableLoot implements ITickable {
/** The inventory of the bookshelf. */ /** The inventory of the bookshelf. */
private NonNullList<ItemStack> inventory; private NonNullList<ItemStack> inventory;
@@ -35,7 +38,8 @@ public class TileEntityBookshelf extends TileEntity implements IInventory, ITick
/** Called to manually sync the tile entity with clients. */ /** Called to manually sync the tile entity with clients. */
public void sync(){ public void sync(){
if(!this.doNotSync) this.world.markAndNotifyBlock(pos, null, world.getBlockState(pos), world.getBlockState(pos), 3); if(!this.doNotSync)
this.world.markAndNotifyBlock(pos, null, world.getBlockState(pos), world.getBlockState(pos), 3);
} }
@Override @Override
@@ -49,16 +53,15 @@ public class TileEntityBookshelf extends TileEntity implements IInventory, ITick
return inventory.size(); return inventory.size();
} }
@Override // Still better to override these three because then we can sync only when necessary
public ItemStack getStackInSlot(int slot){
return inventory.get(slot);
}
@Override @Override
public ItemStack decrStackSize(int slot, int amount){ public ItemStack decrStackSize(int slot, int amount){
this.fillWithLoot(null);
ItemStack stack = getStackInSlot(slot); ItemStack stack = getStackInSlot(slot);
if(!stack.isEmpty()){ if(!stack.isEmpty()){
if(stack.getCount() <= amount){ if(stack.getCount() <= amount){
setInventorySlotContents(slot, ItemStack.EMPTY); setInventorySlotContents(slot, ItemStack.EMPTY);
@@ -70,36 +73,38 @@ public class TileEntityBookshelf extends TileEntity implements IInventory, ITick
} }
this.markDirty(); this.markDirty();
} }
return stack; return stack;
} }
@Override @Override
public ItemStack removeStackFromSlot(int slot){ public ItemStack removeStackFromSlot(int slot){
this.fillWithLoot(null);
ItemStack stack = getStackInSlot(slot); ItemStack stack = getStackInSlot(slot);
if(!stack.isEmpty()){ if(!stack.isEmpty()){
setInventorySlotContents(slot, ItemStack.EMPTY); setInventorySlotContents(slot, ItemStack.EMPTY);
} }
return stack; return stack;
} }
@Override @Override
public void setInventorySlotContents(int slot, ItemStack stack){ public void setInventorySlotContents(int slot, ItemStack stack){
boolean wasEmpty = inventory.get(slot).isEmpty();
ItemStack previous = inventory.set(slot, stack); super.setInventorySlotContents(slot, stack);
// This must be done in the tile entity because containers only exist for player interaction, not hoppers etc. // This must be done in the tile entity because containers only exist for player interaction, not hoppers etc.
if(previous.isEmpty() != stack.isEmpty()) this.sync(); if(wasEmpty != stack.isEmpty()) this.sync();
if(!stack.isEmpty() && stack.getCount() > getInventoryStackLimit()){
stack.setCount(getInventoryStackLimit());
}
} }
@Override
public void fillWithLoot(@Nullable EntityPlayer player){
if(world.getLootTableManager() != null) super.fillWithLoot(player); // IntelliJ is wrong, it can be null
}
@Override @Override
public String getName(){ public String getName(){
return "container." + Wizardry.MODID + ":bookshelf"; return "container." + Wizardry.MODID + ":bookshelf";
@@ -145,14 +150,20 @@ public class TileEntityBookshelf extends TileEntity implements IInventory, ITick
super.readFromNBT(tagCompound); super.readFromNBT(tagCompound);
NBTTagList tagList = tagCompound.getTagList("Inventory", NBT.TAG_COMPOUND); if(!this.checkLootAndRead(tagCompound)){
for(int i = 0; i < tagList.tagCount(); i++){ // TODO: Replace with ItemStackHelper#loadAllItems
NBTTagCompound tag = tagList.getCompoundTagAt(i); NBTTagList tagList = tagCompound.getTagList("Inventory", NBT.TAG_COMPOUND);
byte slot = tag.getByte("Slot");
if(slot >= 0 && slot < getSizeInventory()){ for(int i = 0; i < tagList.tagCount(); i++){
setInventorySlotContents(slot, new ItemStack(tag)); NBTTagCompound tag = tagList.getCompoundTagAt(i);
byte slot = tag.getByte("Slot");
if(slot >= 0 && slot < getSizeInventory()){
setInventorySlotContents(slot, new ItemStack(tag));
}
} }
} }
if(tagCompound.hasKey("CustomName", NBT.TAG_STRING)) this.customName = tagCompound.getString("CustomName");
} }
@Override @Override
@@ -160,16 +171,25 @@ public class TileEntityBookshelf extends TileEntity implements IInventory, ITick
super.writeToNBT(tagCompound); super.writeToNBT(tagCompound);
NBTTagList itemList = new NBTTagList(); if(!this.checkLootAndWrite(tagCompound)){
for(int i = 0; i < getSizeInventory(); i++){
ItemStack stack = getStackInSlot(i); // TODO: Replace with ItemStackHelper#saveAllItems
NBTTagCompound tag = new NBTTagCompound();
tag.setByte("Slot", (byte)i); NBTTagList itemList = new NBTTagList();
stack.writeToNBT(tag);
itemList.appendTag(tag); for(int i = 0; i < getSizeInventory(); i++){
ItemStack stack = getStackInSlot(i);
NBTTagCompound tag = new NBTTagCompound();
tag.setByte("Slot", (byte)i);
stack.writeToNBT(tag);
itemList.appendTag(tag);
}
NBTExtras.storeTagSafely(tagCompound, "Inventory", itemList);
} }
NBTExtras.storeTagSafely(tagCompound, "Inventory", itemList); if(this.hasCustomName()) tagCompound.setString("CustomName", this.customName);
return tagCompound; return tagCompound;
} }
@@ -223,4 +243,20 @@ public class TileEntityBookshelf extends TileEntity implements IInventory, ITick
return true; return true;
} }
@Override
protected NonNullList<ItemStack> getItems(){
return inventory;
}
@Override
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer player){
this.fillWithLoot(player);
return new ContainerBookshelf(playerInventory, this);
}
@Override
public String getGuiID(){
return Wizardry.MODID + ":bookshelf";
}
} }