Posted April 5, 201312 yr I want to detect the stacksize in the inventory. But it will not work! file 242 lines (195 sloc) 5.142 kb EditRawBlameHistory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 package speiger.src.tinychest.common.tileentity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; public abstract class TileEntityAdvancedTinyChest extends TileEntity implements IInventory { private String inventoryName; private ItemStack[] advTinyChest; private int facing = 0; private static boolean isFull = false; private static boolean isEmpty = true; private int maxstack = 0; private int stacksizes = 0; private int inventorymaxstack = 0; private int inventorystack = 64; public TileEntityAdvancedTinyChest(String name, int par1) { inventoryName = name; advTinyChest = new ItemStack[par1]; } public int getSizeInventory() { return this.advTinyChest.length; } public ItemStack getStackInSlot(int par1) { return this.advTinyChest[par1]; } public ItemStack decrStackSize(int par1, int par2) { if (this.advTinyChest[par1] != null) { ItemStack var3; if (this.advTinyChest[par1].stackSize <= par2) { var3 = this.advTinyChest[par1]; this.advTinyChest[par1] = null; return var3; } else { var3 = this.advTinyChest[par1].splitStack(par2); if (this.advTinyChest[par1].stackSize == 0) { this.advTinyChest[par1] = null; } return var3; } } else { return null; } } public ItemStack getStackInSlotOnClosing(int par1) { if (this.advTinyChest[par1] != null) { ItemStack var2 = this.advTinyChest[par1]; this.advTinyChest[par1] = null; return var2; } else { return null; } } public void setInventorySlotContents(int par1, ItemStack par2ItemStack) { this.advTinyChest[par1] = par2ItemStack; if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit()) { par2ItemStack.stackSize = this.getInventoryStackLimit(); } } public String getInvName() { return inventoryName; } public int getInventoryStackLimit() { return 64; } public boolean isUseableByPlayer(EntityPlayer var1) { return true; } public void openChest() { } public void readFromNBT(NBTTagCompound par1NBTTagCompound) { super.readFromNBT(par1NBTTagCompound); NBTTagList var2 = par1NBTTagCompound.getTagList("Items"); this.advTinyChest = new ItemStack[this.getSizeInventory()]; for (int var3 = 0; var3 < var2.tagCount(); ++var3) { NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3); byte var5 = var4.getByte("Slot"); if (var5 >= 0 && var5 < this.advTinyChest.length) { this.advTinyChest[var5] = ItemStack.loadItemStackFromNBT(var4); } } facing = par1NBTTagCompound.getInteger("face"); } /** * Writes a tile entity to NBT. */ public void writeToNBT(NBTTagCompound par1NBTTagCompound) { super.writeToNBT(par1NBTTagCompound); par1NBTTagCompound.setInteger("face", facing); NBTTagList var2 = new NBTTagList(); for (int var3 = 0; var3 < this.advTinyChest.length; ++var3) { if (this.advTinyChest[var3] != null) { NBTTagCompound var4 = new NBTTagCompound(); var4.setByte("Slot", (byte)var3); this.advTinyChest[var3].writeToNBT(var4); var2.appendTag(var4); } } par1NBTTagCompound.setTag("Items", var2); } @Override public void updateEntity() { // the - 1 prevent it from crashing! for(int i = 0; i < getSizeInventory() - 1; i++) { maxstack += advTinyChest[i].getMaxStackSize(); stacksizes += advTinyChest[i].stackSize; inventorymaxstack += getInventoryStackLimit(); } if(maxstack == stacksizes || maxstack == inventorymaxstack) { isFull = true; } else if(maxstack == 0) { isFull = false; } else { isFull = false; } if(stacksizes == 0) { isEmpty = true; } else { isEmpty = false; } } public boolean isFull() { return isFull; } public boolean isEmpty() { return isEmpty; } public void closeChest() { } public void setFacing(int i) { facing = i; } public int getFacing() { return facing; } public void InventoryLimitUP(int i) { if(inventorystack > 64) { inventorystack = 64; } inventorystack += i; } public void InventoryLimitDown(int i) { if(inventorystack < 1) { inventorystack = 1; } inventorystack -= i; } }
April 5, 201312 yr Well, I'm guessing you are talking about @Override public void updateEntity() { // the - 1 prevent it from crashing! for(int i = 0; i < getSizeInventory() - 1; i++) { maxstack += advTinyChest[i].getMaxStackSize(); stacksizes += advTinyChest[i].stackSize; inventorymaxstack += getInventoryStackLimit(); } if(maxstack == stacksizes || maxstack == inventorymaxstack) { isFull = true; } else if(maxstack == 0) { isFull = false; } else { isFull = false; } if(stacksizes == 0) { isEmpty = true; } else { isEmpty = false; } } Since I don't have a precise problem, I'm just going to go through all the things I see and hopefully one of them is the problem you're having. First, you shouldn't need to subtract 1 from getSizeInventory(), since all that does is return the length of the ItemStack array, which is from 0 to par1-1 already. Second, it looks like you don't ever reset the value of maxstack, stacksizes, or inventorymaxstack, so they just keep adding whatever value is returned by the code to themselves every time the tile entity is updated. I'm thinking you want the entity to figure out if it's full by comparing the total number of items inside it to the maximum it can hold, which is what stacksizes and inventorymaxstack is for, since there are items that don't stack to 64. To fix this, just add maxstack=0; stacksizes=0; inventorymaxstack=0; before the for loop. I hope this helps fix the problem, if not then throwing some System.out.println statements returning variables or using debug mode and some breakpoints should give you more information.
April 5, 201312 yr Author Sorry to say but you are wrong. I did find it out after 5 minutes i postet this question! Thanks to powercraft! the code is like this: public void updateEntity() { for(int i = 0; i < getSizeInventory; i++) { if(getStackInSlot(i) == null) { isEmpty = true; } else { isEmpty = false; } if (getStackInSlot(i).stackSize == Math.min(getInventoryStackLimit(), getStackInSlot(i).getMaxStackSize())) { isFull = true; } else { isFull = false; } } }
April 5, 201312 yr Author Who can read is two steps for everyone. I read much mod sources every day to find a fix or learn something. Your Source too. Eloraams too . But her is in my eyes very unclean! Ic2 Is cleaner
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.