I havea chest that I want the contents to change when there are torches surrounding it. I put the code for this in the UpdateTick() method.
BlockEggChest:
package targaryen_dragons.blocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.Random;
import targaryen_dragons.Targaryen_Dragons;
import targaryen_dragons.core.ModStuff;
import targaryen_dragons.entity.tile.TileEntityEggChest;
import targaryen_dragons.inventory.InventoryEggChest;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryEnderChest;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
public class BlockEggChest extends BlockContainer
{
private static final String __OBFID = "CL_00000238";
public BlockEggChest()
{
super(Material.wood);
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
this.setTickRandomly(true);
}
public void updateTick(World world, int x, int y, int z, Random rand) {
TileEntity tileentity = world.getTileEntity(x, y, z);
TileEntityEggChest eggchest = (TileEntityEggChest)tileentity;
if(tileentity instanceof TileEntityEggChest)
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch&&
eggchest.getStackInSlot(5) == new ItemStack(ModStuff.DeadDragonEgg)){
int ColorChoice = rand.nextInt(2);
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
if(tileentity instanceof TileEntityEggChest)
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch&&
eggchest.getStackInSlot(6) == new ItemStack(ModStuff.DeadDragonEgg)){
int ColorChoice = rand.nextInt(2);
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
if(tileentity instanceof TileEntityEggChest)
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch&&
eggchest.getStackInSlot(7) == new ItemStack(ModStuff.DeadDragonEgg)){
int ColorChoice = rand.nextInt(2);
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
super.updateTick(world, x, y, z, rand);
}
/**
* Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two
* adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
*/
public boolean isOpaqueCube()
{
return false;
}
/**
* If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
*/
public boolean renderAsNormalBlock()
{
return false;
}
/**
* The type of render function that is called for this block
*/
public int getRenderType()
{
return 22;
}
/**
* 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_)
{
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;
}
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, b0, 2);
}
/**
* Called upon block activation (right click on the block.)
*/
@Override
public boolean onBlockActivated(World var1, int var2, int var3, int var4, EntityPlayer player, int var6, float var7, float var8, float var9) {
if (!player.isSneaking()) {
player.openGui(Targaryen_Dragons.instance, Targaryen_Dragons.EggChestID, var1, var2, var3, var4);
return true;
}
if (var1.getBlock(var2, var3+1, var4) != Blocks.air){
return false;
}
else {
return false;
}
}
/**
* Returns a new instance of a block's tile entity class. Called on placing the block.
*/
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_)
{
return new TileEntityEggChest();
}
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister p_149651_1_)
{
this.blockIcon = p_149651_1_.registerIcon("targaryen_dragons:EggChest");
}
}
TileEntityEggChest
package targaryen_dragons.entity.tile;
import java.util.Iterator;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
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.util.AxisAlignedBB;
import targaryen_dragons.blocks.BlockEggChest;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class TileEntityEggChest extends TileEntity implements IInventory
{
private ItemStack[] chestContents = new ItemStack[36];
/** Determines if the check for adjacent chests has taken place. */
public boolean adjacentChestChecked;
/** The current angle of the lid (between 0 and 1) */
public float lidAngle;
/** The angle of the lid last tick */
public float prevLidAngle;
/** The number of players currently using this chest */
public int numPlayersUsing;
/** Server sync counter (once per 20 ticks) */
private int ticksSinceSync;
private int cachedChestType;
private String customName;
private static final String __OBFID = "CL_00000346";
public TileEntityEggChest()
{
this.cachedChestType = -1;
}
@SideOnly(Side.CLIENT)
public TileEntityEggChest(int p_i2350_1_)
{
this.cachedChestType = p_i2350_1_;
}
/**
* Returns the number of slots in the inventory.
*/
public int getSizeInventory()
{
return 27;
}
/**
* Returns the stack in slot i
*/
public ItemStack getStackInSlot(int p_70301_1_)
{
return this.chestContents[p_70301_1_];
}
/**
* Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
* new stack.
*/
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;
this.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;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
/**
* When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
* like when you close a workbench GUI.
*/
public ItemStack getStackInSlotOnClosing(int p_70304_1_)
{
if (this.chestContents[p_70304_1_] != null)
{
ItemStack itemstack = this.chestContents[p_70304_1_];
this.chestContents[p_70304_1_] = null;
return itemstack;
}
else
{
return null;
}
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_)
{
this.chestContents[p_70299_1_] = p_70299_2_;
if (p_70299_2_ != null && p_70299_2_.stackSize > this.getInventoryStackLimit())
{
p_70299_2_.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
/**
* Returns the name of the inventory
*/
public String getInventoryName()
{
return this.hasCustomInventoryName() ? this.customName : "tile.EggChest.name";
}
/**
* Returns if the inventory is named
*/
public boolean hasCustomInventoryName()
{
return this.customName != null && this.customName.length() > 0;
}
public void func_145976_a(String p_145976_1_)
{
this.customName = p_145976_1_;
}
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("CustomName", 8))
{
this.customName = p_145839_1_.getString("CustomName");
}
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);
}
}
}
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("CustomName", this.customName);
}
}
/**
* Returns the maximum stack size for a inventory slot.
*/
public int getInventoryStackLimit()
{
return 64;
}
/**
* Do not make give this method the name canInteractWith because it clashes with Container
*/
public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
{
return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : p_70300_1_.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}
/**
* Causes the TileEntity to reset all it's cached values for it's container Block, metadata and in the case of
* chests, the adjacent chest check
*/
public void updateContainingBlockInfo()
{
super.updateContainingBlockInfo();
this.adjacentChestChecked = false;
}
private boolean func_145977_a(int p_145977_1_, int p_145977_2_, int p_145977_3_)
{
if (this.worldObj == null)
{
return false;
}
else
{
Block block = this.worldObj.getBlock(p_145977_1_, p_145977_2_, p_145977_3_);
return block instanceof BlockEggChest;
}
}
public void updateEntity()
{
super.updateEntity();
++this.ticksSinceSync;
float f;
if (!this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + this.xCoord + this.yCoord + this.zCoord) % 200 == 0)
{
this.numPlayersUsing = 0;
f = 5.0F;
List list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox((double)((float)this.xCoord - f), (double)((float)this.yCoord - f), (double)((float)this.zCoord - f), (double)((float)(this.xCoord + 1) + f), (double)((float)(this.yCoord + 1) + f), (double)((float)(this.zCoord + 1) + f)));
Iterator iterator = list.iterator();
while (iterator.hasNext())
{
EntityPlayer entityplayer = (EntityPlayer)iterator.next();
if (entityplayer.openContainer instanceof ContainerChest)
{
IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory();
if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this))
{
++this.numPlayersUsing;
}
}
}
}
this.prevLidAngle = this.lidAngle;
f = 0.1F;
double d2;
if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F)
{
double d1 = (double)this.xCoord + 0.5D;
d2 = (double)this.zCoord + 0.5D;
this.worldObj.playSoundEffect(d1, (double)this.yCoord + 0.5D, d2, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
{
float f1 = this.lidAngle;
if (this.numPlayersUsing > 0)
{
this.lidAngle += f;
}
else
{
this.lidAngle -= f;
}
if (this.lidAngle > 1.0F)
{
this.lidAngle = 1.0F;
}
float f2 = 0.5F;
if (this.lidAngle < f2 && f1 >= f2 )
{
d2 = (double)this.xCoord + 0.5D;
double d0 = (double)this.zCoord + 0.5D;
this.worldObj.playSoundEffect(d2, (double)this.yCoord + 0.5D, d0, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.lidAngle < 0.0F)
{
this.lidAngle = 0.0F;
}
}
}
/**
* Called when a client event is received with the event number and argument, see World.sendClientEvent
*/
public boolean receiveClientEvent(int p_145842_1_, int p_145842_2_)
{
if (p_145842_1_ == 1)
{
this.numPlayersUsing = p_145842_2_;
return true;
}
else
{
return super.receiveClientEvent(p_145842_1_, p_145842_2_);
}
}
public void openInventory()
{
if (this.numPlayersUsing < 0)
{
this.numPlayersUsing = 0;
}
++this.numPlayersUsing;
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.numPlayersUsing);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
}
public void closeInventory()
{
if (this.getBlockType() instanceof BlockEggChest)
{
--this.numPlayersUsing;
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.numPlayersUsing);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
}
}
/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
*/
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_)
{
return true;
}
/**
* invalidates a tile entity
*/
public void invalidate()
{
super.invalidate();
this.updateContainingBlockInfo();
}
}
Container EggChest:
package targaryen_dragons.inventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public class InventoryEggChest extends Container
{
private IInventory lowerChestInventory;
private int numRows;
private static final String __OBFID = "CL_00001742";
public InventoryEggChest(IInventory p_i1806_1_, IInventory p_i1806_2_)
{
this.lowerChestInventory = p_i1806_2_;
this.numRows = p_i1806_2_.getSizeInventory() / 9;
p_i1806_2_.openInventory();
int i = (this.numRows - 4) * 18;
int j;
int k;
this.addSlotToContainer(new Slot(p_i1806_2_, 5, 62, 35));
this.addSlotToContainer(new Slot(p_i1806_2_, 6, 80, 35));
this.addSlotToContainer(new Slot(p_i1806_2_, 7, 98, 35));
for (j = 0; j < 3; ++j)
{
for (k = 0; k < 9; ++k)
{
this.addSlotToContainer(new Slot(p_i1806_1_, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
}
}
for (j = 0; j < 9; ++j)
{
this.addSlotToContainer(new Slot(p_i1806_1_, j, 8 + j * 18, 161 + i));
}
}
public boolean canInteractWith(EntityPlayer p_75145_1_)
{
return this.lowerChestInventory.isUseableByPlayer(p_75145_1_);
}
/**
* Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
*/
public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_)
{
ItemStack itemstack = null;
Slot slot = (Slot)this.inventorySlots.get(p_82846_2_);
if (slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (p_82846_2_ < this.numRows * 9)
{
if (!this.mergeItemStack(itemstack1, this.numRows * 9, this.inventorySlots.size(), true))
{
return null;
}
}
else if (!this.mergeItemStack(itemstack1, 0, this.numRows * 9, false))
{
return null;
}
if (itemstack1.stackSize == 0)
{
slot.putStack((ItemStack)null);
}
else
{
slot.onSlotChanged();
}
}
return itemstack;
}
/**
* Called when the container is closed.
*/
public void onContainerClosed(EntityPlayer p_75134_1_)
{
super.onContainerClosed(p_75134_1_);
this.lowerChestInventory.closeInventory();
}
/**
* Return this chest container's lower chest inventory.
*/
public IInventory getLowerChestInventory()
{
return this.lowerChestInventory;
}
}
GuiEggChest:
package targaryen_dragons.client.gui;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ContainerFurnace;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import targaryen_dragons.entity.tile.TileEntityEggChest;
import targaryen_dragons.inventory.InventoryEggChest;
@SideOnly(Side.CLIENT)
public class GuiEggChest extends GuiContainer
{
private static final ResourceLocation furnaceGuiTextures = new ResourceLocation("targaryen_dragons:textures/gui/EggChest.png");
private TileEntityEggChest tileFurnace;
private static final String __OBFID = "CL_00000758";
public GuiEggChest(InventoryPlayer p_i1091_1_, TileEntityEggChest p_i1091_2_)
{
super(new InventoryEggChest(p_i1091_1_, p_i1091_2_));
this.tileFurnace = p_i1091_2_;
}
/**
* Draw the foreground layer for the GuiContainer (everything in front of the items)
*/
protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_)
{
String s = this.tileFurnace.hasCustomInventoryName() ? this.tileFurnace.getInventoryName() : I18n.format(this.tileFurnace.getInventoryName(), new Object[0]);
this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);
this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 8, this.ySize - 96 + 2, 4210752);
}
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(furnaceGuiTextures);
int k = (this.width - this.xSize) / 2;
int l = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
}
}
In your block's updateTick method, you seem have the same code written three times. Why?
You're also checking if the TileEntity is an instance of TileEntityEggChest, even though the cast on the second line of the method would throw an exception if it wasn't. The TileEntity should always be a TileEntityEggChest, I don't think you need to check it.
You could also just pass the ColorChoice variable (should be named colorChoice, it's a local variable and not a class) directly to the ItemStack constructor instead of checking if its value is 0 and then passing 0.
Aside from that, I can't see any obvious errors (though I don't know all that much about Containers or GUIs). What's actually going wrong? Logs/crash reports would help if appropriate.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
In your block's updateTick method, you seem have the same code written three times. Why?
You're also checking if the TileEntity is an instance of TileEntityEggChest, even though the cast on the second line of the method would throw an exception if it wasn't. The TileEntity should always be a TileEntityEggChest, I don't think you need to check it.
Aside from that, I can't see any obvious errors (though I don't know all that much about Containers or GUIs). What's actually going wrong? Logs/crash reports would help if appropriate.
the code is written 3 times because it is checking 3 slots in the chest. the problem is that nothing is happening, no errors, it just doesn't work.
the code is written 3 times because it is checking 3 slots in the chest.
Ah, I didn't see that. In that case you'd be better off using a single if statement that checks if any of the three slots have the dead dragon egg (assuming you want to perform a single action when any of the slots have the egg) or using a for loop that runs three times (for (int i=0; i < 3; i++)) (assuming you want to perform one action per slot that has the egg).
Copying and pasting a single block of code multiple times and changing one part of it is rarely the best way to achieve something.
the problem is that nothing is happening, no errors, it just doesn't work.
Does it pass into the block of the if statement (i.e. are all the conditions true)? Does it call setInventorySlotContents? Does that slot get set to the dragon egg? Try checking it inside the method after setting the slot and after calling the method.
Rollback Post to RevisionRollBack
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
Ah, I didn't see that. In that case you'd be better off using a single if statement that checks if any of the three slots have the dead dragon egg (assuming you want to perform a single action when any of the slots have the egg) or using a for loop that runs three times (",courier">for (int i=0; i < 3; i++)) (assuming you want to perform one action per slot that has the egg).
Copying and pasting a single block of code multiple times and changing one part of it is rarely the best way to achieve something.
Does it pass into the block of the if statement (i.e. are all the conditions true)? Does it call ",courier">setInventorySlotContents? Does that slot get set to the dragon egg? Try checking it inside the method after setting the slot and after calling the method.
I wasn't able to do the (for (int i=0; i < 3; i++) thing yet, but Everything else i did. I got no response from the sytem.out.println. however, I think there is probably something wrong with the container class, and which slot is which.
Block's updateTick is a "lazy" system rather than "realtime", you need to either queue your block for an update or set it to tick randomly. Even so, the chunk manager will process it when it feels like it - which could range from instantly to over a minute (some might claim it's never that slow, but in every test I've done it's just too inaccurate and unreliable for anything you don't intend to be random or lazy - e.g. plants growing).
You should use "onNeighborBlockChange" instead, if your torches are required to be in one of the 8 blocks directly surrounding it. This method will be called when the user places OR destroys a surrounding block, so just count the surrounding torches in this method. If they require greater distance than the immediate 8 blocks however, then you will need a Tile Entity. Alternatively, you can check for these surrounding blocks only when the user activates the block (on demand) - this would be ideal for best performance, but of course this won't be good if you want the capability of automation (e.g. pipes to pull items from the chest).
Block's updateTick is a "lazy" system rather than "realtime", you need to either queue your block for an update or set it to tick randomly. Even so, the chunk manager will process it when it feels like it - which could range from instantly to over a minute (some might claim it's never that slow, but in every test I've done it's just too inaccurate and unreliable for anything you don't intend to be random or lazy - e.g. plants growing).
You should use "onNeighborBlockChange" instead, if your torches are required to be in one of the 8 blocks directly surrounding it. This method will be called when the user places OR destroys a surrounding block, so just count the surrounding torches in this method. If they require greater distance than the immediate 8 blocks however, then you will need a Tile Entity. Alternatively, you can check for these surrounding blocks only when the user activates the block (on demand) - this would be ideal for best performance, but of course this won't be good if you want the capability of automation (e.g. pipes to pull items from the chest).
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block blk)
{
TileEntity tileentity = world.getTileEntity(x, y, z);
TileEntityEggChest eggchest = (TileEntityEggChest)tileentity;
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch&&
eggchest.getStackInSlot(5) == new ItemStack(ModStuff.DeadDragonEgg)){
System.out.println("Conditions are true");
int ColorChoice = world.rand.nextInt(2);
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch&&
eggchest.getStackInSlot(6) == new ItemStack(ModStuff.DeadDragonEgg)){
System.out.println("Conditions are true");
int ColorChoice = world.rand.nextInt(2);
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
} if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch&&
eggchest.getStackInSlot(7) == new ItemStack(ModStuff.DeadDragonEgg)){
System.out.println("Conditions are true");
int ColorChoice = world.rand.nextInt(2);
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
super.onNeighborBlockChange(world, x, y, z, blk);
}
It should. Have you tried printing to console at the top of this method, instead of assuming your code is 100% working?
EDIT: Correction to my statement of "surrounding 8 blocks", it may only be the surrounding 4 blocks + the top and bottom (so 6 in total - one for each face).
It should. Have you tried printing to console at the top of this method, instead of assuming your code is 100% working?
EDIT: Correction to my statement of "surrounding 8 blocks", it may only be the surrounding 4 blocks + the top and bottom (so 6 in total - one for each face).
did you see the System.out.println("Conditions are true");
Yes I did, but what if the condition(s) are false? As I said, print to console at the very top - your code might be bad. Process of elimination is how all bug fixing starts.
Also, take note of my edit. With a println at the *very top*, try placing a torch (or any block) directly beside it, and also try the block diagonally. See if both or only the former trigger it.
Yes I did, but what if the condition(s) are false? As I said, print to console at the very top - your code might be bad. Process of elimination is how all bug fixing starts.
Also, take note of my edit. With a println at the *very top*, try placing a torch (or any block) directly beside it, and also try the block diagonally. See if both or only the former trigger it.
allright, I dont know what I id wrong before, but know it partially works! the only problem is that eggs will be in the chest even if there were none before. here is the new code:
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block blk)
{
TileEntity tileentity = world.getTileEntity(x, y, z);
TileEntityEggChest eggchest = (TileEntityEggChest)tileentity;
System.out.println("start your engines!");
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch){
System.out.println("Conditions are true");
int ColorChoice = world.rand.nextInt(2);
if( eggchest.getStackInSlot(5) == new ItemStack(ModStuff.DeadDragonEgg)){
}
System.out.println("Egg is warming");
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
if( eggchest.getStackInSlot(6) == new ItemStack(ModStuff.DeadDragonEgg)){
}
System.out.println("Egg is warming");
if(ColorChoice == 0){
eggchest.setInventorySlotContents(6, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(6, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(6, new ItemStack(ModStuff.DragonEgg,1,2));
}
if( eggchest.getStackInSlot(7) == new ItemStack(ModStuff.DeadDragonEgg)){
}
System.out.println("Egg is warming");
if(ColorChoice == 0){
eggchest.setInventorySlotContents(7, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(7, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(7, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
super.onNeighborBlockChange(world, x, y, z, blk);
}
alright, edited the code again. the only thing that doesn't work is it recognising the Dead eggs.
package targaryen_dragons.blocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.Random;
import targaryen_dragons.Targaryen_Dragons;
import targaryen_dragons.core.ModStuff;
import targaryen_dragons.entity.tile.TileEntityEggChest;
import targaryen_dragons.inventory.InventoryEggChest;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryEnderChest;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
public class BlockEggChest extends BlockContainer
{
private static final String __OBFID = "CL_00000238";
public BlockEggChest()
{
super(Material.wood);
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
this.setTickRandomly(true);
}@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block blk)
{
TileEntity tileentity = world.getTileEntity(x, y, z);
TileEntityEggChest eggchest = (TileEntityEggChest)tileentity;
if(world.getBlock(x-1, y, z)==Blocks.redstone_torch&&world.getBlock(x+1, y, z)==Blocks.redstone_torch&&
world.getBlock(x, y, z-1)==Blocks.redstone_torch&&world.getBlock(x, y, z+1)==Blocks.redstone_torch){
Random rand = new Random();
int ColorChoice = rand.nextInt(3);
int ColorChoice1 = rand.nextInt(3);
int ColorChoice2 = rand.nextInt(3);
if(eggchest.getStackInSlot(5)==new ItemStack(ModStuff.DeadDragonEgg,1)){
if(ColorChoice == 0){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice == 1){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice == 2){
eggchest.setInventorySlotContents(5, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
if(eggchest.getStackInSlot(6)==new ItemStack(ModStuff.DeadDragonEgg,1)){
if(ColorChoice1 == 0){
eggchest.setInventorySlotContents(6, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice1 == 1){
eggchest.setInventorySlotContents(6, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice1 == 2){
eggchest.setInventorySlotContents(6, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
if(eggchest.getStackInSlot(7)==new ItemStack(ModStuff.DeadDragonEgg,1)){
if(ColorChoice2 == 0){
eggchest.setInventorySlotContents(7, new ItemStack(ModStuff.DragonEgg,1,0));
}
if(ColorChoice2 == 1){
eggchest.setInventorySlotContents(7, new ItemStack(ModStuff.DragonEgg,1,1));
}
if(ColorChoice2 == 2){
eggchest.setInventorySlotContents(7, new ItemStack(ModStuff.DragonEgg,1,2));
}
}
}
super.onNeighborBlockChange(world, x, y, z, blk);
}
/**
* Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two
* adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
*/
public boolean isOpaqueCube()
{
return false;
}
/**
* If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
*/
public boolean renderAsNormalBlock()
{
return false;
}
/**
* The type of render function that is called for this block
*/
public int getRenderType()
{
return 22;
}
/**
* 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_)
{
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;
}
p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, b0, 2);
}
/**
* Called upon block activation (right click on the block.)
*/
@Override
public boolean onBlockActivated(World var1, int var2, int var3, int var4, EntityPlayer player, int var6, float var7, float var8, float var9) {
if (!player.isSneaking()) {
player.openGui(Targaryen_Dragons.instance, Targaryen_Dragons.EggChestID, var1, var2, var3, var4);
return true;
}
if (var1.getBlock(var2, var3+1, var4) != Blocks.air){
return false;
}
else {
return false;
}
}
/**
* Returns a new instance of a block's tile entity class. Called on placing the block.
*/
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_)
{
return new TileEntityEggChest();
}
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister p_149651_1_)
{
this.blockIcon = p_149651_1_.registerIcon("targaryen_dragons:EggChest");
}
}
might be a problem because "==" in Java means if the two objects point to the same instance, since you did new ItemStack, they can't possible point to the same instance. I don't know, according to you, it worked but whatever. I suggest changing that to:
Of course, this is my preference, You don't have to abide. Back on topic, I'll give you a class that might help with IInventories: StackHelper.java
EDIT: I just want to point out the methods, addToInv(...) method. I think that might help you. Thanks!
might be a problem because "==" in Java means if the two objects point to the same instance, since you did new ItemStack, they can't possible point to the same instance. I don't know, according to you, it worked but whatever. I suggest changing that to:
Of course, this is my preference, You don't have to abide. Back on topic, I'll give you a class that might help with IInventories: StackHelper.java
EDIT: I just want to point out the methods, addToInv(...) method. I think that might help you. Thanks!
thanks, it worked! doy you have a way that I can make it so that it instantly doesn't change, like it has to be in the same conditions for 200 ticks or so?
You could have a variable that counts up to 200 or whatever as long as the conditions are met, if they aren't met, even for a split second, then then counter is set back to 0, or just starts counting down at least.
You could have a variable that counts up to 200 or whatever as long as the conditions are met, if they aren't met, even for a split second, then then counter is set back to 0, or just starts counting down at least.
BlockEggChest:
TileEntityEggChest
Container EggChest:
GuiEggChest:
Please Help!
You're also checking if the TileEntity is an instance of TileEntityEggChest, even though the cast on the second line of the method would throw an exception if it wasn't. The TileEntity should always be a TileEntityEggChest, I don't think you need to check it.
You could also just pass the ColorChoice variable (should be named colorChoice, it's a local variable and not a class) directly to the ItemStack constructor instead of checking if its value is 0 and then passing 0.
Aside from that, I can't see any obvious errors (though I don't know all that much about Containers or GUIs). What's actually going wrong? Logs/crash reports would help if appropriate.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
the code is written 3 times because it is checking 3 slots in the chest. the problem is that nothing is happening, no errors, it just doesn't work.
Ah, I didn't see that. In that case you'd be better off using a single if statement that checks if any of the three slots have the dead dragon egg (assuming you want to perform a single action when any of the slots have the egg) or using a for loop that runs three times (for (int i=0; i < 3; i++)) (assuming you want to perform one action per slot that has the egg).
Copying and pasting a single block of code multiple times and changing one part of it is rarely the best way to achieve something.
Does it pass into the block of the if statement (i.e. are all the conditions true)? Does it call setInventorySlotContents? Does that slot get set to the dragon egg? Try checking it inside the method after setting the slot and after calling the method.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
I wasn't able to do the (for (int i=0; i < 3; i++) thing yet, but Everything else i did. I got no response from the sytem.out.println. however, I think there is probably something wrong with the container class, and which slot is which.
You should use "onNeighborBlockChange" instead, if your torches are required to be in one of the 8 blocks directly surrounding it. This method will be called when the user places OR destroys a surrounding block, so just count the surrounding torches in this method. If they require greater distance than the immediate 8 blocks however, then you will need a Tile Entity. Alternatively, you can check for these surrounding blocks only when the user activates the block (on demand) - this would be ideal for best performance, but of course this won't be good if you want the capability of automation (e.g. pipes to pull items from the chest).
I will try this.
Still getiting no response.
EDIT: Correction to my statement of "surrounding 8 blocks", it may only be the surrounding 4 blocks + the top and bottom (so 6 in total - one for each face).
did you see the System.out.println("Conditions are true");
Also, take note of my edit. With a println at the *very top*, try placing a torch (or any block) directly beside it, and also try the block diagonally. See if both or only the former trigger it.
allright, I dont know what I id wrong before, but know it partially works! the only problem is that eggs will be in the chest even if there were none before. here is the new code:
might be a problem because "==" in Java means if the two objects point to the same instance, since you did new ItemStack, they can't possible point to the same instance. I don't know, according to you, it worked but whatever. I suggest changing that to:
Of course, this is my preference, You don't have to abide. Back on topic, I'll give you a class that might help with IInventories: StackHelper.java
EDIT: I just want to point out the methods, addToInv(...) method. I think that might help you. Thanks!
Hello!
thanks, it worked! doy you have a way that I can make it so that it instantly doesn't change, like it has to be in the same conditions for 200 ticks or so?
Hello!
how could I do that?
int someVar = 0;
while (something) {
someVar++;
if (someVar == 200) {
// do something
break
}
}
Very great example by CosmicDan. This would go in your updateEntity() method. Create that counter field in your updateMethod class.
Hello!