Hello Bukkit, I have two questions for you all. 1) I know this has probably been asked before, but I can't seem to find it anywhere when I search it. (>.>) How to I get nearby entities by using the getNearbyEntities() and check if they are a player in a specified ArrayList? 2) Is it possible to cancel the item dropping from a players inventory and armor spots when a player uses the drop item button (q)? Thanks for any help!
ShadowLAX I'm not sure about the second part but I made a quick method for you to get the players (change it to however you want it used): Code:java public List<Player> getNearby(Player p, double x, double y, double z) { List<Entity> ents = p.getNearbyEntities(x, y, z); List<Player> players = new ArrayList<Player>(); for (Entity e : ents) { if (!(e instanceof Player)) continue; players.add((Player) e); }return players;} EDIT: Cleaned up the Java code slightly EDIT 2: Organized code to increase readability
1) You getNearbyEntities, check if they are instanceof Player, Cast them as a player, then check if the player is in the arraylist, example: Code:java for(Entity e : player.getNearbyEntities(30, 30, 30)) { if(e instanceof Player) { Player p = (Player) e; if(arraylist.contains(p.getName())) { // do your stuff } }} 2) Yes it is possible, use a PlayerDropItemEvent and setCancelled(true). If that doesn't work, try setting the type of the dropped item as Material.AIR. EDIT: JRL1004, I don't believe that's what he was asking, I think he was saying how he could check if the nearby players are in a certain arraylist.
Wolfey Ah, okay. He would just fave to iterate through the list returned by my method then or use yours, it does not matter to me as long as his problem gets solved. As for your idea for question 2, just cancelling the PlayerDropItemEvent is a bad idea as it will stop all item drops (not just armor, as he asked). Maybe see if the slot of the dropped item is an armor slot?
Wolfey I have tried the PlayerDropItemEvent, it works for the normal inventory slots but not the armor slots for some reason. The player is not able to drop their sword in their hotbar but they can dispose of their armor. I have no idea what to do with that :/ JRL1004 Like I failed to tag with Wolfey, the PlayerDropEvent does not work. for some reason. It cancels the drop from the players hotbar but not the armor slots as well. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
ShadowLAX It does armor slots, after cancelling do player.updateInventory() so the items show up again. Although I am not sure why you would want to cancel all drops.
Well he's going to perform some checks before cancelling the event. Although, yea, like jrl said, I thought it did armor slots? I'm testing it right now.
JRL1004 Ah, so I was missing the updateInventory(). I'm guessing that's key here. I want to cancel all drops because I'm making a game sort of, and I don't want people dropping their Items Thank you for your help! I will test this in a moment.
Another question Wolfey or JRL1004 or anyone else who can help: Using the helpful methods from above or any other method to grab nearby entities, how would I use that to if the specified player in an ArrayList is at anytime in a radius of another player not in that same ArrayList, and give the player not in that ArrayList a potion effect? I'm not sure what event to use, I tried PlayerMoveEvent but that is only if they are moving. Is there a better way? Thanks!
Well I wouldn't really suggest it, but try a runnable: Code:java public class Run implements Runnable { public void run() { for(Player players : Bukkit.getOnlinePlayers()) { if(arraylist.contains(players.getName())) { for(Entity e : players.getNearbyEntities(30, 30, 30)) { if(e instanceof Player) { Player p = (Player) e; if(!arraylist.contains(p.getName())) { p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 1)); } } } } } }} Then for your main class: Code:java public void onEnable() { Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Run(), 0, 20);}
If you mean when they hit the 'q' button, try the InventoryClickEvent, I think it's called. If you're correct, however, that's a bug.