Solved OnPlayerMessage not doing anything

Discussion in 'Plugin Development' started by vemacs, Oct 27, 2012.

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

    vemacs

    Hello all,

    I have a plugin that aims to broadcast to the server whenever a player types a string containing /gamemode, or preferably when a player uses the /gamemode command, and says something like:
    "playername used the /gamemode command!"
    Here's the code for the listener I have right now:
    Code:
    package com.vemacs.GamemodeDetector;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
     
    @SuppressWarnings("deprecation")
    public class GamemodeDetectorListener implements Listener {
     
        private GamemodeDetector plugin;
     
        public GamemodeDetectorListener(GamemodeDetector gamemodeDetector) {
            plugin = gamemodeDetector;
        }
     
            @EventHandler
        public void onPlayerMessage(PlayerChatEvent E) {
                Player player = E.getPlayer();
                String playerName = player.getName();
                String message = E.getMessage().toLowerCase();
                if(message.contains("/gamemode")){
                Bukkit.broadcastMessage( playerName + "used the /gamemode command!");
            }
    }
    }
    
    But it doesn't do anything.
    I added a Bukkit.broadcastMessage outside the if construct, and it's still not doing anything.
    There's a problem with either the listener or with the broadcast function, and I suspect it's the listener.
    Help?!
    Thanks in advance.
     
  2. Offline

    Woobie

    PlayerChatEvent doesnt work anymore, use AsyncPlayerChatEvent
     
  3. Offline

    vemacs

    Here's my new code, and it doesn't do anything either:
    Code:
    package com.vemacs.GamemodeDetector;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
     
    public class GamemodeDetectorListener implements Listener {
     
        private GamemodeDetector plugin;
     
        public GamemodeDetectorListener(GamemodeDetector gamemodeDetector) {
            plugin = gamemodeDetector;
        }
     
            @EventHandler
        public void onPlayerMessage(AsyncPlayerChatEvent E) {
                Player player = E.getPlayer();
                String playerName = player.getName();
                String message = E.getMessage().toLowerCase();     
                if(message.contains("/gamemode")){
                Bukkit.broadcastMessage( playerName + "used the /gamemode command!");
            }
    }
    }
    When I switch the if construct to gamemode, nothing happens either. If I put a broadcastmessage outside of the if construct, nothing happens either. I'm pretty sure its a problem with the AsyncPlayerChatEvent.
     
  4. Offline

    ZeusAllMighty11 Retired Staff

    Use PlayerCommandPreprocessEvent -- something like that. Works for me, I just made one a few minutes ago
     
  5. Offline

    Woobie

    Did you register the event?
     
  6. Offline

    vemacs

    I'm a bit of a Java noob, so how do I do that?
    Here's my code right now:
    Code:
    package com.vemacs.GamemodeDetector;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent ;
     
    public class GamemodeDetectorListener implements Listener {
     
        private GamemodeDetector plugin;
     
        public GamemodeDetectorListener(GamemodeDetector gamemodeDetector) {
            plugin = gamemodeDetector;
        }
     
            @EventHandler
        public void onPlayerMessage(PlayerCommandPreprocessEvent  E) {
                Player player = E.getPlayer();
                String playerName = player.getName();
                String message = E.getMessage().toLowerCase(); 
                Bukkit.broadcastMessage( playerName + "used the /gamemode command!");
                if(message.contains("gamemode")){
                Bukkit.broadcastMessage( playerName + "used the /gamemode command!");
            }
    }
    }
    I already put the @eventhandler there.

    EDIT: I tried registering the event:
    Code:
    package com.vemacs.GamemodeDetector;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent ;
     
    public class GamemodeDetectorListener implements Listener {
     
        private GamemodeDetector plugin;
     
        public GamemodeDetectorListener(GamemodeDetector gamemodeDetector) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            plugin = gamemodeDetector;
        }
     
            @EventHandler
        public void onPlayerMessage(PlayerCommandPreprocessEvent E) {
                Player player = E.getPlayer();
                String playerName = player.getName();
                String message = E.getMessage().toLowerCase();   
                Bukkit.broadcastMessage( playerName + "used the /gamemode command!");
                if(message.contains("gamemode")){
                Bukkit.broadcastMessage( playerName + "used the /gamemode command!");
            }
    }
    }
    But now it's throwing a NullPointerException at launch at line 14, and won't load at all.
     
  7. Offline

    Mydayyy

  8. Offline

    Woobie

    Do you have a class that extends JavaPlugin?
     
  9. Offline

    Lolmewn Retired Staff

    Probably GamemodeDetector.java
    Anyway, you could also just make the actual command, instead of listening for it.
     
  10. Offline

    ferrybig

    Code:
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
            plugin = gamemodeDetector;
    
    ^you made an mistake, put the lower line before the upperline and it should work
     
  11. Offline

    RainoBoy97

    You need to register the event in the onEnable() method :p
     
  12. Offline

    vemacs

    Thanks for all the help!
    I managed to get my plugin working using ferrybig's help, and directly using PlayerGameModeChangeEvent. It works perfectly now.
     
Thread Status:
Not open for further replies.

Share This Page