Reloading my plugin

Discussion in 'Plugin Development' started by mattibijnens, Jun 17, 2014.

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

    mattibijnens

    So I got this code (Parts of it did I get from another plugin) and it gives me an error, but everything seems fine to me =/
    (Error: http://pastebin.com/LGUc0RcG)
    Code:java
    1. package me.Matti.Sjabloon;
    2.  
    3. import java.io.File;
    4. import java.util.Map;
    5. import java.io.IOException;
    6. import java.lang.reflect.Field;
    7. import java.net.URLClassLoader;
    8. import java.util.Iterator;
    9. import java.util.List;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.PluginCommand;
    14. import org.bukkit.command.SimpleCommandMap;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.plugin.InvalidDescriptionException;
    17. import org.bukkit.plugin.InvalidPluginException;
    18. import org.bukkit.plugin.Plugin;
    19. import org.bukkit.plugin.SimplePluginManager;
    20. import org.bukkit.plugin.UnknownDependencyException;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22. import org.bukkit.plugin.java.JavaPluginLoader;
    23. import org.bukkit.ChatColor;
    24. import org.bukkit.command.CommandSender;
    25.  
    26.  
    27.  
    28. public class Reloader extends JavaPlugin implements Listener {
    29. private SimpleCommandMap scm;
    30. private Map<String, Command> kc;
    31. private Field loadersF;
    32. public void onEnable() {
    33. getServer().getPluginManager().registerEvents(this, this);
    34.  
    35. }
    36. public Plugin loadPlugin(String name)
    37. {
    38. try
    39. {
    40. return Bukkit.getServer().getPluginManager().loadPlugin(new File("plugins" + File.separator + name + ".jar"));
    41. }
    42. catch (InvalidPluginException|InvalidDescriptionException|UnknownDependencyException e)
    43. {
    44. e.printStackTrace();
    45. }
    46. return null;
    47. }
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59. @SuppressWarnings("unchecked")
    60. public boolean unloadPlugin(Plugin plugin)
    61. {
    62. try
    63. {
    64. plugin.getClass().getClassLoader().getResources("*");
    65. }
    66. catch (IOException e1)
    67. {
    68. e1.printStackTrace();
    69. }
    70. SimplePluginManager spm = (SimplePluginManager)Bukkit.getServer().getPluginManager();
    71. Map<String, Plugin> ln;
    72. List<Plugin> pl;
    73. try
    74. {
    75. Field lnF = spm.getClass().getDeclaredField("lookupNames");
    76. lnF.setAccessible(true);
    77. ln = (Map)lnF.get(spm);
    78.  
    79.  
    80. Field plF = spm.getClass().getDeclaredField("plugins");
    81. plF.setAccessible(true);
    82. pl = (List)plF.get(spm);
    83. }
    84. catch (Exception e)
    85. {
    86. e.printStackTrace();
    87. return false;
    88. }
    89. synchronized (this.scm)
    90. {
    91. Iterator<Map.Entry<String, Command>> it = this.kc.entrySet().iterator();
    92. while (it.hasNext())
    93. {
    94. Map.Entry<String, Command> entry = (Map.Entry)it.next();
    95. if ((entry.getValue() instanceof PluginCommand))
    96. {
    97. PluginCommand c = (PluginCommand)entry.getValue();
    98. if (c.getPlugin().getName().equalsIgnoreCase(plugin.getName()))
    99. {
    100. c.unregister(this.scm);
    101. it.remove();
    102. }
    103. }
    104. }
    105. }
    106. spm.disablePlugin(plugin);
    107. synchronized (spm)
    108. {
    109. ln.remove(plugin.getName());
    110. pl.remove(plugin);
    111. }
    112. JavaPluginLoader jpl = (JavaPluginLoader)plugin.getPluginLoader();
    113. if (this.loadersF == null) {
    114. try
    115. {
    116. this.loadersF = jpl.getClass().getDeclaredField("loaders");
    117. this.loadersF.setAccessible(true);
    118. }
    119. catch (Exception e)
    120. {
    121. e.printStackTrace();
    122. }
    123. }
    124. try
    125. {
    126. Map<String, ?> loaderMap = (Map)this.loadersF.get(jpl);
    127.  
    128. loaderMap.remove(plugin.getDescription().getName());
    129. }
    130. catch (Exception e)
    131. {
    132. e.printStackTrace();
    133. }
    134. closeClassLoader(plugin);
    135. System.gc();
    136. System.gc();
    137.  
    138. return true;
    139. }
    140. public boolean closeClassLoader(Plugin plugin)
    141. {
    142. try
    143. {
    144. ((URLClassLoader)plugin.getClass().getClassLoader()).close();
    145. return true;
    146. }
    147. catch (IOException e)
    148. {
    149. e.printStackTrace();
    150. }
    151. return false;
    152. }
    153. public File getFile(JavaPlugin p)
    154. {
    155. try
    156. {
    157. Field f = JavaPlugin.class.getDeclaredField("file");
    158. f.setAccessible(true);
    159. return (File)f.get(p);
    160. }
    161. catch (Exception e)
    162. {
    163. e.printStackTrace();
    164. }
    165. return null;
    166. }
    167. @Override
    168. public boolean onCommand(CommandSender sender, Command command,
    169. String label, String[] args) {
    170. if(label.equalsIgnoreCase("MattiReload")){
    171. String pName = "Pixelmon";
    172. Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pName);
    173. if (plugin == null)
    174. {
    175. sender.sendMessage(ChatColor.RED + "No such plugin: " + pName);
    176. }
    177. else
    178. {
    179. File file = getFile((JavaPlugin)plugin);
    180. JavaPlugin loaded = null;
    181. if (file == null)
    182. {
    183. sender.sendMessage(ChatColor.RED + plugin.getName() + "'s jar file is missing!");
    184. return true;
    185. }
    186. String fname = file.getName().substring(0, file.getName().length() - 4);
    187.  
    188.  
    189. if (!unloadPlugin(plugin)) {
    190. sender.sendMessage(ChatColor.RED + "An error occurred while unloading " + pName + "!");
    191. } else if ((loaded = (JavaPlugin)loadPlugin(fname)) == null) {
    192. sender.sendMessage(ChatColor.RED + "Failed to load " + fname + "!" + (sender != Bukkit.getConsoleSender() ? "Check console for details!" : ""));
    193. }
    194. Bukkit.getServer().getPluginManager().enablePlugin(loaded);
    195. sender.sendMessage(ChatColor.GREEN + loaded.getDescription().getName() + " reloaded successfully.");
    196.  
    197. }
    198. return true;
    199. }
    200.  
    201. return false;
    202. }
    203. }
     
  2. mattibijnens Something tells me that you're not ready to be doing this sort of thing.
     
    werter318 and Konkz like this.
  3. Offline

    mattibijnens

    AdamQpzm That's not really helping ...
     
  4. mattibijnens Well it's honest. Tell us what you want to do exactly, and maybe we can help more.
     
  5. Offline

    mattibijnens

    AdamQpzm I would like to reload my plugin, but because you can't reload a plugin itself, I made another one for that. I coded this and I don't understand why it is giving this error.
     
  6. What parts of it need to be reloaded, though? Because, chances are, you really don't need to do this, especially if you can't understand the error you're getting.
     
  7. Offline

    theguynextdoor

    mattibijnens Something is null on the line
    Code:java
    1. Iterator<Map.Entry<String, Command>> it = this.kc.entrySet().iterator();


    I can see what. Can you?
     
  8. Offline

    mattibijnens

    AdamQpzm I am reloading it beacuse I am constantly updating it

    theguynextdoor I don't, unless you didn't see
    Code:java
    1. private Map<String, Command> kc;


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  9. Offline

    theguynextdoor

    mattibijnens That's exactly what I saw. If you know java, you know your mistake.
     
  10. Offline

    mattibijnens

  11. Offline

    werter318

    mattibijnens Didn't read the code because AdamQpzm is totally right, but what theguynextdoor means is that kc is null, you have to do: private Map<String, Command> kc = new HashMap<String, Command>();
     
  12. Offline

    gabizou

    mattibijnens You're never assigning kc to anything, therefor it's null.
     
  13. mattibijnens That's the problem with copying code - you miss parts, which can easily cause problems. Again, what parts are you reloading? Because config can be reloaded with a simple method call, plugin variables can be changed/reset, tasks can be cancelled... Off the top of my head, I've no idea why you would need to physically unload and then load the plugin, insomuch that not even enablePlugin() and disablePlugin() would work.
     
  14. Offline

    teej107

    If you want to do a full reload of just your plugin, just execute the same code that you use in your onEnable() method. I don't know if calling the onEnable() method again would cause errors not related to your code but I would assume not.
     
  15. Offline

    mattibijnens

    As I already said before, I am making this because I am constantly updating the plugin, but I don't want to reload the whole server each time.
    And Thanks werter318 =)
     
Thread Status:
Not open for further replies.

Share This Page