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

The following examples show how to use org.bukkit.event.player.PlayerInteractEvent#setUseItemInHand() . 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: EventFilterMatchModule.java    From PGM 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);
    }

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

    ClickType clickType = convertClick(event.getAction(), event.getPlayer());
    if (clickType == null) return;

    match.callEvent(
        new ObserverInteractEvent(
            player, clickType, event.getClickedBlock(), null, event.getItem()));
  }
}
 
Example 2
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 3
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 4
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 5
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 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: PlayerListener.java    From Carbon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
   public void onPlayerInteractMobSpawner(PlayerInteractEvent evt) {
       if (plugin.getConfig().getBoolean("features.monsterEggMobSpawner", true)) {
           if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
               if (evt.getPlayer().getItemInHand().getType() == Material.MONSTER_EGG && evt.getClickedBlock().getType() == Material.MOB_SPAWNER) {
                   ItemStack egg = evt.getPlayer().getItemInHand();
                   CreatureSpawner cs = (CreatureSpawner) evt.getClickedBlock().getState();
                   cs.setSpawnedType(EntityType.fromId(egg.getData().getData()));
                   cs.update(true);
                   evt.setUseItemInHand(Event.Result.DENY);
                   evt.setCancelled(true);
               }
           }
       }
   }
 
Example 8
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 9
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 10
Source File: Compat19.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(PlayerInteractEvent event) {
    Player p = event.getPlayer();
    Block b = event.getClickedBlock();
    ItemStack itemInHand = event.getItem();

    Location l;

    if (b != null) {
        l = b.getLocation();
        RedProtect.get().logger.debug(LogLevel.DEFAULT, "PlayerListener - Is PlayerInteractEvent event. The block is " + b.getType().name());
    } else {
        l = p.getLocation();
    }

    if (RedProtect.get().tpWait.contains(p.getName())) {
        RedProtect.get().tpWait.remove(p.getName());
        RedProtect.get().lang.sendMessage(p, "cmdmanager.region.tpcancelled");
    }

    if (itemInHand != null && (event.getAction().name().equals("RIGHT_CLICK_BLOCK") || b == null)) {
        Material hand = itemInHand.getType();
        Region r = RedProtect.get().rm.getTopRegion(l);
        // Deny chorus teleport
        if (r != null && hand.equals(Material.CHORUS_FRUIT) && !r.canTeleport(p)) {
            RedProtect.get().lang.sendMessage(p, "playerlistener.region.cantuse");
            event.setCancelled(true);
            event.setUseItemInHand(Event.Result.DENY);
        }
        // Deny glide boost
        if (r == null && p.isGliding() && itemInHand.getType().name().contains("FIREWORK") && !p.hasPermission("redprotect.bypass.glide") &&
                !RedProtect.get().config.globalFlagsRoot().worlds.get(p.getWorld().getName()).player_glide.allow_boost) {
            event.setUseItemInHand(Event.Result.DENY);
            event.setCancelled(true);
            RedProtect.get().lang.sendMessage(p, "globallistener.elytra.cantboost");
        }
    }
}
 
Example 11
Source File: UnitMaterial.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onInteract(PlayerInteractEvent event) {
	event.setUseItemInHand(Result.DENY);
	//if (event.getClickedBlock() == null) {
		event.setCancelled(true);
		CivMessage.sendError(event.getPlayer(), "Cannot use this item.");
	//}
}
 
Example 12
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 13
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 14
Source File: Workarounds.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.HIGHEST)
public void onInteract(final PlayerInteractEvent e) {
	if (e.hasItem() && (e.getPlayer().getInventory().getItemInHand() == null || e.getPlayer().getInventory().getItemInHand().getType() == Material.AIR || e.getPlayer().getInventory().getItemInHand().getAmount() == 0))
		e.setUseItemInHand(Result.DENY);
}
 
Example 15
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);
            }
        }
    }
}