Solved Plugin not working no ERRORS

Discussion in 'Plugin Development' started by Dmrtje, May 14, 2014.

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

    Dmrtje

    I have been working on my plugin but there is something wrong with it!
    it doesn't give me an error!
    but it just doesn't work!
    here is my code:

    Code:java
    1. package me.timmaker.dmrtje.commands;
    2.  
    3. import me.timmaker.dmrtje.Dmrtje;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10.  
    11. public class openAttractieCommand implements CommandExecutor{
    12.  
    13. private Dmrtje plugin = Dmrtje.getInstance();
    14. private String ErrorPrefix = ChatColor.DARK_RED + "[" + ChatColor.RED + "Error" + ChatColor.DARK_RED + "] " + ChatColor.RED;
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    17. if (cmd.getName().equalsIgnoreCase("openattractie")) {
    18. if (args.length == 0) {
    19. sender.sendMessage(ErrorPrefix + "JE MOET WEL EEN NAAM OPGEVEN!");
    20. return true;
    21.  
    22. }if (args.length == 1 || args.length > 1) {
    23.  
    24. StringBuilder sb = new StringBuilder();
    25. for (int i = 0; i < args.length; i++) {
    26. sb.append(args[i]);
    27. sb.append(" ");
    28. }
    29.  
    30. String msg = sb.toString().trim();
    31. if (!plugin.attracties.contains(msg)) {
    32. sender.sendMessage(ErrorPrefix + "Deze attractie bestaat niet!");
    33. return true;
    34. }if (plugin.attracties.contains(msg) && plugin.attracties.getBoolean(msg, true)) {
    35. sender.sendMessage(ErrorPrefix + "Deze attractie is al geopend!");
    36. return true;
    37. }if (plugin.attracties.contains(msg) && plugin.attracties.getBoolean(msg, false)) {
    38. sender.sendMessage(msg);
    39. Bukkit.broadcastMessage(msg);
    40. return true;
    41. }return true;
    42. }
    43. }
    44. return true;
    45. }
    46.  
    47. }[/i]


    Sorry for the dutch sentences thats cause i'am dutch!
     
  2. Dmrtje No errors + doesn't work = debug code. Put in some println statements in there to see what's reached and what's not.
     
  3. Note: Please follow Java conventions, make the first letter of your class name capital, OpenAttractieCommand.

    Firstly, are you even setting the executor this class in the main class onEnable() method?
    this.getCommand("openattractie").setExecutor(new OpenAttractieCommand());

    Secondly, are you sure the command is in the plugin.yml file?

    Thirdly, initiate the "plugin" object in a constructor.
    Code:
    public OpenAttractieCommand() {
        this.plugin = Dmrtjie.getInstance();
    }
    
    Fourthly, you're always returning true, only return true if the command IS openattractie, otherwise return false.

    Lastly and probably the main reason, the boolean inside the if statement is always false.
    Are you sure "attracties" list contains the message and the config contains their message? You haven't done anything to handle if all the if statements are false. It should be structured like this:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("openattractie")) {
                if (args.length == 0) {
                    sender.sendMessage(ErrorPrefix + "JE MOET WEL EEN NAAM OPGEVEN!");
                } else if (args.length > 0) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < args.length; i++) {
                        if (i == args.length - 1) sb.append(args[i]);
                        else sb.append(args[i] + " ");
                    }
     
                    String msg = sb.toString();
                    if (!plugin.attracties.contains(msg)) {
                        sender.sendMessage(ErrorPrefix + "Deze attractie bestaat niet!");
                    } else if (plugin.attracties.getBoolean(msg, true)) {
                        sender.sendMessage(ErrorPrefix + "Deze attractie is al geopend!");
                        return true;
                    } else if (plugin.attracties.contains(msg) && plugin.attracties.getBoolean(msg, false)) {
                        sender.sendMessage(msg);
                        Bukkit.broadcastMessage(msg);
                    } else {
                        sender.sendMessage(ErrorPrefix + "Invalid message.");
                    }
                }
                return true;
            }
            return false;
        }
    
     
  4. Offline

    Dmrtje

    KingFaris11 Yeah i know i should do that The first letter capital but it is just private so...

    and here is my main class

    Code:java
    1. package me.timmaker.dmrtje;
    2.  
    3. import me.timmaker.dmrtje.commands.addAttractieCommand;
    4. import me.timmaker.dmrtje.commands.delAttractieCommand;
    5. import me.timmaker.dmrtje.commands.openAttractieCommand;
    6. import me.timmaker.dmrtje.commands.sluitAttracieCommand;
    7. import me.timmaker.dmrtje.logger.DmrtjeLogger;
    8. import me.timmaker.dmrtje.resources.MyConfig;
    9. import me.timmaker.dmrtje.resources.MyConfigManager;
    10.  
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13.  
    14.  
    15. public class Dmrtje extends JavaPlugin{
    16.  
    17. MyConfigManager configManager;
    18. public MyConfig attracties;
    19.  
    20. private static Dmrtje instance;
    21.  
    22. public void onEnable() {
    23. DmrtjeLogger.printInfo("Is sucesvol opgestart!");
    24.  
    25. instance = this;
    26.  
    27. //CONFIGS!
    28.  
    29. configManager = new MyConfigManager(this);
    30.  
    31. attracties = configManager.getNewConfig("Attracties.Dmrtje");
    32.  
    33. //COMMANDS!
    34.  
    35. getCommand("addattractie").setExecutor(new addAttractieCommand());
    36. getCommand("delattractie").setExecutor(new delAttractieCommand());
    37.  
    38. getCommand("openattractie").setExecutor(new openAttractieCommand());
    39. getCommand("sluitattractie").setExecutor(new sluitAttracieCommand());
    40. }
    41.  
    42. public void onDisable() {
    43. DmrtjeLogger.printInfo("Is succesvol gestopt!");
    44. }
    45.  
    46. public static Dmrtje getInstance() {
    47. return instance;
    48. }
    49.  
    50.  
    51. }
    52.  


    and my plugin.yml

    Code:
    name: Dmrtje
    version: 0.0.1
    main: me.timmaker.dmrtje.Dmrtje
    description: De Dmrtje Theme park Plugin!
     
    commands:
        openattractie:
            usage: /<command> <attractie naam>
            description: Opent een attractie!
        addattractie:
            usage: /<command> <attractie naam>
            description: Voegt een attractie toe!
        delattractie:
            usage: /<command> <attractie naam>
            description: verwijdert een attractie!
        sluitattractie:
            usage: /<command> <attractie naam>
            description: Sluit een attractie!
    and finaly for me there is no code in what you posted
     
  5. Test using my code. Your one looks correct, except I don't think you can have spaces in the usage for commands in terms of <>. Make it <attractienaam> not <attractie naam>.
     
  6. Offline

    Dmrtje

    i did this and i is going till the first if statement after the StringBuilder

    yes it can the other commands work.

    KingFaris11 Btw there is something not correct with your code...
    it goed straigth to the else statement

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Dmrtje Which else statement? There are a few of them.

    KingFaris11 If args.length isn't equal to 0, it has to be greater than zero ;) I also love how you've taken his StringBuilder and coverted it to the more inefficient String concatenation yet kept the variable named sb ^^
     
  8. Offline

    Dmrtje

  9. Dmrtje In that case: plugin.attracties.contains(msg) returns true, and plugin.attracties.getBoolean(msg) returns false.
     
  10. Offline

    Dmrtje

    AdamQpzm ooooow in that way

    basicly what i want to do is i want to check if the file contains the msg and if the word behind it is equal to true or false if it is equal to false then i want to broadcast a message and set it to true
    else if it is false i want to send the sender i message that it is already opend.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  11. To your first point, you never know. :) <- That's a joke by the way.
    Second point, I didn't do this on an IDE so I couldn't be bothered to change the variable name since it'd mean that I'd have to change the others. Also, I prefer using String since imo it's more easily customised when changing spaces into commas as you can't use ".trim()" for commas can you?
     
  12. KingFaris11 Huh? Using a StringBuilder in a loop is much more efficient, and you always get a String at the end. I don't know of any reason to concatenate Strings as opposed to something like StringBuilder other than lack of knowledge. Feel free to provide a case where you believe it's better.
     
  13. Offline

    Dmrtje

    .trim() is for removing spaces at the end of the String right?

    And the way i posted at the main post?
    just to be sure

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  14. I don't see why you'd need to be efficient in a command like this. That's basically my main point.
     
  15. KingFaris11 Because it's not overly difficult and the benefits + getting into a good habit are worth it, in my opinion. Especially considering that it was already in the more efficient version, no need to change it back, is there?

    Dmrtje If you mean your StringBuilder then yes it looks fine. And yes, trim() will return the String but without the starting & ending spaces.
     
    KingFaris11 likes this.
  16. Offline

    Dmrtje

    Could you maybe help me with what i am trying cause it isn't working.
     
  17. Offline

    22vortex22

    Just to point out you can change
    Code:java
    1. if(args.length==1|| args.length>1{
    )
    to
    Code:java
    1. }if(args.length>=1){


    I haven't looked into the actually problem yet but I will.

    EDIT: Have you made sure your command executors are setup correctly? Have you made the class an instance?

    EDIT 2:
    Code:java
    1.  
    2. getCommand("addattractie").setExecutor(new addAttractieCommand());
    3. getCommand("delattractie").setExecutor(new delAttractieCommand());
    4. getCommand("openattractie").setExecutor(new openAttractieCommand());
    5. getCommand("sluitattractie").setExecutor(new sluitAttracieCommand());


    Try changing it to

    getCommand("commandName").setExecutor(new commandClass(this));
     
  18. Have you checked the values for this?
     
  19. Offline

    Dmrtje

    i didn't tought about that and Yes i have
     
  20. Offline

    22vortex22

    Check my latest edit.
     
  21. Offline

    Dmrtje

  22. Offline

    22vortex22



    Because it is what I use and it works :p
     
  23. Offline

    Dmrtje

    22vortex22 Did you test it?
    and where uses it this?
     
  24. Offline

    22vortex22


    I told you where to use it and what I meant was that when I compared your executor to my other plugins I noticed yours do not have (this). Just try it out and see if it works.
     
  25. Offline

    Dmrtje

    22vortex22,
    i does totally nothing except that if the msg is false that it sends me the usage of the command
     
  26. Offline

    22vortex22


    Add some debug code then
     
  27. Offline

    Dmrtje

    It is solved it FINALY!!!!

    final code:
    Code:java
    1. package me.timmaker.dmrtje.commands;
    2.  
    3. import me.timmaker.dmrtje.Dmrtje;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10.  
    11. public class OpenAttractieCommand implements CommandExecutor {
    12.  
    13. private Dmrtje plugin;
    14.  
    15. private String OpenPrefix = ChatColor.DARK_GREEN + "[" + ChatColor.GREEN
    16. + "Open" + ChatColor.DARK_GREEN + "] " + ChatColor.WHITE;
    17. private String ErrorPrefix = ChatColor.DARK_RED + "[" + ChatColor.RED
    18. + "Error" + ChatColor.DARK_RED + "] " + ChatColor.RESET;
    19.  
    20. public OpenAttractieCommand(Dmrtje dmrtje) {
    21. this.plugin = Dmrtje.getInstance();
    22. }
    23.  
    24. public boolean onCommand(CommandSender sender, Command cmd, String label,
    25. String[] args) {
    26. if (cmd.getName().equalsIgnoreCase("openattractie")) {
    27. if (sender.isOp() || sender.hasPermission("dmrtje.attractie.open")) {
    28. if (args.length == 0) {
    29. sender.sendMessage(ErrorPrefix
    30. + "JE MOET WEL EEN NAAM OPGEVEN!");
    31. return true;
    32. } else if (args.length >= 1) {
    33. StringBuilder sb = new StringBuilder();
    34. for (int i = 0; i < args.length; i++) {
    35. sb.append(args[i]);
    36. sb.append(" ");
    37. }
    38.  
    39. String msg = sb.toString().trim();
    40. if (!plugin.attracties.contains(msg)) {
    41. sender.sendMessage(ErrorPrefix
    42. + "DEZE ATTRACTIE BESTAAT NIET!");
    43. return true;
    44. } else if (plugin.attracties.contains(msg)) {
    45. if (plugin.attracties.getBoolean(msg) == false) {
    46. Bukkit.broadcastMessage(OpenPrefix
    47. + "De attractie "
    48. + msg.replaceAll("&", "ยง") + " is geopend!");
    49. plugin.attracties.set(msg, true);
    50. plugin.attracties.saveConfig();
    51. return true;
    52. } else {
    53. sender.sendMessage(ErrorPrefix
    54. + "Deze attractie is al geopend!");
    55. return true;
    56. }
    57. }
    58. }
    59. } else
    60. sender.sendMessage(ErrorPrefix
    61. + "Jij mag dit command niet uitvoeren!");
    62. }
    63. return false;
    64. }
    65. }[/i]
     
Thread Status:
Not open for further replies.

Share This Page