Hello,
I am making a mod and what I am trying to do is when my chest is added to the world, it will spawn with an item inside the chest. Right now I have a problem where whenever my custom chest is placed in the world, the items aren't in the chest, unless I reload the world. Here is both my BlockLockedChest file and TileEntityLockedChest file. Some of the code is a bit messy so...
BlockLockedChest.java
package com.angelo.venturemod.blocks;
import static net.minecraftforge.common.util.ForgeDirection.DOWN;
import java.util.Iterator;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockChest;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.ChestGenHooks;
import com.angelo.venturemod.VentureMod;
import com.angelo.venturemod.items.Items;
import com.angelo.venturemod.tileentities.TileEntityLockedChest;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockLockedChest extends BlockChest{
private final Random field_149955_b = new Random();
TileEntityLockedChest telockedchest;
//IInventory iinventory;
protected BlockLockedChest(int par1) {
super(par1);
this.setBlockName("Locked Chest");
this.setBlockTextureName(VentureMod.MODID+":Locked Chest");
this.setBlockUnbreakable();
this.setCreativeTab(VentureMod.ventureBlocksTab);
}
@Override
public boolean canHarvestBlock(EntityPlayer player, int meta){
return false;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer ep, int side, float offsetX, float offsetY, float offsetZ){
if(!world.isRemote){
//iinventory = this.func_149951_m(world, x, y, z);
telockedchest = (TileEntityLockedChest)world.getTileEntity(x, y, z);
if(ep.getHeldItem() != null){
if(ep.getHeldItem().getItem() == Items.key){
if(telockedchest != null){
ep.displayGUIChest(telockedchest);
ep.inventory.consumeInventoryItem(Items.key);
telockedchest.markDirty();
}
}
else{
ep.addChatMessage(new ChatComponentTranslation("You need to use a key to open this chest."));
}
}
else{
ep.addChatMessage(new ChatComponentTranslation("You cannot use your hands to open this chest."));
}
}
else{
return true;
}
return false;
}
@Override
public void onBlockAdded(World world, int x, int y, int z){
super.onBlockAdded(world, x, y, z);
this.func_149954_e(world, x, y, z);
Block block = world.getBlock(x, y, z - 1);
Block block1 = world.getBlock(x, y, z + 1);
Block block2 = world.getBlock(x - 1, y, z);
Block block3 = world.getBlock(x + 1, y, z);
if (block == this)
{
this.func_149954_e(world, x, y, z - 1);
}
if (block1 == this)
{
this.func_149954_e(world, x, y, z + 1);
}
if (block2 == this)
{
this.func_149954_e(world, x - 1, y, z);
}
if (block3 == this)
{
this.func_149954_e(world, x + 1, y, z);
}
telockedchest = (TileEntityLockedChest)world.getTileEntity(x, y, z);
int i;
int j;
Random rand = new Random();
telockedchest.setInventorySlotContents(i = rand.nextInt(27), Items.getRandomItem());
j = rand.nextInt(27);
while(j == i){
j = rand.nextInt(27);
}
telockedchest.setInventorySlotContents(j, Items.getRandomItem());
telockedchest.markDirty();
}
@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_)
{
TileEntityLockedChest tileentitylockedchest = new TileEntityLockedChest();
return tileentitylockedchest;
}
/**
* Called when the block is placed in the world.
*/
public void onBlockPlacedBy(World p_149689_1_, int p_149689_2_, int p_149689_3_, int p_149689_4_, EntityLivingBase p_149689_5_, ItemStack p_149689_6_)
{
Block block = p_149689_1_.getBlock(p_149689_2_, p_149689_3_, p_149689_4_ - 1);
Block block1 = p_149689_1_.getBlock(p_149689_2_, p_149689_3_, p_149689_4_ + 1);
Block block2 = p_149689_1_.getBlock(p_149689_2_ - 1, p_149689_3_, p_149689_4_);
Block block3 = p_149689_1_.getBlock(p_149689_2_ + 1, p_149689_3_, p_149689_4_);
byte b0 = 0;
int l = MathHelper.floor_double((double)(p_149689_5_.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
if (l == 0)
{
b0 = 2;
}
if (l == 1)
{
b0 = 5;
}
if (l == 2)
{
b0 = 3;
}
if (l == 3)
{
b0 = 4;
}
if (block != this && block1 != this && block2 != this && block3 != this)
{
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, b0, 3);
}
else
{
if ((block == this || block1 == this) && (b0 == 4 || b0 == 5))
{
if (block == this)
{
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_ - 1, b0, 3);
}
else
{
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_ + 1, b0, 3);
}
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, b0, 3);
}
if ((block2 == this || block3 == this) && (b0 == 2 || b0 == 3))
{
if (block2 == this)
{
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_ - 1, p_149689_3_, p_149689_4_, b0, 3);
}
else
{
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_ + 1, p_149689_3_, p_149689_4_, b0, 3);
}
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, b0, 3);
}
}
if (p_149689_6_.hasDisplayName())
{
((TileEntityLockedChest)p_149689_1_.getTileEntity(p_149689_2_, p_149689_3_, p_149689_4_)).func_145976_a(p_149689_6_.getDisplayName());
}
}
/**
* Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
* their own) Args: x, y, z, neighbor Block
*/
public void onNeighborBlockChange(World p_149695_1_, int p_149695_2_, int p_149695_3_, int p_149695_4_, Block p_149695_5_)
{
super.onNeighborBlockChange(p_149695_1_, p_149695_2_, p_149695_3_, p_149695_4_, p_149695_5_);
TileEntityLockedChest tileentitychest = (TileEntityLockedChest)p_149695_1_.getTileEntity(p_149695_2_, p_149695_3_, p_149695_4_);
if (tileentitychest != null)
{
tileentitychest.updateContainingBlockInfo();
}
}
public void breakBlock(World p_149749_1_, int p_149749_2_, int p_149749_3_, int p_149749_4_, Block p_149749_5_, int p_149749_6_)
{
TileEntityLockedChest tileentitychest = (TileEntityLockedChest)p_149749_1_.getTileEntity(p_149749_2_, p_149749_3_, p_149749_4_);
if (tileentitychest != null)
{
for (int i1 = 0; i1 < tileentitychest.getSizeInventory(); ++i1)
{
ItemStack itemstack = tileentitychest.getStackInSlot(i1);
if (itemstack != null)
{
float f = this.field_149955_b.nextFloat() * 0.8F + 0.1F;
float f1 = this.field_149955_b.nextFloat() * 0.8F + 0.1F;
EntityItem entityitem;
for (float f2 = this.field_149955_b.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; p_149749_1_.spawnEntityInWorld(entityitem))
{
int j1 = this.field_149955_b.nextInt(21) + 10;
if (j1 > itemstack.stackSize)
{
j1 = itemstack.stackSize;
}
itemstack.stackSize -= j1;
entityitem = new EntityItem(p_149749_1_, (double)((float)p_149749_2_ + f), (double)((float)p_149749_3_ + f1), (double)((float)p_149749_4_ + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
float f3 = 0.05F;
entityitem.motionX = (double)((float)this.field_149955_b.nextGaussian() * f3);
entityitem.motionY = (double)((float)this.field_149955_b.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double)((float)this.field_149955_b.nextGaussian() * f3);
if (itemstack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
}
}
}
}
p_149749_1_.func_147453_f(p_149749_2_, p_149749_3_, p_149749_4_, p_149749_5_);
}
super.breakBlock(p_149749_1_, p_149749_2_, p_149749_3_, p_149749_4_, p_149749_5_, p_149749_6_);
}
private static boolean func_149953_o(World p_149953_0_, int p_149953_1_, int p_149953_2_, int p_149953_3_)
{
return false;
}
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister p_149651_1_)
{
this.blockIcon = p_149651_1_.registerIcon(VentureMod.MODID + ":Locked Chest");
}
/**
* Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
*/
public boolean canPlaceBlockAt(World p_149742_1_, int p_149742_2_, int p_149742_3_, int p_149742_4_)
{
int l = 0;
if (p_149742_1_.getBlock(p_149742_2_ - 1, p_149742_3_, p_149742_4_) == this || p_149742_1_.getBlock(p_149742_2_ - 1, p_149742_3_, p_149742_4_) == Blocks.chest)
{
++l;
return false;
}
if (p_149742_1_.getBlock(p_149742_2_ + 1, p_149742_3_, p_149742_4_) == this || p_149742_1_.getBlock(p_149742_2_ + 1, p_149742_3_, p_149742_4_) == Blocks.chest)
{
++l;
return false;
}
if (p_149742_1_.getBlock(p_149742_2_, p_149742_3_, p_149742_4_ - 1) == this || p_149742_1_.getBlock(p_149742_2_, p_149742_3_, p_149742_4_ -1) == Blocks.chest)
{
++l;
return false;
}
if (p_149742_1_.getBlock(p_149742_2_, p_149742_3_, p_149742_4_ + 1) == this || p_149742_1_.getBlock(p_149742_2_, p_149742_3_, p_149742_4_ + 1) == Blocks.chest)
{
++l;
return false;
}
return true;
}
}
TileEntityLockedChest.java
package com.angelo.venturemod.tileentities;
import java.util.Iterator;
import java.util.List;
import com.angelo.venturemod.blocks.BlockLockedChest;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockChest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.AxisAlignedBB;
public class TileEntityLockedChest extends TileEntityChest{
private ItemStack[] chestContents = new ItemStack[36];
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityLockedChest adjacentChestZNeg;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityLockedChest adjacentChestXPos;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityLockedChest adjacentChestXNeg;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityLockedChest adjacentChestZPos;
private int cachedChestType;
private String customName;
@Override
public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
if (this.chestContents[p_70298_1_] != null)
{
ItemStack itemstack;
if (this.chestContents[p_70298_1_].stackSize <= p_70298_2_)
{
itemstack = this.chestContents[p_70298_1_];
this.chestContents[p_70298_1_] = null;
markDirty();
return itemstack;
}
else
{
itemstack = this.chestContents[p_70298_1_].splitStack(p_70298_2_);
if (this.chestContents[p_70298_1_].stackSize == 0)
{
this.chestContents[p_70298_1_] = null;
}
markDirty();
return itemstack;
}
}
else
{
return null;
}
}
@Override
public void markDirty()
{
super.markDirty();
}
/**
* Returns the name of the inventory
*/
@Override
public String getInventoryName()
{
return this.hasCustomInventoryName() ? this.customName : "Locked Chest";
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
chestContents[i] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
markDirty();
}
@Override
public void readFromNBT(NBTTagCompound p_145839_1_)
{
super.readFromNBT(p_145839_1_);
NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
this.chestContents = new ItemStack[this.getSizeInventory()];
if (p_145839_1_.hasKey("Locked Chest", )
{
this.customName = p_145839_1_.getString("Locked Chest");
}
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j >= 0 && j < this.chestContents.length)
{
this.chestContents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
}
@Override
public void writeToNBT(NBTTagCompound p_145841_1_)
{
super.writeToNBT(p_145841_1_);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.chestContents.length; ++i)
{
if (this.chestContents[i] != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte)i);
this.chestContents[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
p_145841_1_.setTag("Items", nbttaglist);
if (this.hasCustomInventoryName())
{
p_145841_1_.setString("Locked Chest", this.customName);
}
}
private void func_145978_a(TileEntityLockedChest p_145978_1_, int p_145978_2_){
if (p_145978_1_.isInvalid())
{
this.adjacentChestChecked = false;
}
else if (this.adjacentChestChecked)
{
switch (p_145978_2_)
{
case 0:
if (this.adjacentChestZPos != p_145978_1_)
{
this.adjacentChestChecked = false;
}
break;
case 1:
if (this.adjacentChestXNeg != p_145978_1_)
{
this.adjacentChestChecked = false;
}
break;
case 2:
if (this.adjacentChestZNeg != p_145978_1_)
{
this.adjacentChestChecked = false;
}
break;
case 3:
if (this.adjacentChestXPos != p_145978_1_)
{
this.adjacentChestChecked = false;
}
}
}
}
/**
* Performs the check for adjacent chests to determine if this chest is double or not.
*/
@Override
public void checkForAdjacentChests()
{
if (!this.adjacentChestChecked)
{
this.adjacentChestChecked = true;
this.adjacentChestZNeg = null;
this.adjacentChestXPos = null;
this.adjacentChestXNeg = null;
this.adjacentChestZPos = null;
if (this.func_145977_a(this.xCoord - 1, this.yCoord, this.zCoord))
{
this.adjacentChestXNeg = (TileEntityLockedChest)this.worldObj.getTileEntity(this.xCoord - 1, this.yCoord, this.zCoord);
}
if (this.func_145977_a(this.xCoord + 1, this.yCoord, this.zCoord))
{
this.adjacentChestXPos = (TileEntityLockedChest)this.worldObj.getTileEntity(this.xCoord + 1, this.yCoord, this.zCoord);
}
if (this.func_145977_a(this.xCoord, this.yCoord, this.zCoord - 1))
{
this.adjacentChestZNeg = (TileEntityLockedChest)this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord - 1);
}
if (this.func_145977_a(this.xCoord, this.yCoord, this.zCoord + 1))
{
this.adjacentChestZPos = (TileEntityLockedChest)this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord + 1);
}
if (this.adjacentChestZNeg != null)
{
this.adjacentChestZNeg.func_145978_a(this, 0);
}
if (this.adjacentChestZPos != null)
{
this.adjacentChestZPos.func_145978_a(this, 2);
}
if (this.adjacentChestXPos != null)
{
this.adjacentChestXPos.func_145978_a(this, 1);
}
if (this.adjacentChestXNeg != null)
{
this.adjacentChestXNeg.func_145978_a(this, 3);
}
}
}
private boolean func_145977_a(int p_145977_1_, int p_145977_2_, int p_145977_3_)
{
Block block = this.worldObj.getBlock(p_145977_1_, p_145977_2_, p_145977_3_);
return block instanceof BlockLockedChest && ((BlockLockedChest)block).field_149956_a == this.func_145980_j();
}
@Override
public int func_145980_j()
{
if (this.cachedChestType == -1)
{
if (this.worldObj == null || !(this.getBlockType() instanceof BlockLockedChest))
{
return 0;
}
this.cachedChestType = ((BlockLockedChest)this.getBlockType()).field_149956_a;
}
return this.cachedChestType;
}
}