onEnable problem help.

Discussion in 'Plugin Development' started by EndureBlackout, Aug 31, 2016.

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

    EndureBlackout

    So I have two classes. My main "VoteKickMain" and my command listener class "CommandListener" here is the code
    Main:
    Code:
    package me.endureblackout;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class VoteKickMain extends JavaPlugin {
       
        public void onEnable() {
    
           
            File dataFolder = getDataFolder();
            if(!dataFolder.exists()) {
                dataFolder.mkdir();
            }
           
            File file = new File(getDataFolder(), "config.yml");
            if(!(file.exists())) {
                try {
                    saveConfig();
                    setupConfig(getConfig());
                    getConfig().options().copyDefaults(true);
                    saveConfig();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
            Bukkit.getPluginManager().registerEvents(new CommandListener(this), this);
            getCommand("vkick").setExecutor(new CommandListener(this));
        }
       
        private void setupConfig(FileConfiguration config) throws IOException {
            if (!new File(getDataFolder(), "RESET.FILE").exists()) {
                new File(getDataFolder(), "RESET.FILE").createNewFile();
                getConfig().set("Percent to kick", 0.5);
            }
        }
    }
    
    Command Listener:
    Code:
    package me.endureblackout;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    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.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    
    public class CommandListener implements Listener, CommandExecutor {
    
        VoteKickMain plugin;
        public CommandListener(VoteKickMain instance) {
            this.plugin = instance;
        }
       
        public Map<Player, List<String>> kickList = new HashMap<>();
       
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player p = (Player) sender;
               
                File cfg = new File(plugin.getDataFolder(), "config.yml");
                YamlConfiguration y = new YamlConfiguration();
               
                try {
                    y.load(cfg);
                } catch (IOException | InvalidConfigurationException e) {
                    e.printStackTrace();
                }
               
                    if(cmd.getName().equalsIgnoreCase("vkick")) {
                        if(args.length == 1) {
                            List<String> kicker = new ArrayList<>();
                            if(!(kickList.containsKey(args[0].toLowerCase()))) {
                                kicker.add(p.getName().toLowerCase());
                               
                                for(Player p1 : Bukkit.getServer().getOnlinePlayers()) {
                                    if(p1.getName().equalsIgnoreCase(args[0])) {
                                        kickList.put(p1, kicker);
                                    }
                                }
                                Bukkit.getServer().broadcastMessage(ChatColor.GOLD + args[0] + " has been voted to be kicked!");
                            } else {
                               
                                for(Player p1 : Bukkit.getServer().getOnlinePlayers()) {
                                    if(kickList.containsKey(p1)) {
                                        kickList.get(p1).addAll(kicker);
                                        if(kickList.get(p1).size() / Bukkit.getOnlinePlayers().size() >= y.getInt("Percent to kick")) {
                                            p1.kickPlayer(ChatColor.RED + "You have been voted to be kicked by the online players!");
                                        }
                                    }
                                }
                            }
                    }
                }
            }
           
            return false;
        }
    }
    
    here is the error I get when enabling the plugin:
    Code:
    [00:14:37 INFO]: [VoteKick] Enabling VoteKick v1.0
    [00:14:37 ERROR]: Error occurred while enabling VoteKick v1.0 (Is it up to date?)
    java.lang.NoSuchMethodError: me.endureblackout.CommandListener.<init>(Lme/endureblackout/VoteKickMain;)V
            at me.endureblackout.VoteKickMain.onEnable(VoteKickMain.java:31) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    The problem is with the command executor and the registerevents but I can't seem to figure it out. Any help would be appreciated.
     
  2. Offline

    kameronn

    @EndureBlackout
    It says "NoSuchMethodError" so maybe its a problem when your exporting the plugin. Are you sure all you dont have any jars missing in your library? It may also be a exporting error, so make sure your exporting the right plugin.

    Also we cant see what line, line 31 is so that would be helpful to tell us
     
  3. Offline

    EndureBlackout

    this is line 31 and 32 which are both having problems
    Code:
            Bukkit.getPluginManager().registerEvents(new CommandListener(this), this);
            getCommand("vkick").setExecutor(new CommandListener(this));
     
  4. Offline

    kameronn

    @EndureBlackout
    instead of making doing "new CommandListener(instance)" twice you can a commandlistener variable, then in your on enable do

    commandlistener = new CommandListener(instance)

    other then that i dont see anything else that would be a problem, hows your plugin.yml looking?
     
  5. Offline

    EndureBlackout

    name: VoteKick
    main: me.endureblackout.VoteKickMain
    version: 1.0
    description: >
    Test. port plugin.
    commands:
    vkick:
    description: Starter command.
     
  6. Offline

    kameronn

    @EndureBlackout
    Next time when you export, when its asking you where you want to save it, make sure your saving it in the right place and make sure that where it says select the resources to export, its the right one. Just incase double click the project and it should turn from a black box to a check mark, I'm not really sure why its doing that I dont see anything wrong
     
  7. Offline

    EndureBlackout

    The error isn't with exporting it is with the actual code. It for some reason isn't registering/setting the executor because of something but I can't figure out what it is.
     
  8. Offline

    kameronn

    Could just be the class, try copying and pasting the code into a new class with a different name
     
  9. Offline

    InstanceofDeath

    You created the instance from the CommandListener-Class
    Code:
    getCommand("vkick").setExecutor(new CommandListener(this), this);
    
    and then in the onCommand Method in the CommandListener-Class you checked the command again

    Code:
    if(cmd.getName().equalsIgnoreCase("vkick") {
    
    }
    
    But its a bit silly from bukkit, that this mistake results in a NoSuchMethod Error

    And P.S. Its a common mistake dont worry :D
     
  10. Offline

    Rayzr522

    Remove @EventHandler from onCommand. That's not how CommandExecutors work. Also remove the "implements Listener", and "
    Bukkit.getPluginManager().registerEvents(new CommandListener(this), this);"

    They're all unnecessary, and quite likely the source of your problem. You just implement CommandExecutor and then set the executor of your command to "new CommandListener(this)" like you were doing. Remove all the event stuff.
     
  11. Offline

    EndureBlackout

    That still didn't work. Don't know what else to try...
     
  12. Offline

    Rayzr522

    Hmmm.... anyways, that was unnecessary and wrong. You still should remove it.
     
Thread Status:
Not open for further replies.

Share This Page