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

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

Introduction

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

Prototype

public Entity getEntity() 

Source Link

Usage

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  .  ja  v a 2 s. c o  m*/

                            heldItem.shrink(1);

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

From source file:vazkii.quark.tweaks.feature.RightClickSignEdit.java

License:Creative Commons License

@SubscribeEvent
public void onInteract(PlayerInteractEvent.RightClickBlock event) {
    if (event.getUseBlock() == Result.DENY)
        return;/*from w  w w . j  a  v  a  2 s .  co  m*/

    TileEntity tile = event.getWorld().getTileEntity(event.getPos());
    if (tile instanceof TileEntitySign
            && (!emptyHand || event.getEntityPlayer().getHeldItemMainhand().isEmpty())
            && event.getEntityPlayer().capabilities.allowEdit && !event.getEntity().isSneaking()) {
        IBlockState state = event.getWorld().getBlockState(event.getPos());
        if (state.getBlock().getRegistryName().toString().contains("tcguideposts"))
            return;

        TileEntitySign sign = (TileEntitySign) tile;
        sign.setPlayer(event.getEntityPlayer());
        ReflectionHelper.setPrivateValue(TileEntitySign.class, sign, true, LibObfuscation.IS_EDITABLE);

        event.getEntityPlayer().openGui(Quark.instance, LibGuiIDs.SIGN, event.getWorld(), event.getPos().getX(),
                event.getPos().getY(), event.getPos().getZ());
    }
}