Java Code Examples for org.bukkit.event.player.PlayerInteractEvent#setUseInteractedBlock()

The following examples show how to use org.bukkit.event.player.PlayerInteractEvent#setUseInteractedBlock() . 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: RegionMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
private void handleUse(Event event, BlockState blockState, MatchPlayer player) {
  if (!player.canInteract()) return;

  PlayerBlockQuery query = new PlayerBlockQuery(event, player, blockState);

  for (RegionFilterApplication rfa : this.rfaContext.get(RFAScope.USE)) {
    if (rfa.region.contains(blockState)) {
      if (processQuery(rfa, query)) {
        if (query.getEvent() instanceof PlayerInteractEvent
            && ((PlayerInteractEvent) query.getEvent()).isCancelled()) {
          PlayerInteractEvent pie = (PlayerInteractEvent) query.getEvent();
          pie.setCancelled(false);
          pie.setUseItemInHand(Event.Result.ALLOW);
          pie.setUseInteractedBlock(Event.Result.DENY);

          if (rfa.message != null) {
            player.sendWarning(rfa.message);
          }
        }
        if (this.useRegionPriority || rfa.useRegionPriority) {
          break;
        }
      }
    }
  }
}
 
Example 2
Source File: EventRuleMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
private void handleUse(Event event, BlockState blockState, MatchPlayer player) {
    if(!player.canInteract()) return;

    PlayerBlockEventQuery query = new PlayerBlockEventQuery(player, event, blockState);

    for(EventRule rule : this.ruleContext.get(EventRuleScope.USE)) {
        if(rule.region().contains(blockState)) {
            if(processQuery(rule, query)) {
                if(query.getEvent() instanceof PlayerInteractEvent && ((PlayerInteractEvent) query.getEvent()).isCancelled()) {
                    PlayerInteractEvent pie = (PlayerInteractEvent) query.getEvent();
                    pie.setCancelled(false);
                    pie.setUseItemInHand(Event.Result.ALLOW);
                    pie.setUseInteractedBlock(Event.Result.DENY);

                    if(rule.message() != null) {
                        player.sendWarning(rule.message(), false);
                    }
                }
                if(this.useRegionPriority) {
                    break;
                }
            }
        }
    }
}
 
Example 3
Source File: EventFilterMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onInteract(final PlayerInteractEvent event) {
    if(cancelUnlessInteracting(event, event.getPlayer())) {
        // Allow the how-to book to be read
        if(event.getMaterial() == Material.WRITTEN_BOOK) {
            event.setUseItemInHand(Event.Result.ALLOW);
        } else {
            event.setUseItemInHand(Event.Result.DENY);
            event.setUseInteractedBlock(Event.Result.DENY);
        }

        MatchPlayer player = getMatch().getPlayer(event.getPlayer());
        if(player == null) return;

        if(!player.isSpawned()) {
            ClickType clickType = convertClick(event.getAction(), event.getPlayer());
            if(clickType == null) return;

            getMatch().callEvent(new ObserverInteractEvent(player, clickType, event.getClickedBlock(), null, event.getItem()));
        }

        // Right-clicking armor will put it on unless we do this
        event.getPlayer().updateInventory();
    }
}
 
Example 4
Source File: ButtonManager.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onInteract(PlayerInteractEvent event) {
    if(!event.hasItem()) return;

    final ClickType click;
    switch(event.getAction()) {
        case LEFT_CLICK_AIR:
        case LEFT_CLICK_BLOCK:
            click = ClickType.LEFT;
            break;

        case RIGHT_CLICK_AIR:
        case RIGHT_CLICK_BLOCK:
            click = ClickType.RIGHT;
            break;

        default:
            return;
    }

    if(onButtonClick(event.getItem(), event.getActor(), click, event)) {
        event.setUseItemInHand(Event.Result.DENY);
        event.setUseInteractedBlock(Event.Result.DENY);
    }
}
 
Example 5
Source File: ServerListener.java    From ZombieEscape with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This is specific to my test server to prevent Crop trample.
 */
@EventHandler
public void onTrample(PlayerInteractEvent e) {
    if (e.getClickedBlock() == null) {
        return;
    }

    if (e.getAction() == Action.PHYSICAL) {
        Block block = e.getClickedBlock();

        Material material = block.getType();

        if (material == Material.CROPS || material == Material.SOIL) {
            e.setUseInteractedBlock(PlayerInteractEvent.Result.DENY);
            e.setCancelled(true);

            block.setType(material);
            block.setData(block.getData());
        }
    }
}
 
Example 6
Source File: SpawnEvents.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onSpawnEggEvent(PlayerInteractEvent event) {
    Player player = event != null ? event.getPlayer() : null;
    if (player == null || event.isCancelled() || !plugin.getWorldManager().isSkyWorld(player.getWorld())) {
        return; // Bail out, we don't care
    }
    if (player.hasPermission("usb.mod.bypassprotection") || player.isOp()) {
        return;
    }
    ItemStack item = event.getItem();
    if (RIGHT_CLICKS.contains(event.getAction()) && item != null && isSpawnEgg(item)) {
        if (!plugin.playerIsOnIsland(player)) {
            event.setCancelled(true);
            plugin.notifyPlayer(player, tr("\u00a7eYou can only use spawn-eggs on your own island."));
            return;
        }
        SpawnEgg spawnEgg = (SpawnEgg) item.getData();
        checkLimits(event, spawnEgg.getSpawnedType(), player.getLocation());
        if (event.isCancelled()) {
            plugin.notifyPlayer(player, tr("\u00a7cYou have reached your spawn-limit for your island."));
            event.setUseItemInHand(Event.Result.DENY);
            event.setUseInteractedBlock(Event.Result.DENY);
        }
    }
}
 
Example 7
Source File: EnvironmentControlListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void interactEntity(PlayerInteractEvent event) {
    if(!event.getPlayer().isOp()) {
        event.setCancelled(true);
        event.setUseInteractedBlock(Event.Result.DENY);
        event.setUseItemInHand(Event.Result.DENY);
    }
}
 
Example 8
Source File: ClickEventTracker.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Processes a click event from a player.
 * @param player Player who caused it.
 * @param event The event.
 * @param hand Slot associated with the event.
 * @return If the event should be passed to scripts.
 */
public boolean checkEvent(Player player, Cancellable event, EquipmentSlot hand) {
	UUID uuid = player.getUniqueId();
	TrackedEvent first = firstEvents.get(uuid);
	if (first != null && first.event != event) { // We've checked an event before, and it is not this one
		if (!modifiedEvents.contains(first.event)) {
			// Do not modify cancellation status of event, Skript did not touch it
			// This avoids issues like #2389
			return false;
		}
		
		// Ignore this, but set its cancelled status based on one set to first event
		if (event instanceof PlayerInteractEvent) { // Handle use item/block separately
			// Failing to do so caused issue SkriptLang/Skript#2303
			PlayerInteractEvent firstClick = (PlayerInteractEvent) first.event;
			PlayerInteractEvent click = (PlayerInteractEvent) event;
			click.setUseInteractedBlock(firstClick.useInteractedBlock());
			click.setUseItemInHand(firstClick.useItemInHand());
		} else {
			event.setCancelled(first.event.isCancelled());
		}
		return false;
	} else { // Remember and run this
		firstEvents.put(uuid, new TrackedEvent(event, hand));
		return true;
	}
}
 
Example 9
Source File: SlimefunItemListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onRightClick(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        if (SlimefunUtils.isItemSimilar(e.getItem(), SlimefunItems.DEBUG_FISH, true)) {
            return;
        }

        PlayerRightClickEvent event = new PlayerRightClickEvent(e);
        Bukkit.getPluginManager().callEvent(event);

        boolean itemUsed = e.getHand() == EquipmentSlot.OFF_HAND;

        if (event.useItem() != Result.DENY) {
            rightClickItem(e, event, itemUsed);
        }

        if (!itemUsed && event.useBlock() != Result.DENY && !rightClickBlock(e, event)) {
            return;
        }

        if (e.useInteractedBlock() != Result.DENY) {
            e.setUseInteractedBlock(event.useBlock());
        }

        if (e.useItemInHand() != Result.DENY) {
            e.setUseItemInHand(event.useItem());
        }
    }
}
 
Example 10
Source File: UseRegion.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.isCancelled() || event.getClickedBlock() == null || !event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) return;
    if (region.contains(new BlockRegion(null, event.getClickedBlock().getLocation().toVector())) && filter.evaluate(event.getPlayer(), event.getClickedBlock(), event).equals(FilterState.DENY)) {
        event.setUseItemInHand(Event.Result.ALLOW);
        event.setUseInteractedBlock(Event.Result.DENY);
        ChatUtil.sendWarningMessage(event.getPlayer(), message);
    }
}
 
Example 11
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteractBlockSecondary(PlayerInteractEvent event) {
    final Block clickedBlock = event.getClickedBlock();
    if (clickedBlock == null) {
        return;
    }

    final String id = GDPermissionManager.getInstance().getPermissionIdentifier(clickedBlock);
    final GDBlockType gdBlock = BlockTypeRegistryModule.getInstance().getById(id).orElse(null);
    if (gdBlock == null || (!gdBlock.isInteractable() && event.getAction() != Action.PHYSICAL)) {
        return;
    }
    if (NMSUtil.getInstance().isBlockStairs(clickedBlock) && event.getAction() != Action.PHYSICAL) {
        return;
    }

    final Player player = event.getPlayer();
    GDCauseStackManager.getInstance().pushCause(player);
    final GDPlayerData playerData = this.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
    if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.PHYSICAL) {
        onPlayerInteractBlockPrimary(event, player);
        return;
    }

    final Location location = clickedBlock.getLocation();
    GDClaim claim = this.dataStore.getClaimAt(location);
    final Sign sign = SignUtil.getSign(location);
    // check sign
    if (SignUtil.isSellSign(sign)) {
        EconomyUtil.getInstance().buyClaimConsumerConfirmation(player, claim, sign);
        return;
    }
    if (SignUtil.isRentSign(claim, sign)) {
        EconomyUtil.getInstance().rentClaimConsumerConfirmation(player, claim, sign);
        return;
    }

    final BlockState state = clickedBlock.getState();
    final ItemStack itemInHand = event.getItem();
    final boolean hasInventory = NMSUtil.getInstance().isTileInventory(location) || clickedBlock.getType() == Material.ENDER_CHEST;
    if (hasInventory) {
        onInventoryOpen(event, state.getLocation(), state, player);
        return;
    }

    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUID())) {
        return;
    }
    if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.INTERACT_BLOCK_SECONDARY.getName(), event.getClickedBlock(), player.getWorld().getUID())) {
        return;
    }

    GDTimings.PLAYER_INTERACT_BLOCK_SECONDARY_EVENT.startTiming();
    final Object source = player;

    final TrustType trustType = NMSUtil.getInstance().isTileInventory(location) || clickedBlock.getType() == Material.ENDER_CHEST ? TrustTypes.CONTAINER : TrustTypes.ACCESSOR;
    if (GDFlags.INTERACT_BLOCK_SECONDARY && playerData != null) {
        Flag flag = Flags.INTERACT_BLOCK_SECONDARY;
        if (event.getAction() == Action.PHYSICAL) {
            flag = Flags.COLLIDE_BLOCK;
        }
        Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, flag, source, clickedBlock, player, trustType, true);
        if (result == Tristate.FALSE) {
            // if player is holding an item, check if it can be placed
            if (GDFlags.BLOCK_PLACE && itemInHand != null && itemInHand.getType().isBlock()) {
                if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.BLOCK_PLACE.getName(), itemInHand, player.getWorld().getUID())) {
                    GDTimings.PLAYER_INTERACT_BLOCK_SECONDARY_EVENT.stopTiming();
                    return;
                }
                if (GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.BLOCK_PLACE, source, itemInHand, player, TrustTypes.BUILDER, true) == Tristate.TRUE) {
                    GDTimings.PLAYER_INTERACT_BLOCK_SECONDARY_EVENT.stopTiming();
                    return;
                }
            }
            // Don't send a deny message if the player is in claim mode or is holding an investigation tool
            if (lastInteractItemCancelled != true) {
                if (!playerData.claimMode && (GriefDefenderPlugin.getInstance().investigationTool == null || !NMSUtil.getInstance().hasItemInOneHand(player, GriefDefenderPlugin.getInstance().investigationTool))) {
                    if (event.getAction() == Action.PHYSICAL) {
                        if (player.getWorld().getTime() % 100 == 0L) {
                            this.sendInteractBlockDenyMessage(itemInHand, clickedBlock, claim, player, playerData);
                        }
                    } else {
                        if (gdBlock != null && gdBlock.isInteractable()) {
                            this.sendInteractBlockDenyMessage(itemInHand, clickedBlock, claim, player, playerData);
                        }
                    }
                }
            }

            event.setUseInteractedBlock(Result.DENY);
            GDTimings.PLAYER_INTERACT_BLOCK_SECONDARY_EVENT.stopTiming();
            return;
        }
    }

    GDTimings.PLAYER_INTERACT_BLOCK_SECONDARY_EVENT.stopTiming();
}
 
Example 12
Source File: ObserverModule.java    From CardinalPGM with MIT License 4 votes vote down vote up
@EventHandler
public void onInteraction(PlayerInteractEvent event) {
    if (testObserver(event.getPlayer())) {
        event.setCancelled(true);
        if ((event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && (event.getPlayer().getInventory().getItemInMainHand() != null && event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WRITTEN_BOOK))){
            event.setUseInteractedBlock(Event.Result.DENY);
            event.setUseItemInHand(Event.Result.ALLOW);
        }
        if (event.getClickedBlock() != null && !event.getPlayer().isSneaking() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (event.getClickedBlock().getType().equals(Material.CHEST) || event.getClickedBlock().getType().equals(Material.TRAPPED_CHEST)) {
                Inventory chest = Bukkit.createInventory(null, ((Chest) event.getClickedBlock().getState()).getInventory().getSize());
                for (int i = 0; i < ((Chest) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    chest.setItem(i, ((Chest) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(chest);
            }
            if (event.getClickedBlock().getType().equals(Material.FURNACE) || event.getClickedBlock().getType().equals(Material.BURNING_FURNACE)) {
                Inventory furnace = Bukkit.createInventory(null, InventoryType.FURNACE);
                for (int i = 0; i < ((Furnace) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    furnace.setItem(i, ((Furnace) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(furnace);
            }
            if (event.getClickedBlock().getType().equals(Material.DISPENSER)) {
                Inventory dispenser = Bukkit.createInventory(null, InventoryType.DISPENSER);
                for (int i = 0; i < ((Dispenser) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    dispenser.setItem(i, ((Dispenser) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(dispenser);
            }
            if (event.getClickedBlock().getType().equals(Material.DROPPER)) {
                Inventory dropper = Bukkit.createInventory(null, InventoryType.DROPPER);
                for (int i = 0; i < ((Dropper) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    dropper.setItem(i, ((Dropper) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(dropper);
            }
            if (event.getClickedBlock().getType().equals(Material.HOPPER)) {
                Inventory hopper = Bukkit.createInventory(null, InventoryType.HOPPER);
                for (int i = 0; i < ((Hopper) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    hopper.setItem(i, ((Hopper) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(hopper);
            }
            if (event.getClickedBlock().getType().equals(Material.BREWING_STAND)) {
                Inventory brewingStand = Bukkit.createInventory(null, InventoryType.BREWING);
                for (int i = 0; i < ((BrewingStand) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    brewingStand.setItem(i, ((BrewingStand) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(brewingStand);
            }
            if (event.getClickedBlock().getType().equals(Material.BEACON)) {
                Inventory beacon = Bukkit.createInventory(null, InventoryType.BEACON);
                for (int i = 0; i < ((Beacon) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    beacon.setItem(i, ((Beacon) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(beacon);
            }
        }
    }
}