PlayerMoveEvent

Discussion in 'Plugin Development' started by goomonster3, Apr 5, 2013.

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

    goomonster3

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event){
    Player player = event.getPlayer();
    if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.STONE_PLATE || player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.WOOD_PLATE) {
    player.setHealth(0);
    }
    }


    Hey Guys, why will that code no work?? Please tell me the error,
    Thanks,
    Goo
     
  2. Offline

    JayzaSapphire

    Make sure you have your events registered, and i don't think you need to get the block that's down, get the player's block.
     
  3. Offline

    goomonster3

    Alright, But what do you mean by "Registered"?
     
  4. Offline

    JayzaSapphire

    You should be registering your event class in your main class.
     
  5. Offline

    goomonster3

    Im new to coding, How would I do this.. I can register COMMANDS but im not sure about events?
     
  6. Offline

    kreashenz

    goomonster3 In the onEnable, put this.
    Code:
    getServer().getPluginManager().registerEvents(this,this);
     
  7. Offline

    goomonster3

    That did not work D:

    public void onEnable() {
    getServer().getPluginManager().registerEvents(this,this);

    thats my onEnable, and I have register Commands as well.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    CorrieKay

    Looks like you're trying to kill a player when they step on a plate. You can do this much easier by listening to the player interact event, and checking for Action.PHYSICAL.

    Interact Method (open)

    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent event){
      if(event.getAction() == Action.PHYSICAL){
        event.getPlayer().setHealth(0);
      }
    }
    


    Furthermore, you should take a look at my crash-course in my signature, will help a lot :p

    Lastly, to explain listeners:
    A listener method is a method that will be fired when a specific event is thrown. However, you cant just code the method into your class and expect the event handler system to work. You need to register a class as a listener with the plugin manager before the events will be listened to by your listener methods (the one with @EventHandler above them)

    To register your listener, you need to do this:
    Bukkit.getPluginManager().registerEvents(Listener,Plugin);
    The listener is your class that implements "Listener" (aka, your listener :p)
    the plugin is your plugin instance (your main class, that extends JavaPlugin)

    Typically, in smaller plugins, your main class both extends JavaPlugin, and implements Listener. This is why you see a lot of
    Bukkit.getPluginManager().registerEvents(this,this);
    Because the main class acts as both a listener, and a plugin.

    Also, in case you were wondering, the "this" keyword is the reference of the object that uses it.

    Any questions?
     
  9. Offline

    jorisk322

    Doesn't that event also fire when walking over tripwire?
     
  10. Offline

    CorrieKay

    I think so, yes. But you can still filter that out, i believe, by querying the clicked block.
     
Thread Status:
Not open for further replies.

Share This Page