From 276859566ed8d036127ad3b13a1ac2a591a4cc16 Mon Sep 17 00:00:00 2001 From: alexbegt Date: Wed, 17 May 2017 21:12:33 -0400 Subject: [PATCH] Actually fix Crystal Chest's crashing clients with ArrayIndexOutOfBoundsException due to two different items having two 64 stacks in the chest, also Items will now render if there is more than one item stack, but only one will be rendered. (Before it wouldn't render any items being in the chest) (pulled from 1.11.2) #89 --- .../mods/ironchest/TileEntityIronChest.java | 64 ++++++++++++++----- .../client/TileEntityIronChestRenderer.java | 57 +++++++++++------ 2 files changed, 87 insertions(+), 34 deletions(-) diff --git a/src/main/java/cpw/mods/ironchest/TileEntityIronChest.java b/src/main/java/cpw/mods/ironchest/TileEntityIronChest.java index ee952f2..4c913b1 100755 --- a/src/main/java/cpw/mods/ironchest/TileEntityIronChest.java +++ b/src/main/java/cpw/mods/ironchest/TileEntityIronChest.java @@ -37,16 +37,35 @@ import net.minecraftforge.common.util.Constants; public class TileEntityIronChest extends TileEntityLockableLoot implements ITickable, IInventory { - private int ticksSinceSync = -1; - public float prevLidAngle; - public float lidAngle; - private int numPlayersUsing; + /** Chest Contents */ public ItemStack[] chestContents; + + /** Crystal chest top stacks */ private ItemStack[] topStacks; + + /** 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; + + /** Direction chest is facing */ private EnumFacing facing; + + /** If the inventory got touched */ private boolean inventoryTouched; + + /** If the inventory had items */ private boolean hadStuff; + private String customName; + private IronChestType chestType; public TileEntityIronChest() @@ -139,17 +158,29 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick mainLoop: for (int i = 0; i < this.getSizeInventory(); i++) { - if (this.chestContents[i] != null) + ItemStack itemStack = this.chestContents[i]; + + if (itemStack != null) { for (int j = 0; j < compressedIdx; j++) { - if (tempCopy[j].isItemEqual(this.chestContents[i])) + ItemStack tempCopyStack = tempCopy[j]; + + if (ItemStack.areItemsEqual(tempCopyStack, itemStack)) { - tempCopy[j].stackSize += this.chestContents[i].stackSize; + if (itemStack.stackSize != tempCopyStack.stackSize) + { + tempCopyStack.stackSize += this.chestContents[i].stackSize; + } + continue mainLoop; } } - tempCopy[compressedIdx++] = this.chestContents[i].copy(); + + tempCopy[compressedIdx] = itemStack.copy(); + + compressedIdx++; + hasStuff = true; } } @@ -166,6 +197,7 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick if (this.worldObj != null) { IBlockState iblockstate = this.worldObj.getBlockState(this.pos); + this.worldObj.notifyBlockUpdate(this.pos, iblockstate, iblockstate, 3); } @@ -199,12 +231,14 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick { if (element != null && element.stackSize > 0) { - this.topStacks[p++] = element; - if (p == this.topStacks.length) { break; } + + this.topStacks[p] = element; + + p++; } } @@ -408,9 +442,7 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick double y = this.pos.getY() + 0.5D; double z = this.pos.getZ() + 0.5D; - //@formatter:off this.worldObj.playSound(null, x, y, z, SoundEvents.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F); - //@formatter:on } if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F) @@ -439,9 +471,7 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick double y = this.pos.getY() + 0.5D; double z = this.pos.getZ() + 0.5D; - //@formatter:off this.worldObj.playSound(null, x, y, z, SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F); - //@formatter:on } if (this.lidAngle < 0.0F) @@ -613,12 +643,14 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick { if (is != null) { - sortList[pos++] = is; + sortList[pos] = is; } else { - sortList[pos++] = null; + sortList[pos] = null; } + + pos++; } return sortList; diff --git a/src/main/java/cpw/mods/ironchest/client/TileEntityIronChestRenderer.java b/src/main/java/cpw/mods/ironchest/client/TileEntityIronChestRenderer.java index d5236ea..aa6a1f2 100755 --- a/src/main/java/cpw/mods/ironchest/client/TileEntityIronChestRenderer.java +++ b/src/main/java/cpw/mods/ironchest/client/TileEntityIronChestRenderer.java @@ -31,12 +31,16 @@ import net.minecraft.util.EnumFacing; public class TileEntityIronChestRenderer extends TileEntitySpecialRenderer { private Random random; + private RenderEntityItem itemRenderer; + private ModelChest model; private static float[][] shifts = { { 0.3F, 0.45F, 0.3F }, { 0.7F, 0.45F, 0.3F }, { 0.3F, 0.45F, 0.7F }, { 0.7F, 0.45F, 0.7F }, { 0.3F, 0.1F, 0.3F }, { 0.7F, 0.1F, 0.3F }, { 0.3F, 0.1F, 0.7F }, { 0.7F, 0.1F, 0.7F }, { 0.5F, 0.32F, 0.5F } }; + private static EntityItem customitem = new EntityItem(null); + private static float halfPI = (float) (Math.PI / 2D); public TileEntityIronChestRenderer() @@ -46,26 +50,26 @@ public class TileEntityIronChestRenderer extends TileEntitySpecialRenderer= 0) + if (destroyStage >= 0) { - this.bindTexture(DESTROY_STAGES[breakStage]); + this.bindTexture(DESTROY_STAGES[destroyStage]); GlStateManager.matrixMode(5890); GlStateManager.pushMatrix(); GlStateManager.scale(4F, 4F, 1F); @@ -76,11 +80,14 @@ public class TileEntityIronChestRenderer extends TileEntitySpecialRenderer= 0) + + if (destroyStage >= 0) { GlStateManager.matrixMode(5890); GlStateManager.popMatrix(); GlStateManager.matrixMode(5888); } + if (type == IronChestType.CRYSTAL) { GlStateManager.enableCull(); } + GlStateManager.popMatrix(); GlStateManager.color(1F, 1F, 1F, 1F); - if (type.isTransparent() - && tile.getDistanceSq(this.rendererDispatcher.entityX, this.rendererDispatcher.entityY, this.rendererDispatcher.entityZ) < 128d) + if (type.isTransparent() && te.getDistanceSq(this.rendererDispatcher.entityX, this.rendererDispatcher.entityY, this.rendererDispatcher.entityZ) < 128d) { this.random.setSeed(254L); + float shiftX; float shiftY; float shiftZ; int shift = 0; float blockScale = 0.70F; - float timeD = (float) (360D * (System.currentTimeMillis() & 0x3FFFL) / 0x3FFFL) - partialTick; - if (tile.getTopItemStacks()[1] == null) + float timeD = (float) (360D * (System.currentTimeMillis() & 0x3FFFL) / 0x3FFFL) - partialTicks; + + if (te.getTopItemStacks()[1] == null) { shift = 8; blockScale = 0.85F; } + GlStateManager.pushMatrix(); GlStateManager.translate((float) x, (float) y, (float) z); customitem.setWorld(this.getWorld()); customitem.hoverStart = 0F; - for (ItemStack item : tile.getTopItemStacks()) + + for (ItemStack item : te.getTopItemStacks()) { - if (shift > shifts.length - 1) + if (shift > shifts.length) { break; } + if (item == null) { shift++; continue; } + shiftX = shifts[shift][0]; shiftY = shifts[shift][1]; shiftZ = shifts[shift][2]; shift++; + GlStateManager.pushMatrix(); GlStateManager.translate(shiftX, shiftY, shiftZ); GlStateManager.rotate(timeD, 0F, 1F, 0F); GlStateManager.scale(blockScale, blockScale, blockScale); + customitem.setEntityItemStack(item); if (this.itemRenderer == null) { - this.itemRenderer = new RenderEntityItem(Minecraft.getMinecraft().getRenderManager(), Minecraft.getMinecraft().getRenderItem()) { + this.itemRenderer = new RenderEntityItem(Minecraft.getMinecraft().getRenderManager(), Minecraft.getMinecraft().getRenderItem()) + { @Override public int getModelCount(ItemStack stack) { @@ -205,7 +225,8 @@ public class TileEntityIronChestRenderer extends TileEntitySpecialRenderer