} else { help

Discussion in 'Plugin Development' started by iiHeroo, Sep 19, 2013.

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


    Well, I want to send them a message if they have the permission, but when I tested it, it didn't work, here's my code;

    1. if(commandLabel.equalsIgnoreCase("hi")) {
    2. if(player.hasPermission("hi.hi")){
    3. } else {
    4. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to execute this command!");
    5. }
    6. }
  2. Offline


    looks about right to me. Except that you missed return true; and return false; but that is not the Issue here. Uhm. You sure you weren't opped or anything?
  3. Offline


    Here's more of my code, and what I was attempting to do, and no, wasnt OP, because when I removed the player.sendMessage, it said "You don't have permission blah blah blah!"

    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if(player.hasPermission("hi.hi")){
    3. } else {
    4. player.sendMessage(ChatColor.DARK_RED + "You don't have permission to execute this command!");
    5. } else {
    6. player.sendMessage("hi");
    7. }
    8. }
    10. return false;
  4. Offline


    You do not check for a command being executed
    2. public boolean onCommand(....){
    3. if (cmd.getName().equalsIgnoreCase("yourcommand") {
    4. if(sender instanceof player) {
    5. Player player = (Player) sender;
    6. }
    7. if(player.hasPermission("hi.hi")) {
    8. // Do what you want if he has permissions
    9. return true; // So Bukkit knows command got executed correctly and it does not go deeper
    10. } else {
    11. player.sendMessage(...);
    12. return true; // same reason as above
    13. }
    14. return false; // Command was not executed correctly. get 'usage' string from plugin.yml
    15. }
    16. }
  5. Offline


    You know you need to have a player field right?
  6. Offline


    Oh yeah. should have added that too. My code snipet updated
  7. Offline


    ok thanks ! I'll give that a shot !

    uhh, just a quick question, what's suppose to go at where the "...." is ?

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


    the same than you had. was too lazy to copy&paste it :D
  9. Offline


    omg xD, I deleted but I think I copied it, I hope, or, look at one of my old threads where it is.

    EDIT: I copied it *saved* you should've put like "same thing" though :p
  10. Offline


  11. Offline


    thanks, but reload your page :p Thanks anyway, I wouldn't have even noticed it, blondes these days ;-;
  12. Offline


    But don't put your complete code in there.. just the bits that is in your code inbetween each of those brackets ;)
  13. Offline


    On this line
    1. return false; // Command was not executed correctly. get 'usage' string from plugin.yml

    It gives me a error saying "Unreachable Code"

    Here's the full code

    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. Player player = (Player) sender;
    3. if(cmd.getName().equalsIgnoreCase("hi")) {
    4. if(sender instanceof Player) {
    5. }
    6. if(player.hasPermission("hi.hi")) {
    7. player.sendMessage("hi");
    8. return true;
    9. } else {
    10. player.sendMessage(ChatColor.GOLD + ("its working"));
    11. return true;
    12. }
    13. return false; // Command was not executed correctly. get 'usage' string from plugin.yml
    14. }
    15. }
    16. }
  14. Offline


    you got one } at the end too much my bad for that
  15. Offline


    You can always tell when people don't know Java, and I just found another one. You MUST practice Java more regular Java before getting into 3rd party APIs.
    filoghost and Rocoty like this.
  16. Offline


    But when I remove the "}" the code is reached, but it then says Add a "}" here, where it was originally.

    Don't know Java ? I MUST practice ? Do you not know what this thread is for ?

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


    Ok. By looking into the code again I was semi right. You need to REMOVE one AFTER the return false; and ADD one BEFORE the return false;
  18. Offline


    You are asking about basic if statements. I'm pretty certain you don't know Java.
  19. Offline


    Well, sir, you're wrong there for many reasons.

    1. Was asking how to return a message if he had the permission using } else {
    2. This thread is for asking help with your plugin, no matter how simple it is or how hard.
    3. Why must I study Java ? You mean the Bukkit API ?
    4. This may be "basic" to you or some people, but not for some other people
    5. And do you know what the thread is for ? Does it say I must know every Bukkit API or a lot ?
    6. I don't know Java ? Oh really ? Your judging a book by it's cover, just for a question, you're thinking "he's stupid"
    7. And why did you bother commenting if you weren't going to help ?
  20. Offline


    hes right though, if you can't figure out { and } usage correctly to see why its not doing what you want then you clearly don't know any basic programming syntax actually.
  21. Offline


    Woah, look....

    1. By looking at your code, I know you don't know exactly how to use if statements. I also cannot comprehend your question.
    2. Typically, if someone on the forums still needs to learn more Java, we will give them some sources for where you can learn it.
    3. Because of the lack of knowledge. Getting into Bukkit before having basic understandings in Java can be dangerous and frustrating.
    4. It's below the basic Java knowledge.
    5. This thread is mostly unrelated to Bukkit. If I understand this thread correctly, you already had all the Bukkit code you needed at the start!
    6. Okay then. Tell me, did you start programming before getting into Bukkit? And when did you even start? Of course, I could be wrong about your current level and you could've nicely corrected me, but instead you wrote a huge hate list.
    7. Oblivious question, I am helping you. I want you to succeed rather than fail miserably here.
    Rocoty likes this.
  22. Offline


    This forum is for help, and I don't think it's fair to just say "you need to learn more java". However, I do understand the frustration when people ask basic questions like that. I think some people forget that coding bukkit plugins is not a thing in itself. It's using the Bukkit API and Java to write programs that are executed on a server.

    It's unfortunate how many people try to jump right into plugin coding without knowing java. You're only in for more frustration than it's worth :/

    To answer your question... For my plugins personally, I don't worry about the onCommand() return statement. I prefer to leave the permission message empty in my plugin.yml and just write a custom error message. I found that the return statement tends to confuse people who are newer to java (I'm not insulting you so don't get your panties in a bunch. Just stating a fact)

    Try removing all return statements in your onCommand() method except the very last one, where you simply return false, and write the rest of the code as normal.

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


    lol, I fixed this, what they told me is wrong, there's suppose to be two before "return false;" and 2 after "return false;" and I mean the "}". So it works now, but thanks.
  24. Offline


    Anytime, glad you figured it out :)
  25. Offline


    iiHeroo I agree. You really need to learn java. If statements are the backbone of java, and by your awnsers, google it.

    Appljuze I agree with what you said. Not everyone knows java before bukkit, and actaully most people learm them at the same time. This causes a conflict because people ask stupid questions, that can be googled.

    iiHeroo I also forgot to mention you forgot the player variable. You probally dont even know what that is...

    Anyways, is your code still having errors?

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


    It's completely true though, we are not here to teach each and every single person all the basics of Java. That's where you have to put some effort in.

    If you are willing and are highly interested at learning Java, I would suggest buying a book, like Java For Dummies. Or you can try to watch tutorials online, like from thenewboston. Personally, a book is way easier to understand.
    Rocoty likes this.
  27. Offline


    The_Doctor_123 Post #3

    Are you saying this should've helped me because you said "I am helping you" and this is all you told me, if your saying you're trying to help me by telling me to study Java, I know Java, and I didn't want to sound rude in any way, it may have sounded like it, but I needed the help to fix it. And if that's you're "help", that wasn't going to help me fix my code just like that.

    moose517 Post

    I've looked at GitHub codes from Bukkit, none said anything I wanted to know or answer this question, they were all fun plugins or other plugins, and finding a plugin page using GitHub is rare imo, it's 1/5 it seems.

    This is what I coded within 1 week of using Bukkit,

    1. package server.stuff;
    3. import java.util.logging.Logger;
    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.java.JavaPlugin;
    11. public class info extends JavaPlugin{
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14. public static info plugin;
    15. public static String colorize(String str) {
    16. return ChatColor.translateAlternateColorCodes('&', str);
    17. }
    19. @Override
    20. public void onDisable(){
    21. }
    22. @Override
    23. public void onEnable(){
    24. getConfig().options().copyDefaults(true);
    25. saveConfig();
    26. }
    27. String var1 = "Skype: ";
    28. String var2 = "Facebook: ";
    29. String var3 = "Twitter: ";
    30. String var4 = "Youtube: ";
    31. String var5 = "Website: ";
    32. String var6 = "IP: ";
    33. String var7 = "Open: ";
    34. String var8 = "by iiHeroo ";
    35. String var9 = "v1.4.5 ";
    36. String var10 = "Server Info Plus ";
    37. String var11 = "/ip";
    38. String var12 = "/website";
    39. String var13 = "/facebook";
    40. String var14 = "/twitter";
    41. String var15 = "/youtube";
    42. String var16 = "/skype";
    43. String var17 = "Tell's you the server IP. ";
    44. String var18 = "Tell's you the server website. ";
    45. String var19 = "Tell's you the Facebook Page of the server. ";
    46. String var20 = "Tell's you the Twitter account of the owner. ";
    47. String var21 = "Tell's you the Youtube account of the owner. ";
    48. String var22 = "Tells's you the Owner's Skype account. ";
    49. String var23 = " - ";
    50. String var24 = " /ip";
    51. String var25 = "/open";
    52. String var26 = "Tell's you when the server opened ";
    53. String var27 = " /trialmods";
    54. String var28 = "/mods";
    55. String var29 = "/admins";
    56. String var30 = "/superadmins";
    57. String var31 = "/headmod";
    58. String var32 = "/headadmin";
    59. String var33 = "/coowner";
    60. String var34 = "/owner";
    61. String var35 = " - ";
    62. String var36 = "Tell's you the TrialMods of the server. ";
    63. String var37 = "Tell's you the Mods of the server.. ";
    64. String var38 = "Tell's you the Admins of the server. ";
    65. String var39 = "Tell's you the Super-Admins of the server. ";
    66. String var40 = "Tell's you the Head-Mod of the server. ";
    67. String var41 = "Tell's you the Head-Admin of the server. ";
    68. String var42 = "Tell's you the Co-Owner of the server ";
    69. String var43 = "Tell's you the Owner of the server. ";
    70. String var44 = "/donate";
    71. String var45 = "/email";
    72. String var46 = "Tell's you how to donate ";
    73. String var47 = "Tell's you the email of the server or owner ";
    74. String var48 = "/twitch";
    75. String var49 = "Tell's you the Owner's twitch channel. ";
    76. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    77. Player player = (Player) sender;
    78. if(commandLabel.equalsIgnoreCase("Skype"))
    79. if(player.hasPermission("serverinfoplus.skype"))
    80. player.sendMessage(getConfig().getString("Skype"));
    81. if(commandLabel.equalsIgnoreCase("Facebook"))
    82. if(player.hasPermission("serverinfoplus.facebook"))
    83. player.sendMessage(getConfig().getString("Facebook"));
    84. if(commandLabel.equalsIgnoreCase("Twitter"))
    85. if(player.hasPermission("serverinfoplus.twitter"))
    86. player.sendMessage(getConfig().getString("Twitter"));
    87. if(commandLabel.equalsIgnoreCase("YouTube"))
    88. if(player.hasPermission("serverinfoplus.youtube"))
    89. player.sendMessage(getConfig().getString("Youtube"));
    90. if(commandLabel.equalsIgnoreCase("Website"))
    91. if(player.hasPermission("serverinfoplus.website"))
    92. player.sendMessage(getConfig().getString("Website"));
    93. if(commandLabel.equalsIgnoreCase("IP"))
    94. if(player.hasPermission("serverinfoplus.ip"))
    95. player.sendMessage(getConfig().getString("IP"));
    96. if(commandLabel.equalsIgnoreCase("Open"))
    97. if(player.hasPermission("serverinfoplus.open"))
    98. player.sendMessage(getConfig().getString("Open"));
    99. if(commandLabel.equalsIgnoreCase("sip"))
    100. if(args.length == 1) {
    101. if(args[0].equalsIgnoreCase("1"))
    102. if(player.hasPermission("serverinfoplus.help"))
    103. player.sendMessage(ChatColor.BLUE + var10 + ChatColor.GREEN + var9 + ChatColor.GOLD + var8 + ChatColor.AQUA + var16 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var22 + ChatColor.AQUA + ChatColor.AQUA + var24 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var17 + ChatColor.AQUA + var13 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var19 + ChatColor.AQUA + var14 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var20 + ChatColor.AQUA + var15 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var21 + ChatColor.AQUA + var25 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var26 + ChatColor.AQUA + var12 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var18 );
    104. if(args[0].equalsIgnoreCase("2"))
    105. if(player.hasPermission("serverinfoplus.help"))
    106. player.sendMessage(ChatColor.BLUE + var10 + ChatColor.GREEN + var9 + ChatColor.GOLD + var8 + ChatColor.AQUA + var27 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var36 + ChatColor.AQUA + var28 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var37 + ChatColor.AQUA + var29 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var38 + ChatColor.AQUA + var30 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var39 + ChatColor.AQUA + var31 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var40 + ChatColor.AQUA + var32 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var41 + ChatColor.AQUA + var33 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var42 + ChatColor.AQUA + var34 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var43);
    107. if(args[0].equalsIgnoreCase("3"))
    108. if(player.hasPermission("serverinfoplus.help"))
    109. player.sendMessage(ChatColor.BLUE + var10 + ChatColor.GREEN + var9 + ChatColor.GOLD + var8 + ChatColor.AQUA + var44 + ChatColor.GRAY + var23 + ChatColor.DARK_AQUA + var46 + ChatColor.AQUA + var45 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var47 + ChatColor.AQUA + var48 + ChatColor.GRAY + var35 + ChatColor.DARK_AQUA + var49 );
    110. }
    111. if(commandLabel.equalsIgnoreCase("mods"))
    112. if(player.hasPermission("serverinfoplus.mods"))
    113. player.sendMessage(getConfig().getString("Mods"));
    114. if(commandLabel.equalsIgnoreCase("admins"))
    115. if(player.hasPermission("serverinfoplus.admins"))
    116. player.sendMessage(getConfig().getString("Admins"));
    117. if(commandLabel.equalsIgnoreCase("superadmins"))
    118. if(player.hasPermission("serverinfoplus.superadmins"))
    119. player.sendMessage(getConfig().getString("Superadmins"));
    120. if(commandLabel.equalsIgnoreCase("headmmod"))
    121. if(player.hasPermission("serverinfoplus.headmod"))
    122. player.sendMessage(getConfig().getString("Headmod"));
    123. if(commandLabel.equalsIgnoreCase("headadmin"))
    124. if(player.hasPermission("serverinfoplus.headadmin"))
    125. player.sendMessage(getConfig().getString("Headadmin"));
    126. if(commandLabel.equalsIgnoreCase("coowner"))
    127. if(player.hasPermission("serverinfoplus.coowner"))
    128. player.sendMessage(getConfig().getString("Coowner"));
    129. if(commandLabel.equalsIgnoreCase("owner"))
    130. if(player.hasPermission("serverinfoplus.owner"))
    131. player.sendMessage(getConfig().getString("Owner"));
    132. if(commandLabel.equalsIgnoreCase("trialmods"))
    133. if(player.hasPermission("serverinfoplus.trialmods"))
    134. player.sendMessage(getConfig().getString("Trialmods"));
    135. if(commandLabel.equalsIgnoreCase("email"))
    136. if(player.hasPermission("serverinfoplus.email"))
    137. player.sendMessage(getConfig().getString("Email"));
    138. if(commandLabel.equalsIgnoreCase("donate"))
    139. if(player.hasPermission("serverinfoplus.donate"))
    140. player.sendMessage(getConfig().getString("Donate"));
    141. if(commandLabel.equalsIgnoreCase("twitch"))
    142. if(player.hasPermission("serverinfoplus.twitch"))
    143. player.sendMessage(getConfig().getString("Twitch"));
    144. return false;
    145. }
    146. }

    Pretty simple, coded it within 2 weeks of starting programming.

    The_Doctor_123 Post #3 then why didn't you give me a source to learn it if you think I needed to ? And I could've nicely corrected you, but you started of with this ?

    And you expect me to take that easy and reply like I've just been complimented ?

    And no, I didn't start programming before getting into Bukkit, I've been working with Bukkit and the plugins, understanding them since April.

    I started programming in late July. And I was getting into Bukkit's code 3 weeks before programming.

    The_Doctor_123 Post #2
    How is this suppose to help me also ?
  28. Offline


  29. Offline


    What, am I not a good enough coder to even talk to you ?
  30. Offline


    Let me, as respectfully as I can, reply to your posts. But that's quite hard to do with this..

    I want to say to you that you pretty much fought yourself in your reply. The code particularly was a big hit. All I'm going to say is *cough* arrays and *cough* use && in if statements.

    On my very first post here, I was not trying to be cruel or put you down because "you know less than me" but I suggested to learn more of the basics. You took that the wrong way, and sorta started a flame war here.

    Multiple people believe you need some more knowledge in the language, it's not just me. Wouldn't you think that Java developers themselves would know what's best? We've all gone through the learning process and know what new learners should do.

    Please, do not take any offense, everyone on these forums are here to help and I'm doing just that, whether you believe it or not.
    Rocoty likes this.
Thread Status:
Not open for further replies.

Share This Page