2014-09-24 14:32:58 +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
|
2016-05-19 01:47:25 +02:00
|
|
|
* <p>
|
2014-09-24 14:32:58 +02:00
|
|
|
* Contributors:
|
2016-05-19 01:47:25 +02:00
|
|
|
* cpw - initial API and implementation
|
2014-09-24 14:32:58 +02:00
|
|
|
******************************************************************************/
|
|
|
|
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;
|
2014-09-24 14:32:58 +02: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;
|
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;
|
2014-09-24 14:32:58 +02:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2015-11-23 07:07:24 +01:00
|
|
|
public class ItemChestChanger extends Item
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
public final ChestChangerType type;
|
2014-09-24 14:32:58 +02:00
|
|
|
|
|
|
|
public ItemChestChanger(ChestChangerType type)
|
|
|
|
{
|
|
|
|
this.type = type;
|
2014-09-25 12:08:22 +02:00
|
|
|
this.setMaxStackSize(1);
|
|
|
|
this.setUnlocalizedName("ironchest:" + type.name());
|
2016-04-21 20:29:22 +02:00
|
|
|
this.setCreativeTab(CreativeTabs.MISC);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
2014-11-26 12:55:24 +01:00
|
|
|
@Override
|
2016-04-21 20:29:22 +02:00
|
|
|
//@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)
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:on
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
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;
|
2015-11-23 07:07:24 +01:00
|
|
|
}
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
//@formatter:off
|
|
|
|
if (worldIn.getBlockState(pos) != IronChest.ironChestBlock.getStateFromMeta(IronChestType.valueOf(this.type.source.getName().toUpperCase()).ordinal()))
|
|
|
|
//@formatter:on
|
2016-03-21 17:44:27 +01:00
|
|
|
{
|
|
|
|
return EnumActionResult.PASS;
|
2015-11-23 07:07:24 +01:00
|
|
|
}
|
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();
|
2016-05-19 12:42:14 +02:00
|
|
|
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
|
|
|
}
|
2016-05-19 12:42:14 +02:00
|
|
|
newchest = this.type.target.makeEntity();
|
2015-01-29 17:58:38 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-29 00:19: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-05-20 00:01:50 +02:00
|
|
|
IBlockState iblockstate = IronChest.ironChestBlock.getDefaultState().withProperty(BlockIronChest.VARIANT_PROP, this.type.target);
|
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
|
|
|
}
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|