Example usage for net.minecraftforge.event.entity.player PlayerInteractEvent.RightClickBlock getItemStack

List of usage examples for net.minecraftforge.event.entity.player PlayerInteractEvent.RightClickBlock getItemStack

Introduction

In this page you can find the example usage for net.minecraftforge.event.entity.player PlayerInteractEvent.RightClickBlock getItemStack.

Prototype

@Nonnull
public ItemStack getItemStack() 

Source Link

Usage

From source file:de.ellpeck.actuallyadditions.mod.blocks.BlockLaserRelay.java

@SubscribeEvent
public void onBlockRightClick(PlayerInteractEvent.RightClickBlock event) {
    EntityPlayer player = event.getEntityPlayer();
    World world = event.getWorld();/*from  w ww .j  a v a 2  s  .c om*/
    ItemStack stack = event.getItemStack();
    BlockPos pos = event.getPos();

    if (player != null && world != null && StackUtil.isValid(stack) && pos != null) {
        IBlockState state = event.getWorld().getBlockState(pos);
        if (state != null && state.getBlock() instanceof BlockLaserRelay) {
            if (stack.getItem() instanceof ItemCompass && player.isSneaking()) {
                event.setUseBlock(Event.Result.ALLOW);
            }
        }
    }
}

From source file:de.kitsunealex.projectx.event.BlockEventHandler.java

License:Open Source License

@SubscribeEvent
public void handleRightClickBlockEvent(PlayerInteractEvent.RightClickBlock event) {
    EntityPlayer player = event.getEntityPlayer();
    World world = event.getWorld();/*from w ww  . j ava2 s  . c om*/
    ItemStack stack = event.getItemStack();
    BlockPos pos = event.getPos();

    if (player != null && world != null && !stack.isEmpty()) {
        IBlockState state = world.getBlockState(pos);

        for (Block block : BYPASSED_BLOCKS) {
            if (state.getBlock() == block) {
                event.setUseBlock(Event.Result.ALLOW);
            }
        }
    }
}

From source file:de.sanandrew.mods.sanlib.sanplayermodel.event.ItemClickEvent.java

License:Creative Commons License

@SubscribeEvent
public void onItemUse(PlayerInteractEvent.RightClickBlock event) {
    if (event.getItemStack().getItem() instanceof ItemArmorStand && event.getEntity() instanceof EntityPlayer) {
        EntityPlayer player = (EntityPlayer) event.getEntity();
        if (SanPlayerModel.isSanPlayer(player) && !player.isSneaking()) {
            EnumFacing facing = event.getFace();
            if (facing != null && facing != EnumFacing.DOWN) {
                World world = event.getEntity().world;
                boolean replaceable = world.getBlockState(event.getPos()).getBlock().isReplaceable(world,
                        event.getPos());
                BlockPos pos1 = replaceable ? event.getPos() : event.getPos().offset(facing);
                ItemStack heldItem = player.getHeldItem(event.getHand());

                if (player.canPlayerEdit(pos1, facing, heldItem)) {
                    BlockPos pos2 = pos1.up();
                    boolean isOccupied = !world.isAirBlock(pos1)
                            && !world.getBlockState(pos1).getBlock().isReplaceable(world, pos1);
                    isOccupied = isOccupied | (!world.isAirBlock(pos2)
                            && !world.getBlockState(pos2).getBlock().isReplaceable(world, pos2));

                    if (!isOccupied) {
                        double posX = pos1.getX();
                        double posY = pos1.getY();
                        double posZ = pos1.getZ();
                        List<Entity> entities = world.getEntitiesWithinAABBExcludingEntity(null,
                                new AxisAlignedBB(posX, posY, posZ, posX + 1.0D, posY + 2.0D, posZ + 1.0D));

                        if (entities.isEmpty()) {
                            if (!world.isRemote) {
                                world.setBlockToAir(pos1);
                                world.setBlockToAir(pos2);
                                EntitySanArmorStand stand = new EntitySanArmorStand(world, posX + 0.5D, posY,
                                        posZ + 0.5D);
                                float rotation = (float) MathHelper.floor(
                                        (MathHelper.wrapDegrees(player.rotationYaw - 180.0F) + 22.5F) / 45.0F)
                                        * 45.0F;
                                stand.setLocationAndAngles(posX + 0.5D, posY, posZ + 0.5D, rotation, 0.0F);
                                doRandomRotations(stand);
                                ItemMonsterPlacer.applyItemEntityDataToEntity(world, player, heldItem, stand);
                                world.spawnEntity(stand);
                                world.playSound(null, stand.posX, stand.posY, stand.posZ,
                                        SoundEvents.ENTITY_ARMORSTAND_PLACE, SoundCategory.BLOCKS, 0.75F, 0.8F);
                            }// w w  w  . j a  va  2 s  .c  o  m

                            heldItem.shrink(1);

                            event.setCanceled(true);
                        }
                    }
                }
            }
        }
    }
}

From source file:hellfirepvp.astralsorcery.common.event.listener.EventHandlerServer.java

License:Open Source License

@SubscribeEvent
public void onRightClick(PlayerInteractEvent.RightClickBlock event) {
    ItemStack hand = event.getItemStack();
    if (event.getHand() == EnumHand.OFF_HAND) {
        hand = event.getEntityPlayer().getHeldItem(EnumHand.MAIN_HAND);
    }/*from ww w  .  jav a  2  s .  c  o  m*/
    if (hand.isEmpty())
        return;
    if (hand.getItem() instanceof ISpecialInteractItem) {
        ISpecialInteractItem i = (ISpecialInteractItem) hand.getItem();
        if (i.needsSpecialHandling(event.getWorld(), event.getPos(), event.getEntityPlayer(), hand)) {
            i.onRightClick(event.getWorld(), event.getPos(), event.getEntityPlayer(), event.getFace(),
                    event.getHand(), hand);
            event.setCanceled(true);
        }
    }
}

From source file:net.doubledoordev.inventorylock.server.ServerEventHandler.java

License:Open Source License

@SubscribeEvent
public void onPlayerInteractRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
    EntityPlayer player = event.getEntityPlayer();
    if (player == null || player instanceof FakePlayer || player.world.isRemote)
        return;// www. j av a2s.  co  m
    ItemStack stack = event.getItemStack();
    if (stack.isEmpty())
        return;
    NBTTagCompound nbt = stack.getSubCompound(MOD_ID);
    if (nbt == null)
        return;

    event.setCanceled(true);
    Action action = Action.values()[nbt.getByte(ACTION)];

    TileEntity te = event.getWorld().getTileEntity(event.getPos());
    if (te == null)
        return;
    if (!(te instanceof ILockableContainer)) {
        Helper.chat(player, "This block is not lockable :(", RED);
        return;
    }
    ILockableContainer lc = ((ILockableContainer) te);
    Block block = event.getWorld().getBlockState(event.getPos()).getBlock();
    if (block instanceof BlockChest) {
        lc = ((BlockChest) block).getLockableContainer(event.getWorld(), event.getPos());
        if (lc == null)
            lc = (ILockableContainer) te;
    }

    LockCode existingLc = lc.getLockCode();

    if (action == Action.LOCK) // We want to lock
    {
        //noinspection ConstantConditions
        if (existingLc == null || existingLc.isEmpty()) // There is no lock yet, OK
        {
            lc.setLockCode(new BetterLockCode().add(player.getUniqueID()));
            Helper.chat(player, "Locked!", GREEN);
        } else
            Helper.chat(player, "This block is already locked.", RED);

        return; // End of any LOCK case
    }
    // Beyond here the block needs to already be locked, via OUR lock, and the player needs to have access.
    //noinspection ConstantConditions
    if (existingLc == null || existingLc.isEmpty()) {
        Helper.chat(player, "This block is not locked.", RED);
        return;
    }
    if (!(existingLc instanceof BetterLockCode)) {
        Helper.chat(player, "This block is not locked via " + MOD_ID + ".", RED);
        return;
    }
    BetterLockCode blc = ((BetterLockCode) existingLc);
    if (action == Action.INSPECT) {
        printList(player, blc); // Bypass contains check
        return;
    }
    if (!blc.canEdit(player)) {
        Helper.chat(player, "You do not have access to this block.", RED);
        return;
    }
    // LOCK is already handled.
    if (action == Action.UNLOCK) // We want to unlock (set back to the EMPTY_CODE singleton)
    {
        lc.setLockCode(LockCode.EMPTY_CODE);
        Helper.chat(player, "Unlocked!", GREEN);
        return;
    } else if (action == Action.PUBLIC) // We want to make public (set back to the EMPTY_CODE singleton)
    {
        blc.setPublic(!blc.isPublic());
        Helper.chat(player, "Chest now " + (blc.isPublic() ? "public!" : "private!"), GREEN);
        return;
    } else if (action == Action.ADD) // We want to add uuids
    {
        NBTTagList list = nbt.getTagList(UUIDS, TAG_STRING);
        for (int i = 0; i < list.tagCount(); i++)
            blc.add(UUID.fromString(list.getStringTagAt(i)));
    } else if (action == Action.REMOVE) // We want to remove uuids
    {
        NBTTagList list = nbt.getTagList(UUIDS, TAG_STRING);
        for (int i = 0; i < list.tagCount(); i++) {
            UUID uuid = UUID.fromString(list.getStringTagAt(i));
            if (!player.getUniqueID().equals(uuid))
                blc.remove(uuid); // Anti self lockout
        }
    }
    // Print this for ADD, REMOVE (LOCK, INSPECT & UNLOCK return early)
    printList(player, blc);
}

From source file:vazkii.quark.decoration.feature.FlatItemFrames.java

License:Creative Commons License

@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) {
    if (event.getFace() == null)
        return;//from  www  . j  a  va 2 s  . c  om

    ItemStack itemstack = event.getItemStack();
    EnumFacing facing = event.getFace();
    BlockPos blockpos = event.getPos().offset(facing);
    World world = event.getWorld();
    EntityPlayer player = event.getEntityPlayer();

    if (!player.canPlayerEdit(blockpos, facing, itemstack) || facing.getAxis() != EnumFacing.Axis.Y
            || itemstack.getItem() != Items.ITEM_FRAME)
        return;

    EntityHanging entityhanging = new EntityFlatItemFrame(world, blockpos, facing);

    if (entityhanging != null && entityhanging.onValidSurface()) {
        if (!event.getWorld().isRemote) {
            entityhanging.playPlaceSound();
            world.spawnEntity(entityhanging);
            event.setCanceled(true);

            if (!player.capabilities.isCreativeMode)
                itemstack.shrink(1);
        } else
            player.swingArm(event.getHand());
    }
}