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

The following examples show how to use org.bukkit.event.player.PlayerInteractEvent#hasItem() . 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: 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 2
Source File: LobbyListener.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false)
  public void onClick(final PlayerInteractEvent e) {
if (Util.get().isSpawnWorld(e.getPlayer().getWorld())) {
	if (SkyWarsReloaded.getCfg().protectLobby()) {
		e.setCancelled(true);
   		if (e.getPlayer().hasPermission("sw.alterlobby")) {
   			e.setCancelled(false);
   		}
	}
	
	if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
      		if (e.hasItem()) {
                  if (e.getItem().equals(SkyWarsReloaded.getIM().getItem("optionselect"))) {
                  	e.setCancelled(true);
                  	Util.get().playSound(e.getPlayer(), e.getPlayer().getLocation(), SkyWarsReloaded.getCfg().getOpenOptionsMenuSound(), 0.5F, 1);
                  	new OptionsSelectionMenu(e.getPlayer());
                  } else if (e.getItem().equals(SkyWarsReloaded.getIM().getItem("joinselect"))) {
                  	e.setCancelled(true);
                  	Util.get().playSound(e.getPlayer(), e.getPlayer().getLocation(), SkyWarsReloaded.getCfg().getOpenJoinMenuSound(), 1, 1);
                  	e.getPlayer().openInventory(joinMenu);
                  } 
      		}
      	}
}
  }
 
Example 3
Source File: InventoryListener.java    From ChestCommands with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
public void onInteract(PlayerInteractEvent event) {
	if (event.hasItem() && event.getAction() != Action.PHYSICAL) {
		for (BoundItem boundItem : ChestCommands.getBoundItems()) {
			if (boundItem.isValidTrigger(event.getItem(), event.getAction())) {
				if (event.getPlayer().hasPermission(boundItem.getMenu().getPermission())) {
					boundItem.getMenu().open(event.getPlayer());
				} else {
					boundItem.getMenu().sendNoPermissionMessage(event.getPlayer());
				}
			}
		}
	}
}
 
Example 4
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 5
Source File: SitListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onClickBlock(PlayerInteractEvent event) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && !event.hasItem()) {
        Block block = event.getClickedBlock();
        BlockFace face = event.getBlockFace();
        if (face == BlockFace.DOWN || block.isLiquid() || !plugin.cfg.sit_blocks.contains(block.getType())) {
            return;
        }
        Block relative = block.getRelative(0, 1, 0);
        Player player = event.getPlayer();
        if (messageCooldown.getIfPresent(player.getUniqueId()) != null) {
            return;
        }
        messageCooldown.put(player.getUniqueId(), true);
        if (!player.hasPermission("nu.sit") || !enabledPlayers.contains(player.getUniqueId()) || player.isInsideVehicle() || !player.getPassengers().isEmpty() || player.getGameMode() == GameMode.SPECTATOR || !player.isOnGround()) {
            return;
        }
        if (relative.isLiquid() || !(relative.isEmpty() || relative.isPassable())) {
            player.sendMessage(I18n.format("user.sit.invalid_location"));
            return;
        }
        Vector vector = block.getBoundingBox().getCenter().clone();
        Location loc = vector.setY(block.getBoundingBox().getMaxY()).toLocation(player.getWorld()).clone();
        for (SitLocation sl : plugin.cfg.sit_locations.values()) {
            if (sl.blocks != null && sl.x != null && sl.y != null && sl.z != null && sl.blocks.contains(block.getType().name())) {
                loc.add(sl.x, sl.y, sl.z);
            }
        }
        if (block.getBlockData() instanceof Directional) {
            face = ((Directional) block.getBlockData()).getFacing();
            if (face == BlockFace.EAST) {
                loc.setYaw(90);
            } else if (face == BlockFace.WEST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(-180);
            }
        } else {
            if (face == BlockFace.WEST) {
                loc.setYaw(90);
            } else if (face == BlockFace.EAST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(-180);
            } else {
                loc.setYaw(player.getEyeLocation().getYaw());
            }
        }
        for (Entity e : loc.getWorld().getNearbyEntities(loc, 0.5, 0.7, 0.5)) {
            if (e instanceof LivingEntity) {
                if (e.hasMetadata(metadata_key) || (e instanceof Player && e.isInsideVehicle() && e.getVehicle().hasMetadata(metadata_key))) {
                    player.sendMessage(I18n.format("user.sit.invalid_location"));
                    return;
                }
            }
        }
        Location safeLoc = player.getLocation().clone();
        ArmorStand armorStand = loc.getWorld().spawn(loc, ArmorStand.class, (e) -> {
            e.setVisible(false);
            e.setPersistent(false);
            e.setCanPickupItems(false);
            e.setBasePlate(false);
            e.setArms(false);
            e.setMarker(true);
            e.setInvulnerable(true);
            e.setGravity(false);
        });
        if (armorStand != null) {
            armorStand.setMetadata(metadata_key, new FixedMetadataValue(plugin, true));
            if (armorStand.addPassenger(player)) {
                safeLocations.put(player.getUniqueId(), safeLoc);
            } else {
                armorStand.remove();
            }
        }
    }
}
 
Example 6
Source File: Library.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
public void add_enchantment_to_tool(Player player, StructureSign sign, PlayerInteractEvent event) throws CivException {
	int special_id = Integer.valueOf(sign.getAction());

	if (!event.hasItem()) {
		CivMessage.send(player, CivColor.Rose+"You must have the item you wish to enchant in hand.");
		return;
	}
	ItemStack item = event.getItem();
	
	if (special_id >= this.enchantments.size()) {
		throw new CivException("Library enchantment not ready.");
	}
	
	
	LibraryEnchantment ench = this.enchantments.get(special_id);
	this.validateEnchantment(item, ench);
	
	int payToTown = (int) Math.round(ench.price*getNonResidentFee());
	Resident resident;
			
	resident = CivGlobal.getResident(player.getName());
	Town t = resident.getTown();	
	if (t == this.getTown()) {
			// Pay no taxes! You're a member.
			payToTown = 0;
	}					
			
	// Determine if resident can pay.
	if (!resident.getTreasury().hasEnough(ench.price+payToTown)) {
		CivMessage.send(player, CivColor.Rose+"You do not have enough money, you need "+ench.price+payToTown+ " coins.");
		return;
	}
			
	// Take money, give to server, TEH SERVER HUNGERS ohmnom nom
	resident.getTreasury().withdraw(ench.price);
	
	// Send money to town for non-resident fee
	if (payToTown != 0) {
		getTown().depositDirect(payToTown);
		
		CivMessage.send(player, CivColor.Yellow + "Paid "+ payToTown+" coins in non-resident taxes.");
	}
			
	// Successful payment, process enchantment.
	ItemStack newStack = this.addEnchantment(item, ench);
	player.setItemInHand(newStack);
	CivMessage.send(player, CivColor.LightGreen+"Enchanted with "+ench.displayName+"!");
}
 
Example 7
Source File: BukkitPlotListener.java    From PlotMe-Core with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    IPlayer player = plugin.wrapPlayer(event.getPlayer());
    Location location = BukkitUtil.adapt(event.getClickedBlock().getLocation());
    PlotMapInfo pmi = manager.getMap(location);
    if (player.hasPermission(PermissionNames.ADMIN_BUILDANYWHERE)) {
        return;
    }
    if (manager.isPlotWorld(location)) {
        Plot plot = manager.getPlot(location);
        if (plot == null) {
            player.sendMessage(api.C("CannotBuild"));
            event.setCancelled(true);
        } else if (!plot.getOwnerId().equals(event.getPlayer().getUniqueId())) {
            Optional<Plot.AccessLevel> member = plot.isMember(player.getUniqueId());
            if (member.isPresent()) {
                if (member.get().equals(Plot.AccessLevel.TRUSTED)) {
                    if (!api.getServerBridge().getOfflinePlayer(plot.getOwnerId())
                            .isOnline()) {
                        if (event.hasBlock() && pmi.isProtectedBlock(event.getClickedBlock().getTypeId())) {
                            if (!player.hasPermission("plotme.unblock." + event.getClickedBlock().getTypeId())) {
                                player.sendMessage(api.C("CannotBuild"));
                                event.setCancelled(true);
                                return;
                            } else {
                                return;
                            }
                        }
                        if (event.hasItem() && (pmi.isPreventedItem(String.valueOf(event.getItem().getTypeId())) || pmi
                                .isPreventedItem(event.getItem().getTypeId() + ":" + event.getItem().getData()))) {
                            if (!player.hasPermission("plotme.unblock." + event.getClickedBlock().getTypeId())) {
                                player.sendMessage(api.C("CannotBuild"));
                                event.setCancelled(true);
                            }

                        }
                    }
                }
            } else {
                if (event.hasBlock() && pmi.isProtectedBlock(event.getClickedBlock().getTypeId())) {
                    if (player.hasPermission("plotme.unblock." + event.getClickedBlock().getTypeId())) {
                        return;
                    } else {
                        player.sendMessage(api.C("CannotBuild"));
                        event.setCancelled(true);
                        return;
                    }
                }
                if (event.hasItem() && (pmi.isPreventedItem(String.valueOf(event.getItem().getTypeId())) || pmi.isPreventedItem(
                        event.getItem().getTypeId() + ":" + event.getItem().getData()))) {
                    if (!player.hasPermission("plotme.unblock." + event.getClickedBlock().getTypeId())) {
                        player.sendMessage(api.C("CannotBuild"));
                        event.setCancelled(true);
                    }

                }
            }
        }
    }
}