Reloading a PLUGIN

Discussion in 'Plugin Development' started by CaLxCyMru, Sep 15, 2013.

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

    CaLxCyMru

    Hello!

    I was wondering if I can create a command that reloads the actual .jar of the plugin - Not the config file.

    Here is the code I have.
    Code:java
    1. if(commandLabel.equalsIgnoreCase("umsfreload")) {
    2. if (args.length > 0) {
    3. sender.sendMessage(prefix + ChatColor.RED + "Syntax Error!\nUsage: /umsfreload ");
    4. return false;
    5. }
    6. if (sender instanceof Player) {
    7.  
    8. if (player.hasPermission(new Permissions().umsfreload)) {
    9. plugin.getPluginLoader().disablePlugin(plugin);
    10. plugin.getPluginLoader().enablePlugin(plugin);
    11. sender.sendMessage(ChatColor.GREEN + "Reloaded "+ ChatColor.GRAY + "UnitedMcSvrs - Factions!");
    12. } else {
    13. sender.sendMessage(prefix + ChatColor.RED + "You do not have access to that.");
    14. }
    15. } else {
    16. plugin.getPluginLoader().disablePlugin(plugin);
    17. plugin.getPluginLoader().enablePlugin(plugin);
    18. sender.sendMessage(prefix + ChatColor.GREEN + "Reloaded "+ ChatColor.GRAY + "UnitedMcSvrs - Factions!");
    19. }
    20. return true;
    21. }

    I have tried

    Code:java
    1. plugin.getPluginLoader().disablePlugin(plugin);
    2. plugin.getPluginLoader().enablePlugin(plugin);

    But it throws off a NPE.
    [​IMG]

    If anyone can tell me where I am going wrong that would be great!
     
  2. Offline

    Bammerbom

    CaLxCyMru
    getServer().getPluginManager().disablePlugin()
     
  3. Offline

    Shevchik

    I'm currently testing it, but looks like this code will work.
    I'm using this in development version of one of my plugins
    Code:java
    1.  
    2. PluginManager pluginmanager = Bukkit.getPluginManager();
    3. Class<? extends PluginManager> managerclass = pluginmanager.getClass();
    4. //disable plugin
    5. pluginmanager.disablePlugin(plugin);
    6. //remove from plugins field
    7. Field pluginsField = managerclass.getDeclaredField("plugins");
    8. pluginsField.setAccessible(true);
    9. List<Plugin> plugins = (List<Plugin>) pluginsField.get(pluginmanager);
    10. plugins.remove(plugin);
    11. //remove from lookupnames
    12. Field lookupNamesField = managerclass.getDeclaredField("lookupNames");
    13. lookupNamesField.setAccessible(true);
    14. Map<String, Plugin> lookupNames = (Map<String, Plugin>) lookupNamesField.get(pluginmanager);
    15. lookupNames.remove(plugin.getName());
    16. //remove from command fields
    17. Field commandMapField = managerclass.getDeclaredField("commandMap");
    18. commandMapField.setAccessible(true);
    19. CommandMap commandMap = (CommandMap) commandMapField.get(pluginmanager);
    20. Field knownCommandsField = null;
    21. Map<String, Command> knownCommands = null;
    22. knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
    23. knownCommandsField.setAccessible(true);
    24. knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
    25. for (String plugincommandName : new HashSet<String>(knownCommands.keySet()))
    26. {
    27. if (knownCommands.get(plugincommandName) instanceof PluginCommand)
    28. {
    29. PluginCommand plugincommand = (PluginCommand) knownCommands.get(plugincommandName);
    30. if (plugincommand.getPlugin().getName().equals(plugin.getName()))
    31. {
    32. plugincommand.unregister(commandMap);
    33. knownCommands.remove(plugincommandName);
    34. }
    35. }
    36. }
    37. //load plugin
    38. File pluginfile = new File(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
    39. Plugin p = Bukkit.getPluginManager().loadPlugin(pluginfile);
    40. //enable plugin
    41. pluginmanager.enablePlugin(p);
    42.  
     
Thread Status:
Not open for further replies.

Share This Page