Solved Can Someone please help me with this code?

Discussion in 'Plugin Development' started by xxCoderForLifexx, Feb 5, 2013.

Thread Status:
Not open for further replies.
  1. Code:java
    1. package me.Coderforlife.Disable;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.Plugin;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Me extends JavaPlugin {
    14.  
    15. final String prefix = ChatColor.RED + "[" +ChatColor.DARK_RED+ "DisableMe" + ChatColor.RED + "]";
    16. final String error = ChatColor.RED + "Error";
    17. Logger logger = Logger.getLogger("Minecraft");
    18. @Override
    19. public void onEnable() {
    20. PluginDescriptionFile pdffile = getDescription();
    21. this.logger.info(pdffile.getName() + ChatColor.RED +
    22. " Has Been Enabled." + "Version: " + pdffile.getVersion() + " Website: " + pdffile.getWebsite());
    23. }
    24. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    25. if(cmd.getName().equalsIgnoreCase("disableme")){
    26. if(sender instanceof Player){
    27. Player player = (Player) sender;
    28. if(player.hasPermission("disableme.help")){
    29.  
    30. }
    31. }
    32. }if(args.length == 1){
    33. if(sender instanceof Player){
    34. Player player = (Player) sender;
    35. if(player.hasPermission("disableme.disable.all")){
    36. if(args[0].equalsIgnoreCase("disable")){
    37. for (Plugin p : getServer().getPluginManager().getPlugins()) {
    38. if (!p.getName().equalsIgnoreCase("disableme")) {
    39. getServer().getPluginManager().disablePlugin(p);
    40. }
    41. }
    42. sender.sendMessage(prefix + ChatColor.RED+ " All Plugins disabled.");
    43. }else{
    44. if(sender instanceof Player){
    45. if(player.hasPermission("disableme.disable.plugin")){
    46. try {
    47. getServer().getPluginManager().disablePlugin(getServer().getPluginManager().getPlugin(args[1]));
    48. sender.sendMessage(prefix + ChatColor.RED + "The Plugin " + ChatColor.BOLD + args[1] + " is now disabled.");
    49. }
    50. catch (Exception exc) {
    51. sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + ChatColor.BOLD + args[1] + ChatColor.RED + " was not found!");
    52. }
    53. }else{
    54. sender.sendMessage(ChatColor.DARK_RED + "You Don't Have Permission To Do That!");
    55. }
    56. }else{
    57. try {
    58. getServer().getPluginManager().disablePlugin(getServer().getPluginManager().getPlugin(args[1]));
    59. sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.BOLD + args[1] + " is now disabled.");
    60. }
    61. catch (Exception exc) {
    62. sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + args[1] + ChatColor.RED + " was not found!");
    63. }
    64. }
    65.  
    66. }
    67.  
    68. }else{
    69. sender.sendMessage(ChatColor.DARK_RED + "You Don't Have Permission To Do That!");
    70. }
    71. }else{
    72. if(args[0].equalsIgnoreCase("disable")){
    73. for (Plugin p : getServer().getPluginManager().getPlugins()) {
    74. if (!p.getName().equalsIgnoreCase("disableme")) {
    75. getServer().getPluginManager().disablePlugin(p);
    76. }
    77. }
    78. }sender.sendMessage(prefix + ChatColor.RED+ " All Plugins disabled.");
    79.  
    80. }
    81. }else if(args.length == 1){
    82. if(sender instanceof Player){
    83. Player player = (Player) sender;
    84. if(player.hasPermission("disableme.enable.all")){
    85. if(args[0].equalsIgnoreCase("enable")){
    86. for (Plugin p : getServer().getPluginManager().getPlugins()) {
    87. if (!p.getName().equalsIgnoreCase("disableme")) {
    88. getServer().getPluginManager().enablePlugin(p);
    89. }
    90. }
    91. sender.sendMessage(prefix + ChatColor.RED+ " All Plugins enabled.");
    92. }
    93.  
    94. }else{
    95. sender.sendMessage(ChatColor.DARK_RED + "You Don't Have Permission To Do That!");
    96. }
    97. }else{
    98. if(args[0].equalsIgnoreCase("enable")){
    99. for (Plugin p : getServer().getPluginManager().getPlugins()) {
    100. if (!p.getName().equalsIgnoreCase("disableme")) {
    101. getServer().getPluginManager().enablePlugin(p);
    102. }
    103. }
    104. }sender.sendMessage(prefix + ChatColor.RED+ " All Plugins Enabled.");
    105.  
    106. }
    107. }else{
    108.  
    109. if(sender instanceof Player){
    110. Player player = (Player) sender;
    111. if(player.hasPermission("disableme.enable.plugin")){
    112. try {
    113. getServer().getPluginManager().enablePlugin(getServer().getPluginManager().getPlugin(args[1]));
    114. sender.sendMessage(prefix + ChatColor.RED + "The Plugin " + ChatColor.BOLD + args[1] + " is now enabled.");
    115. }
    116. catch (Exception exc) {
    117. sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + ChatColor.BOLD + args[1] + ChatColor.RED + " was not found!");
    118. }
    119. }else{
    120. sender.sendMessage(ChatColor.DARK_RED + "You Don't Have Permission To Do That!");
    121. }
    122. }else{
    123. try {
    124. getServer().getPluginManager().enablePlugin(getServer().getPluginManager().getPlugin(args[1]));
    125. sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.BOLD + args[1] + " is now enabled.");
    126. }
    127. catch (Exception exc) {
    128. sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + args[1] + ChatColor.RED + " was not found!");
    129. }
    130. }
    131. }
    132. return true;
    133. }
    134. }

    Now the problem is that when I use the console commands it only does the enable not the disable!
     
  2. xxCoderForLifexx
    Instead of checking instance of player for each command. Just do it once and reuse your player variable, there is no need to perform the casts multiple times, the sender object will not change.

    If you want your commands to be allowed to be sent from the console then just remove the instanceof checks or place the code to run for a server executed command in an else block.

    Code:java
    1.  
    2. if(sender instanceof Player){
    3. Player p = (Player) sender;
    4. //check for player commands
    5. } else {
    6. //check for console commands
    7. }
     
    AlexLeporiday likes this.
    • Lots of repeated code chunks
      • Create methods for repeated code and use them
    • Important checks are repeated over and over instead of called once
      • Do the check once, you can store the result in the variable
    • Doesn't follow a few Java naming conventions
      • Update the file name to match the plugin name (DisableMe)
    There are a number of errors here but I've managed to sift it all apart and come up with this:
    Code:
    package alexnet.disableme;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class DisableMe extends JavaPlugin {
     
        final String prefix = ChatColor.RED + "[" + ChatColor.DARK_RED
                + "DisableMe" + ChatColor.RED + "]";
        final String error = ChatColor.RED + "Error";
        Logger logger = Logger.getLogger("Minecraft");
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info(pdffile.getName() + ChatColor.RED + " Has Been Enabled." + "Version: " + pdffile.getVersion() + " Website: " + pdffile.getWebsite());
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("disableme")) {
                    if (player.hasPermission("disableme.help")) {
     
                    }
                } else if (cmdLabel.equalsIgnoreCase("disable")) {
                    if(args.length == 0 && player.hasPermission("disableme.disable.all")) {
                        disableAll();
                        sender.sendMessage(prefix + ChatColor.RED + " All Plugins disabled.");
                    } else if(args.length > 0 && player.hasPermission("disableme.disable")) {
                        if(disablePlugin(args[0])) {
                            sender.sendMessage(prefix + ChatColor.RED + "The Plugin " + ChatColor.BOLD + args[0] + " is now disabled.");
                        } else sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + ChatColor.BOLD + args[0] + ChatColor.RED + " was not found!");
                    } else sender.sendMessage(ChatColor.DARK_RED+ "You Don't Have Permission To Do That!");
                } else if (cmdLabel.equalsIgnoreCase("enable")) {
                    if(args.length == 0 && player.hasPermission("disableme.enable.all")) {
                        enableAll();
                        sender.sendMessage(prefix + ChatColor.RED + " All Plugins enabled.");
                    } else if(args.length > 0 && player.hasPermission("disableme.enable")) {
                        if(enablePlugin(args[0])) {
                            sender.sendMessage(prefix + ChatColor.RED + "The Plugin " + ChatColor.BOLD + args[0] + " is now enabled.");
                        } else sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + ChatColor.BOLD + args[0] + ChatColor.RED + " was not found!");
                    } else sender.sendMessage(ChatColor.DARK_RED+ "You Don't Have Permission To Do That!");
                }
            } else if (cmdLabel.equalsIgnoreCase("disable")) {
                if(args.length == 0) {
                    disableAll();
                    sender.sendMessage(prefix + ChatColor.RED + " All Plugins disabled.");
                } else if(args.length > 0) {
                    if(disablePlugin(args[0])) {
                        sender.sendMessage(prefix + ChatColor.RED + "The Plugin " + ChatColor.BOLD + args[0] + " is now disabled.");
                    } else sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + ChatColor.BOLD + args[0] + ChatColor.RED + " was not found!");
                }
            } else if (cmdLabel.equalsIgnoreCase("enable")) {
                if(args.length == 0) {
                    enableAll();
                    sender.sendMessage(prefix + ChatColor.RED + " All Plugins enabled.");
                } else if(args.length > 0) {
                    if(enablePlugin(args[0])) {
                        sender.sendMessage(prefix + ChatColor.RED + "The Plugin " + ChatColor.BOLD + args[0] + " is now enabled.");
                    } else sender.sendMessage(prefix + ChatColor.RED + " The Plugin " + ChatColor.GREEN + ChatColor.BOLD + args[0] + ChatColor.RED + " was not found!");
                }
            }
            return true;
        }
     
        private void enableAll() {
            for (Plugin p : getServer().getPluginManager().getPlugins()) {
                getServer().getPluginManager().enablePlugin(p);
            }
        }
     
        private boolean enablePlugin(String pluginName) {
            Plugin plugin = getServer().getPluginManager().getPlugin(pluginName);
            if(plugin != null) {
                getServer().getPluginManager().enablePlugin(plugin);
                return true;
            } else {
                return false;
            }
        }
     
        private void disableAll() {
            for (Plugin p : getServer().getPluginManager().getPlugins()) {
                if (!p.getName().equalsIgnoreCase("disableme")) {
                    getServer().getPluginManager().disablePlugin(p);
                }
            }
        }
     
        private boolean disablePlugin(String pluginName) {
            Plugin plugin = getServer().getPluginManager().getPlugin(pluginName);
            if(plugin != null) {
                getServer().getPluginManager().disablePlugin(plugin);
                return true;
            } else {
                return false;
            }
        }
    }
    Testing this and it works as long as your plugin.yml is up to date. It should look something like this: (You'll need to change the main to point to your package and not mine)
    Code:
    name: DisableMe
    main: alexnet.disableme.DisableMe
    version: 0.0
     
    commands:
      disable:
        description: Disables all plugins or a given plugin by name
        usage: /<command>
      enable:
        description: Enables all plugins or a given plugin by name
        usage: /<command>
     
  3. Thanks So Much I have a lot of work to do and this helps out a lot Thank you ;3
     
  4. This could still be massively improved but I just wanted to get you something that worked. I pointed out a few areas to improve next time and if you're feeling generous you could always give my response a like. ;3

    Another suggestion I have is that using a java editor rather than a text editor might provide better results. You might already be using one. By the look it it you may be using NetBeans (Because of every few characters being forced to a new ugly line) which is okay but certainly not my favorite.

    Best of luck to you!
     
  5. I will and can make a way more stable code when I have time and I use Eclipse but thanks for the code and everything
     
Thread Status:
Not open for further replies.

Share This Page