Enable a plugin with another plugin

Discussion in 'Plugin Development' started by Endro32, Jan 8, 2014.

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

    Endro32

    I'm very new to Bukkit plugin Development, and ran into a situation I've had before in Java programming. I never really found the solution however, but now I need it. I have a map voting plugin that needs to start the mini-game plugin when the players are teleported to the map. Is there a way to have the Map Voting plugin call or enable the Mini-game plugin?
     
  2. Offline

    xigsag

    As for loading and unloading a plugin, you could search up plugins like that and take a peek at their source code using this.

    For your convenience, I'll paste it here for you.
    Code:java
    1. import java.io.File;
    2. import java.lang.reflect.Field;
    3. import java.util.Iterator;
    4. import java.util.List;
    5. import java.util.Map;
    6. import java.util.SortedSet;
    7. import java.util.logging.Level;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.command.PluginCommand;
    13. import org.bukkit.command.SimpleCommandMap;
    14. import org.bukkit.event.Event;
    15. import org.bukkit.plugin.InvalidDescriptionException;
    16. import org.bukkit.plugin.InvalidPluginException;
    17. import org.bukkit.plugin.Plugin;
    18. import org.bukkit.plugin.PluginDescriptionFile;
    19. import org.bukkit.plugin.PluginManager;
    20. import org.bukkit.plugin.RegisteredListener;
    21. import org.bukkit.plugin.UnknownDependencyException;
    22.  
    23. public class Utils {
    24. public PluginReload plugin;
    25.  
    26. private static final String SPECIFY_PLUGIN = ChatColor.RED + "Must specify a plugin!";
    27. private static final String PLUGIN_NOT_FOUND = ChatColor.RED + "Plugin not found!";
    28.  
    29. public Utils(PluginReload instance) {
    30. plugin = instance;
    31. }
    32.  
    33. public Plugin getPlugin(String p) {
    34. for (Plugin pl : plugin.getServer().getPluginManager().getPlugins()) {
    35. if (pl.getDescription().getName().equalsIgnoreCase(p)) {
    36. return pl;
    37. }
    38. }
    39. return null;
    40. }
    41.  
    42. private String consolidateArgs(String[] args) {
    43. String pl = args[1];
    44. if (args.length > 2) {
    45. for (int i = 2; i < args.length; i++) {
    46. pl = pl + " " + args[i];
    47. }
    48. }
    49. return pl;
    50. }
    51.  
    52. public String loadPlugin(String pl) {
    53. Plugin targetPlugin = null;
    54. String msg = "";
    55. File pluginDir = new File("plugins");
    56. if (!pluginDir.isDirectory()) {
    57. return (ChatColor.RED + "Plugin directory not found!");
    58. }
    59. File pluginFile = new File(pluginDir, pl + ".jar");
    60. // plugin.getLogger().info("Want: " + pluginFile);
    61. if (!pluginFile.isFile()) {
    62. for (File f : pluginDir.listFiles()) {
    63. try {
    64. if (f.getName().endsWith(".jar")) {
    65. PluginDescriptionFile pdf = plugin.getPluginLoader().getPluginDescription(f);
    66. // plugin.getLogger().info("Searching for " + pl + ": " + f + " -> " + pdf.getName());
    67. if (pdf.getName().equalsIgnoreCase(pl)) {
    68. pluginFile = f;
    69. msg = "(via search) ";
    70. break;
    71. }
    72. }
    73. } catch (InvalidDescriptionException e) {
    74. return (ChatColor.RED + "Couldn't find file and failed to search descriptions!");
    75. }
    76. }
    77. }
    78. try {
    79. plugin.getServer().getPluginManager().loadPlugin(pluginFile);
    80. targetPlugin = getPlugin(pl);
    81. targetPlugin.onLoad();
    82. plugin.getServer().getPluginManager().enablePlugin(targetPlugin);
    83. return (ChatColor.GREEN + "" + getPlugin(pl) + " loaded " + msg + "and enabled!");
    84. } catch (UnknownDependencyException e) {
    85. return (ChatColor.RED + "File exists, but is missing a dependency!");
    86. } catch (InvalidPluginException e) {
    87. plugin.getLogger().log(Level.SEVERE, "Tried to load invalid Plugin.\n", e);
    88. return (ChatColor.RED + "File exists, but isn't a loadable plugin file!");
    89. } catch (InvalidDescriptionException e) {
    90. return (ChatColor.RED + "Plugin exists, but has an invalid description!");
    91. }
    92. }
    93.  
    94. public String unloadPlugin(String pl) {
    95.  
    96. PluginManager pm = plugin.getServer().getPluginManager();
    97. SimpleCommandMap cmdMap = null;
    98. List<Plugin> plugins = null;
    99. Map<String, Plugin> names = null;
    100. Map<String, Command> commands = null;
    101. Map<Event, SortedSet<RegisteredListener>> listeners = null;
    102. boolean reloadlisteners = true;
    103. if (pm != null) {
    104. try {
    105. Field pluginsField = plugin.getServer().getPluginManager().getClass().getDeclaredField("plugins");
    106. pluginsField.setAccessible(true);
    107. plugins = (List<Plugin>) pluginsField.get(pm);
    108.  
    109. Field lookupNamesField = plugin.getServer().getPluginManager().getClass().getDeclaredField("lookupNames");
    110. lookupNamesField.setAccessible(true);
    111. names = (Map<String, Plugin>) lookupNamesField.get(pm);
    112.  
    113. try {
    114. Field listenersField = plugin.getServer().getPluginManager().getClass().getDeclaredField("listeners");
    115. listenersField.setAccessible(true);
    116. listeners = (Map<Event, SortedSet<RegisteredListener>>) listenersField.get(pm);
    117. } catch (Exception e) {
    118. reloadlisteners = false;
    119. }
    120.  
    121. Field commandMapField = plugin.getServer().getPluginManager().getClass().getDeclaredField("commandMap");
    122. commandMapField.setAccessible(true);
    123. cmdMap = (SimpleCommandMap) commandMapField.get(pm);
    124.  
    125. Field knownCommandsField = cmdMap.getClass().getDeclaredField("knownCommands");
    126. knownCommandsField.setAccessible(true);
    127. commands = (Map<String, Command>) knownCommandsField.get(cmdMap);
    128. } catch (NoSuchFieldException e) {
    129. return (ChatColor.RED + "Failed to unload plugin!");
    130. } catch (IllegalAccessException e) {
    131. return (ChatColor.RED + "Failed to unload plugin!");
    132. }
    133. }
    134.  
    135. String tp = "";
    136. for (Plugin p : plugin.getServer().getPluginManager().getPlugins()) {
    137. if (p.getDescription().getName().equalsIgnoreCase(pl)) {
    138. pm.disablePlugin(p);
    139. tp += p.getName() + " ";
    140. if (plugins != null && plugins.contains(p)) {
    141. plugins.remove(p);
    142. }
    143.  
    144. if (names != null && names.containsKey(pl)) {
    145. names.remove(pl);
    146. }
    147.  
    148. if (listeners != null && reloadlisteners) {
    149. for (SortedSet<RegisteredListener> set : listeners.values()) {
    150. for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext();) {
    151. RegisteredListener value = it.next();
    152.  
    153. if (value.getPlugin() == p) {
    154. it.remove();
    155. }
    156. }
    157. }
    158. }
    159.  
    160. if (cmdMap != null) {
    161. for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext();) {
    162. Map.Entry<String, Command> entry = it.next();
    163. if (entry.getValue() instanceof PluginCommand) {
    164. PluginCommand c = (PluginCommand) entry.getValue();
    165. if (c.getPlugin() == p) {
    166. c.unregister(cmdMap);
    167. it.remove();
    168. }
    169. }
    170. }
    171. }
    172. }
    173. }
    174. return (ChatColor.GREEN + tp + "has been unloaded and disabled!");
    175. }
    176.  
    177. }[/i]


    This is my version of the original, Util.class.
     
  3. Offline

    Endro32

    Thanks, and by the way, I wrote the plugins. So how would I tell bukkit to not load the mini-game plugin, so that it can be enabled for the first time later on?
     
  4. Offline

    xigsag

    (Sorry for double-posting. It'd screw up the formatting of the code.)
    As for calling methods from another plugin, or at least that's what I think you're referring to, you will need to include it as a reference library in your project, the same way you include bukkit.jar and craftbukkit.jar as reference libraries.

    In case you don't know how that works, you are basically referencing the classes and methods from that .jar file.
    public non-static methods will have to be called the same way you would with an actual class from your project workspace,
    public static methods can be called the same way you would with an actual class from your project workspace, interfaces can be used the same way too.

    #EDIT: Just in case you got confused as to what 'PluginReload' is, its the main class of my plugin. So you'd want to replace that with the main class of your plugin.
     
  5. Offline

    Endro32

    I thought referencing classes from another jar would import those classes into your project, but okay. Thanks for the help! :)
     
  6. Offline

    xigsag

    You could disable the plugin with your other plugin using the Util.class that I sourced you. Then, enable it when you want to, by using the same Util.class.
    No problem. It does import those classes into your project. Sorry if my explanation was a little confusing, but I guess you already know what that does, so I don't think I need to explain that to you anymore.
     
  7. Offline

    Endro32

    I had one other problem: When I type Plugin.meth(); for example, it says it can't make a static reference to a non-static method. But when I create a reference variable, Bukkit gives me errors saying that one of the plugins has already been initialized. I don't know exactly which one it's talking about, but I'm assuming that creating a reference to it is reloading the plugin? Any Ideas?
     
  8. Offline

    xTrollxDudex

    Endro32
    You could've just done
    PHP:
    Bukkit.getServer().getPluginManager().enablePlugin(Bukkit.getServer().getPluginManager().getPlugin("PLUGIN NAME"));
     
    Panjab likes this.
  9. Offline

    xigsag

    Endro32 You can check if that second plugin which you want disabled at start is enabled or not. If it isn't, then enable it.

    I'm not too sure about that, but when you create a new instance, yeah, it might enable it. I'm not on the computer right now, so I can't give you much or any code. Just to make sure, you DID disable it on start-up by checking if the plugin is present, then disabling it?
     
  10. Offline

    Endro32

    No, but I will definitely do that. Like I said, I literally started coding Bukkit Plugins like a couple days ago
     
  11. Offline

    Panjab

    Endro32

    Just do it this way..?
     
  12. Offline

    Endro32

    Disable the other plugin on startup
     
  13. Offline

    Cirno

    A bit of a stupid trick, but it works (I think?):

    Rename the extension to something else (prevents plugin from loading on startup).
    Call loadPlugin to load the plugin file.
     
Thread Status:
Not open for further replies.

Share This Page