From fcc7f72a6b8a6fee4bf779d79169b0392c20ef7e Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 29 Jun 2012 12:47:09 -0400 Subject: [PATCH] Support vanilla to ironchest upgrades. Hi florastar and faunastar! --- .../cpw/mods/ironchest/BlockIronChest.java | 13 +++--- .../cpw/mods/ironchest/ChestChangerType.java | 5 ++- .../cpw/mods/ironchest/IronChestType.java | 39 ++++++++++++----- .../cpw/mods/ironchest/ItemChestChanger.java | 40 ++++++++++++++---- .../cpw/mods/ironchest/ItemIronChest.java | 10 ++--- .../mods/ironchest/TileEntityIronChest.java | 8 ++-- .../cpw/mods/ironchest/mod_IronChest.java | 2 +- .../mods/ironchest/sprites/item_textures.png | Bin 1672 -> 1892 bytes 8 files changed, 80 insertions(+), 37 deletions(-) diff --git a/IronChests2/common/cpw/mods/ironchest/BlockIronChest.java b/IronChests2/common/cpw/mods/ironchest/BlockIronChest.java index 5219ac7..6547030 100644 --- a/IronChests2/common/cpw/mods/ironchest/BlockIronChest.java +++ b/IronChests2/common/cpw/mods/ironchest/BlockIronChest.java @@ -4,7 +4,7 @@ * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html - * + * * Contributors: * cpw - initial API and implementation ******************************************************************************/ @@ -18,6 +18,7 @@ import net.minecraft.src.EntityItem; import net.minecraft.src.EntityLiving; import net.minecraft.src.EntityPlayer; import net.minecraft.src.IBlockAccess; +import net.minecraft.src.IInventory; import net.minecraft.src.ItemStack; import net.minecraft.src.Material; import net.minecraft.src.MathHelper; @@ -163,12 +164,12 @@ public class BlockIronChest extends BlockContainer implements ITextureProvider { TileEntityIronChest tileentitychest = (TileEntityIronChest) world.getBlockTileEntity(i, j, k); if (tileentitychest != null) { - dropContent(0, tileentitychest, world); + dropContent(0, tileentitychest, world, tileentitychest.xCoord, tileentitychest.yCoord, tileentitychest.zCoord); } super.onBlockRemoval(world, i, j, k); } - public void dropContent(int newSize, TileEntityIronChest chest, World world) { + public void dropContent(int newSize, IInventory chest, World world, int xCoord, int yCoord, int zCoord) { for (int l = newSize; l < chest.getSizeInventory(); l++) { ItemStack itemstack = chest.getStackInSlot(l); @@ -187,7 +188,7 @@ public class BlockIronChest extends BlockContainer implements ITextureProvider { i1 = itemstack.stackSize; } itemstack.stackSize -= i1; - EntityItem entityitem = new EntityItem(world, (float) chest.xCoord + f, (float) chest.yCoord + (newSize > 0 ? 1 : 0) + f1, (float) chest.zCoord + f2, + EntityItem entityitem = new EntityItem(world, (float) xCoord + f, (float) yCoord + (newSize > 0 ? 1 : 0) + f1, (float) zCoord + f2, new ItemStack(itemstack.itemID, i1, itemstack.getItemDamage())); float f3 = 0.05F; entityitem.motionX = (float) random.nextGaussian() * f3; @@ -206,7 +207,9 @@ public class BlockIronChest extends BlockContainer implements ITextureProvider { @Override public void addCreativeItems(ArrayList itemList) { for (IronChestType type : IronChestType.values()) { - itemList.add(new ItemStack(this, 1, type.ordinal())); + if (type.isValidForCreativeMode()) { + itemList.add(new ItemStack(this, 1, type.ordinal())); + } } } } diff --git a/IronChests2/common/cpw/mods/ironchest/ChestChangerType.java b/IronChests2/common/cpw/mods/ironchest/ChestChangerType.java index 0704455..cfc24bf 100644 --- a/IronChests2/common/cpw/mods/ironchest/ChestChangerType.java +++ b/IronChests2/common/cpw/mods/ironchest/ChestChangerType.java @@ -12,6 +12,7 @@ import static cpw.mods.ironchest.IronChestType.GOLD; import static cpw.mods.ironchest.IronChestType.IRON; import static cpw.mods.ironchest.IronChestType.SILVER; import static cpw.mods.ironchest.IronChestType.CRYSTAL; +import static cpw.mods.ironchest.IronChestType.WOOD; import net.minecraft.src.Block; import net.minecraft.src.ItemStack; @@ -23,7 +24,9 @@ public enum ChestChangerType { COPPERSILVER(COPPER, SILVER, "copperSilverUpgrade", "Copper to Silver Chest Upgrade", "mmm", "msm", "mmm"), SILVERGOLD(SILVER, GOLD, "silverGoldUpgrade", "Silver to Gold Chest Upgrade", "mGm", "GsG", "mGm"), COPPERIRON(COPPER, IRON, "copperIronUpgrade", "Copper to Iron Chest Upgrade", "mGm", "GsG", "mGm"), - DIAMONDCRYSTAL(DIAMOND, CRYSTAL, "diamondCrystalUpgrade", "Diamond to Crystal Chest Upgrade", "GGG", "GOG", "GGG"); + DIAMONDCRYSTAL(DIAMOND, CRYSTAL, "diamondCrystalUpgrade", "Diamond to Crystal Chest Upgrade", "GGG", "GOG", "GGG"), + WOODIRON(WOOD, IRON, "woodIronUpgrade", "Normal chest to Iron Chest Upgrade", "mmm", "msm", "mmm"), + WOODCOPPER(WOOD, COPPER, "woodCopperUpgrade", "Normal chest to Copper Chest Upgrade", "mmm", "msm", "mmm"); private IronChestType source; private IronChestType target; diff --git a/IronChests2/common/cpw/mods/ironchest/IronChestType.java b/IronChests2/common/cpw/mods/ironchest/IronChestType.java index c065c6f..417fa73 100644 --- a/IronChests2/common/cpw/mods/ironchest/IronChestType.java +++ b/IronChests2/common/cpw/mods/ironchest/IronChestType.java @@ -26,7 +26,8 @@ public enum IronChestType { DIAMOND(108, 12, true, "Diamond Chest", "diamondchest.png", 2, Arrays.asList("gemDiamond"), TileEntityDiamondChest.class, "GGGmPmGGG", "GGGG4Gmmm"), COPPER(45, 9, false, "Copper Chest", "copperchest.png", 3, Arrays.asList("ingotCopper"), TileEntityCopperChest.class, "mmmmCmmmm"), SILVER(72, 9, false, "Silver Chest", "silverchest.png", 4, Arrays.asList("ingotSilver"), TileEntitySilverChest.class, "mmmm0mmmm", "mGmG3GmGm"), - CRYSTAL(108, 12, true, "Crystal Chest", "crystalchest.png", 5, Arrays.asList("blockGlass"), TileEntityCrystalChest.class, "GGGGPGGGG"); + CRYSTAL(108, 12, true, "Crystal Chest", "crystalchest.png", 5, Arrays.asList("blockGlass"), TileEntityCrystalChest.class, "GGGGPGGGG"), + WOOD(0,0,false,"","",-1,Arrays.asList("blockPlanks"),null); int size; private int rowLength; public String friendlyName; @@ -61,16 +62,18 @@ public enum IronChestType { public static TileEntityIronChest makeEntity(int metadata) { // Compatibility - int chesttype = metadata; - try { - TileEntityIronChest te = values()[chesttype].clazz.newInstance(); - return te; - } catch (InstantiationException e) { - // unpossible - e.printStackTrace(); - } catch (IllegalAccessException e) { - // unpossible - e.printStackTrace(); + int chesttype = validateMeta(metadata); + if (chesttype == metadata) { + try { + TileEntityIronChest te = values()[chesttype].clazz.newInstance(); + return te; + } catch (InstantiationException e) { + // unpossible + e.printStackTrace(); + } catch (IllegalAccessException e) { + // unpossible + e.printStackTrace(); + } } return null; } @@ -117,6 +120,8 @@ public enum IronChestType { return Item.diamond; } else if (mat == "blockGlass") { return Block.glass; + } else if (mat == "blockPlanks") { + return Block.planks; } return mat; } @@ -143,4 +148,16 @@ public enum IronChestType { return matList; } + public static int validateMeta(int i) { + if (i < values().length && values()[i].size>0) { + return i; + } else { + return 0; + } + } + + public boolean isValidForCreativeMode() { + return validateMeta(ordinal())==ordinal(); + } + } diff --git a/IronChests2/common/cpw/mods/ironchest/ItemChestChanger.java b/IronChests2/common/cpw/mods/ironchest/ItemChestChanger.java index e771bd7..279c6d4 100644 --- a/IronChests2/common/cpw/mods/ironchest/ItemChestChanger.java +++ b/IronChests2/common/cpw/mods/ironchest/ItemChestChanger.java @@ -4,16 +4,18 @@ * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html - * + * * Contributors: * cpw - initial API and implementation ******************************************************************************/ package cpw.mods.ironchest; +import cpw.mods.fml.common.ReflectionHelper; import net.minecraft.src.EntityPlayer; import net.minecraft.src.Item; import net.minecraft.src.ItemStack; import net.minecraft.src.TileEntity; +import net.minecraft.src.TileEntityChest; import net.minecraft.src.World; import net.minecraft.src.forge.ITextureProvider; @@ -32,21 +34,43 @@ public class ItemChestChanger extends Item implements ITextureProvider { @Override public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int X, int Y, int Z, int side) { TileEntity te=world.getBlockTileEntity(X,Y,Z); + TileEntityIronChest newchest; if (te!=null && te instanceof TileEntityIronChest) { TileEntityIronChest ironchest=(TileEntityIronChest)te; - TileEntityIronChest newchest=ironchest.applyUpgradeItem(this); + newchest=ironchest.applyUpgradeItem(this); if (newchest==null) { return false; } - world.setBlockTileEntity(X, Y, Z, newchest); - world.setBlockMetadataWithNotify(X, Y, Z, newchest.getType().ordinal()); - world.notifyBlocksOfNeighborChange(X, Y, Z, world.getBlockId(X, Y, Z)); - world.markBlockNeedsUpdate(X, Y, Z); - stack.stackSize=0; - return true; + } else if (te!=null && te instanceof TileEntityChest) { + TileEntityChest tec = (TileEntityChest) te; + if (tec.numUsingPlayers > 0) { + return false; + } + if (!getType().canUpgrade(IronChestType.WOOD)) { + return false; + } + newchest = IronChestType.makeEntity(getTargetChestOrdinal(IronChestType.WOOD.ordinal())); + int newSize = newchest.chestContents.length; + ItemStack[] chestContents = ReflectionHelper.getPrivateValue(TileEntityChest.class, tec, "chestContents"); + System.arraycopy(chestContents, 0, newchest.chestContents, 0, Math.min(newSize, chestContents.length)); + BlockIronChest block = mod_IronChest.ironChestBlock; + block.dropContent(newSize, tec, world, tec.xCoord, tec.yCoord, tec.zCoord); + newchest.setFacing((byte)tec.getBlockMetadata()); + newchest.sortTopStacks(); + for (int i = 0; i< Math.min(newSize, chestContents.length); i++) + { + chestContents[i]=null; + } + world.setBlock(X, Y, Z, block.blockID); } else { return false; } + world.setBlockTileEntity(X, Y, Z, newchest); + world.setBlockMetadataWithNotify(X, Y, Z, newchest.getType().ordinal()); + world.notifyBlocksOfNeighborChange(X, Y, Z, world.getBlockId(X, Y, Z)); + world.markBlockNeedsUpdate(X, Y, Z); + stack.stackSize=0; + return true; } @Override diff --git a/IronChests2/common/cpw/mods/ironchest/ItemIronChest.java b/IronChests2/common/cpw/mods/ironchest/ItemIronChest.java index aaaa28c..e8aad0d 100644 --- a/IronChests2/common/cpw/mods/ironchest/ItemIronChest.java +++ b/IronChests2/common/cpw/mods/ironchest/ItemIronChest.java @@ -4,7 +4,7 @@ * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html - * + * * Contributors: * cpw - initial API and implementation ******************************************************************************/ @@ -25,17 +25,13 @@ public class ItemIronChest extends ItemBlock { @Override public int getMetadata(int i) { - if (iZ@Rd*Qx@aMu{Feoo8 z6F0{PXcZO)(M~f4$sEpP%MwktjE_2uOZbLwF-AVkFJ{qu`E z#Xr>d)zR(As*9-pf6%ey55=UkRoes_cQq#M=h{nKwM`)F_;)t#PL6b3EN#^`fsc!S zqT@8hd-mMf(pGH|XN~9oTW+f2X?W)G$mJrE$i+zMBysrd1F`9wlgEBs{6Bnnb<7?; z9v=Pq^M4(WQ(s(@OH=%tH*bzyZse^=GV@pxu%18Ck8$-1v> zh$I)0OOnyICXqxgauLzC@8G0GRDbALvUTl9@ehyldef1~n?U2P#$@BhjgiacN`-f; zmX?-Dn?Tm_fA4JCJ!SsMn!wh!u5(T6YFDIb4igpcrpGo-n7{e`=1GeM67=7^QBgCyyc&%FWz+Ictk`z{m{)-z}}CtQU5|MAYscbJ5;2@a%pQ#zJM+Kyqc2y+pZs0)5 zC`6JZr7!W=ng6w2uU5pj4n$nPc0C@ycgK|DA2!`H@;Qz zVs}?(486BK2Koz;+*(27cS_=i2i_fAQOn{!|~t;-!yNj=yi@_tszN|HR|* z2oQzq*KdA#54;^q);=1uBchluj66Y4@%Q6dr*7`Q+4`M$V$~}VcSRHmg;ay-@3~Ui zKTud1>xV8x+?PJSr{`*E|H7#=QS+%~5xMl^A2rzxrjJ>wmnuw#9+QK@>ldpbTn7a@*S54 zBkoUEKxa;$DnGGsWn8|}ow@;hboz9~xhq$$MAW7m|AhHdj{nU|m9y;0{+kWqrBgle zQvKoyk?n=*b?U0vAD?853pHo$Q&7zvJ zu4`D>9jay%La88y15N1gWZ(&a%~0$@7zDem{;A+C>srH@3MvN z=*D296vnWluqwD2yfAJobH=JQ5^8iw)FdY7hqjkl%bPe_ftVE}eMVb~4ruSP! zO=r8)_ljwHzfGX6y)FId#z*P>Hi3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