2012-04-25 03:17:23 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2012 cpw.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* 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
|
2012-06-29 18:47:09 +02:00
|
|
|
*
|
2012-04-25 03:17:23 +02:00
|
|
|
* Contributors:
|
|
|
|
* cpw - initial API and implementation
|
|
|
|
******************************************************************************/
|
2012-02-02 05:24:31 +01:00
|
|
|
package cpw.mods.ironchest;
|
|
|
|
|
2013-03-04 17:18:53 +01:00
|
|
|
import net.minecraft.client.renderer.texture.IconRegister;
|
2012-12-13 14:02:41 +01:00
|
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.tileentity.TileEntityChest;
|
|
|
|
import net.minecraft.world.World;
|
2012-08-11 07:46:49 +02:00
|
|
|
import cpw.mods.fml.common.ObfuscationReflectionHelper;
|
2013-03-04 17:18:53 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2012-02-02 05:24:31 +01:00
|
|
|
|
2012-08-11 07:46:49 +02:00
|
|
|
public class ItemChestChanger extends Item {
|
2012-02-02 05:24:31 +01:00
|
|
|
|
2012-12-18 17:22:21 +01:00
|
|
|
private ChestChangerType type;
|
2012-02-02 05:24:31 +01:00
|
|
|
|
2012-12-18 17:22:21 +01:00
|
|
|
public ItemChestChanger(int id, ChestChangerType type)
|
|
|
|
{
|
|
|
|
super(id);
|
|
|
|
setMaxStackSize(1);
|
|
|
|
this.type = type;
|
2013-07-09 22:40:47 +02:00
|
|
|
setUnlocalizedName("ironchest:"+type.name());
|
2012-12-18 17:22:21 +01:00
|
|
|
setCreativeTab(CreativeTabs.tabMisc);
|
|
|
|
}
|
2012-02-02 05:24:31 +01:00
|
|
|
|
2013-03-22 20:30:32 +01:00
|
|
|
|
2013-03-04 17:18:53 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2013-04-11 14:43:34 +02:00
|
|
|
public void registerIcons(IconRegister par1IconRegister)
|
2013-03-04 17:18:53 +01:00
|
|
|
{
|
2013-04-11 14:43:34 +02:00
|
|
|
this.itemIcon = par1IconRegister.registerIcon("ironchest:"+type.itemName);
|
2013-03-04 17:18:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-18 17:22:21 +01:00
|
|
|
@Override
|
|
|
|
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int X, int Y, int Z, int side, float hitX, float hitY, float hitZ)
|
|
|
|
{
|
|
|
|
if (world.isRemote) return false;
|
|
|
|
TileEntity te = world.getBlockTileEntity(X, Y, Z);
|
|
|
|
TileEntityIronChest newchest;
|
|
|
|
if (te != null && te instanceof TileEntityIronChest)
|
|
|
|
{
|
|
|
|
TileEntityIronChest ironchest = (TileEntityIronChest) te;
|
|
|
|
newchest = ironchest.applyUpgradeItem(this);
|
|
|
|
if (newchest == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (te != null && te instanceof TileEntityChest)
|
|
|
|
{
|
|
|
|
TileEntityChest tec = (TileEntityChest) te;
|
|
|
|
if (tec.numUsingPlayers > 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!getType().canUpgrade(IronChestType.WOOD))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Force old TE out of the world so that adjacent chests can update
|
|
|
|
newchest = IronChestType.makeEntity(getTargetChestOrdinal(IronChestType.WOOD.ordinal()));
|
|
|
|
int newSize = newchest.chestContents.length;
|
|
|
|
ItemStack[] chestContents = ObfuscationReflectionHelper.getPrivateValue(TileEntityChest.class, tec, 0);
|
|
|
|
System.arraycopy(chestContents, 0, newchest.chestContents, 0, Math.min(newSize, chestContents.length));
|
|
|
|
BlockIronChest block = 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;
|
|
|
|
}
|
|
|
|
// Clear the old block out
|
2013-03-22 20:30:32 +01:00
|
|
|
world.setBlock(X, Y, Z, 0, 0, 3);
|
2012-12-18 17:22:21 +01:00
|
|
|
// Force the Chest TE to reset it's knowledge of neighbouring blocks
|
|
|
|
tec.updateContainingBlockInfo();
|
|
|
|
// Force the Chest TE to update any neighbours so they update next
|
|
|
|
// tick
|
|
|
|
tec.checkForAdjacentChests();
|
|
|
|
// And put in our block instead
|
2013-03-22 20:30:32 +01:00
|
|
|
world.setBlock(X, Y, Z, block.blockID, newchest.getType().ordinal(), 3);
|
2012-12-18 17:22:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
world.setBlockTileEntity(X, Y, Z, newchest);
|
2013-01-22 17:23:32 +01:00
|
|
|
world.setBlockMetadataWithNotify(X, Y, Z, newchest.getType().ordinal(), 3);
|
2012-12-18 17:22:21 +01:00
|
|
|
stack.stackSize = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-02 05:24:31 +01:00
|
|
|
|
2012-12-18 17:22:21 +01:00
|
|
|
public int getTargetChestOrdinal(int sourceOrdinal)
|
|
|
|
{
|
|
|
|
return type.getTarget();
|
|
|
|
}
|
2012-02-02 05:24:31 +01:00
|
|
|
|
2012-12-18 17:22:21 +01:00
|
|
|
public ChestChangerType getType()
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
2012-02-02 05:24:31 +01:00
|
|
|
}
|