The Meaning of Life, the Universe, and Everything.
Join Date:
10/3/2012
Posts:
133
Member Details
I have been working on a mod that requires it to update items damage values, and I was wondering if there was a way to make it update those values while in a chest, I have it working in inventory of the player, but not in the chests.
In the TileEntity of your new chest you can update every ItemStack in the updateEntity() method of the TileEntity. Just cycle through every ItemStack in the inventory with a for loop and call updateAnimation() of every ItemStack. If this messes up the animation of items, you can choose to call onUpdate() of the Item of the ItemStack.
I have the chest complete would you mind showing me how
package mods.ring.blocks;
import java.util.Iterator;
import java.util.List;
import mods.ring.ModsRing;
import net.minecraft.entity.player.EntityPlayer;
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;
public class TileEntityDchest extends TileEntity implements IInventory
{
private ItemStack[] chestContents = new ItemStack[36];
public boolean adjacentChestChecked = false;
public TileEntityDchest adjacentChestZNeg;
public TileEntityDchest adjacentChestXPos;
public TileEntityDchest adjacentChestXNeg;
public TileEntityDchest adjacentChestZPosition;
public float lidAngle;
public float prevLidAngle;
public int numUsingPlayers;
private int ticksSinceSync;
public int getSizeInventory()
{
return 27;
}
/**
* Returns the stack in slot i
*/
public ItemStack getStackInSlot(int par1)
{
return this.chestContents[par1];
}
/**
* 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 par1, int par2)
{
if (this.chestContents[par1] != null)
{
ItemStack var3;
if (this.chestContents[par1].stackSize <= par2)
{
var3 = this.chestContents[par1];
this.chestContents[par1] = null;
this.onInventoryChanged();
return var3;
}
else
{
var3 = this.chestContents[par1].splitStack(par2);
if (this.chestContents[par1].stackSize == 0)
{
this.chestContents[par1] = null;
}
this.onInventoryChanged();
return var3;
}
}
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 par1)
{
if (this.chestContents[par1] != null)
{
ItemStack var2 = this.chestContents[par1];
this.chestContents[par1] = null;
return var2;
}
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 par1, ItemStack par2ItemStack)
{
this.chestContents[par1] = par2ItemStack;
if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
{
par2ItemStack.stackSize = this.getInventoryStackLimit();
}
this.onInventoryChanged();
}
/**
* Returns the name of the inventory.
*/
public String getInvName()
{
return "container.treetrunk";
}
/**
* Reads a tile entity from NBT.
*/
public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
super.readFromNBT(par1NBTTagCompound);
NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
this.chestContents = new ItemStack[this.getSizeInventory()];
for (int var3 = 0; var3 < var2.tagCount(); ++var3)
{
NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
int var5 = var4.getByte("Slot") & 255;
if (var5 >= 0 && var5 < this.chestContents.length)
{
this.chestContents[var5] = ItemStack.loadItemStackFromNBT(var4);
}
}
}
/**
* Writes a tile entity to NBT.
*/
public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
super.writeToNBT(par1NBTTagCompound);
NBTTagList var2 = new NBTTagList();
for (int var3 = 0; var3 < this.chestContents.length; ++var3)
{
if (this.chestContents[var3] != null)
{
NBTTagCompound var4 = new NBTTagCompound();
var4.setByte("Slot", (byte)var3);
this.chestContents[var3].writeToNBT(var4);
var2.appendTag(var4);
}
}
par1NBTTagCompound.setTag("Items", var2);
}
/**
* Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
* this more of a set than a get?*
*/
public int getInventoryStackLimit()
{
return 64;
}
/**
* Do not make give this method the name canInteractWith because it clashes with Container
*/
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
{
return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.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, blockID, metaData and in the case
* of chests, the adjcacent chest check
*/
public void updateContainingBlockInfo()
{
super.updateContainingBlockInfo();
this.adjacentChestChecked = false;
}
private void func_90009_a(TileEntityDchest par1TileEntityTreeTrunk, int par2)
{
if (par1TileEntityTreeTrunk.isInvalid())
{
this.adjacentChestChecked = false;
}
else if (this.adjacentChestChecked)
{
switch (par2)
{
case 0:
if (this.adjacentChestZPosition != par1TileEntityTreeTrunk)
{
this.adjacentChestChecked = false;
}
break;
case 1:
if (this.adjacentChestXNeg != par1TileEntityTreeTrunk)
{
this.adjacentChestChecked = false;
}
break;
case 2:
if (this.adjacentChestZNeg != par1TileEntityTreeTrunk)
{
this.adjacentChestChecked = false;
}
break;
case 3:
if (this.adjacentChestXPos != par1TileEntityTreeTrunk)
{
this.adjacentChestChecked = false;
}
}
}
}
/**
* Performs the check for adjacent chests to determine if this chest is double or not.
*/
public void checkForAdjacentChests()
{
if (!this.adjacentChestChecked)
{
this.adjacentChestChecked = true;
this.adjacentChestZNeg = null;
this.adjacentChestXPos = null;
this.adjacentChestXNeg = null;
this.adjacentChestZPosition = null;
if (this.worldObj.getBlockId(this.xCoord - 1, this.yCoord, this.zCoord) == ModsRing.Dchest.blockID)
{
this.adjacentChestXNeg = (TileEntityDchest)this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord);
}
if (this.worldObj.getBlockId(this.xCoord + 1, this.yCoord, this.zCoord) == ModsRing.Dchest.blockID)
{
this.adjacentChestXPos = (TileEntityDchest)this.worldObj.getBlockTileEntity(this.xCoord + 1, this.yCoord, this.zCoord);
}
if (this.worldObj.getBlockId(this.xCoord, this.yCoord, this.zCoord - 1) == ModsRing.Dchest.blockID)
{
this.adjacentChestZNeg = (TileEntityDchest)this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1);
}
if (this.worldObj.getBlockId(this.xCoord, this.yCoord, this.zCoord + 1) == ModsRing.Dchest.blockID)
{
this.adjacentChestZPosition = (TileEntityDchest)this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord + 1);
}
if (this.adjacentChestZNeg != null)
{
this.adjacentChestZNeg.func_90009_a(this, 0);
}
if (this.adjacentChestZPosition != null)
{
this.adjacentChestZPosition.func_90009_a(this, 2);
}
if (this.adjacentChestXPos != null)
{
this.adjacentChestXPos.func_90009_a(this, 1);
}
if (this.adjacentChestXNeg != null)
{
this.adjacentChestXNeg.func_90009_a(this, 3);
}
}
}
/**
* Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
* ticks and creates a new spawn inside its implementation.
*/
public void updateEntity()
{
super.updateEntity();
this.checkForAdjacentChests();
++this.ticksSinceSync;
float var1;
if (!this.worldObj.isRemote && this.numUsingPlayers != 0 && (this.ticksSinceSync + this.xCoord + this.yCoord + this.zCoord) % 200 == 0)
{
this.numUsingPlayers = 0;
var1 = 5.0F;
List var2 = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getAABBPool().getAABB((double)((float)this.xCoord - var1), (double)((float)this.yCoord - var1), (double)((float)this.zCoord - var1), (double)((float)(this.xCoord + 1) + var1), (double)((float)(this.yCoord + 1) + var1), (double)((float)(this.zCoord + 1) + var1)));
Iterator var3 = var2.iterator();
while (var3.hasNext())
{
EntityPlayer var4 = (EntityPlayer)var3.next();
if (var4.openContainer instanceof ContainerDchest)
{
IInventory var5 = ((ContainerDchest)var4.openContainer).getLowerChestInventory();
if (var5 == this || var5 instanceof InventoryLargeChest && ((InventoryLargeChest)var5).isPartOfLargeChest(this))
{
++this.numUsingPlayers;
}
}
}
}
this.prevLidAngle = this.lidAngle;
var1 = 0.1F;
double var11;
if (this.numUsingPlayers > 0 && this.lidAngle == 0.0F && this.adjacentChestZNeg == null && this.adjacentChestXNeg == null)
{
double var8 = (double)this.xCoord + 0.5D;
var11 = (double)this.zCoord + 0.5D;
if (this.adjacentChestZPosition != null)
{
var11 += 0.5D;
}
if (this.adjacentChestXPos != null)
{
var8 += 0.5D;
}
this.worldObj.playSoundEffect(var8, (double)this.yCoord + 0.5D, var11, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.numUsingPlayers == 0 && this.lidAngle > 0.0F || this.numUsingPlayers > 0 && this.lidAngle < 1.0F)
{
float var9 = this.lidAngle;
if (this.numUsingPlayers > 0)
{
this.lidAngle += var1;
}
else
{
this.lidAngle -= var1;
}
if (this.lidAngle > 1.0F)
{
this.lidAngle = 1.0F;
}
float var10 = 0.5F;
if (this.lidAngle < var10 && var9 >= var10 && this.adjacentChestZNeg == null && this.adjacentChestXNeg == null)
{
var11 = (double)this.xCoord + 0.5D;
double var6 = (double)this.zCoord + 0.5D;
if (this.adjacentChestZPosition != null)
{
var6 += 0.5D;
}
if (this.adjacentChestXPos != null)
{
var11 += 0.5D;
}
this.worldObj.playSoundEffect(var11, (double)this.yCoord + 0.5D, var6, "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 par1, int par2)
{
if (par1 == 1)
{
this.numUsingPlayers = par2;
}
return adjacentChestChecked;
}
public void openChest()
{
++this.numUsingPlayers;
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, ModsRing.Dchest.blockID, 1, this.numUsingPlayers);
}
public void closeChest()
{
--this.numUsingPlayers;
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, ModsRing.Dchest.blockID, 1, this.numUsingPlayers);
}
/**
* invalidates a tile entity
*/
public void invalidate()
{
super.invalidate();
this.updateContainingBlockInfo();
this.checkForAdjacentChests();
}
public byte getDirection()
{
return 0;
}
@Override
public boolean isInvNameLocalized() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isStackValidForSlot(int i, ItemStack itemstack) {
// TODO Auto-generated method stub
return false;
}
}
This will call the onUpdate() function of every item in existing ItemStacks in the inventory. In the item you want to update add:
public void onUpdate(ItemStack iStack, World world, Entity entity, int integer, boolean bool){
iStack.setItemDamage(iStack.getItemDamage() + 1);
}
As a demonstration the line in this onUpdate() method increases the itemDamage on every update.
Btw, please don't send PM's about this, I'll look if I have time, I already have email notification.
sorry for pm, but this code works only if the chest is closed but thanks for the help. Also is there a way to make it update only in the chest rather then all the time
also I'm getting this error when its in a chest
-- Head --
Stacktrace:
at mods.ring.Item.ItemStoneEther.onUpdate(ItemStoneEther.java:33)
at mods.ring.TileEntity.TileEntityDchest.updateEntity(TileEntityDchest.java:332)
-- Tile entity being ticked --
Details:
Name: Dchest // mods.ring.TileEntity.TileEntityDchest
Block type: ID #223 (tile.hj // mods.ring.blocks.BlockDchest)
Block data value: 3 / 0x3 / 0b0011
Block location: World: (148,68,64), Chunk: (at 4,4,0 in 9,4; contains blocks 144,0,64 to 159,255,79), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
Actual block type: ID #223 (tile.hj // mods.ring.blocks.BlockDchest)
Actual block data value: 3 / 0x3 / 0b0011
Stacktrace:
at net.minecraft.world.World.updateEntities(World.java:2199)
-- Affected level --
Details:
Level name: MpServer
All players: 1 total; [EntityClientPlayerMP['Player930'/92, l='MpServer', x=148.41, y=69.62, z=66.17]]
Chunk stats: MultiplayerChunkCache: 441
Level seed: 0
Level generator: ID 00 - default, ver 1. Features enabled: false
Level generator options:
Level spawn location: World: (244,64,124), Chunk: (at 4,4,12 in 15,7; contains blocks 240,0,112 to 255,255,127), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
Level time: 423077 game time, 79501 day time
Level dimension: 0
Level storage version: 0x00000 - Unknown?
Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
Forced entities: 42 total; [EntityBat['Bat'/478, l='MpServer', x=128.40, y=28.09, z=125.55], EntityChicken['Chicken'/0, l='MpServer', x=70.47, y=72.00, z=-3.34], EntityBat['Bat'/479, l='MpServer', x=128.94, y=28.00, z=122.44], EntityChicken['Chicken'/2, l='MpServer', x=73.66, y=73.00, z=32.53], EntityChicken['Chicken'/8, l='MpServer', x=85.34, y=73.00, z=-2.63], EntityChicken['Chicken'/9, l='MpServer', x=95.44, y=72.00, z=10.38], EntityChicken['Chicken'/10, l='MpServer', x=91.44, y=67.00, z=34.56], EntityChicken['Chicken'/11, l='MpServer', x=93.53, y=67.00, z=41.47], EntityChicken['Chicken'/12, l='MpServer', x=77.56, y=69.00, z=76.56], EntityChicken['Chicken'/14, l='MpServer', x=105.53, y=72.00, z=-11.47], EntityChicken['Chicken'/16, l='MpServer', x=100.56, y=67.00, z=28.41], EntityChicken['Chicken'/19, l='MpServer', x=111.56, y=69.00, z=52.59], EntityPig['Pig'/18, l='MpServer', x=112.66, y=68.00, z=23.53], EntityChicken['Chicken'/21, l='MpServer', x=123.59, y=70.00, z=58.59], EntityChicken['Chicken'/20, l='MpServer', x=115.44, y=69.00, z=49.31], EntityChicken['Chicken'/22, l='MpServer', x=113.47, y=74.00, z=95.56], EntityBat['Bat'/31, l='MpServer', x=135.25, y=20.10, z=79.75], EntityBat['Bat'/448, l='MpServer', x=90.72, y=20.51, z=15.06], EntityBat['Bat'/30, l='MpServer', x=143.50, y=34.10, z=51.56], EntityBat['Bat'/34, l='MpServer', x=153.50, y=54.10, z=36.25], EntityBat['Bat'/35, l='MpServer', x=159.03, y=49.10, z=55.75], EntityWolf['Wolf'/32, l='MpServer', x=153.69, y=70.00, z=3.38], EntityChicken['Chicken'/33, l='MpServer', x=144.47, y=68.00, z=-8.47], EntityMinecartChest['entity.MinecartChest.name'/38, l='MpServer', x=146.50, y=45.50, z=121.50], EntityChicken['Chicken'/39, l='MpServer', x=168.19, y=70.00, z=19.22], EntityMinecartChest['entity.MinecartChest.name'/36, l='MpServer', x=150.50, y=36.50, z=69.50], EntityBat['Bat'/447, l='MpServer', x=89.88, y=17.92, z=13.44], EntityChicken['Chicken'/37, l='MpServer', x=153.41, y=64.00, z=99.38], EntityBat['Bat'/42, l='MpServer', x=160.25, y=44.10, z=56.69], EntityClientPlayerMP['Player930'/92, l='MpServer', x=148.41, y=69.62, z=66.17], EntityBat['Bat'/43, l='MpServer', x=160.59, y=45.10, z=63.50], EntityPig['Pig'/40, l='MpServer', x=174.13, y=70.00, z=23.13], EntityBat['Bat'/44, l='MpServer', x=168.66, y=43.10, z=61.13], EntityChicken['Chicken'/50, l='MpServer', x=179.34, y=74.00, z=88.78], EntityBat['Bat'/49, l='MpServer', x=190.25, y=62.10, z=82.47], EntityChicken['Chicken'/48, l='MpServer', x=184.53, y=69.00, z=28.53], EntityBat['Bat'/55, l='MpServer', x=199.88, y=39.10, z=67.53], EntityBat['Bat'/485, l='MpServer', x=128.49, y=28.48, z=123.49], EntityChicken['Chicken'/59, l='MpServer', x=218.63, y=69.00, z=5.41], EntityPig['Pig'/62, l='MpServer', x=226.97, y=69.00, z=18.47], EntityBat['Bat'/483, l='MpServer', x=128.48, y=27.42, z=123.55], EntityChicken['Chicken'/60, l='MpServer', x=218.59, y=76.00, z=69.28]]
Retry entities: 0 total; []
Stacktrace:
at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:441)
at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2414)
at net.minecraft.client.Minecraft.run(Minecraft.java:776)
at java.lang.Thread.run(Unknown Source)
-- System Details --
Details:
Minecraft Version: 1.5.2
Operating System: Windows 7 (x86) version 6.1
Java Version: 1.7.0_09, Oracle Corporation
Java VM Version: Java HotSpot(TM) Client VM (mixed mode), Oracle Corporation
Memory: 911725784 bytes (869 MB) / 1046937600 bytes (998 MB) up to 1046937600 bytes (998 MB)
JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
AABB Pool Size: 514 (28784 bytes; 0 MB) allocated, 451 (25256 bytes; 0 MB) used
Suspicious classes: FML and Forge are installed
IntCache: cache: 0, tcache: 0, allocated: 1, tallocated: 63
FML: MCP v7.51 FML v5.2.10.705 Minecraft Forge 7.8.0.705 4 mods loaded, 4 mods active
mcp{7.44} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
FML{5.2.10.705} [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
Forge{7.8.0.705} [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
ring{1.0} [Rings] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available
LWJGL: 2.4.2
OpenGL: AMD Radeon(TM) HD 6620G GL version 4.1.11156 Compatibility Profile Context, ATI Technologies Inc.
Is Modded: Definitely; Client brand changed to 'fml,forge'
Type: Client (map_client.txt)
Texture Pack: Default
Profiler Position: N/A (disabled)
Vec3 Pool Size: 1650 (92400 bytes; 0 MB) allocated, 81 (4536 bytes; 0 MB) used
java.lang.NullPointerException
at mods.ring.Item.ItemStoneEther.onUpdate(ItemStoneEther.java:33)
at mods.ring.TileEntity.TileEntityDchest.updateEntity(TileEntityDchest.java:332)
at net.minecraft.world.World.updateEntities(World.java:2199)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1853)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:835)
at net.minecraft.client.Minecraft.run(Minecraft.java:760)
at java.lang.Thread.run(Unknown Source)
--- END ERROR REPORT e8775e0a ----------
It also should work when the chest is opened... It could be that the changing damage values isn't properly updated to the client though. try adding 'onInventoryChanged();' in the TileEntity's onUpdate() after you've updated all items.
By only updating in the chest you mean only when it is opened or?
About the error: Are you doing anything with the 'EntityLiving' parameter in the onUpdate() method? You can't because this enitity is null when it gets called by the chest (it doesn't have an entity to pass along).
what I'm trying to do is add item when item damage = 0 but when I try to put an if statement along with itemdamage it doesn't work
I've thought of that but even though i know how to create a chest I don't really know how to update it
I have the chest complete would you mind showing me how
sorry for pm, but this code works only if the chest is closed but thanks for the help. Also is there a way to make it update only in the chest rather then all the time
also I'm getting this error when its in a chest
what I'm trying to do is add item when item damage = 0 but when I try to put an if statement along with itemdamage it doesn't work