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

129 lines
4.6 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
2016-05-19 01:47:25 +02:00
* <p>
* Contributors:
2016-05-19 01:47:25 +02:00
* 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
{
2016-05-19 01:47:25 +02:00
public final ChestChangerType type;
public ItemChestChanger(ChestChangerType type)
{
this.type = type;
this.setMaxStackSize(1);
this.setUnlocalizedName("ironchest:" + type.name());
this.setCreativeTab(CreativeTabs.MISC);
}
2014-11-26 12:55:24 +01:00
@Override
//@formatter:off
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)
//@formatter:on
{
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.source.getName().toUpperCase()).ordinal()))
2016-03-21 17:44:27 +01:00
{
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-05-19 01:47:25 +02:00
EnumFacing chestFacing = EnumFacing.DOWN;
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();
newchest = this.type.target.makeEntity();
2015-01-29 17:58:38 +01:00
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);
2016-05-19 01:47:25 +02:00
chestFacing = chestState.getValue(BlockChest.FACING);
TileEntityChest chest = (TileEntityChest) te;
if (chest.numPlayersUsing > 0)
2016-03-21 17:44:27 +01:00
{
return EnumActionResult.PASS;
}
2016-05-19 01:47:25 +02:00
if (!this.type.canUpgrade(IronChestType.WOOD))
2016-03-21 17:44:27 +01:00
{
return EnumActionResult.PASS;
}
2016-05-19 01:47:25 +02:00
chestContents = new ItemStack[chest.getSizeInventory()];
2015-01-29 17:58:38 +01:00
for (int i = 0; i < chestContents.length; i++)
2016-03-21 17:44:27 +01:00
{
2016-05-19 01:47:25 +02:00
chestContents[i] = chest.getStackInSlot(i);
2016-03-21 17:44:27 +01:00
}
newchest = this.type.target.makeEntity();
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-05-19 01:47:25 +02:00
((TileEntityIronChest) te2).setFacing(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
}
}