@EventHandler(priority = EventPriority.MONITOR) public void onPlayerInteract(PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.useInteractedBlock() == Result.DENY) return; //For using a hoe for farming if (event.getItem() != null && event.getItem().getType() != null && (event.getMaterial() == Material.DIRT || event.getMaterial() == Material.GRASS) && ((event.getItem().getType() == Material.WOOD_HOE) || (event.getItem().getType() == Material.IRON_HOE) || (event.getItem().getType() == Material.GOLD_HOE) || (event.getItem().getType() == Material.DIAMOND_HOE))) { BlockUpdate.Update(event.getClickedBlock()); } }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void equipWeaponEvent(InventoryDragEvent event) { if (Result.DENY.equals(event.getResult())) return; HumanEntity human = event.getWhoClicked(); Inventory inv = event.getInventory(); ItemStack prior = event.getOldCursor(); StandardGun priorGun = findGun(prior); // Don't really care what sort of drag, if we have a gun in inv and this was a gun, deny. if (priorGun != null && inv != null && !InventoryType.PLAYER.equals(inv.getType()) && priorGun.isLimitToOne() && priorGun.hasGun(human)) { event.setResult(Result.DENY); // yes this will prevent drag style of weapon in chest invs. Oh well. } }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void equipWeaponEvent(InventoryDragEvent event) { if (!enabled) return; HumanEntity human = event.getWhoClicked(); boolean alreadyHasGun = hasGun(human); Inventory inv = event.getInventory(); ItemStack prior = event.getOldCursor(); // Don't really care what sort of drag, if we have a gun in inv and this was a gun, deny. if (alreadyHasGun && (inv != null && !InventoryType.PLAYER.equals(inv.getType())) && (prior != null && isGun(prior))) { event.setResult(Result.DENY); // yes this will prevent drag style of weapon in chest invs. Oh well. } }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) void onPlayerInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); Block block = event.getClickedBlock(); // check for sign shop if (event.getAction() == Action.RIGHT_CLICK_BLOCK && Utils.isSign(block.getType())) { Shopkeeper shopkeeper = plugin.getShopkeeperByBlock(block); if (shopkeeper != null) { // only trigger shopkeeper interaction for main-hand events: if (NMSManager.getProvider().isMainHandInteraction(event)) { Log.debug("Player " + player.getName() + " is interacting with sign shopkeeper at " + block.getWorld().getName() + "," + block.getX() + "," + block.getY() + "," + block.getZ()); if (event.useInteractedBlock() == Result.DENY) { Log.debug(" Cancelled by another plugin"); } else { shopkeeper.onPlayerInteraction(player); } } // always cancel interactions with shopkeepers, to prevent any default behavior: event.setCancelled(true); } } }
@EventHandler(priority = EventPriority.LOWEST) public void onPlayerInteractEvent(PlayerInteractEvent event) { if (WorldGuardExtraFlagsPlugin.isMythicMobsEnabled()) { Player player = event.getPlayer(); if (!WorldGuardUtils.hasBypass(player)) { Action action = event.getAction(); if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { if (event.hasItem()) { ItemStack item = event.getItem(); if (item.getType() == Material.MONSTER_EGG) { if (item.getItemMeta().hasLore()) { List<String> lore = item.getItemMeta().getLore(); if (lore.get(0).equals(ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + "A Mythical Egg that can")) { MythicMob mm = EggManager.getMythicMobFromEgg(lore.get(2)); if (mm != null) { ApplicableRegionSet regions = WorldGuardExtraFlagsPlugin.getWorldGuardPlugin().getRegionContainer().createQuery().getApplicableRegions(action == Action.RIGHT_CLICK_BLOCK ? event.getClickedBlock().getLocation() : player.getLocation()); if (regions.queryValue(WorldGuardUtils.wrapPlayer(player), FlagUtils.MYTHICMOB_EGGS) == State.DENY) { event.setCancelled(true); event.setUseItemInHand(Result.DENY); } } } } } } } } } }
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false) public void onBlockPlaceEvent(PlaceBlockEvent event) { Result originalResult = event.getResult(); Object cause = event.getCause().getRootCause(); if (cause instanceof Player) { Player player = (Player)cause; if (!WorldGuardUtils.hasBypass(player)) { for(Block block : event.getBlocks()) { ApplicableRegionSet regions = WorldGuardExtraFlagsPlugin.getWorldGuardPlugin().getRegionContainer().createQuery().getApplicableRegions(block.getLocation()); Set<Material> state = regions.queryValue(WorldGuardUtils.wrapPlayer(player), FlagUtils.ALLOW_BLOCK_PLACE); if (state != null && state.contains(block.getType())) { event.setResult(Result.ALLOW); } else { Set<Material> state2 = regions.queryValue(WorldGuardUtils.wrapPlayer(player), FlagUtils.DENY_BLOCK_PLACE); if (state2 != null && state2.contains(block.getType())) { event.setResult(Result.DENY); return; } else { event.setResult(originalResult); return; } } } } } }
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false) public void onBlockBreakEvent(BreakBlockEvent event) { Result originalResult = event.getResult(); Object cause = event.getCause().getRootCause(); if (cause instanceof Player) { Player player = (Player)cause; if (!WorldGuardUtils.hasBypass(player)) { for(Block block : event.getBlocks()) { ApplicableRegionSet regions = WorldGuardExtraFlagsPlugin.getWorldGuardPlugin().getRegionContainer().createQuery().getApplicableRegions(block.getLocation()); Set<Material> state = regions.queryValue(WorldGuardUtils.wrapPlayer(player), FlagUtils.ALLOW_BLOCK_BREAK); if (state != null && state.contains(block.getType())) { event.setResult(Result.ALLOW); } else { Set<Material> state2 = regions.queryValue(WorldGuardUtils.wrapPlayer(player), FlagUtils.DENY_BLOCK_BREAK); if (state2 != null && state2.contains(block.getType())) { event.setResult(Result.DENY); return; } else { event.setResult(originalResult); return; } } } } } }
@SuppressWarnings("deprecation") @EventHandler(priority = EventPriority.HIGHEST) public void onInteract(final PlayerInteractEvent e) { //TODO Check if it's needed to do one for off hand? if (Skript.isRunningMinecraft(1, 9)) { if (e.hasItem() && (e.getPlayer().getInventory().getItemInMainHand() == null || e.getPlayer().getInventory().getItemInMainHand().getType() == Material.AIR || e.getPlayer().getInventory().getItemInMainHand().getAmount() == 0)) e.setUseItemInHand(Result.DENY); } else { //Compatibility reasons 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); } }
@Override public void execute(final Event e) { if (e instanceof Cancellable) ((Cancellable) e).setCancelled(cancel); if (e instanceof PlayerInteractEvent) { ((PlayerInteractEvent) e).setUseItemInHand(cancel ? Result.DENY : Result.DEFAULT); ((PlayerInteractEvent) e).setUseInteractedBlock(cancel ? Result.DENY : Result.DEFAULT); } else if (e instanceof BlockCanBuildEvent) { ((BlockCanBuildEvent) e).setBuildable(!cancel); } else if (e instanceof PlayerDropItemEvent) { PlayerUtils.updateInventory(((PlayerDropItemEvent) e).getPlayer()); } }
@EventHandler public void onInventoryDrag(InventoryDragEvent e) { if (e == null) return; if (e.getInventory() == null) return; if (e.getInventory().getTitle() == null) return; if (e.getInventory().getTitle().equals(CC(Main.finalconfig.getString("atm_title")))) { e.setResult(Result.DENY); } }
@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."); //} }
@SuppressWarnings("deprecation") @Override public void onInvShiftClick(InventoryClickEvent event, Inventory fromInv, Inventory toInv, ItemStack stack) { if (fromInv.equals(toInv)) { event.setCancelled(true); event.setResult(Result.DENY); return; } if (toInv.getHolder() instanceof Player) { Player player = (Player)toInv.getHolder(); if(!validateUnitUse(player, stack)) { CivMessage.sendError(player, "You cannot use this unit because it does not belong to your civlization."); event.setCancelled(true); return; } //Prevent dropping in two unit materials. ConfigUnit unit = Unit.getPlayerUnit(player); if (unit != null) { //player already has a unit item, cancel this event. CivMessage.sendError(player, "You already are a "+unit.name+" cannot pickup another unit item."); event.setCancelled(true); event.setResult(Result.DENY); event.getView().close(); player.updateInventory(); return; } onItemToPlayer(player, stack); } else if (fromInv.getHolder() instanceof Player) { onItemFromPlayer((Player)fromInv.getHolder(), stack); } }
/** * Event handler for inventory clicks. * @param event The event. */ @EventHandler(priority = EventPriority.HIGH) protected void onInventoryClick(InventoryClickEvent event) { if((event.getView().getBottomInventory() != null && event.getView().getBottomInventory().getTitle().equals(name)) || (event.getView().getTopInventory() != null && event.getView().getTopInventory().getTitle().equals(name))){ // Stop dupes event.setCancelled(true); event.setResult(Result.DENY); } if (event.getInventory().getTitle().equals(name) && event.getWhoClicked() instanceof Player) { event.setCancelled(true); int slot = event.getRawSlot(); if (slot >= 0 && slot < getSize() && optionIcons[slot] != null && optionIcons[slot].length > 0) { OptionClickEvent e = new OptionClickEvent( (Player) event.getWhoClicked(), slot, event.getCurrentItem()); for(OptionClickEvent.Handler handlr :_eventHandlers){ handlr.onOptionClick(e); } if (e.willClose()) { final Player p = (Player) event.getWhoClicked(); Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() { public void run() { p.closeInventory(); } }, 1); } if (e.willDestroy()) { destroy(); } } } }
/** * Event handler for inventory clicks. * @param event The event. */ @EventHandler(priority = EventPriority.HIGH) protected void onInventoryClick(InventoryClickEvent event) { if((event.getView().getBottomInventory() != null && event.getView().getBottomInventory().getTitle().equals(name)) || (event.getView().getTopInventory() != null && event.getView().getTopInventory().getTitle().equals(name))){ // Stop dupes event.setCancelled(true); event.setResult(Result.DENY); } if (event.getInventory().getTitle().equals(name) && event.getWhoClicked() instanceof Player) { event.setCancelled(true); int slot = event.getRawSlot(); if (slot >= 0 && slot < getSize() && optionIcons[slot] != null) { OptionClickEvent e = new OptionClickEvent( (Player) event.getWhoClicked(), slot, event.getCurrentItem()); for(OptionClickEvent.Handler handlr :_eventHandlers){ handlr.onOptionClick(e); } if (e.willClose()) { final Player p = (Player) event.getWhoClicked(); Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() { public void run() { p.closeInventory(); } }, 1); } if (e.willDestroy()) { destroy(); } } } }
@EventHandler(priority = EventPriority.HIGHEST) public void onPlayerInteract(PlayerInteractEvent event){ if(event.getAction() == Action.RIGHT_CLICK_BLOCK){ Material type = event.getClickedBlock().getType(); if(doors.contains(type)){ if(event.getPlayer().hasPermission("lockette.admin.bypass")){ event.setUseInteractedBlock(Result.ALLOW); event.setUseItemInHand(Result.ALLOW); event.setCancelled(false); } else { if(checkForBrokenSign(event.getClickedBlock())){ event.setCancelled(true); event.getPlayer().sendMessage("There was a broken lock sign on this door. Try again."); } } } if(event.getClickedBlock().getState() instanceof InventoryHolder){ Block[] edges = getEdges(event.getClickedBlock(), false, false); for(Block b : edges){ if(b.getType() == Material.WALL_SIGN){ BlockFace face = event.getClickedBlock().getFace(b); if(face == getFacingBlockFace(b)){ Sign s = (Sign) b.getState(); String line1 = s.getLine(1); String line0 = s.getLine(0); if(line0.equalsIgnoreCase("[private]")){ Player p = Bukkit.getPlayer(line1); if(p != null){ p.sendMessage("Don't lock container blocks."); } b.breakNaturally(); } } } } } } }
@EventHandler public void onInventoryClick(InventoryClickEvent e) { if(controller.isPlaying()) { boolean move = e.getRawSlot() >= e.getInventory().getSize() && e.getCurrentItem().getType() == Material.EYE_OF_ENDER && e.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY; boolean inventory = e.getRawSlot() < e.getInventory().getSize() && e.getCursor().getType() == Material.EYE_OF_ENDER && e.getAction() != InventoryAction.PICKUP_ALL; if(move || inventory) e.setResult(Result.DENY); } }
@EventHandler public void onInventoryDrag(InventoryDragEvent e) { if(controller.isPlaying() && e.getOldCursor().getType() == Material.EYE_OF_ENDER) { int size = e.getInventory().getSize(); for(int slot : e.getRawSlots()) { if(slot < size) { e.setResult(Result.DENY); break; } } } }
@EventHandler public void onItemClick(final InventoryClickEvent event) { try { if (event.getClickedInventory() != null && event.getClickedInventory().equals(this.inventory) && event.getWhoClicked() instanceof Player) { event.setCancelled(true); event.setResult(Result.DENY); final ItemStack clickedItem = event.getCurrentItem(); final int clickedSlot = event.getSlot(); for (final MenuItem item : this.items) { final int itemIndex = this.items.indexOf(item); if (item.getItemStack() != null && item.getClickHandler() != null && (item.getItemStack().equals(clickedItem) || itemIndex == clickedSlot)) { // CraftoMessenger.broadcast("MenuItem is valid!"); final MenuItemClickEvent newEvent = new MenuItemClickEvent(this, item, item.getItemStack(), (Player) event.getWhoClicked(), event); item.getClickHandler().accept(newEvent); return; } } event.setCancelled(true); event.setResult(Result.DENY); } if (!event.isCancelled()) { InventoryView view = event.getView(); if (view != null && view.getBottomInventory() != null && view.getBottomInventory().equals(this.inventory)) { event.setCancelled(true); event.setResult(Result.DENY); } else if (view != null && view.getTopInventory() != null && view.getTopInventory().equals(this.inventory)) { event.setCancelled(true); event.setResult(Result.DENY); } } } catch (final Exception e) { CraftoMessenger.report(this, "Failed to handle item click event of player: " + event.getWhoClicked().getName(), e); } }
@EventHandler public void onItemDrag(final InventoryDragEvent event) { if (event.getInventory() != null && event.getInventory().equals(this.inventory) && event.getWhoClicked() instanceof Player) { event.setCancelled(true); event.setResult(Result.DENY); } }
/** * * @param ice */ @EventHandler public void onInventoryClick(InventoryClickEvent ice) { if (ice.getWhoClicked() instanceof Player) { Player player = (Player) ice.getWhoClicked(); if (player.getInventory() != ice.getInventory()) { if (DataHandler.isPlayerActive(ConfigHandler.getDefaultBukkitConfig().getBoolean("use.UUIDs", true) ? player.getUniqueId().toString() : player.getName())) { ice.setResult(Result.DENY); ice.setCancelled(true); player.updateInventory(); } } } }
@EventHandler public void onPlayerInteract(PlayerInteractEvent e) { if (!(e.getAction() == Action.RIGHT_CLICK_AIR) && !(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return; if (ArenaManager.getInstance().getArena(e.getPlayer()) == null) return; if (e.getItem().getType() == Material.SNOW_BALL) { e.setCancelled(true); e.setUseItemInHand(Result.DENY); e.getPlayer().launchProjectile(Snowball.class); } }
@SuppressWarnings("deprecation") @Override public void onInvItemDrop(InventoryClickEvent event, Inventory toInv, ItemStack stack) { if (toInv.getHolder() instanceof Player) { //A hack to make sure we are always moving the item to the player's inv. //A player inv is always on the bottom, toInv could be the 'crafting' inv toInv = event.getView().getBottomInventory(); Player player = (Player)toInv.getHolder(); if(!validateUnitUse(player, stack)) { CivMessage.sendError(player, "You cannot use this unit because it does not belong to your civlization."); event.setCancelled(true); return; } //Prevent dropping in two unit materials. ConfigUnit unit = Unit.getPlayerUnit(player); if (unit != null) { //player already has a unit item, cancel this event. CivMessage.sendError(player, "You already are a "+unit.name+" cannot pickup another unit item."); event.setCancelled(true); event.setResult(Result.DENY); event.getView().close(); player.updateInventory(); return; } // Reposition item to the last quickbar slot if (event.getSlot() != LAST_SLOT) { DelayMoveInventoryItem task = new DelayMoveInventoryItem(); task.fromSlot = event.getSlot(); task.toSlot = LAST_SLOT; task.inv = toInv; task.playerName = player.getName(); TaskMaster.syncTask(task); } onItemToPlayer(player, stack); } }
/** * Monitor inventory clicking to allow (un)equipping of barding at the horse inventory screen. * * @param event The click event */ @SuppressWarnings("deprecation") @EventHandler public void onDeadHorseInventoryClick(InventoryClickEvent event) { InventoryHolder topHolder = event.getView().getTopInventory().getHolder(); if (topHolder instanceof Horse && isDeadHorse((Horse) topHolder)) { Horse horse = (Horse) topHolder; if (!plugin.getPluginConfig().isArmorEquippable(horse.getVariant())) { return; } switch (event.getAction()) { case PLACE_ALL: case PLACE_ONE: case PLACE_SOME: if (event.getRawSlot() == BARDING_RAW_INDEX && event.getResult() != Result.DENY) { if (isEmpty(event.getCurrentItem()) && isBarding(event.getCursor())) { /* Placing held barding into horse armor slot: swap cursor and slot. */ event.setCancelled(true); event.setCurrentItem(event.getCursor()); event.setCursor(new ItemStack(Material.AIR, 0)); updateInventories(horse, event); } } return; case PICKUP_ALL: case PICKUP_HALF: case PICKUP_ONE: case PICKUP_SOME: case DROP_ALL_SLOT: case DROP_ONE_SLOT: if (event.getRawSlot() == BARDING_RAW_INDEX && event.getResult() != Result.DENY && isEmpty(event.getCursor()) && !isEmpty(event.getCurrentItem())) { /* Dropping barding from horse armor slot: trigger horse metadata packet. */ updateHorseAfter(horse); } return; case MOVE_TO_OTHER_INVENTORY: if (event.getRawSlot() == BARDING_RAW_INDEX && event.getResult() != Result.DENY && !isEmpty(event.getCurrentItem())) { /* Removing barding from horse via shift click: trigger horse metadata packet. */ updateHorseAfter(horse); } else if (isBarding(event.getCurrentItem()) && event.getResult() != Result.DENY && isEmpty(horse.getInventory().getArmor())) { /* Equpping barding to horse armor slot via shift click: trigger horse metadata packet. */ event.setCancelled(true); horse.getInventory().setArmor(event.getCurrentItem()); event.setCurrentItem(new ItemStack(Material.AIR, 0)); updateInventories(horse, event); } return; case SWAP_WITH_CURSOR: case NOTHING: if (event.getRawSlot() == BARDING_RAW_INDEX && event.getResult() != Result.DENY && isBarding(event.getCursor())) { /* Switching held barding with the equipped barding.: swap itemstacks. */ event.setCancelled(true); ItemStack temp = event.getCurrentItem(); event.setCurrentItem(event.getCursor()); event.setCursor(temp); updateInventories(horse, event); } return; case DROP_ALL_CURSOR: case CLONE_STACK: case COLLECT_TO_CURSOR: case DROP_ONE_CURSOR: case HOTBAR_MOVE_AND_READD: case HOTBAR_SWAP: case UNKNOWN: default: return; } } }
@EventHandler(priority= EventPriority.HIGHEST) private void onInventorySharing(InventoryClickEvent event) { HumanEntity humanEntity = event.getWhoClicked(); if (humanEntity == null) return; if (!(humanEntity instanceof Player)) return; final Player p = (Player)humanEntity; IArenaPlayer player = ArenaPlayer.get(p); if (player == null) return; IArena arena = player.getArena(); if (arena == null) return; IContextSettings settings = player.getContextSettings(); if (settings == null) return; if (settings.isSharingEnabled()) return; // prevent sharing if ((event.getAction() == InventoryAction.PLACE_ALL || event.getAction() == InventoryAction.PLACE_ONE || event.getAction() == InventoryAction.PLACE_SOME)) { if (event.getInventory().getHolder() instanceof Chest || event.getInventory().getHolder() instanceof DoubleChest) { if (event.getRawSlot() >= event.getInventory().getContents().length) return; event.setCancelled(true); event.setResult(Result.DENY); // close chest to prevent sharing bug // i.e. If you try enough, eventually the event wont fire but the item // will make it into chest. Bukkit.getScheduler().runTask(PVStarAPI.getPlugin(), new Runnable() { @Override public void run() { p.closeInventory(); } }); } } }
public Event.Result useInteractedBlock() { return null; }
public void setUseInteractedBlock(Event.Result useInteractedBlock) { }
public Event.Result useItemInHand() { return null; }
public void setUseItemInHand(Event.Result useItemInHand) { }
public void setResult(Event.Result newResult) { }
public Event.Result getResult() { return null; }
public PlayerInteractEvent() { this.useItemInHand = Event.Result.ALLOW; this.useClickedBlock = Event.Result.DENY; }
public boolean isCancelled() { return useInteractedBlock() == Event.Result.DENY; }
public Event.Result useInteractedBlock() { return this.useClickedBlock; }
public Event.Result useItemInHand() { return this.useItemInHand; }
/** * Sets the result of this event. This will change whether or not this * event is considered cancelled. * * @see #isCancelled() * @param newResult the new {@link Result} for this event */ public void setResult(Result newResult) { result = newResult; }
/** * Gets the {@link Result} of this event. The Result describes the * behavior that will be applied to the inventory in relation to this * event. * * @return the Result of this event. */ public Result getResult() { return result; }
/** * Gets whether or not this event is cancelled. This is based off of the * Result value returned by {@link #getResult()}. Result.ALLOW and * Result.DEFAULT will result in a returned value of false, but * Result.DENY will result in a returned value of true. * <p> * {@inheritDoc} * * @return whether the event is cancelled */ public boolean isCancelled() { return getResult() == Result.DENY; }
/** * Proxy method to {@link #setResult(Event.Result)} for the Cancellable * interface. {@link #setResult(Event.Result)} is preferred, as it allows * you to specify the Result beyond Result.DENY and Result.ALLOW. * <p> * {@inheritDoc} * * @param toCancel result becomes DENY if true, ALLOW if false */ public void setCancelled(boolean toCancel) { setResult(toCancel ? Result.DENY : Result.ALLOW); }