Two Questions...

Discussion in 'Plugin Development' started by ShadowLAX, Dec 7, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    ShadowLAX

    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!
     
  2. Offline

    JRL1004

    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
    1. public List<Player> getNearby(Player p, double x, double y, double z) {
    2. List<Entity> ents = p.getNearbyEntities(x, y, z);
    3. List<Player> players = new ArrayList<Player>();
    4. for (Entity e : ents) {
    5. if (!(e instanceof Player)) continue;
    6. players.add((Player) e);
    7. }
    8. return players;
    9. }


    EDIT: Cleaned up the Java code slightly
    EDIT 2: Organized code to increase readability
     
  3. Offline

    Wolfey

    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
    1.  
    2. for(Entity e : player.getNearbyEntities(30, 30, 30)) {
    3. if(e instanceof Player) {
    4. Player p = (Player) e;
    5. if(arraylist.contains(p.getName())) {
    6. // do your stuff
    7. }
    8. }
    9. }
    10.  

    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.
     
  4. Offline

    ShadowLAX

  5. Offline

    JRL1004

    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?
     
  6. Offline

    ShadowLAX

    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.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    JRL1004

    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.
     
  8. Offline

    Wolfey

    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.
     
  9. Offline

    ShadowLAX

    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 :p
    Thank you for your help! I will test this in a moment.
     
  10. Offline

    ShadowLAX

    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!
     
  11. Offline

    Wolfey

    Well I wouldn't really suggest it, but try a runnable:
    Code:java
    1.  
    2. public class Run implements Runnable {
    3.  
    4. public void run() {
    5. for(Player players : Bukkit.getOnlinePlayers()) {
    6. if(arraylist.contains(players.getName())) {
    7. for(Entity e : players.getNearbyEntities(30, 30, 30)) {
    8. if(e instanceof Player) {
    9. Player p = (Player) e;
    10. if(!arraylist.contains(p.getName())) {
    11. p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 1));
    12. }
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
    19.  

    Then for your main class:
    Code:java
    1.  
    2. public void onEnable() {
    3. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Run(), 0, 20);
    4. }
    5.  
     
  12. Offline

    ShadowLAX

    Wolfey Ohh, that gives me an idea. Never would have thought of that :p Thanks!
     
  13. Offline

    Garris0n

    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.
     
Thread Status:
Not open for further replies.

Share This Page