Plugin Help

Discussion in 'Plugin Development' started by BDKing88, Jul 8, 2013.

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

    BDKing88

    Hey everybody, I'm making a plugin, but when I test it, only 2 of the three commands work. I am 100% sure I did the plugin.yml right. I will be so grateful if someone could help! I've made plugins before, but that was a long time ago, and I'm a little rusty. Thanks in advance! Here's my code:
    Code:java
    1. package BDKing88;
    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.event.Listener;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.potion.PotionEffect;
    13. import org.bukkit.potion.PotionEffectType;
    14.  
    15. public class SimpleBlind extends JavaPlugin implements Listener{
    16.  
    17. public SimpleBlind plugin;
    18.  
    19. Logger log = Logger.getLogger("Minecraft");
    20.  
    21. public void onEnable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.log.info("[" + pdfFile.getName() + "] " + pdfFile.getVersion() + " has been enabled.");
    24. }
    25.  
    26. public void onDisable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.log.info("[" + pdfFile.getName() + "] " + pdfFile.getVersion() + " has been disabled.");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    32.  
    33.  
    34. Player s = (Player) sender;
    35.  
    36. if(cmd.getName().equalsIgnoreCase("blind")) {
    37. if(args.length == 0) {
    38. s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /blind [player]");
    39.  
    40. } else {
    41.  
    42. if(args.length == 1)
    43. if(sender.hasPermission("sb.blind")) {
    44. sender.sendMessage(ChatColor.GREEN + "Player blinded!");
    45. Player targetPlayer = s.getServer().getPlayer(args[1]);
    46. targetPlayer.sendMessage(ChatColor.GREEN + "You have been blinded by " + ChatColor.GOLD + sender.getName());
    47. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 5000, 1));
    48.  
    49. } else {
    50.  
    51. if(cmd.getName().equalsIgnoreCase("unblind")) {
    52. if(args.length == 0) {
    53. s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /unblind [player]");
    54.  
    55. } else {
    56.  
    57.  
    58. if(args.length == 1){
    59. if(sender.hasPermission("sb.unblind")) {
    60. sender.sendMessage(ChatColor.GREEN + "Player unblinded!");
    61. Player targetPlayer = s.getServer().getPlayer(args[1]);
    62. targetPlayer.sendMessage(ChatColor.GREEN + "You have been unblinded by " + ChatColor.GOLD + sender.getName());
    63. targetPlayer.getActivePotionEffects().clear();
    64. }
    65.  
    66. if (cmd.getName().equalsIgnoreCase("simpleblind")) {
    67. sender.sendMessage(ChatColor.GREEN + " -=[SimpleBlind] Commands/Info=-");
    68. sender.sendMessage(ChatColor.GREEN + "/blind [Player] - Blinds the player.");
    69. sender.sendMessage(ChatColor.GREEN + "/unblind [Player] - Unblinds the player.");
    70. sender.sendMessage(ChatColor.GREEN + "/simpleblind - Shows this message again.");
    71. sender.sendMessage(ChatColor.GREEN + "-----------------------------------------------------");
    72. sender.sendMessage(ChatColor.YELLOW + " SimpleBlind was created by BDKing88.");
    73. return true;
    74.  
    75.  
    76.  
    77. }
    78.  
     
  2. Offline

    Vandrake

    did you add the command to the plugin.yml and refresh the project?
     
  3. Offline

    BDKing88

    Yes, I added all the commands and all the perms. I did the right spacing and everything, and yes, I did refresh the project.
     
  4. Offline

    Bammerbom

    BDKing88 What is the error of the third command? Uknown Command?
    I also give you a tip. Use spaces in your code. It is difficult to read now.
    What is the command that is not working?
     
  5. Offline

    BDKing88

    No text comes up, nothing happens. The ones that work: /blind and /simpleblind. The command that doesent work /unblind. I have no idea why. :/
     
  6. Offline

    Tim4209

    I think u have the brackets wrong i added a few see if it works like this

    Code:
        package BDKing88;
       
        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.event.Listener;
        import org.bukkit.plugin.PluginDescriptionFile;
        import org.bukkit.plugin.java.JavaPlugin;
        import org.bukkit.potion.PotionEffect;
        import org.bukkit.potion.PotionEffectType;
       
        public class SimpleBlind extends JavaPlugin implements Listener{
       
        public SimpleBlind plugin;
       
        Logger log = Logger.getLogger("Minecraft");
       
        public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.log.info("[" + pdfFile.getName() + "] " + pdfFile.getVersion() + " has been enabled.");
        }
       
        public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.log.info("[" + pdfFile.getName() + "] " + pdfFile.getVersion() + " has been disabled.");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
       
       
        Player s = (Player) sender;
       
        if(cmd.getName().equalsIgnoreCase("blind")) {
            if(args.length == 0) {
                s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /blind [player]");
            }
            else {
                if(args.length == 1) {
                    if(sender.hasPermission("sb.blind")) {
                        sender.sendMessage(ChatColor.GREEN + "Player blinded!");
                        Player targetPlayer = s.getServer().getPlayer(args[1]);
                        targetPlayer.sendMessage(ChatColor.GREEN + "You have been blinded by " + ChatColor.GOLD + sender.getName());
                        targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 5000, 1));
                    }
                }
            else {
           
            if(cmd.getName().equalsIgnoreCase("unblind")) {
            if(args.length == 0) {
            s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /unblind [player]");
       
        } else {
       
       
        if(args.length == 1){
        if(sender.hasPermission("sb.unblind")) {
        sender.sendMessage(ChatColor.GREEN + "Player unblinded!");
        Player targetPlayer = s.getServer().getPlayer(args[1]);
        targetPlayer.sendMessage(ChatColor.GREEN + "You have been unblinded by " + ChatColor.GOLD + sender.getName());
        targetPlayer.getActivePotionEffects().clear();
        }
       
        if (cmd.getName().equalsIgnoreCase("simpleblind")) {
        sender.sendMessage(ChatColor.GREEN + " -=[SimpleBlind] Commands/Info=-");
        sender.sendMessage(ChatColor.GREEN + "/blind [Player] - Blinds the player.");
        sender.sendMessage(ChatColor.GREEN + "/unblind [Player] - Unblinds the player.");
        sender.sendMessage(ChatColor.GREEN + "/simpleblind - Shows this message again.");
        sender.sendMessage(ChatColor.GREEN + "-----------------------------------------------------");
        sender.sendMessage(ChatColor.YELLOW + " SimpleBlind was created by BDKing88.");
        return true;
       
       
       
        }
     
  7. Offline

    BDKing88

    Now /unblind and /simpleblind don't work, and /blind gives an internal error.
     
  8. Offline

    Bammerbom

    BDKing88 Use debugging and check where it is wrong.
    plugin.yml is correct because the error is not: "unkown command"

    BDKing88 I think i now the error.
    Change:
    Code:java
    1. if(sender.hasPermission("sb.blind")) {

    To:
    Code:java
    1. if(sender.hasPermission("sb.blind") || sender.isOp() == true){


    And add a else for the permission check because the otherways happens nothing if the player has no permissions.

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

    Tim4209

    Try like this and u can set the permissions in the yml file

    Code:
     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (cmd.getName().equalsIgnoreCase("blind"))
                    return commandBlind(sender, args);
                if (cmd.getName().equalsIgnoreCase("unblind"))
                    return commandUnblind(sender, args);
                if (cmd.getName().equalsIgnoreCase("simpleblind"))
                    return commandSimpleblind(sender, args);
                return true;
            }
               
               
            private boolean commandBlind(CommandSender sender, String[] args) {
              sender.sendMessage(ChatColor.GREEN + "Player blinded!");
              Player targetPlayer = s.getServer().getPlayer(args[1]);
              targetPlayer.sendMessage(ChatColor.GREEN + "You have been blinded by " + ChatColor.GOLD + sender.getName());
              targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 5000, 1));
              return true;
            }
           
            private boolean commandUnblind(CommandSender sender, String[] args) {
                sender.sendMessage(ChatColor.GREEN + "Player unblinded!");
                Player targetPlayer = s.getServer().getPlayer(args[1]);
                targetPlayer.sendMessage(ChatColor.GREEN + "You have been unblinded by " + ChatColor.GOLD + sender.getName());
                targetPlayer.getActivePotionEffects().clear();
                return true;
            }
           
            private boolean commandSimpleblind(CommandSender sender, String[] args) {
                sender.sendMessage(ChatColor.GREEN + " -=[SimpleBlind] Commands/Info=-");
                sender.sendMessage(ChatColor.GREEN + "/blind [Player] - Blinds the player.");
                sender.sendMessage(ChatColor.GREEN + "/unblind [Player] - Unblinds the player.");
                sender.sendMessage(ChatColor.GREEN + "/simpleblind - Shows this message again.");
                sender.sendMessage(ChatColor.GREEN + "-----------------------------------------------------");
                sender.sendMessage(ChatColor.YELLOW + " SimpleBlind was created by BDKing88.");
                return true;
            }
    
     
  10. Offline

    Bammerbom

    Tim4209 That is the difficult way of debug.
     
  11. Offline

    Tim4209

    I make all my small plugins similar to that, for big plugins i put every command in its own class.
     
  12. Offline

    BDKing88

    Yea, the entire thing is screwed up. None of the commands work. So which persons advice should I follow?
     
  13. Offline

    mcoder

    Well, there are a few things in your code which is / can be making trouble.

    First, Tim4209 was right. You are missing quite a few brackets, while you also have some in places you shouldn't. I took the freedom to organize this a little, here it is:

    Code:java
    1. package BDKing88;
    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.event.Listener;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.potion.PotionEffect;
    13. import org.bukkit.potion.PotionEffectType;
    14.  
    15. public class SimpleBlind extends JavaPlugin implements Listener{
    16.  
    17. public SimpleBlind plugin;
    18.  
    19. Logger log = Logger.getLogger("Minecraft");
    20.  
    21. public void onEnable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.log.info("[" + pdfFile.getName() + "] " + pdfFile.getVersion() + " has been enabled.");
    24. }
    25.  
    26. public void onDisable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.log.info("[" + pdfFile.getName() + "] " + pdfFile.getVersion() + " has been disabled.");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    32.  
    33. Player s = (Player) sender;
    34.  
    35. if (cmd.getName().equalsIgnoreCase("blind")) {
    36.  
    37. if (args.length == 0) {
    38. s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /blind [player]");
    39. }
    40.  
    41. else if (args.length == 1) {
    42.  
    43. if (sender.hasPermission("sb.blind")) {
    44. sender.sendMessage(ChatColor.GREEN + "Player blinded!");
    45. Player targetPlayer = s.getServer().getPlayer(args[1]);
    46. targetPlayer.sendMessage(ChatColor.GREEN + "You have been blinded by " + ChatColor.GOLD + sender.getName());
    47. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 5000, 1));
    48. }
    49. }
    50. }
    51.  
    52. else if(cmd.getName().equalsIgnoreCase("unblind")) {
    53.  
    54. if(args.length == 0) {
    55. s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /unblind [player]");
    56.  
    57. } else if(args.length == 1){
    58.  
    59. if(sender.hasPermission("sb.unblind")) {
    60. sender.sendMessage(ChatColor.GREEN + "Player unblinded!");
    61. Player targetPlayer = s.getServer().getPlayer(args[1]);
    62. targetPlayer.sendMessage(ChatColor.GREEN + "You have been unblinded by " + ChatColor.GOLD + sender.getName());
    63. targetPlayer.getActivePotionEffects().clear();
    64. }
    65. }
    66. }
    67.  
    68. else if (cmd.getName().equalsIgnoreCase("simpleblind")) {
    69. sender.sendMessage(ChatColor.GREEN + " -=[SimpleBlind] Commands/Info=-");
    70. sender.sendMessage(ChatColor.GREEN + "/blind [Player] - Blinds the player.");
    71. sender.sendMessage(ChatColor.GREEN + "/unblind [Player] - Unblinds the player.");
    72. sender.sendMessage(ChatColor.GREEN + "/simpleblind - Shows this message again.");
    73. sender.sendMessage(ChatColor.GREEN + "-----------------------------------------------------");
    74. sender.sendMessage(ChatColor.YELLOW + " SimpleBlind was created by BDKing88.");
    75. return true;
    76. }
    77.  
    78. return false;
    79. }
    80. }


    If you adapt your code to look exactly like this, all 3 commands should have some effect. Either they work, or they give an "Internal Error"-Message - which is at least something :)

    Try to change it, recompile it and see if now all 3 commands do something - either a chat display or an error message.

    Once you got that, we can look into the other errors :)
     
  14. Offline

    epicfacecreeper

    I noticed you had no else clause checking for more arguments on unblind. Are you sure you are sending it with 0 or 1 args?
     
  15. Offline

    BDKing88

    1, doesent that mean /unblind [player] ?

    Ok, I recoded it, it's neater and I fixed the brackets, but it still doesent work. code:
    Code:java
    1. package BDKing88;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5.  
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.potion.PotionEffect;
    14. import org.bukkit.potion.PotionEffectType;
    15.  
    16. public class SimpleB extends JavaPlugin {
    17.  
    18. public SimpleB plugin;
    19.  
    20. Logger log = Logger.getLogger("Minecraft");
    21.  
    22. public void onDisable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.log.info("[" + pdfFile.getName() + "]" + " has been disabled");
    25. }
    26.  
    27. public void onEnable() {
    28. PluginDescriptionFile pdfFile = this.getDescription();
    29. this.log.info("[" + pdfFile.getName() + "]" + " has been enabled");
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    33. Player s = (Player) sender;
    34. if(cmd.getName().equalsIgnoreCase("blind")) {
    35. if((!(sender instanceof Player) && args.length == 0)) {
    36. s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /blind [Player]");
    37. }
    38.  
    39. if(args.length == 1) {
    40. if(s.hasPermission("sb.blind") || sender.isOp() == true) {
    41. s.sendMessage(ChatColor.GREEN + "Player blinded!");
    42. Player targetPlayer = s.getServer().getPlayer(args[1]);
    43. targetPlayer.sendMessage(ChatColor.YELLOW + "You have been blinded by " + s.getName());
    44. targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 5000, 1));
    45. }
    46.  
    47. if(cmd.getName().equalsIgnoreCase("unblind")) {
    48. if((!(sender instanceof Player) && args.length == 0)) {
    49. s.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage: /unblind [Player]");
    50.  
    51. }
    52.  
    53. if(args.length == 1) {
    54. if(s.hasPermission("sb.unblind") || sender.isOp() == true) {
    55. s.sendMessage(ChatColor.GREEN + "Player unblinded!");
    56. Player targetPlayer = s.getServer().getPlayer(args[1]);
    57. targetPlayer.sendMessage(ChatColor.YELLOW + "You have been unblinded by " + s.getName());
    58. targetPlayer.getActivePotionEffects().clear();
    59. }
    60.  
    61. if(cmd.getName().equalsIgnoreCase("simpleblind")) {
    62. if((!(sender instanceof Player) && args.length < 0)) {
    63. s.sendMessage("Incorrect usage. Correct usage: /simpleblind");
    64. }
    65.  
    66. s.sendMessage(ChatColor.GREEN + " -=[SimpleBlind]=-");
    67. s.sendMessage(ChatColor.GREEN + "/blind [Player] - Blinds player.");
    68. s.sendMessage(ChatColor.GREEN + "/unblind [Player] - Unblinds the player.");
    69. s.sendMessage(ChatColor.GREEN + "/simpleblind - Takes you back to these commands.");
    70. s.sendMessage(ChatColor.GREEN + "------------------------------------------------");
    71. s.sendMessage(ChatColor.YELLOW + " Plugin made by: BDKing88.");
    72.  
    73. }
    74. }
    75.  
    76. }
    77.  
    78. }
    79. }
    80. return false;
    81. }
    82.  
    83. }
    84.  


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

    AmShaegar

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("blind")) {
    2. ...
    3. if(cmd.getName().equalsIgnoreCase("unblind")) {
    4.  

    Take a closer look. How could the second if statement ever be true if the first one is? But the second one will only be called if the first one is true.
     
  17. Offline

    Tim4209

    just try it the way i showed u, its alot easier to maintain and looks so much cleaner.
     
  18. Offline

    BDKing88

    So, how would I make it disable /blind when it is true?

    Tim, I don't see what you changed because I'm a noob. Could you please explain?

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

    Tim4209

    i put every command into its own method, so it would be almost impossible for you to mess the brackets up and not notice it
     
  20. Offline

    BDKing88

    Okay, I did that tim, but now the plugin won't load into the server. It gives no errors, it just doesen't show up. I'm having the worst luck today :/
     
  21. Offline

    Tim4209

    make sure ur still compiling it with bukkit and everything is checked
     
Thread Status:
Not open for further replies.

Share This Page