Adding a command which disables/enables a plugin

Discussion in 'Plugin Development' started by Vidsify, Aug 26, 2014.

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

    Vidsify

    Hi

    I'm wanting to know how you would add a command which disables the plugin until you reenable it. If I wanted this plugin disabled I would do /durexp toggle which disables it but when I do that command again it reenables it. You would only be able to do this if you have the permission DoubleurEXP.use.

    Also this isn't related to this thread but how would you add a custom config where the player specifies what type of multipliers they want but at the same time it creates custom permissions eg DoubleurEXP.multiplier.<number>
    Have a look at my plugin to see what I'm on about. http://dev.bukkit.org/bukkit-plugins/double-your-experience/

    Thanks in advance
     
  2. Offline

    wptcraft

    Im not sure but i think this might work im quite new to Bukkit
    Code:java
    1. //IF the command equals disable, Plugin
    2. //then do
    3. getServer().getPluginManager().disablePlugin(Plugin);
    4. //If command equals eneable, Plugin
    5. getServer().getPluginManager().enablePlugin(plugin)
    6.  

    I think this will work,
     
  3. Offline

    TheMcScavenger

    Question 1:
    Code:java
    1. void enablePlugin(Plugin plugin)

    http://jd.bukkit.org/dev/apidocs/or...r.html#enablePlugin(org.bukkit.plugin.Plugin)
    Code:java
    1. void disablePlugin(Plugin plugin)

    http://jd.bukkit.org/rb/apidocs/org....html#disablePlugin(org.bukkit.plugin.Plugin)

    Question 2:
    Permission nodes are strings, meaning you can create them using variables. So:
    Code:java
    1. String s = getConfig().getString(path);
    2. int i = getConfig().getInt(path);
    3.  
    4. if(player.hasPermission("my.permission." + s){
    5.  
    6. }
    7.  
    8. if(player.hasPermission("my.permission." + i){
    9.  
    10. }
     
  4. Offline

    Vidsify

    wptcraft I'm quite new to bukkit to :p

    TheMcScavenger OK so how would I create a config? The config should look like this:

    key: <> changeable variables

    <name>:
    Multiplier: <numbers can include decimals>

    So the <name> would end up at the end of the permission node eg DoubleurEXP.multiplier.<name> and the multipler part would be what the exp is times by depending if they have the node.
     
  5. Offline

    TheOddPuff

    Code:java
    1. public static void enablePlugin(String pluginName) {
    2. PluginManager manager = Bukkit.getPluginManager();
    3. Plugin plugin = manager.getPlugin(pluginName);
    4. if(plugin != null && !plugin.isEnabled()) {
    5. manager.enablePlugin(plugin);
    6. }
    7. }


    Same goes for disabling plugins, just revert all 'enable' functions to 'disable'
     
  6. Offline

    Vidsify

    TheOddPuff How would I add that to a command?

    Here's my code:

    Code:java
    1. // Add custom multipliers
    2. // Add config for it
    3. // Add toggle commands
    4.  
    5. package me.Vidsify.DurEXP;
    6.  
    7. import java.io.IOException;
    8. import java.util.logging.Logger;
    9.  
    10.  
    11.  
    12.  
    13.  
    14. import net.milkbowl.vault.chat.Chat;
    15. import net.milkbowl.vault.economy.Economy;
    16. import net.milkbowl.vault.permission.Permission;
    17.  
    18.  
    19.  
    20.  
    21. //import org.bukkit.configuration.file.FileConfiguration;
    22. import org.bukkit.event.EventHandler;
    23. import org.bukkit.event.player.PlayerExpChangeEvent;
    24. import org.bukkit.plugin.PluginDescriptionFile;
    25. import org.bukkit.plugin.RegisteredServiceProvider;
    26. import org.bukkit.plugin.java.JavaPlugin;
    27.  
    28. public class DurEXP extends JavaPlugin {
    29. public final Logger logger = Logger.getLogger("Minecraft");
    30. public DurEXP plugin;
    31.  
    32. @Override
    33. public void onDisable() {
    34. PluginDescriptionFile pdfFile = this.getDescription();
    35. this.logger.info(pdfFile.getName() + " Has been Disabled!");
    36. }
    37.  
    38. @Override
    39. public void onEnable() {
    40. try {
    41. Metrics metrics = new Metrics(this);
    42. metrics.start();
    43. } catch (IOException e) {
    44. e.printStackTrace(); // Failed to submit the stats :-(
    45. }
    46. // initialiseConfig();
    47. PluginDescriptionFile pdfFile = this.getDescription();
    48. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    49. + " Has been Enabled!");
    50. setupPermissions();
    51. setupEconomy();
    52. setupChat();
    53. getLogger().info("Linked to Vault!");
    54.  
    55. }
    56.  
    57. public static Permission permission = null;
    58. public static Economy economy = null;
    59. public static Chat chat = null;
    60.  
    61. private boolean setupPermissions()
    62. {
    63. RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
    64. if (permissionProvider != null) {
    65. permission = permissionProvider.getProvider();
    66. }
    67. return (permission != null);
    68. }
    69.  
    70. private boolean setupChat()
    71. {
    72. RegisteredServiceProvider<Chat> chatProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
    73. if (chatProvider != null) {
    74. chat = chatProvider.getProvider();
    75. }
    76.  
    77. return (chat != null);
    78. }
    79.  
    80. private boolean setupEconomy()
    81. {
    82. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    83. if (economyProvider != null) {
    84. economy = economyProvider.getProvider();
    85. }
    86.  
    87. return (economy != null);
    88. }
    89.  
    90. // public void initialiseConfig() {
    91. // FileConfiguration config = getConfig();
    92. // config.addDefault("Setings.Enable", true);
    93. // config.options().copyDefaults(true);
    94. // saveConfig();
    95. // }
    96.  
    97. @EventHandler
    98. public void doubleExperience(PlayerExpChangeEvent e) {
    99. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.2 ")) {
    100. e.setAmount(e.getAmount() * 2);
    101. } else if (e.getPlayer().hasPermission("")) {
    102. e.setAmount(e.getAmount() * 1);
    103.  
    104. }
    105.  
    106. }
    107.  
    108. @EventHandler
    109. public void doubleExperience1(PlayerExpChangeEvent e) {
    110. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.3 ")) {
    111. e.setAmount(e.getAmount() * 3);
    112. } else if (e.getPlayer().hasPermission("")) {
    113. e.setAmount(e.getAmount() * 1);
    114. }
    115.  
    116. }
    117.  
    118. @EventHandler
    119. public void doubleExperience2(PlayerExpChangeEvent e) {
    120. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.4 ")) {
    121. e.setAmount(e.getAmount() * 4);
    122. } else if (e.getPlayer().hasPermission("")) {
    123. e.setAmount(e.getAmount() * 1);
    124. }
    125.  
    126. }
    127.  
    128. @EventHandler
    129. public void doubleExperience3(PlayerExpChangeEvent e) {
    130. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.5 ")) {
    131. e.setAmount(e.getAmount() * 5);
    132. } else if (e.getPlayer().hasPermission("")) {
    133. e.setAmount(e.getAmount() * 1);
    134. }
    135.  
    136. }
    137.  
    138. @EventHandler
    139. public void doubleExperience4(PlayerExpChangeEvent e) {
    140. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.6 ")) {
    141. e.setAmount(e.getAmount() * 6);
    142. } else if (e.getPlayer().hasPermission("")) {
    143. e.setAmount(e.getAmount() * 1);
    144. }
    145.  
    146. }
    147.  
    148. @EventHandler
    149. public void doubleExperience5(PlayerExpChangeEvent e) {
    150. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.7 ")) {
    151. e.setAmount(e.getAmount() * 7);
    152. } else if (e.getPlayer().hasPermission("")) {
    153. e.setAmount(e.getAmount() * 1);
    154. }
    155.  
    156. }
    157.  
    158. @EventHandler
    159. public void doubleExperience6(PlayerExpChangeEvent e) {
    160. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.8")) {
    161. e.setAmount(e.getAmount() * 8);
    162. } else if (e.getPlayer().hasPermission("")) {
    163. e.setAmount(e.getAmount() * 1);
    164. }
    165.  
    166. }
    167.  
    168. @EventHandler
    169. public void doubleExperience7(PlayerExpChangeEvent e) {
    170. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.9 ")) {
    171. e.setAmount(e.getAmount() * 9);
    172. } else if (e.getPlayer().hasPermission("")) {
    173. e.setAmount(e.getAmount() * 1);
    174. }
    175.  
    176. }
    177.  
    178. @EventHandler
    179. public void doubleExperience8(PlayerExpChangeEvent e) {
    180. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.10 ")) {
    181. e.setAmount(e.getAmount() * 10);
    182. } else if (e.getPlayer().hasPermission("")) {
    183. e.setAmount(e.getAmount() * 1);
    184. }
    185.  
    186. }
    187.  
    188. @EventHandler
    189. public void doubleExperience9(PlayerExpChangeEvent e) {
    190. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.half ")) {
    191. e.setAmount((int) (e.getAmount() * 0.5));
    192. } else if (e.getPlayer().hasPermission("")) {
    193. e.setAmount(e.getAmount() * 1);
    194. }
    195.  
    196. }
    197.  
    198. @EventHandler
    199. public void doubleExperience10(PlayerExpChangeEvent e) {
    200. if (e.getPlayer().hasPermission(" DoubleurEXP.multiplier.1.5 ")) {
    201. e.setAmount((int) (e.getAmount() * 1.5));
    202. } else if (e.getPlayer().hasPermission("")) {
    203. e.setAmount(e.getAmount() * 1);
    204. }
    205.  
    206. }
    207. }
     
  7. Offline

    TheOddPuff

    Vidsify Just put enablePlugin("nameOfPlugin"); on the place where you want to enable the plugin
     
  8. Offline

    Vidsify

    TheOddPuff I don't get it. I'm not to all the Plugin Manager and config stuff
     
  9. Offline

    TheMcScavenger

    You've got hundreds of different event handlers, that will each get called when an event fires, instead of putting it all in a single handler. I suggest you learn Java and the Bukkit API, watch tutorials, and then retry.
     
    Vidsify likes this.
  10. Offline

    Vidsify

    TheMcScavenger I have watch tutorials and before I made this plugin someone said it was best to put it all on separate handlers
     
  11. Offline

    FabeGabeMC

    Vidsify
    just do what TheOddPuff said on the command actions:
    enablePlugin(String pluginName);
    obviously like this:
    Code:java
    1. // your onCommand();
    2. if(cmd.getName().equalsIgnoreCase("yourcommand")) {
    3. enablePlugin("yourPlugin");
    4. //OR
    5. disablePlugin("yourPlugin");
    6. }
     
  12. Offline

    Necrodoom

    Sounds like youd rather need to learn java than watch these tutorials.
    Theres absolutely no reason to have different event handlers, copy-pasted for each permission node.
     
  13. Offline

    Vidsify

    Necrodoom I know the basics of Java I just need a hand with the API
     
  14. Offline

    Necrodoom

    Vidsify trying to pass a string without string quotations isn't a bukkit API related error.
     
Thread Status:
Not open for further replies.

Share This Page