Creating a MOTD change plugin

Discussion in 'Plugin Development' started by mig4ng, Jan 26, 2013.

Thread Status:
Not open for further replies.
  1. So, I know that there are already plugins like this out there, but i want to try something by myself so i need a little help to finish this.

    Code:
    package me.mig4ng.MaxPlayers;
     
    import java.util.logging.Logger;
     
    import me.mig4ng.MaxPlayers.PlayerListener;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MaxPlayers extends JavaPlugin {
        public static MaxPlayers plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public final PlayerListener pl = new PlayerListener(this);
     
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.pl, this);
        }
       
        @EventHandler(ignoreCancelled = true)
        public void onServerPing(ServerListPingEvent event)
        {
            String motd = "Teste";
            event.setMotd(motd);
        }
    }
    
    Code:
    package me.mig4ng.MaxPlayers;
     
    import org.bukkit.event.Listener;
    import org.bukkit.event.server.ServerListPingEvent;
     
    public class PlayerListener implements Listener {
        public static MaxPlayers plugin;
       
        @SuppressWarnings("static-access")
        public PlayerListener(MaxPlayers plugin){
            this.plugin = plugin;
        }
       
        public void onServerListPing(ServerListPingEvent event) {
            event.setMotd("Test MOTD");
        }
     
        public static String colorize(String s){
            if(s == null) return null;
            return s.replaceAll("&([0-9a-f])", "\u00A7$1");
        }
    }
    
    These are my classes, but idk why it doesn't work :(
     
  2. Offline

    skore87

    You aren't registering the events properly. You're creating a new instance of a class (PlayerListener) that doesn't have the event you're wanting to trigger. You put the "onServerListPing" method in your plugin's main class instead of the listener class. Do that and it should work.
     
  3. Offline

    ZeusAllMighty11

    You are mising the '@EventHandler' annotation :)
     
  4. Offline

    skore87

    No, actually he isn't. Look at my previous post... He has his event in the wrong class file.
     
  5. Offline

    ZeusAllMighty11

    He does in the 2nd code block it's not there
     
Thread Status:
Not open for further replies.

Share This Page