Cant reload plugin: java.lang.IllegalArgumentException

Discussion in 'Spigot Plugin Development' started by woarth, Apr 29, 2017.

  1. Hi people, i have been doing my best trying to know why this doesnt work but I cant find a clue. I have been using this reload system since a long time ago, and i dont know when it started to stop working. Before it did work, so I assume that it stopped working in the last versions. This reload commands disables and enables my plugin, restarting everything it has. I am actually using spigot 1.11.2

    This is the reload code:

    Code (Text):
    if(cmd.getName().equalsIgnoreCase("pingpluginreload")){
                                          reload(sender);
                                  }
                              }
    Code (Text):
    @SuppressWarnings("unchecked")
    public void reload(CommandSender s) {

    final Plugin plugin = this;
    final CommandSender sender = s;

    try {
            new BukkitRunnable() {
                            @Override
                            public void run() {
                                    try {
                                            long start_time = System.currentTimeMillis();
                                    PluginManager pluginManager = Bukkit.getPluginManager();
                                    SimpleCommandMap commandMap = null;
                                    List<Plugin> plugins = null;
                                    Map<String, Plugin> names = null;
                                    Map<String, Command> commands = null;
                                    Map<Event, SortedSet<RegisteredListener>> listeners = null;
                                    boolean reloadlisteners = true;
                                    plugin.onDisable();
                                    if (pluginManager != null) {
                                        try {
                                            Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
                                            pluginsField.setAccessible(true);
                                            plugins = (List<Plugin>) pluginsField.get(pluginManager);
                                            Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
                                            lookupNamesField.setAccessible(true);
                                            names = (Map<String, Plugin>) lookupNamesField.get(pluginManager);
                                            try {
                                                Field listenersField = Bukkit.getPluginManager().getClass().getDeclaredField("listeners");
                                                listenersField.setAccessible(true);
                                                listeners = (Map<Event, SortedSet<RegisteredListener>>) listenersField.get(pluginManager);
                                            } catch (Exception e) {
                                                reloadlisteners = false;
                                            }
                                            Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
                                            commandMapField.setAccessible(true);
                                            commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
                                            Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
                                            knownCommandsField.setAccessible(true);
                                            commands = (Map<String, Command>) knownCommandsField.get(commandMap);
                                        } catch (NoSuchFieldException e) {
                                            e.printStackTrace();
                                            return;
                                        } catch (IllegalAccessException e) {
                                            e.printStackTrace();
                                            return;
                                        }
                                    }
                                    pluginManager.disablePlugin(plugin);
                                    if (plugins != null && plugins.contains(plugin))
                                        plugins.remove(plugin);
                                    if (names != null && names.containsKey(plugin))
                                        names.remove(plugin);
                                    if (listeners != null && reloadlisteners) {
                                        for (SortedSet<RegisteredListener> set : listeners.values()) {
                                            for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext(); ) {
                                                RegisteredListener value = it.next();
                                                if (value.getPlugin() == plugin) {
                                                    it.remove();
                                                }
                                            }
                                        }
                                    }
                                    if (commandMap != null) {
                                        for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext(); ) {
                                            Map.Entry<String, Command> entry = it.next();
                                            if (entry.getValue() instanceof PluginCommand) {
                                                PluginCommand c = (PluginCommand) entry.getValue();
                                                if (c.getPlugin() == plugin) {
                                                    c.unregister(commandMap);
                                                    it.remove();
                                                }
                                            }
                                        }
                                    }
                                    ClassLoader cl = plugin.getClass().getClassLoader();
                                    if (cl instanceof URLClassLoader) {
                                        try {
                                            ((URLClassLoader) cl).close();
                                        } catch (IOException ex) {
                                            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                                        }
                                    }
                                    System.gc();
                                    Plugin target = null;
                                    try {
                                        target = Bukkit.getPluginManager().loadPlugin(getFile());
                                    } catch (InvalidDescriptionException e) {
                                        e.printStackTrace();
                                        return;
                                    } catch (InvalidPluginException e) {
                                        e.printStackTrace();
                                        return;
                                    }
                                    target.onLoad();
                                    Bukkit.getPluginManager().enablePlugin(target);
                                    long final_time = System.currentTimeMillis() - start_time;
                                    sender.sendMessage(ChatColor.GREEN + "PingPlugin sucessfuly reloaded!");
                                    } catch(Exception e){
                                    sender.sendMessage(ChatColor.RED + "Couldn't reload PingPlugin!");
                            }
                            }
                    }.runTaskLater(this, 10);
    } catch(Exception ex){
            sender.sendMessage(ChatColor.RED + "Couldn't reload PingPlugin!");
    }
    }

    Code (Text):
    [01:17:57 INFO]: woarth issued server command: /pingpluginreload
    [01:17:57 INFO]: [PingPlugin] Plugin disabled. Plugin created by woarth
    [01:17:57 INFO]: [PingPlugin] Disabling PingPlugin
    [01:17:57 INFO]: [PingPlugin] Plugin disabled. Plugin created by woarth
    [01:17:58 WARN]: org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Plugin already initialized!
    [01:17:58 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133)
    [01:17:58 WARN]:        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329)
    [01:17:58 WARN]:        at me.woarth.PingPlugin.Main$4.run(Main.java:1548)
    [01:17:58 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71)
    [01:17:58 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353)
    [01:17:58 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:738)
    [01:17:58 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399)
    [01:17:58 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678)
    [01:17:58 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576)
    [01:17:58 WARN]:        at java.lang.Thread.run(Unknown Source)
    [01:17:58 WARN]: Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
    [01:17:58 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122)
    [01:17:58 WARN]:        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67)
    [01:17:58 WARN]:        at me.woarth.PingPlugin.Main.<init>(Main.java:168)
    [01:17:58 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [01:17:58 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [01:17:58 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [01:17:58 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [01:17:58 WARN]:        at java.lang.Class.newInstance(Unknown Source)
    [01:17:58 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76)
    [01:17:58 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129)
    [01:17:58 WARN]:        ... 9 more
    [01:17:58 WARN]: Caused by: java.lang.IllegalStateException: Initial initialization
    [01:17:58 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125)
    [01:17:58 WARN]:        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67)
    [01:17:58 WARN]:        at me.woarth.PingPlugin.Main.<init>(Main.java:168)
    [01:17:58 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [01:17:58 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [01:17:58 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [01:17:58 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [01:17:58 WARN]:        at java.lang.Class.newInstance(Unknown Source)
    [01:17:58 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76)
    [01:17:58 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129)
    [01:17:58 WARN]:        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329)
    [01:17:58 WARN]:        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251)
    [01:17:58 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.loadPlugins(CraftServer.java:301)
    [01:17:58 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.init(DedicatedServer.java:204)
    [01:17:58 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:544)
    [01:17:58 WARN]:        ... 1 more

    Code (Text):
                                        target = Bukkit.getPluginManager().loadPlugin(getFile());
    Code (Text):
    public class Main extends JavaPlugin implements Listener {

    Any help would be really apprecaited! Thanks a lot for reading and for trying to help me!
     
  2. sothatsit

    Patron

    Read the error: "Plugin already initialized!".

    The code that disables the plugin is not working correctly. I am not sure if you wrote this code, but it seems incredibly over-complicated.
     
    • Agree Agree x 2
  3. I have a friend who shared it to me, but it worked in the past and now it doesnt work anymore

    This reload command seems complex, but i have been using it because the normal reload system always deleted me the # comments to help in the config.yml
     
    #3 woarth, May 3, 2017
    Last edited: May 3, 2017
  4. Don't take my word on it, but it seems like the plugin doesn't actually disable in the first place. All the other errors source from trying to load a plugin that's already initialized.
     
  5. And do you have an idea of why? Because the code disables the plugin with:
    Code (Text):
    plugin.onDisable();
    Thanks!
     
  6. It is exactly what the name suggest. A method that fires when the plugin disables. Hence onDisable. Calling onDisable will not disable the plugin, otherwise the method name would be something like disable().
     
  7. That doesn't disable the plugin. That simply just calls the code inside of the plugin meant to tell it to clean up and save it's data. If you want to disable the plugin, use Bukkit.getPluginManager().disablePlugin(plugin).
     
  8. @woarth I really recommend you manually reload specific parts of your plugin. Your current method might work, but it's lazy, inefficient and can cause problems when there are traces of your plugin somewhere which will result in multiple different plugin instances.