Search in sources :

Example 1 with BlockContactButton

use of lumien.randomthings.block.BlockContactButton in project Random-Things by lumien231.

the class RTEventHandler method playerInteract.

@SubscribeEvent
public void playerInteract(PlayerInteractEvent event) {
    if (event instanceof RightClickBlock) {
        ItemStack equipped = event.getEntityPlayer().getHeldItem(event.getHand());
        if (!equipped.isEmpty() && equipped.getItem() instanceof ItemSpade) {
            RightClickBlock rcEvent = (RightClickBlock) event;
            IBlockState targetState = event.getWorld().getBlockState(event.getPos());
            if (targetState.getBlock() == Blocks.SLIME_BLOCK) {
                event.getEntityPlayer().swingArm(event.getHand());
                if (!event.getWorld().isRemote) {
                    if (!event.getWorld().isRemote) {
                        event.getWorld().setBlockState(event.getPos(), ModBlocks.compressedSlimeBlock.getDefaultState());
                        event.getWorld().playSound(null, event.getPos().getX(), event.getPos().getY(), event.getPos().getZ(), Blocks.SLIME_BLOCK.getSoundType().getPlaceSound(), SoundCategory.PLAYERS, 1, 0.8f);
                        equipped.damageItem(1, event.getEntityPlayer());
                    }
                }
            } else if (targetState.getBlock() == ModBlocks.compressedSlimeBlock) {
                int currentCompression = targetState.getValue(BlockCompressedSlimeBlock.COMPRESSION);
                event.getEntityPlayer().swingArm(event.getHand());
                if (!event.getWorld().isRemote) {
                    if (currentCompression < 2) {
                        rcEvent.getWorld().setBlockState(rcEvent.getPos(), targetState.withProperty(BlockCompressedSlimeBlock.COMPRESSION, currentCompression + 1));
                        event.getWorld().playSound(null, event.getPos().getX(), event.getPos().getY(), event.getPos().getZ(), Blocks.SLIME_BLOCK.getSoundType().getPlaceSound(), SoundCategory.PLAYERS, 1, 0.8f - ((currentCompression + 1) * 0.2f));
                        equipped.damageItem(1, event.getEntityPlayer());
                    }
                }
            }
        }
        if (!event.getWorld().isRemote && !equipped.isEmpty() && equipped.getItem() == Items.PAPER) {
            RightClickBlock rcEvent = (RightClickBlock) event;
            IBlockState targetState = event.getWorld().getBlockState(event.getPos());
            if (targetState.getBlock() == ModBlocks.runeBase) {
                TileEntityRuneBase te = (TileEntityRuneBase) event.getWorld().getTileEntity(rcEvent.getPos());
                int[][] runeData = te.getRuneData();
                int[] savedData = new int[16];
                int counter = 0;
                for (int y = 0; y < runeData[0].length; y++) {
                    for (int x = 0; x < runeData.length; x++) {
                        savedData[counter] = runeData[x][y];
                        counter++;
                    }
                }
                ItemStack patternStack = new ItemStack(ModItems.runePattern);
                patternStack.setTagCompound(new NBTTagCompound());
                patternStack.getTagCompound().setIntArray("runeData", savedData);
                ((RightClickBlock) event).setUseItem(Result.ALLOW);
                if (equipped.getCount() == 1) {
                    int slot;
                    if (event.getHand() == EnumHand.MAIN_HAND) {
                        slot = event.getEntityPlayer().inventory.currentItem;
                    } else {
                        slot = 40;
                    }
                    event.getEntityPlayer().inventory.setInventorySlotContents(slot, patternStack);
                } else {
                    equipped.shrink(1);
                    event.getEntityPlayer().inventory.addItemStackToInventory(patternStack);
                }
            }
        }
        if (!event.getWorld().isRemote && event.getHand() == EnumHand.MAIN_HAND) {
            for (EnumFacing facing : EnumFacing.values()) {
                BlockPos pos = event.getPos().offset(facing);
                IBlockState state = event.getWorld().getBlockState(pos);
                if (state.getBlock() == ModBlocks.contactButton) {
                    if (state.getValue(BlockContactButton.FACING).getOpposite() == facing) {
                        ((BlockContactButton) state.getBlock()).activate(event.getWorld(), event.getPos().offset(facing), facing.getOpposite());
                        break;
                    }
                } else if (state.getBlock() == ModBlocks.contactLever) {
                    if (state.getValue(BlockContactLever.FACING).getOpposite() == facing) {
                        ((BlockContactLever) state.getBlock()).activate(event.getWorld(), event.getPos().offset(facing), facing.getOpposite());
                        break;
                    }
                }
            }
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ItemSpade(net.minecraft.item.ItemSpade) EnumFacing(net.minecraft.util.EnumFacing) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BlockContactButton(lumien.randomthings.block.BlockContactButton) BlockPos(net.minecraft.util.math.BlockPos) RightClickBlock(net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock) ItemStack(net.minecraft.item.ItemStack) TileEntityRuneBase(lumien.randomthings.tileentity.TileEntityRuneBase) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

BlockContactButton (lumien.randomthings.block.BlockContactButton)1 TileEntityRuneBase (lumien.randomthings.tileentity.TileEntityRuneBase)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemSpade (net.minecraft.item.ItemSpade)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 EnumFacing (net.minecraft.util.EnumFacing)1 BlockPos (net.minecraft.util.math.BlockPos)1 RightClickBlock (net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1