Search in sources :

Example 1 with MetaTileEntityClipboard

use of gregtech.common.metatileentities.MetaTileEntityClipboard in project GregTech by GregTechCEu.

the class ClipboardBehavior method onItemUse.

@Override
public ActionResult<ItemStack> onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (!world.isRemote && facing.getAxis() != EnumFacing.Axis.Y) {
        ItemStack heldItem = player.getHeldItem(hand).copy();
        // don't place multiple items at a time
        heldItem.setCount(1);
        // Make sure it's the right block
        IBlockState testState = world.getBlockState(pos);
        Block testBlock = testState.getBlock();
        if (!testBlock.isAir(world.getBlockState(pos), world, pos) && testState.isSideSolid(world, pos, facing)) {
            // Step away from the block so you don't replace it, and then give it our fun blockstate
            BlockPos shiftedPos = pos.offset(facing);
            Block shiftedBlock = world.getBlockState(shiftedPos).getBlock();
            if (shiftedBlock.isAir(world.getBlockState(shiftedPos), world, shiftedPos)) {
                IBlockState state = MACHINE.getDefaultState();
                world.setBlockState(shiftedPos, state);
                // Get new TE
                shiftedBlock.createTileEntity(world, state);
                // And manipulate it to our liking
                MetaTileEntityHolder holder = (MetaTileEntityHolder) world.getTileEntity(shiftedPos);
                if (holder != null) {
                    MetaTileEntityClipboard clipboard = (MetaTileEntityClipboard) holder.setMetaTileEntity(CLIPBOARD_TILE, heldItem);
                    if (clipboard != null) {
                        clipboard.setFrontFacing(facing.getOpposite());
                        clipboard.setClipboard(heldItem);
                        ItemStack returnedStack = player.getHeldItem(hand);
                        if (!player.isCreative()) {
                            returnedStack.setCount(player.getHeldItem(hand).getCount() - 1);
                        }
                        return ActionResult.newResult(EnumActionResult.SUCCESS, returnedStack);
                    }
                }
            }
        }
    }
    return ActionResult.newResult(EnumActionResult.PASS, player.getHeldItem(hand));
}
Also used : MetaTileEntityHolder(gregtech.api.metatileentity.MetaTileEntityHolder) IBlockState(net.minecraft.block.state.IBlockState) MetaTileEntityClipboard(gregtech.common.metatileentities.MetaTileEntityClipboard) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

MetaTileEntityHolder (gregtech.api.metatileentity.MetaTileEntityHolder)1 MetaTileEntityClipboard (gregtech.common.metatileentities.MetaTileEntityClipboard)1 Block (net.minecraft.block.Block)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 BlockPos (net.minecraft.util.math.BlockPos)1