Add support for loot tables, fix #76.

This commit is contained in:
alexbegt 2016-07-03 19:56:39 -04:00
parent 7c8d3986e7
commit cac7322894
2 changed files with 79 additions and 113 deletions

View File

@ -11,7 +11,6 @@
package cpw.mods.ironchest; package cpw.mods.ironchest;
import java.util.List; import java.util.List;
import java.util.Random;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
@ -21,13 +20,12 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs; import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType; import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
@ -178,52 +176,17 @@ public class BlockIronChest extends Block
} }
@Override @Override
public void breakBlock(World world, BlockPos pos, IBlockState blockState) public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{ {
TileEntityIronChest tileentitychest = (TileEntityIronChest) world.getTileEntity(pos); TileEntityIronChest tileentity = (TileEntityIronChest) worldIn.getTileEntity(pos);
if (tileentitychest != null)
{
tileentitychest.removeAdornments();
this.dropContent(0, tileentitychest, world, tileentitychest.getPos());
}
super.breakBlock(world, pos, blockState);
}
public void dropContent(int newSize, IInventory chest, World world, BlockPos pos) if (tileentity != null)
{ {
Random random = world.rand; tileentity.removeAdornments();
for (int l = newSize; l < chest.getSizeInventory(); l++) InventoryHelper.dropInventoryItems(worldIn, pos, tileentity);
{
ItemStack itemstack = chest.getStackInSlot(l);
if (itemstack == null)
{
continue;
}
float f = random.nextFloat() * 0.8F + 0.1F;
float f1 = random.nextFloat() * 0.8F + 0.1F;
float f2 = random.nextFloat() * 0.8F + 0.1F;
while (itemstack.stackSize > 0)
{
int i1 = random.nextInt(21) + 10;
if (i1 > itemstack.stackSize)
{
i1 = itemstack.stackSize;
}
itemstack.stackSize -= i1;
EntityItem entityitem = new EntityItem(world, pos.getX() + f, (float) pos.getY() + (newSize > 0 ? 1 : 0) + f1, pos.getZ() + f2,
new ItemStack(itemstack.getItem(), i1, itemstack.getMetadata()));
float f3 = 0.05F;
entityitem.motionX = (float) random.nextGaussian() * f3;
entityitem.motionY = (float) random.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float) random.nextGaussian() * f3;
if (itemstack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}
world.spawnEntityInWorld(entityitem);
}
} }
super.breakBlock(worldIn, pos, state);
} }
@Override @Override

View File

@ -22,19 +22,20 @@ import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.SoundEvents; import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ItemStackHelper;
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.TileEntityLockable; import net.minecraft.tileentity.TileEntityLockableLoot;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable; import net.minecraft.util.ITickable;
import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.Constants;
public class TileEntityIronChest extends TileEntityLockable implements ITickable, IInventory public class TileEntityIronChest extends TileEntityLockableLoot implements ITickable, IInventory
{ {
private int ticksSinceSync = -1; private int ticksSinceSync = -1;
public float prevLidAngle; public float prevLidAngle;
@ -95,6 +96,7 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
if (this.hasWorldObj()) if (this.hasWorldObj())
{ {
IBlockState state = this.worldObj.getBlockState(this.pos); IBlockState state = this.worldObj.getBlockState(this.pos);
if (state.getBlock() == IronChest.ironChestBlock) if (state.getBlock() == IronChest.ironChestBlock)
{ {
type = state.getValue(BlockIronChest.VARIANT_PROP); type = state.getValue(BlockIronChest.VARIANT_PROP);
@ -107,6 +109,8 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
@Override @Override
public ItemStack getStackInSlot(int index) public ItemStack getStackInSlot(int index)
{ {
this.fillWithLoot((EntityPlayer) null);
this.inventoryTouched = true; this.inventoryTouched = true;
return this.chestContents[index]; return this.chestContents[index];
@ -218,41 +222,26 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
} }
@Override @Override
@Nullable
public ItemStack decrStackSize(int index, int count) public ItemStack decrStackSize(int index, int count)
{ {
if (this.chestContents[index] != null) this.fillWithLoot((EntityPlayer) null);
{
if (this.chestContents[index].stackSize <= count)
{
ItemStack stack = this.chestContents[index];
this.chestContents[index] = null; ItemStack itemstack = ItemStackHelper.getAndSplit(this.chestContents, index, count);
if (itemstack != null)
{
this.markDirty(); this.markDirty();
return stack;
} }
ItemStack stack = this.chestContents[index].splitStack(count); return itemstack;
if (this.chestContents[index].stackSize == 0)
{
this.chestContents[index] = null;
}
this.markDirty();
return stack;
}
else
{
return null;
}
} }
@Override @Override
public void setInventorySlotContents(int index, @Nullable ItemStack stack) public void setInventorySlotContents(int index, @Nullable ItemStack stack)
{ {
this.fillWithLoot((EntityPlayer) null);
this.chestContents[index] = stack; this.chestContents[index] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit()) if (stack != null && stack.stackSize > this.getInventoryStackLimit())
@ -285,8 +274,6 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
{ {
super.readFromNBT(compound); super.readFromNBT(compound);
NBTTagList tagList = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
this.chestContents = new ItemStack[this.getSizeInventory()]; this.chestContents = new ItemStack[this.getSizeInventory()];
if (compound.hasKey("CustomName", Constants.NBT.TAG_STRING)) if (compound.hasKey("CustomName", Constants.NBT.TAG_STRING))
@ -294,15 +281,20 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
this.customName = compound.getString("CustomName"); this.customName = compound.getString("CustomName");
} }
for (int i = 0; i < tagList.tagCount(); i++) if (!this.checkLootAndRead(compound))
{ {
NBTTagCompound tag = tagList.getCompoundTagAt(i); NBTTagList itemList = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
int slot = tag.getByte("Slot") & 0xff; for (int itemNumber = 0; itemNumber < itemList.tagCount(); ++itemNumber)
{
NBTTagCompound item = itemList.getCompoundTagAt(itemNumber);
int slot = item.getByte("Slot") & 255;
if (slot >= 0 && slot < this.chestContents.length) if (slot >= 0 && slot < this.chestContents.length)
{ {
this.chestContents[slot] = ItemStack.loadItemStackFromNBT(tag); this.chestContents[slot] = ItemStack.loadItemStackFromNBT(item);
}
} }
} }
@ -316,9 +308,11 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
{ {
super.writeToNBT(compound); super.writeToNBT(compound);
NBTTagList tagList = new NBTTagList(); if (!this.checkLootAndWrite(compound))
{
NBTTagList itemList = new NBTTagList();
for (int slot = 0; slot < this.chestContents.length; slot++) for (int slot = 0; slot < this.chestContents.length; ++slot)
{ {
if (this.chestContents[slot] != null) if (this.chestContents[slot] != null)
{ {
@ -328,11 +322,12 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
this.chestContents[slot].writeToNBT(tag); this.chestContents[slot].writeToNBT(tag);
tagList.appendTag(tag); itemList.appendTag(tag);
} }
} }
compound.setTag("Items", tagList); compound.setTag("Items", itemList);
}
compound.setByte("facing", (byte) this.facing.ordinal()); compound.setByte("facing", (byte) this.facing.ordinal());
@ -370,7 +365,6 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
public void update() public void update()
{ {
// Resynchronizes clients with the server state // Resynchronizes clients with the server state
//@formatter:off //@formatter:off
if (this.worldObj != null && !this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + this.pos.getX() + this.pos.getY() + this.pos.getZ()) % 200 == 0) if (this.worldObj != null && !this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + this.pos.getX() + this.pos.getY() + this.pos.getZ()) % 200 == 0)
//@formatter:on //@formatter:on
@ -492,9 +486,11 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
this.numPlayersUsing = 0; this.numPlayersUsing = 0;
} }
this.numPlayersUsing++; ++this.numPlayersUsing;
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 1, this.numPlayersUsing); this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 1, this.numPlayersUsing);
this.worldObj.notifyNeighborsOfStateChange(this.pos, IronChest.ironChestBlock);
this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), IronChest.ironChestBlock);
} }
} }
@ -508,9 +504,11 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
return; return;
} }
this.numPlayersUsing--; --this.numPlayersUsing;
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 1, this.numPlayersUsing); this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 1, this.numPlayersUsing);
this.worldObj.notifyNeighborsOfStateChange(this.pos, IronChest.ironChestBlock);
this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), IronChest.ironChestBlock);
} }
} }
@ -528,22 +526,30 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
public SPacketUpdateTileEntity getUpdatePacket() public SPacketUpdateTileEntity getUpdatePacket()
{ {
NBTTagCompound compound = new NBTTagCompound(); NBTTagCompound compound = new NBTTagCompound();
compound.setByte("facing", (byte) this.facing.ordinal()); compound.setByte("facing", (byte) this.facing.ordinal());
ItemStack[] stacks = this.buildItemStackDataList(); ItemStack[] stacks = this.buildItemStackDataList();
if (stacks != null) if (stacks != null)
{ {
NBTTagList tagList = new NBTTagList(); NBTTagList itemList = new NBTTagList();
for (int slot = 0; slot < stacks.length; slot++) for (int slot = 0; slot < stacks.length; slot++)
{ {
if (stacks[slot] != null) if (stacks[slot] != null)
{ {
NBTTagCompound tag = new NBTTagCompound(); NBTTagCompound item = new NBTTagCompound();
tag.setByte("Slot", (byte) slot);
stacks[slot].writeToNBT(tag); item.setByte("Slot", (byte) slot);
tagList.appendTag(tag);
stacks[slot].writeToNBT(item);
itemList.appendTag(item);
} }
} }
compound.setTag("stacks", tagList);
compound.setTag("stacks", itemList);
} }
return new SPacketUpdateTileEntity(this.pos, 0, compound); return new SPacketUpdateTileEntity(this.pos, 0, compound);
@ -558,15 +564,15 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
this.facing = EnumFacing.VALUES[compound.getByte("facing")]; this.facing = EnumFacing.VALUES[compound.getByte("facing")];
NBTTagList tagList = compound.getTagList("stacks", Constants.NBT.TAG_COMPOUND); NBTTagList itemList = compound.getTagList("stacks", Constants.NBT.TAG_COMPOUND);
ItemStack[] stacks = new ItemStack[this.topStacks.length]; ItemStack[] stacks = new ItemStack[this.topStacks.length];
for (int item = 0; item < stacks.length; item++) for (int item = 0; item < stacks.length; item++)
{ {
NBTTagCompound itemStack = tagList.getCompoundTagAt(item); NBTTagCompound itemStack = itemList.getCompoundTagAt(item);
int slot = itemStack.getByte("Slot") & 0xff; int slot = itemStack.getByte("Slot") & 255;
if (slot >= 0 && slot < stacks.length) if (slot >= 0 && slot < stacks.length)
{ {
@ -622,20 +628,12 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
} }
@Override @Override
@Nullable
public ItemStack removeStackFromSlot(int index) public ItemStack removeStackFromSlot(int index)
{ {
if (this.chestContents[index] != null) this.fillWithLoot((EntityPlayer) null);
{
ItemStack stack = this.chestContents[index];
this.chestContents[index] = null; return ItemStackHelper.getAndRemove(this.chestContents, index);
return stack;
}
else
{
return null;
}
} }
@Override @Override
@ -647,6 +645,7 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
public void rotateAround() public void rotateAround()
{ {
this.setFacing(this.facing.rotateY()); this.setFacing(this.facing.rotateY());
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 2, this.facing.ordinal()); this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 2, this.facing.ordinal());
} }
@ -678,6 +677,8 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
@Override @Override
public void clear() public void clear()
{ {
this.fillWithLoot((EntityPlayer) null);
for (int slot = 0; slot < this.chestContents.length; ++slot) for (int slot = 0; slot < this.chestContents.length; ++slot)
{ {
this.chestContents[slot] = null; this.chestContents[slot] = null;
@ -687,6 +688,8 @@ public class TileEntityIronChest extends TileEntityLockable implements ITickable
@Override @Override
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
{ {
this.fillWithLoot((EntityPlayer) null);
return new ContainerIronChest(playerInventory, this, this.chestType, this.chestType.xSize, this.chestType.ySize); return new ContainerIronChest(playerInventory, this, this.chestType, this.chestType.xSize, this.chestType.ySize);
} }