Mistakes in my Plugin

Discussion in 'Plugin Development' started by ugotraidedbro, Nov 25, 2014.

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

    ugotraidedbro

    Hello,

    I am trying to create a Plugin which includes some basic commands for my server. But everytime I add a command (in this case I added /night) the second command isnt working. /day works out fine.

    Code:java
    1. package me.ugotraidedbro.utime;
    2.  
    3.  
    4. import org.bukkit.World;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class main extends JavaPlugin{
    11.  
    12. String noperm = "Du hast keine Rechte dafür!";
    13. String prefix = "§5[uTime]§f ";
    14.  
    15. @Override
    16. public void onEnable() {
    17. System.out.println("§5[uTime]§f Plugin aktiviert!");
    18. }
    19.  
    20. @Override
    21. public void onDisable() {
    22. System.out.println("§5[uTime]§f Plugin deaktiviert!");
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26.  
    27. Player p = (Player) sender;
    28.  
    29. if(cmd.getName().equalsIgnoreCase("day")) {
    30. onDay(p);
    31. if(p.hasPermission("ucore.day")) {
    32. } else {
    33. p.sendMessage(prefix + noperm);
    34. onDay(p);
    35.  
    36. }
    37. if(cmd.getName().equalsIgnoreCase("night")) {
    38. onNight(p);
    39. if(p.hasPermission("ucore.night")) {
    40. } else {
    41. p.sendMessage(prefix + noperm);
    42. onNight(p);
    43. }
    44. }
    45. }
    46.  
    47. return true;
    48. }
    49.  
    50. public void onNight(Player p) {
    51. if(p.hasPermission("ucore.night") | p.isOp()) {
    52. World w = p.getWorld();
    53. w.setTime(14000);
    54. p.sendMessage(prefix + "Es ist nun §aNacht§f!");
    55. } else {
    56. p.sendMessage(prefix + noperm);
    57. }
    58. }
    59.  
    60. public void onDay(Player p) {
    61. if(p.hasPermission("ucore.day") | p.isOp()) {
    62. World w = p.getWorld();
    63. w.setTime(0);
    64. p.sendMessage(prefix + "Es ist nun §aTag§f!");
    65. } else {
    66. p.sendMessage(prefix + noperm);
    67. }
    68.  
    69. }
    70. }


    This is my java code.

    plugin.yml:
    Code:
    name: uCore
    author: ugotraidedbro
    version: 1.0
     
    main: me.ugotraidedbro.utime.main
     
    commands:
      day:
        description: Zeit auf Tag aendern
        usage: /<command>
        permission: utime.day
       
      night:
        description: Zeit auf Nacht aendern
        usage: /<command>
        permission: utime.night 
     
    permissions:
      ucore.night:
        default: false
       
      ucore.day:
        default: false
    Could you please help me?
    Kind regards
     
  2. ugotraidedbro
    [1] You're checking if they have the permission twice (both in the onNight/onDay method and in the onCommand).
    [2] You're using | instead of || (bitwise OR instead of logical OR).
    [3] I would use a switch statement and switch their command to lowercase:
    Code:
    switch(cmd.getName().toLowerCase()) {
        case "day":
            //code
            break;
        case "night":
            //code
            break;
    }
    [4] Do you have the right permissions to run the command? If not, then nothing will happen since you return true regardless of the situation.
     
    es359 and xTrollxDudex like this.
  3. Offline

    msnijder30

    ugotraidedbro

    I think you forgot to put a bracket after the day check. You also have a bracket too much after you check if the command == night
     
    Zupsub likes this.
  4. Offline

    ugotraidedbro

    Ok fixed all the problems.

    The last problem is that I cant see my personal No-Perms Text.
    It just shows the normal red bukkit "contact the server admins..." text.

    How 2 fix it?
     
  5. Offline

    Skionz

  6. Offline

    ugotraidedbro

    Code:java
    1. package me.ugotraidedbro.utime;
    2.  
    3.  
    4. import org.bukkit.World;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class main extends JavaPlugin{
    11.  
    12. String noperm = "Du hast keine Rechte dafür!";
    13. String prefix = "§5[uTime]§f ";
    14.  
    15. @Override
    16. public void onEnable() {
    17. System.out.println("§5[uTime]§f Plugin aktiviert!");
    18. }
    19.  
    20. @Override
    21. public void onDisable() {
    22. System.out.println("§5[uTime]§f Plugin deaktiviert!");
    23. }
    24.  
    25. @SuppressWarnings("deprecation")
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27.  
    28. Player p = (Player) sender;
    29.  
    30. if(cmd.getName().equalsIgnoreCase("day")) {
    31. if(p.hasPermission("ucore.day")) {
    32. onDay(p);
    33. } else {
    34. p.sendMessage(prefix + noperm);
    35.  
    36. }
    37. }
    38.  
    39. if(cmd.getName().equalsIgnoreCase("night")) {
    40. if(p.hasPermission("ucore.night")) {
    41. onNight(p);
    42. } else {
    43. p.sendMessage(prefix + noperm);
    44.  
    45. }
    46. }
    47.  
    48. if(cmd.getName().equalsIgnoreCase("heal")) {
    49. if(p.hasPermission("ucore.heal")) {
    50. p.setHealth(20);
    51. p.setFoodLevel(20);
    52. p.sendMessage(prefix + "Du hast dich geheilt!");
    53. } else {
    54. p.sendMessage(prefix + noperm);
    55.  
    56. }
    57. }
    58. return true;
    59. }
    60.  
    61. public void onNight(Player p) {
    62. if(p.hasPermission("ucore.night") | p.isOp()) {
    63. World w = p.getWorld();
    64. w.setTime(14000);
    65. p.sendMessage(prefix + "Es ist nun §aNacht§f!");
    66. } else {
    67. p.sendMessage(prefix + noperm);
    68. }
    69. }
    70.  
    71. public void onDay(Player p) {
    72. if(p.hasPermission("ucore.day") | p.isOp()) {
    73. World w = p.getWorld();
    74. w.setTime(0);
    75. p.sendMessage(prefix + "Es ist nun §aTag§f!");
    76. } else {
    77. p.sendMessage(prefix + noperm);
    78. }
    79.  
    80. }
    81. }


    plugin.yml
    Code:
    name: uCore
    author: ugotraidedbro
    version: 1.0
     
    main: me.ugotraidedbro.utime.main
     
    commands:
      day:
        description: Zeit auf Tag aendern
        usage: /<command>
        permission: ucore.day
       
      night:
        description: Zeit auf Nacht aendern
        usage: /<command>
        permission: ucore.night 
       
      heal:
        description: Selbstheilung
        usage: /<command>
        permission: ucore.heal 
     
    permissions:
      ucore.night:
        default: false
       
      ucore.day:
        default: false
       
      ucore.heal:
        default: false
     
  7. Offline

    Skionz

  8. Offline

    ugotraidedbro

    Code:java
    1. package me.ugotraidedbro.utime;
    2.  
    3.  
    4. import org.bukkit.World;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class main extends JavaPlugin{
    11.  
    12. String noperm = "Du hast keine Rechte dafür!";
    13. String prefix = "§5[uTime]§f ";
    14.  
    15. @Override
    16. public void onEnable() {
    17. System.out.println("§5[uTime]§f Plugin aktiviert!");
    18. }
    19.  
    20. @Override
    21. public void onDisable() {
    22. System.out.println("§5[uTime]§f Plugin deaktiviert!");
    23. }
    24.  
    25. @SuppressWarnings("deprecation")
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27.  
    28. Player p = (Player) sender;
    29.  
    30. if(cmd.getName().equalsIgnoreCase("day")) {
    31. if(p.hasPermission("ucore.day")) {
    32. onDay(p);
    33. } else {
    34. p.sendMessage(prefix + noperm);
    35.  
    36. }
    37. }
    38.  
    39. if(cmd.getName().equalsIgnoreCase("night")) {
    40. if(p.hasPermission("ucore.night")) {
    41. onNight(p);
    42. } else {
    43. p.sendMessage(prefix + noperm);
    44.  
    45. }
    46. }
    47.  
    48. if(cmd.getName().equalsIgnoreCase("heal")) {
    49. if(p.hasPermission("ucore.heal")) {
    50. p.setHealth(20);
    51. p.setFoodLevel(20);
    52. p.sendMessage(prefix + "Du hast dich geheilt!");
    53. } else {
    54. p.sendMessage(prefix + noperm);
    55.  
    56. }
    57. }
    58. return true;
    59. }
    60.  
    61. public void onNight(Player p) {
    62. World w = p.getWorld();
    63. w.setTime(14000);
    64. p.sendMessage(prefix + "Es ist nun §aNacht§f!");
    65. }
    66.  
    67. public void onDay(Player p) {
    68. World w = p.getWorld();
    69. w.setTime(0);
    70. p.sendMessage(prefix + "Es ist nun §aTag§f!");
    71.  
    72. }
    73. }


    better?
     
  9. Offline

    Skionz

    ugotraidedbro Check before casting sender to Player. What happens when the console executes a command? Other then that the class looks good so I would debug and see if your messages are actually being sent or if the method is actually being called.
     
  10. Offline

    ugotraidedbro

    Tested it, message still the same. Console gets the same red message. You know what to do? Skionz
     
  11. Offline

    Skionz

    ugotraidedbro Debug it. The whole point of debugging is to see if the method is being called or the program is passing an if statement. If you are executing the command from the console you will get a casting exception because you casted sender to Player without checking anything.
     
  12. Offline

    MajorSkillage

    A switch statement would make it look cleaner but try use if(cmdblahblahblah){ } else if(cmdblahblahblah){ else if}(so on){ }
     
  13. Offline

    xTrollxDudex

    ugotraidedbro
    You have to return true after running the command logic, and at the end return true as well. You place your command not found logic before the last return true.
     
  14. Offline

    Rocoty

    Did you know the bitwise OR operator is overloaded to support booleans? That right there is really a logical non short circuiting OR operator. It is only called a bitwise operator when the operands are integers (read: any kind of integer datatype)
     
    Skionz likes this.
  15. Offline

    ugotraidedbro

    could u pls tell me exactly what to do?
     
  16. Offline

    nlthijs48

    ugotraidedbro If you want to send your own permission messages then get rid of the lines starting with 'permission:' in your plugin.yml. Bukkit will check those permissions and send some red message if a player does not have permission for it otherwise.
     
  17. Offline

    mythbusterma


    Yes, add debug statements to find the issue, and fix it.
     
  18. Offline

    ugotraidedbro

    What is a debug statement? I am very new to this stuff.
     
  19. Offline

    Gater12

    ugotraidedbro
    Just something you print out to the console to see what's up. You can use it to see if the code reaches to the area where you put the debug lines, or print out contents of Collections to see what's inside.
     
  20. Offline

    mythbusterma

  21. Offline

    ugotraidedbro

    Instead of these unuseful lmgtfy Link u could tell a beginner what exactly to do.

    System.err.println ("var: "+ var );
    where to put this?
    void dbug (Object obj ){String variableName = obj.somehowGetVariableName();String variableContents = obj.toString();System.out.println ( variableName +": "+ variableContents );}
    where to put this?
     
  22. Offline

    Skionz

    ugotraidedbro mythbusterma was just pointing out that it takes 10 seconds to google how to debug. Basically you would do something like this
    Code:
    System.out.println(0);
    if(statement.equals(statement)) {
        System.out.println(1);
        if(otherStatement.equals(5) {
            System.out.println(2);
        }
    }
     
  23. Offline

    ugotraidedbro

    there were like 10 different forms how to do it, but thank you for being the helpful one Skionz.

    I try it and tell u then.
     
  24. Offline

    Dragonphase

  25. Offline

    mythbusterma

    Sorry I don't have time to waste telling you EXACTLY WHAT TO DO when Google is such a valuable resource and doesn't mind searching benign things that are so easy to find on the web.
     
    nlthijs48 likes this.
Thread Status:
Not open for further replies.

Share This Page