ironbarrels/src/main/java/cpw/mods/ironchest/ItemChestChanger.java

152 lines
5.3 KiB
Java
Raw Normal View History

/*******************************************************************************
* 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
*
* Contributors:
* cpw - initial API and implementation
******************************************************************************/
package cpw.mods.ironchest;
2015-07-31 00:03:00 +02:00
import net.minecraft.block.BlockChest;
2016-03-21 17:44:27 +01:00
import net.minecraft.block.state.IBlockState;
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;
2016-03-21 17:44:27 +01:00
import net.minecraft.util.EnumActionResult;
2014-11-26 12:55:24 +01:00
import net.minecraft.util.EnumFacing;
2016-03-21 17:44:27 +01:00
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class ItemChestChanger extends Item
{
private ChestChangerType type;
public ItemChestChanger(ChestChangerType type)
{
this.type = type;
this.setMaxStackSize(1);
this.setUnlocalizedName("ironchest:" + type.name());
this.setCreativeTab(CreativeTabs.tabMisc);
}
2014-11-26 12:55:24 +01:00
@Override
2016-03-21 17:44:27 +01:00
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
2016-03-21 17:44:27 +01:00
if (worldIn.isRemote)
{
return EnumActionResult.PASS;
}
if (this.type.canUpgrade(IronChestType.WOOD))
{
if (!(worldIn.getBlockState(pos).getBlock() instanceof BlockChest))
{
return EnumActionResult.PASS;
}
2016-03-21 17:44:27 +01:00
}
else
{
if (worldIn.getBlockState(pos) != IronChest.ironChestBlock
.getStateFromMeta(IronChestType.valueOf(this.type.getSource().getName().toUpperCase()).ordinal()))
{
return EnumActionResult.PASS;
}
2015-07-31 00:03:00 +02:00
}
2016-03-21 17:44:27 +01:00
TileEntity te = worldIn.getTileEntity(pos);
2015-01-29 17:58:38 +01:00
TileEntityIronChest newchest = new TileEntityIronChest();
ItemStack[] chestContents = new ItemStack[27];
2016-03-21 17:44:27 +01:00
int chestFacing = 0;
2015-01-29 17:58:38 +01:00
if (te != null)
{
if (te instanceof TileEntityIronChest)
{
chestContents = ((TileEntityIronChest) te).chestContents;
2016-03-21 17:44:27 +01:00
chestFacing = ((TileEntityIronChest) te).getFacing();
2015-01-29 17:58:38 +01:00
newchest = IronChestType.makeEntity(this.getTargetChestOrdinal(this.type.ordinal()));
if (newchest == null)
2016-03-21 17:44:27 +01:00
{
return EnumActionResult.PASS;
}
2015-01-29 17:58:38 +01:00
}
else if (te instanceof TileEntityChest)
{
2016-03-21 17:44:27 +01:00
IBlockState chestState = worldIn.getBlockState(pos);
EnumFacing orientation = chestState.getValue(BlockChest.FACING);
if (orientation == EnumFacing.NORTH)
{
chestFacing = 2;
}
if (orientation == EnumFacing.EAST)
{
chestFacing = 5;
}
if (orientation == EnumFacing.SOUTH)
{
chestFacing = 3;
}
if (orientation == EnumFacing.WEST)
{
chestFacing = 4;
}
2015-01-29 17:58:38 +01:00
if (((TileEntityChest) te).numPlayersUsing > 0)
2016-03-21 17:44:27 +01:00
{
return EnumActionResult.PASS;
}
if (!this.getType().canUpgrade(IronChestType.WOOD))
{
return EnumActionResult.PASS;
}
2015-01-29 17:58:38 +01:00
chestContents = new ItemStack[((TileEntityChest) te).getSizeInventory()];
for (int i = 0; i < chestContents.length; i++)
2016-03-21 17:44:27 +01:00
{
2015-01-29 17:58:38 +01:00
chestContents[i] = ((TileEntityChest) te).getStackInSlot(i);
2016-03-21 17:44:27 +01:00
}
2015-07-31 00:02:00 +02:00
newchest = IronChestType.makeEntity(this.getTargetChestOrdinal(this.type.ordinal()));
2015-01-29 17:58:38 +01:00
}
}
2015-01-29 17:58:38 +01:00
te.updateContainingBlockInfo();
if (te instanceof TileEntityChest)
2016-03-21 17:44:27 +01:00
{
2015-01-29 17:58:38 +01:00
((TileEntityChest) te).checkForAdjacentChests();
2016-03-21 17:44:27 +01:00
}
worldIn.removeTileEntity(pos);
worldIn.setBlockToAir(pos);
2015-01-29 17:58:38 +01:00
2016-03-21 17:44:27 +01:00
IBlockState iblockstate = IronChest.ironChestBlock.getStateFromMeta(newchest.getType().ordinal());
2015-01-29 17:58:38 +01:00
2016-03-21 17:44:27 +01:00
worldIn.setTileEntity(pos, newchest);
worldIn.setBlockState(pos, iblockstate, 3);
2015-01-29 17:58:38 +01:00
2016-03-21 17:44:27 +01:00
worldIn.notifyBlockUpdate(pos, iblockstate, iblockstate, 3);
2015-01-29 17:58:38 +01:00
2016-03-21 17:44:27 +01:00
TileEntity te2 = worldIn.getTileEntity(pos);
2015-01-29 17:58:38 +01:00
if (te2 instanceof TileEntityIronChest)
{
((TileEntityIronChest) te2).setContents(chestContents);
2016-03-21 17:44:27 +01:00
((TileEntityIronChest) te2).setFacing((byte) chestFacing);
2015-01-29 17:58:38 +01:00
}
2016-03-21 17:44:27 +01:00
stack.stackSize = playerIn.capabilities.isCreativeMode ? stack.stackSize : stack.stackSize - 1;
return EnumActionResult.SUCCESS;
2014-11-26 12:55:24 +01:00
}
public int getTargetChestOrdinal(int sourceOrdinal)
{
2016-03-21 17:44:27 +01:00
return this.type.getTarget();
}
public ChestChangerType getType()
{
2016-03-21 17:44:27 +01:00
return this.type;
}
}