Getting Player From Inside Listener

Discussion in 'Plugin Development' started by McStormify, Apr 2, 2013.

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

    McStormify

    I'm working on a plugin to disable drops for specific players. I have an ArrayList<String> containing player names who use the /disabledrops command.

    Here is my event handler. The problem is getting the current user's name. How would I do this?

    Code:java
    1. @EventHandler
    2. public void itemSpawn(ItemSpawnEvent event) {
    3. String playerName = "???";
    4.  
    5. if (this.plugin.disableDrops.contains(playerName)) {
    6. event.getEntity().remove();
    7. }
    8. }


    Thanks! :D
     
  2. Offline

    chasechocolate

    ItemSpawnEvent won't let you get the player, since the item could be spawned from a dispenser, or dropper. If you want to disable player dropping items, use PlayerDropItemEvent:
    Code:java
    1. Player player = event.getPlayer();
    2. String playerName = player.getName();
     
    McStormify likes this.
  3. Offline

    McStormify

    Haha, I figured this out myself just as this was posted! Thanks for the help regardless :)
     
  4. Offline

    JoTyler

    McStormify I have been looking for how to do this much love guys :)
     
Thread Status:
Not open for further replies.

Share This Page