From ffc4ca93084d97454040debea769c195c4eb5b5f Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Wed, 11 Feb 2015 22:03:53 -0500 Subject: [PATCH] Fix reamining present issues: - Fix dupes - Fix bounding boxes - Fix inventory order being swapped in the GUI - Fix in-world chiseling not working - Fix losing items when metadata changes --- .../chisel/block/BlockPresent.java | 28 +++++-- .../chisel/block/tileentity/IDoubleChest.java | 29 +++++++ .../block/tileentity/TileEntityPresent.java | 79 ++++++++++++++----- .../client/render/tile/RenderPresent.java | 12 +-- .../chisel/item/chisel/ChiselMode.java | 6 +- 5 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/cricketcraft/chisel/block/tileentity/IDoubleChest.java diff --git a/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java b/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java index dc8aff01..6990e035 100644 --- a/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java +++ b/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java @@ -14,6 +14,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.MathHelper; +import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -76,13 +77,13 @@ public class BlockPresent extends Block implements ICarvable { super.onBlockPlacedBy(world, x, y, z, player, stack); int heading = MathHelper.floor_double(player.rotationYaw * 4.0F / 360.0F + 0.5D) & 3; TileEntityPresent te = (TileEntityPresent) world.getTileEntity(x, y, z); - world.setBlockMetadataWithNotify(x, y, z, heading, 3); + world.setBlockMetadataWithNotify(x, y, z, stack.getItemDamage(), 3); + te.setRotation(heading); te.findConnections(); if (te.isConnected()) { TileEntityPresent other = te.getConnection(); - world.setBlockMetadataWithNotify(other.xCoord, other.yCoord, other.zCoord, heading, 3); + other.setRotation(heading); } - te.setType(stack.getItemDamage()); world.markBlockForUpdate(x, y, z); } @@ -92,8 +93,8 @@ public class BlockPresent extends Block implements ICarvable { TileEntityPresent tileEntityPresent = (TileEntityPresent) world.getTileEntity(x, y, z); if (tileEntityPresent != null) { - for (int c = 0; c < tileEntityPresent.getSizeInventory(); c++) { - ItemStack itemStack = tileEntityPresent.getStackInSlot(c); + for (int c = 0; c < tileEntityPresent.getTrueSizeInventory(); c++) { + ItemStack itemStack = tileEntityPresent.getTrueStackInSlot(c); if (itemStack != null) { dropBlockAsItem(world, x, y, z, itemStack); @@ -127,12 +128,14 @@ public class BlockPresent extends Block implements ICarvable { public AxisAlignedBB getBoundingBox(TileEntityPresent me) { if (me == null) { + System.out.println(-1); return AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ); } int x = me.xCoord, y = me.yCoord, z = me.zCoord; if (me.isConnected()) { ForgeDirection dir = me.getConnectionDir(); - if (dir != ForgeDirection.NORTH && (!me.isParent() || dir == ForgeDirection.SOUTH)) { + // warning: magic below! Do not change this conditional + if (dir == ForgeDirection.EAST || (me.isParent() && dir == ForgeDirection.SOUTH) || (!me.isParent() && dir == ForgeDirection.SOUTH)) { return AxisAlignedBB.getBoundingBox(x + minX, y + minY, z + minZ, x + maxX + dir.offsetX, y + maxY, z + maxZ + dir.offsetZ); } else { return AxisAlignedBB.getBoundingBox(x + dir.offsetX + minX, y + minY, z + dir.offsetZ + minZ, x + maxX, y + maxY, z + maxZ); @@ -191,6 +194,16 @@ public class BlockPresent extends Block implements ICarvable { } return 0; } + + @Override + public int damageDropped(int meta) { + return meta; + } + + @Override + public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) { + return new ItemStack(this, 1, world.getBlockMetadata(x, y, z)); + } @Override @SideOnly(Side.CLIENT) @@ -200,8 +213,7 @@ public class BlockPresent extends Block implements ICarvable { @Override public CarvableVariation getVariation(IBlockAccess world, int x, int y, int z, int metadata) { - TileEntity te = world.getTileEntity(x, y, z); - return te instanceof TileEntityPresent ? carverHelper.getVariation(((TileEntityPresent) te).getType()) : carverHelper.getVariation(metadata); + return carverHelper.getVariation(metadata); } @Override diff --git a/src/main/java/com/cricketcraft/chisel/block/tileentity/IDoubleChest.java b/src/main/java/com/cricketcraft/chisel/block/tileentity/IDoubleChest.java new file mode 100644 index 00000000..90ded712 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/block/tileentity/IDoubleChest.java @@ -0,0 +1,29 @@ +package com.cricketcraft.chisel.block.tileentity; + +import net.minecraft.item.ItemStack; + +public interface IDoubleChest { + + /** + * @return The size of this specific chest's inventory. Does not account for the connection. + */ + int getTrueSizeInventory(); + + /** + * @param slot + * The true slot index. + * @return The actual stack in this chest's slot. Does not account for the connection. + */ + ItemStack getTrueStackInSlot(int slot); + + /** + * Puts a stack in this chest's real slot at the given true index. + * + * @param slot + * The true slot index. + * @param stack + * The ItemStack to place in the slot. + */ + void putStackInTrueSlot(int slot, ItemStack stack); + +} diff --git a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityPresent.java b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityPresent.java index f60c7864..944c62e1 100644 --- a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityPresent.java +++ b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityPresent.java @@ -1,5 +1,6 @@ package com.cricketcraft.chisel.block.tileentity; +import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; @@ -10,19 +11,20 @@ import net.minecraft.network.Packet; import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import com.cricketcraft.chisel.init.ChiselBlocks; import com.cricketcraft.chisel.network.PacketHandler; import com.cricketcraft.chisel.network.message.MessagePresentConnect; -public class TileEntityPresent extends TileEntity implements IInventory { +public class TileEntityPresent extends TileEntity implements IInventory, IDoubleChest { private TileEntityPresent connection = null; private ForgeDirection cachedDir = null; private boolean isParent; - private ItemStack[] inventory = new ItemStack[27]; - private int type; + private final ItemStack[] inventory = new ItemStack[27]; + private int rotation; private boolean autoSearch = true; @Override @@ -46,12 +48,14 @@ public class TileEntityPresent extends TileEntity implements IInventory { } private boolean connectTo(TileEntityPresent present, ForgeDirection dir) { - if (present.type == type && !present.isConnected() && Math.abs(present.xCoord - xCoord + present.yCoord - yCoord + present.zCoord - zCoord) == 1) { + if (present.getBlockMetadata() == getBlockMetadata() && !present.isConnected() && Math.abs(present.xCoord - xCoord + present.yCoord - yCoord + present.zCoord - zCoord) == 1) { connection = present; connection.connection = this; connection.cachedDir = dir.getOpposite(); + connection.markDirty(); cachedDir = dir; isParent = !present.isParent; + markDirty(); PacketHandler.INSTANCE.sendToDimension(new MessagePresentConnect(this, dir, true), worldObj.provider.dimensionId); return true; } @@ -70,8 +74,10 @@ public class TileEntityPresent extends TileEntity implements IInventory { if (isConnected()) { this.connection.cachedDir = null; this.connection.connection = null; + this.connection.markDirty(); this.cachedDir = null; this.connection = null; + this.markDirty(); PacketHandler.INSTANCE.sendToDimension(new MessagePresentConnect(this, ForgeDirection.UNKNOWN, false), worldObj.provider.dimensionId); } } @@ -88,12 +94,12 @@ public class TileEntityPresent extends TileEntity implements IInventory { return isParent || !isConnected(); } - public int getType() { - return type; + public int getRotation() { + return rotation; } - public void setType(int type) { - this.type = type; + public void setRotation(int rot) { + this.rotation = rot; } public void findConnections() { @@ -123,7 +129,7 @@ public class TileEntityPresent extends TileEntity implements IInventory { super.writeToNBT(tag); NBTTagList nbttaglist = new NBTTagList(); - for (int i = 0; i < inventory.length; ++i) + for (int i = 0; i < getTrueSizeInventory(); ++i) { if (inventory[i] != null) { @@ -136,7 +142,7 @@ public class TileEntityPresent extends TileEntity implements IInventory { tag.setTag("Items", nbttaglist); tag.setBoolean("isParent", isParent); - tag.setInteger("presentType", type); + tag.setInteger("rotation", rotation); if (cachedDir != null) { tag.setInteger("conDir", cachedDir.ordinal()); } @@ -147,14 +153,13 @@ public class TileEntityPresent extends TileEntity implements IInventory { { super.readFromNBT(tag); NBTTagList nbttaglist = tag.getTagList("Items", 10); - inventory = new ItemStack[this.getSizeInventory()]; for (int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); int j = nbttagcompound1.getByte("Slot") & 255; - if (j >= 0 && j < inventory.length) + if (j >= 0 && j < getTrueSizeInventory()) { inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1); } @@ -162,7 +167,7 @@ public class TileEntityPresent extends TileEntity implements IInventory { this.isParent = tag.getBoolean("isParent"); - this.type = tag.getInteger("presentType"); + this.rotation = tag.getInteger("rotation"); if (tag.hasKey("conDir")) { cachedDir = ForgeDirection.values()[tag.getInteger("conDir")]; } @@ -180,18 +185,33 @@ public class TileEntityPresent extends TileEntity implements IInventory { public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) { readFromNBT(packet.func_148857_g()); } + + @Override + public boolean shouldRefresh(Block oldBlock, Block newBlock, int oldMeta, int newMeta, World world, int x, int y, int z) { + // prevent losing TE data when in-world chiseling + return oldBlock != newBlock; + } + private int getAdjustedSlot(int slot) { + slot %= getSizeInventory(); + if (isConnected() && !isParent) { + slot = (slot + getTrueSizeInventory()) % getSizeInventory(); + } + return slot; + } + /* IInventory */ @Override public int getSizeInventory() { - return isConnected() ? inventory.length + connection.inventory.length : inventory.length; + return isConnected() ? getTrueSizeInventory() + connection.getTrueSizeInventory() : getTrueSizeInventory(); } @Override public ItemStack getStackInSlot(int slot) { - if (slot >= inventory.length) { - return isConnected() ? connection.inventory[slot % inventory.length] : null; + slot = getAdjustedSlot(slot); + if (slot >= getTrueSizeInventory()) { + return isConnected() ? connection.inventory[slot % getTrueSizeInventory()] : null; } else { return inventory[slot]; } @@ -200,10 +220,11 @@ public class TileEntityPresent extends TileEntity implements IInventory { @Override public ItemStack decrStackSize(int slot, int amount) { + slot = getAdjustedSlot(slot); ItemStack[] inv = inventory; - if (isConnected() && slot >= inventory.length) { + if (isConnected() && slot >= getTrueSizeInventory()) { inv = connection.inventory; - slot %= inventory.length; + slot %= getTrueSizeInventory(); } if (inv[slot] != null) @@ -243,10 +264,11 @@ public class TileEntityPresent extends TileEntity implements IInventory { @Override public void setInventorySlotContents(int slot, ItemStack stack) { - if (slot < inventory.length) { + slot = getAdjustedSlot(slot); + if (slot < getTrueSizeInventory()) { inventory[slot] = stack; } else if (isConnected()) { - connection.inventory[slot % inventory.length] = stack; + connection.inventory[slot % getTrueSizeInventory()] = stack; } } @@ -284,4 +306,21 @@ public class TileEntityPresent extends TileEntity implements IInventory { public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) { return true; } + + /* IDoubleChest */ + + @Override + public int getTrueSizeInventory() { + return inventory.length; + } + + @Override + public ItemStack getTrueStackInSlot(int slot) { + return inventory[slot % getTrueSizeInventory()]; + } + + @Override + public void putStackInTrueSlot(int slot, ItemStack stack) { + inventory[slot % getTrueSizeInventory()] = stack; + } } diff --git a/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderPresent.java b/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderPresent.java index dcf906cf..0c66ba49 100644 --- a/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderPresent.java +++ b/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderPresent.java @@ -34,11 +34,11 @@ public class RenderPresent extends TileEntitySpecialRenderer implements IItemRen GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); GL11.glTranslatef((float) x, (float) y + 1.0F, (float) z + 1.0F); GL11.glScalef(1.0F, -1.0F, -1.0F); - int meta = present.getBlockMetadata(); + int rotation = present.getRotation(); bindTexture(present); if (!present.isConnected()) { - GL11.glRotatef(90 * meta + 180, 0, 1, 0); - switch(present.getBlockMetadata()) { + GL11.glRotatef(90 * rotation + 180, 0, 1, 0); + switch(rotation) { case 0: GL11.glTranslatef(-1, 0, -1); break; @@ -67,7 +67,7 @@ public class RenderPresent extends TileEntitySpecialRenderer implements IItemRen default: break; } - if (meta == 0 || meta == 3) { + if (rotation == 0 || rotation == 3) { GL11.glRotatef(180, 0, 1, 0); GL11.glTranslatef(-2, 0, -1); } @@ -77,10 +77,10 @@ public class RenderPresent extends TileEntitySpecialRenderer implements IItemRen } private void bindTexture(TileEntityPresent present) { - int idx = present.getType() + (present.isConnected() ? 0 : 16); + int idx = present.getWorldObj().getBlockMetadata(present.xCoord, present.yCoord, present.zCoord) + (present.isConnected() ? 0 : 16); ResourceLocation rl = textureCache.get(idx); if (rl == null) { - String res = ChiselBlocks.present.getModelTexture(present.getType()); + String res = ChiselBlocks.present.getModelTexture(idx % 16); res += present.isConnected() ? "_double.png" : ".png"; rl = new ResourceLocation("chisel", res); textureCache.put(idx, rl); diff --git a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java index 0d2446c5..96c39d7f 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java @@ -102,7 +102,11 @@ public enum ChiselMode implements IChiselMode { } if (held != null && held.getItem() instanceof IChiselItem) { - world.setBlock(x, y, z, v.getBlock(), v.getBlockMeta(), 3); + if (origBlock == block) { + world.setBlockMetadataWithNotify(x, y, z, v.getBlockMeta(), 3); + } else { + world.setBlock(x, y, z, v.getBlock(), v.getBlockMeta(), 3); + } boolean breakChisel = false; if (((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.getCurrentEquippedItem(), v)) { player.getCurrentEquippedItem().damageItem(1, player);