Problem/Bug Importing Listeners Issue

Discussion in 'Bukkit Help' started by MrWaffleman, Jul 4, 2015.

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

    MrWaffleman

  2. Offline

    scrollbar

    Please send us the error that you are getting.
     
  3. Offline

    Boomer

    only one class in a plugin extends javaplugin - the main class.


    You have motd extending javaplugin which makes it a complete plugin.
    But you also have Test doing the same, as well as calling a new MOTD

    This is gunna crap out big time, with two onEnables, etc, etc within the same plugin
     
  4. Offline

    MrWaffleman

    I couldn't find the exact error, I could try again though. EDIT: Error over here.
    I tried removing 'extends JavaPlugin' before, but it gave me a bunch of errors and it couldn't recognize most of the code I put because I hadn't included JavaPlugin. I tried removing the OnEnable, and replacing it with another name, and that worked out fine. I didn't do 'extend JavaPlugin' for my other classes and it worked fine. But in my first listener it wont work if I remove it.

    EDIT: The imports seems to be working fine, it's just that anything that has to do with Config and registerEvents returns an error. Such as:

    The method saveConfig() is undefined for the type MOTD
    The method getConfig() is undefined for the type MOTD
    The method registerEvents(Listener, Plugin) in the type PluginManager is not applicable for the arguments (MOTD, MOTD)

    Hope this information helps! And thanks for helping me.
     
  5. Offline

    Boomer

    its onEnable, not OnEnable - one is correct, the other might as well be oooghaboohgaaah() as far as the system is concerned, since its not the case-sensitive function name that the server is expecting to find and launch

    Dont use (MOTD, MOTD) as your listener arguments ... look at the arguments required for that registration function -- listener, plugin:
    Register your listener class in the onEnable(), the listener is (new MOTD()) and the plugin is "this" at that point.

    You cant also just registerEvents... you need to get the plugin manager to register them

    Bukkit.getServer().getPluginManager().registerEvents(x,y)


    private static Plugin plugin; << NOOOOO. There is no Plugin object. And if you are trying to refer to your own plugin you would be calling an object called Test instead. But your system does not require setting up an object in this way in your code anyways.
     
    MrWaffleman likes this.
  6. Offline

    MrWaffleman

    I followed everything you said, my code is shown below... I'm still getting errors with Config.

    Test.java (open)

    Code:
    package mrwaffleman.me;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Test extends JavaPlugin {
        private static Test plugin;
       
        public void onEnable() {
            plugin = this;
            Bukkit.getServer().getPluginManager().registerEvents(new MOTD(), this);
            getCommand("heal").setExecutor(new Heal());
            getCommand("feed").setExecutor(new Feed());
            getCommand("prankop").setExecutor(new Prank());
            getCommand("prankjoin").setExecutor(new Prank());
            Bukkit.getServer().getLogger().info("BukkitTutorial has been activated!");
        }
        public void onDisable() {
            plugin = null;
            Bukkit.getServer().getLogger().info("BukkitTutorial has been deactivated!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.GREEN + "This plugin is working, Console!");
                return true;
            }
           
            Player player = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("Test")) {
                player.sendMessage(ChatColor.AQUA + "This plugin is working, Player!");
            }
            return true;
        }
         public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for(Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }
       
        public static Plugin getPlugin() {
            return plugin;
        }
    }
    


    MOTD.java (open)

    Code:
    package mrwaffleman.me;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    
    
    public class MOTD implements Listener, CommandExecutor {
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            p.sendMessage(ChatColor.GREEN + getConfig().getString("message"));
        }
       
        public void Waffle() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, new Test());
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("motd")) {
                sender.sendMessage(ChatColor.GREEN + "MOTD: " + getConfig().getString("message"));
            }
            if(cmd.getName().equalsIgnoreCase("setmotd")) {
                if(!sender.hasPermission("motd.set")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission!");
                }
                if(args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a message!");
                }
                StringBuilder str = new StringBuilder();
                for(int i = 0; i < args.length; i++) {
                    str.append(args[i] + " ");
                }
                String motd = str.toString();
                getConfig().set("message", motd);
                saveConfig();
                sender.sendMessage(ChatColor.GREEN + "MOTD set to: " + motd);
            }
            return true;
        }
    }
    
     
  7. Online

    timtower Moderator Moderator

    @MrWaffleman That is because none of those executors / listeners are plugins. They don't have that method.
    And please don't create new plugin instances, your server won't like it.
     
Thread Status:
Not open for further replies.

Share This Page