Solved Listeners..........

Discussion in 'Plugin Development' started by Datdenkikniet, Jul 2, 2013.

Thread Status:
Not open for further replies.
  1. I have this code:
    Code:java
    1. package me.sapply.Hellower;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerMoveEvent;
    8.  
    9. public class AFKListener implements Listener {
    10. @EventHandler
    11. public void onPlayerMove(PlayerMoveEvent event, Location from, Location to) {
    12. Player player = event.getPlayer();
    13. if(AFKCommandExecutor.quest.contains(player.getName())){
    14. Location loc = player.getLocation();
    15. player.teleport(loc);
    16. player.sendMessage("You are afk! You cant move!");
    17. }
    18. }
    19.  
    20. }
    21.  

    but it doesn't prevent a player who is afk (which i have defined correctely in another class) from moving, what do I do wrong here?
     
  2. Offline

    MP5K

    Hello Datdenkikniet,
    because:
    A. an Listener Delegator (the Method with the @EventHandler()) only takes one argument which is the Event itself.
    B. don't use teleporation use event.setCancelled(true);
    so the code should look like that:

    Code:java
    1. public class AFKListener implements Listener {
    2.  
    3. @EventHandler(ignoreCancelled = true)
    4. public void onPlayerMove(PlayerMoveEvent event) {
    5. Player player = event.getPlayer();
    6. if(AFKCommandExecutor.quest.contains(player.getName())){
    7. event.setCancelled(true);
    8. player.sendMessage("You are afk! You cant move!");
    9. }
    10.  
    11. }
    12. }
     
  3. Offline

    ProtoTempus

    Datdenkikniet MP5K Cancelling the event will make it look super glitchy... try:
    Code:java
    1. event.setTo(event.getFrom());

    Instead of cancelling the event.
     
    MP5K likes this.
  4. it still doesn't work?!
     
  5. Offline

    MP5K

    are you sure you added the player to your "quest" List and that you registered your listener?
     
  6. yes, because in my main class, if you use /back when you aren't registered in the hashset it says you are back, and if you are not in the hashmap it says that you aren't afk. (like it should do)
    and if you might need it, here is the afk class:
    Code:java
    1. public static HashSet<String> quest = new HashSet<String>();
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    3. if (cmd.getName().equalsIgnoreCase("afk")){
    4. if (!(sender instanceof Player)){
    5. sender.sendMessage("The console cant be afk!");
    6. return true;
    7. }
    8. if (!(quest.contains(sender.getName()))) {
    9. quest.add(sender.getName());
    10. sender.sendMessage("You are now afk!");
    11. Bukkit.broadcastMessage("");
    12. return true;
    13. }
    14. if (quest.contains(sender.getName())){
    15. sender.sendMessage("You are afk already! Use /back to get back!");
    16. }
    17. }
    18. if (cmd.getName().equalsIgnoreCase("back")){
    19. if (!(sender instanceof Player)){
    20. sender.sendMessage("The console cant be afk!");
    21. return true;
    22. }
    23. if (!(quest.contains(sender.getName()))){
    24. sender.sendMessage("You are not afk!");
    25. }
    26. else {
    27. quest.remove(sender.getName());
    28. sender.sendMessage("You are back!");
    29. }
    30. }
    31. return false;
    32. }
     
  7. Offline

    MP5K

     
  8. aha, how do I register my listener if I may ask?
     
  9. Offline

    MP5K

    call that in your onEnable method.
    Code:java
    1. this.getServer().getPluginManager().registerEvents(YOUR LISTENER, this);
    2.  
     
Thread Status:
Not open for further replies.

Share This Page