Java Code Examples for net.minecraftforge.event.entity.player.PlayerInteractEvent#RightClickBlock

The following examples show how to use net.minecraftforge.event.entity.player.PlayerInteractEvent#RightClickBlock . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModGenuinePeoplePersonalities.java    From CommunityMod with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void onBlockActivated(PlayerInteractEvent.RightClickBlock event) {
    EventData data = new EventData(event);
    if (data.getPlayer() == null || data.getPlayer().world.isRemote) return;
    if (data.getHand() != EnumHand.MAIN_HAND) return;
    if (!testCooldown(data.getPlayer())) return;
    if (generateComplaint(StringType.ACTIVATE, data.getPlayer(), data.getBlock(), data)) {
        resetCooldown(data.getPlayer());
    }
}
 
Example 2
Source File: EventHandlers.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SubscribeEvent
public static void onPlayerInteraction(PlayerInteractEvent.RightClickBlock event) {
    ItemStack stack = event.getItemStack();
    if (!stack.isEmpty() && stack.getItem() == Items.FLINT_AND_STEEL) {
        if (!event.getWorld().isRemote
            && !event.getEntityPlayer().capabilities.isCreativeMode
            && event.getWorld().rand.nextInt(100) >= ConfigHolder.flintChanceToCreateFire) {
            stack.damageItem(1, event.getEntityPlayer());
            if (stack.getItemDamage() >= stack.getMaxDamage()) {
                stack.shrink(1);
            }
            event.setCanceled(true);
        }
    }
}
 
Example 3
Source File: EssentialsMissingHandler.java    From Cyberware with MIT License 5 votes vote down vote up
@SubscribeEvent
public void handleRightClickBlock(PlayerInteractEvent.RightClickBlock event)
{
	EntityLivingBase e = event.getEntityLiving();
	
	if (CyberwareAPI.hasCapability(e))
	{
		ICyberwareUserData cyberware = CyberwareAPI.getCapability(e);
		processEvent(event, event.getHand(), event.getEntityPlayer(), cyberware);
	}
}
 
Example 4
Source File: BuildBattleDecoratorImplementation.java    From malmo with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent event)
{
    // Disallow creating or destroying events in the player structure:
    if (event instanceof PlayerInteractEvent.LeftClickBlock)
    {
        // Destroy block
        if (blockInBounds(event.getPos(), this.sourceBounds))
            event.setCanceled(true);
    }
    else if (event instanceof PlayerInteractEvent.RightClickBlock)
    {
        // Place block - need to work out *where* the block would be placed.
        // This code was cribbed from ItemBlock.onItemUse()
        IBlockState iblockstate = event.getWorld().getBlockState(event.getPos());
        Block block = iblockstate.getBlock();
        EnumFacing side = event.getFace();
        BlockPos pos = event.getPos();
        if (block == Blocks.SNOW_LAYER && ((Integer)iblockstate.getValue(BlockSnow.LAYERS)).intValue() < 1)
        {
            side = EnumFacing.UP;
        }
        else if (!block.isReplaceable(event.getWorld(), pos))
        {
            pos = pos.offset(side);
        }
        if (blockInBounds(pos, this.sourceBounds))
            event.setCanceled(true);
    }
}
 
Example 5
Source File: PlanetEventHandler.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
@SubscribeEvent
public void blockPlaceEvent(RightClickBlock event) {
	EnumFacing direction = event.getFace();
	if(!event.getWorld().isRemote && direction != null  && event.getEntityPlayer() != null  && AtmosphereHandler.getOxygenHandler(event.getWorld().provider.getDimension()) != null &&
			!AtmosphereHandler.getOxygenHandler(event.getWorld().provider.getDimension()).getAtmosphereType(event.getPos().offset(direction)).allowsCombustion()) {

		if(event.getEntityPlayer().getHeldItem(event.getHand()) != null) {
			if(event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == Item.getItemFromBlock(Blocks.TORCH) && 
					event.getWorld().getBlockState(event.getPos()).isSideSolid(event.getWorld(), event.getPos(), direction)) {
				event.setCanceled(true);
				event.getWorld().setBlockState(event.getPos().offset(direction), AdvancedRocketryBlocks.blockUnlitTorch.getDefaultState().withProperty(BlockTorch.FACING, direction));
			}
			else if(zmaster587.advancedRocketry.api.Configuration.torchBlocks.contains(Block.getBlockFromItem(event.getEntityPlayer().getHeldItem(event.getHand()).getItem())) )
				event.setCanceled(true);
			else if(event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == Items.FLINT_AND_STEEL || event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == Items.FIRE_CHARGE|| event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == Items.BLAZE_POWDER || event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == Items.BLAZE_ROD || event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == Items.LAVA_BUCKET)
				event.setCanceled(true);
		}
	}

	if(!event.getWorld().isRemote && event.getItemStack() != null && event instanceof PlayerInteractEvent.RightClickBlock && event.getItemStack().getItem() == Items.BED && event.getWorld().provider instanceof WorldProviderPlanet) {
		AdvancedRocketryItems.itemAstroBed.onItemUse( event.getEntityPlayer(),  event.getEntityPlayer().world, event.getPos(), event.getHand(), event.getFace(), 0, 0, 0);
		event.setCanceled(true);
	}

	if(!event.getWorld().isRemote && event.getItemStack() != null && event.getItemStack().getItem() == Item.getItemFromBlock(AdvancedRocketryBlocks.blockGenericSeat) && event.getWorld().getBlockState(event.getPos()).getBlock() == Blocks.TNT) {
		ARAchivements.BEER.trigger((EntityPlayerMP) event.getEntityPlayer());
	}
}
 
Example 6
Source File: DrinkWaterHandler.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
@SubscribeEvent
public void onRightClick(PlayerInteractEvent.RightClickBlock event)
{
	if(event.getSide() == Side.CLIENT)
	{
		/*if(event.getHitVec() == null || event.getItemStack() != null)
		{
			return;
		}
		BlockPos blockpos = event.getPos().offset(event.getFace());
		IBlockState state = event.getWorld().getBlockState(blockpos);
		if(state.getBlock() == Blocks.WATER && Core.isFreshWater(event.getWorld(), blockpos))
		{
			Minecraft.getMinecraft().playerController.processRightClickBlock((EntityPlayerSP)event.getEntityPlayer(), 
					(WorldClient)event.getWorld(), event.getItemStack(), blockpos, event.getFace(), event.getHitVec(), event.getHand());
		}*/
	}
	else
	{
		BlockPos blockpos = event.getPos().offset(event.getFace());
		IBlockState state = event.getWorld().getBlockState(blockpos);
		if((state.getBlock() == Blocks.WATER || state.getBlock() == Blocks.FLOWING_WATER) && Core.isFreshWater(event.getWorld(), blockpos))
		{
			if(event.getEntityPlayer().getHeldItem(event.getHand()) == ItemStack.EMPTY)
			{
				IFoodStatsTFC food = (IFoodStatsTFC)event.getEntityPlayer().getFoodStats();
				food.setWaterLevel((Math.min(food.getWaterLevel()+0.1f, 20)));
				TFC.network.sendTo(new CFoodPacket(food), (EntityPlayerMP) event.getEntityPlayer());
			}
			else if(ItemPotteryJug.IsCeramicJug(event.getEntityPlayer().getHeldItem(event.getHand())))
			{
				event.getEntityPlayer().getHeldItem(event.getHand()).setItemDamage(2);
			}
		}
	}
}
 
Example 7
Source File: PlayerInteractEventHandler.java    From AgriCraft with MIT License 5 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void waterPadCreation(PlayerInteractEvent.RightClickBlock event) {
    // Fetch held item.
    final ItemStack stack = event.getItemStack();

    // Check if holding shovel.
    if (!StackHelper.isValid(stack, ItemSpade.class)) {
        return;
    }

    // Fetch world information.
    final BlockPos pos = event.getPos();
    final World world = event.getWorld();
    final IBlockState state = world.getBlockState(pos);

    // Fetch the block at the location.
    final Block block = state.getBlock();

    // Test that clicked block was farmland.
    if (block != Blocks.FARMLAND) {
        return;
    }

    // Deny the event.
    event.setUseBlock(Event.Result.DENY);
    event.setUseItem(Event.Result.DENY);
    event.setResult(Event.Result.DENY);

    // If we are on the client side we are done.
    if (event.getSide().isClient()) {
        return;
    }

    // Fetch the player.
    final EntityPlayer player = event.getEntityPlayer();

    // Create the new block on the server side.
    world.setBlockState(pos, AgriBlocks.getInstance().WATER_PAD.getDefaultState(), 3);

    // Damage player's tool if not in creative.
    if (!player.capabilities.isCreativeMode) {
        stack.damageItem(1, player);
    }
}
 
Example 8
Source File: PlayerInteractEventHandler.java    From AgriCraft with MIT License 5 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void applyVinesToGrate(PlayerInteractEvent.RightClickBlock event) {
    // Fetch the ItemStack
    final ItemStack stack = event.getItemStack();

    // If the stack is null, or otherwise invalid, who cares?
    if (!StackHelper.isValid(stack)) {
        return;
    }

    // If the player is not holding a stack of vines, who cares?
    if (stack.getItem() != Item.getItemFromBlock(Blocks.VINE)) {
        return;
    }

    // Fetch world information.
    final BlockPos pos = event.getPos();
    final World world = event.getWorld();
    final IBlockState state = world.getBlockState(pos);

    // Fetch the block at the location.
    final Block block = state.getBlock();

    // If the player isn't clicking a grate, who cares?
    if (!(block instanceof BlockGrate)) {
        return;
    }

    // The player is trying to place vines! Scandalous!
    // We better deny the event!
    event.setUseItem(Event.Result.DENY);
}
 
Example 9
Source File: PlayerInteractEventHandler.java    From AgriCraft with MIT License 5 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void denyBonemeal(PlayerInteractEvent.RightClickBlock event) {
    if (!event.getEntityPlayer().isSneaking()) {
        return;
    }
    ItemStack heldItem = event.getEntityPlayer().getActiveItemStack();
    if (!heldItem.isEmpty() && heldItem.getItem() == Items.DYE && heldItem.getItemDamage() == 15) {
        TileEntity te = event.getWorld().getTileEntity(event.getPos());
        if (te != null && (te instanceof TileEntityCrop)) {
            event.setUseItem(Event.Result.DENY);
        }
    }
}
 
Example 10
Source File: ProxyCommon.java    From WearableBackpacks with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onPlayerInteractBlock(PlayerInteractEvent.RightClickBlock event) {

	// This event is fired twice, once for each hand. Unfortunately there
	// is no way to set the result of the main hand interaction to SUCCESS
	// so the off hand one will be skipped. So: Hacky code!

	if (cancelOffHand) {
		cancelOffHand = false;
		if (event.getHand() == EnumHand.OFF_HAND)
			{ event.setCanceled(true); return; }
	}

	// When players sneak-right-click the ground with an
	// empty hand, place down their equipped backpack.

	EntityPlayer player = event.getEntityPlayer();
	World world = event.getWorld();
	if (!player.isSneaking() || (event.getHand() != EnumHand.MAIN_HAND) ||
	    !player.getHeldItem(EnumHand.MAIN_HAND).isEmpty()) return;

	IBackpack backpack = BackpackHelper.getBackpack(player);
	if (backpack == null) return;

	// Try place the equipped backpack on the ground by using it. Also takes
	// care of setting the tile entity stack and data as well as unequipping.
	// See ItemBackpack.onItemUse.
	player.inventory.mainInventory.set(player.inventory.currentItem, backpack.getStack());
	if (backpack.getStack().onItemUse(
			player, world, event.getPos(), EnumHand.MAIN_HAND,
			event.getFace(), 0.5F, 0.5F, 0.5F) == EnumActionResult.SUCCESS) {

		player.swingArm(EnumHand.MAIN_HAND);
		event.setCanceled(true);
		cancelOffHand = true;

	} else player.inventory.mainInventory.set(player.inventory.currentItem, ItemStack.EMPTY);

}
 
Example 11
Source File: DyeWashingHandler.java    From WearableBackpacks with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onPlayerInteractBlock(PlayerInteractEvent.RightClickBlock event) {
	
	if (event.getWorld().isRemote ||
	    event.getEntityPlayer().isSneaking()) return;
	
	// Check if item is washable and currently dyed.
	ItemStack stack = event.getItemStack();
	if (!(stack.getItem() instanceof IDyeableItem) ||
	    !((IDyeableItem)stack.getItem()).canWash(stack) ||
	    !NbtUtils.has(stack, "display", "color")) return;
	
	// Check if block is a cauldron.
	IBlockState state = event.getWorld().getBlockState(event.getPos());
	if (!(state.getBlock() instanceof BlockCauldron)) return;
	BlockCauldron block = (BlockCauldron)state.getBlock();
	
	// Check if water is in the cauldron.
	int level = state.getValue(BlockCauldron.LEVEL);
	if (level <= 0) return;
	
	// Remove the color from the item!
	NbtUtils.remove(stack, "display", "color");
	// Use up some water from the cauldron!
	block.setWaterLevel(event.getWorld(), event.getPos(), state, level - 1);
	// Increase "armor cleaned" statistic! Wheee!
	event.getEntityPlayer().addStat(StatList.ARMOR_CLEANED);
	
	// Cancel the event, as the item / cauldron was used.
	event.setCanceled(true);
	
}
 
Example 12
Source File: ServerEventHandler.java    From HoloInventory with MIT License 5 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onRightClick(PlayerInteractEvent.RightClickBlock event)
{
    if (catchNext == Type.NONE) return;
    boolean ban = catchNext == Type.BAN;
    catchNext = Type.NONE;
    event.setCanceled(true);

    TileEntity te = event.getWorld().getTileEntity(event.getPos());

    if (te == null)
    {
        event.getEntityPlayer().sendMessage(new TextComponentString("That block does not have a TileEntity.").setStyle(new Style().setColor(TextFormatting.RED)));
        return;
    }

    if (ban)
    {
        if (Helper.banned.add(te.getClass().getCanonicalName()))
            event.getEntityPlayer().sendMessage(new TextComponentString("Banned " + te.getClass().getCanonicalName()).setStyle(new Style().setColor(TextFormatting.GREEN)));
        else
            event.getEntityPlayer().sendMessage(new TextComponentString(te.getClass().getCanonicalName() + " is already banned.").setStyle(new Style().setColor(TextFormatting.RED)));
    }
    else
    {
        boolean wasBanned = Helper.banned.remove(te.getClass().getCanonicalName());
        if (wasBanned)
            event.getEntityPlayer().sendMessage(new TextComponentString("Unbanned " + te.getClass().getCanonicalName()).setStyle(new Style().setColor(TextFormatting.GREEN)));
        else
            event.getEntityPlayer().sendMessage(new TextComponentString(te.getClass().getCanonicalName() + " is not banned. Perhaps it is banned on the " + (FMLCommonHandler.instance().getSide().isClient() ? "server" : "client") + "?").setStyle(new Style().setColor(TextFormatting.RED)));
    }

    HoloInventory.getInstance().saveBanned();
}
 
Example 13
Source File: MiningGadgets.java    From MiningGadgets with MIT License 4 votes vote down vote up
@SubscribeEvent
public void rightClickEvent(PlayerInteractEvent.RightClickBlock event) {
    ItemStack stack = MiningGadget.getGadget(event.getPlayer());
    if( stack.getItem() instanceof MiningGadget )
        event.setCanceled(true);
}
 
Example 14
Source File: PlayerInteractEventHandler.java    From AgriCraft with MIT License 4 votes vote down vote up
/**
 * Event handler to disable vanilla farming.
 * 
 * @param event
 */
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void vanillaSeedPlanting(PlayerInteractEvent.RightClickBlock event) {
    // If not disabled, don't bother.
    if (!AgriCraftConfig.disableVanillaFarming) {
        return;
    }

    // Fetch the event itemstack.
    final ItemStack stack = event.getItemStack();

    // If the stack is null, or otherwise invalid, who cares?
    if (!StackHelper.isValid(stack)) {
        return;
    }

    // If the item in the player's hand is not a seed, who cares?
    if (!AgriApi.getSeedRegistry().hasAdapter(stack)) {
        return;
    }

    // Fetch world information.
    final BlockPos pos = event.getPos();
    final World world = event.getWorld();
    final IBlockState state = world.getBlockState(pos);

    // Fetch the block at the location.
    final Block block = state.getBlock();

    // If clicking crop block, who cares?
    if (block instanceof IAgriCrop) {
        return;
    }

    // If the item is an instance of IPlantable we need to perfom an extra check.
    if (stack.getItem() instanceof IPlantable) {
        // If the clicked block cannot support the given plant, then who cares?
        if (!block.canSustainPlant(state, world, pos, EnumFacing.UP, (IPlantable) stack.getItem())) {
            return;
        }
    }

    // If clicking crop tile, who cares?
    if (WorldHelper.getTile(event.getWorld(), event.getPos(), IAgriCrop.class).isPresent()) {
        return;
    }

    // The player is attempting to plant a seed, which is simply unacceptable.
    // We must deny this event.
    event.setUseItem(Event.Result.DENY);

    // If we are on the client side we are done.
    if (event.getSide().isClient()) {
        return;
    }

    // Should the server notify the player that vanilla farming has been disabled?
    if (AgriCraftConfig.showDisabledVanillaFarmingWarning) {
        MessageUtil.messagePlayer(event.getEntityPlayer(), "`7Vanilla planting is disabled!`r");
    }
}