Solved Error /a command?

Discussion in 'Plugin Development' started by nastasescu, Sep 9, 2014.

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

    nastasescu

    I have a custom command named /a for sending help requests to the helpers, mods etc.
    But all woks fine, without the receiving message
    Code:java
    1. if (label.equalsIgnoreCase("a")) {
    2. if (args.length != 0) {
    3. for (Player playerz : Bukkit.getOnlinePlayers()) {
    4. if (playerz.hasPermission("lobby.receivehelp")) {
    5. playerz.sendMessage(ChatColor.BLUE + "HelpRequest> "
    6. + ChatColor.GRAY + p.getDisplayName()
    7. + ChatColor.GRAY
    8. + " has sent the following help request:");
    9. playerz.sendMessage(ChatColor.WHITE + ""
    10. + ChatColor.ITALIC + args);
    11. playerz.playSound(playerz.getLocation(),
    12. Sound.NOTE_PLING, 99999, 3);
    13. }
    14. }
    15. p.sendMessage(ChatColor.GREEN
    16. + "Your message was sent to our staff! Thank you!");
    17. } else if (args.length == 0) {
    18. p.sendMessage(ChatColor.RED + "The message is too small!");
    19. }
    20. }

    playerz.sendMessage(ChatColor.WHITE + ""
    + ChatColor.ITALIC + args); - don't work, why? And how can i do to repair this thing
     
  2. nastasescu Please learn the basics of Java from either the oracle tutorials or a Java book, and then please learn the basics of plugin development from the Bukkit wiki :)
     
  3. Offline

    nastasescu

    i don't need any troll, if i send a help request in Plugin Development, i need a response.
     
  4. nastasescu I'm not trolling, it's serious. Provide your full class, or at least your full onCommand(), and I'll be able to explain some problems.
     
  5. Offline

    nastasescu

    Code:java
    1. if (label.equalsIgnoreCase("stats")) {
    2. if (sender instanceof Player) {
    3. sender.sendMessage(ChatColor.GREEN
    4. + "===================================================");
    5. p.sendMessage(ChatColor.DARK_AQUA + "Nickname: "
    6. + ChatColor.YELLOW + "" + p.getName());
    7. p.sendMessage(ChatColor.DARK_AQUA + "You have "
    8. + ChatColor.GREEN + "" + (int) economy.getBalance(p)
    9. + " Gems" + ChatColor.DARK_AQUA + "!");
    10. p.sendMessage(ChatColor.GREEN
    11. + "===================================================");
    12. }
    13. }
    14. if (label.equalsIgnoreCase("hub")) {
    15. if (joc1kitpvp.contains(p)) {
    16. removeKit(p);
    17. for (PotionEffect effect : p.getActivePotionEffects()) {
    18. p.removePotionEffect(effect.getType());
    19. }
    20. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
    21. 999999999, 3));
    22. p.sendMessage(ChatColor.GREEN
    23. + "===================================================");
    24. p.sendMessage(ChatColor.DARK_AQUA + "Nickname: "
    25. + ChatColor.YELLOW + "" + p.getName());
    26. p.sendMessage(ChatColor.DARK_AQUA + "You have "
    27. + ChatColor.GREEN + "" + (int) economy.getBalance(p)
    28. + " Gems" + ChatColor.DARK_AQUA + "!");
    29. p.sendMessage(ChatColor.GREEN
    30. + "===================================================");
    31. p.getInventory().clear();
    32. p.getInventory().setArmorContents(null);
    33. pl1kitpvp--;
    34. Bukkit.broadcastMessage(ChatColor.GRAY + p.getName()
    35. + " has left the KitPVP #1 game!");
    36. p.teleport(Bukkit.getWorld("world").getSpawnLocation());
    37. joc1kitpvp.remove(p);
    38. }
    39. }
    40. if (label.equalsIgnoreCase("a")) {
    41. if (args.length != 0) {
    42. for (Player playerz : Bukkit.getOnlinePlayers()) {
    43. if (playerz.hasPermission("lobby.receivehelp")) {
    44. playerz.sendMessage(ChatColor.BLUE + "HelpRequest> "
    45. + ChatColor.GRAY + p.getDisplayName()
    46. + ChatColor.GRAY
    47. + " has sent the following help request:");
    48. playerz.sendMessage(ChatColor.WHITE + ""
    49. + ChatColor.ITALIC + args);
    50. playerz.playSound(playerz.getLocation(),
    51. Sound.NOTE_PLING, 99999, 3);
    52. }
    53. }
    54. p.sendMessage(ChatColor.GREEN
    55. + "Your message was sent to our staff! Thank you!");
    56. } else if (args.length == 0) {
    57. p.sendMessage(ChatColor.RED + "The message is too small!");
    58. }
    59. }
    60. return false;
    61. }
     
  6. nastasescu You're missing some, I mean the declaration of the method right the way down to the closing brace, otherwise I might miss something important.
     
  7. Offline

    nastasescu

    AdamQpzm
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command,
    3. String label, String[] args) {
    4. Player p = (Player) sender;
    5. if (label.equalsIgnoreCase("stats")) {
    6. if (sender instanceof Player) {
    7. sender.sendMessage(ChatColor.GREEN
    8. + "===================================================");
    9. p.sendMessage(ChatColor.DARK_AQUA + "Nickname: "
    10. + ChatColor.YELLOW + "" + p.getName());
    11. p.sendMessage(ChatColor.DARK_AQUA + "You have "
    12. + ChatColor.GREEN + "" + (int) economy.getBalance(p)
    13. + " Gems" + ChatColor.DARK_AQUA + "!");
    14. p.sendMessage(ChatColor.GREEN
    15. + "===================================================");
    16. }
    17. }
    18. if (label.equalsIgnoreCase("hub")) {
    19. if (joc1kitpvp.contains(p)) {
    20. removeKit(p);
    21. for (PotionEffect effect : p.getActivePotionEffects()) {
    22. p.removePotionEffect(effect.getType());
    23. }
    24. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
    25. 999999999, 3));
    26. p.sendMessage(ChatColor.GREEN
    27. + "===================================================");
    28. p.sendMessage(ChatColor.DARK_AQUA + "Nickname: "
    29. + ChatColor.YELLOW + "" + p.getName());
    30. p.sendMessage(ChatColor.DARK_AQUA + "You have "
    31. + ChatColor.GREEN + "" + (int) economy.getBalance(p)
    32. + " Gems" + ChatColor.DARK_AQUA + "!");
    33. p.sendMessage(ChatColor.GREEN
    34. + "===================================================");
    35. p.getInventory().clear();
    36. p.getInventory().setArmorContents(null);
    37. pl1kitpvp--;
    38. Bukkit.broadcastMessage(ChatColor.GRAY + p.getName()
    39. + " has left the KitPVP #1 game!");
    40. p.teleport(Bukkit.getWorld("world").getSpawnLocation());
    41. joc1kitpvp.remove(p);
    42. }
    43. }
    44. if (label.equalsIgnoreCase("a")) {
    45. if (args.length != 0) {
    46. for (Player playerz : Bukkit.getOnlinePlayers()) {
    47. if (playerz.hasPermission("lobby.receivehelp")) {
    48. playerz.sendMessage(ChatColor.BLUE + "HelpRequest> "
    49. + ChatColor.GRAY + p.getDisplayName()
    50. + ChatColor.GRAY
    51. + " has sent the following help request:");
    52. playerz.sendMessage(ChatColor.WHITE + ""
    53. + ChatColor.ITALIC + args);
    54. playerz.playSound(playerz.getLocation(),
    55. Sound.NOTE_PLING, 99999, 3);
    56. }
    57. }
    58. p.sendMessage(ChatColor.GREEN
    59. + "Your message was sent to our staff! Thank you!");
    60. } else if (args.length == 0) {
    61. p.sendMessage(ChatColor.RED + "The message is too small!");
    62. }
    63. }
    64. return false;
    65. }
     
  8. nastasescu
    I don't know if there's a limit to the sound volume, if not, you're basically killing them with this
    Code:java
    1. Sound.NOTE_PLING, 99999, 3);


    On topic: Debug. Also sending 'args' doesn't do what you'd expect it to do, rather build a String using all the elements in the array. Apache Commons has a nice method to do it
    Code:java
    1. String message = StringUtils.join(args, ' ', 0, args.length);
     
  9. nastasescu Okay, problems I see:
    1) Unchecked sender -> Player cast (basic Java)
    2) Checking the command's label rather than it's name (basic Bukkit)
    3) Type check for a cast you've already performed (basic Java)
    4) For their "nickname" you send their username (basic Bukkit)
    5) Sending the player to argument array rather than a more readable form (basic Java)
    6) No debug attempts to try and solve own problem (basic Java)
    7) Never return except for false at the end (basic Bukkit)
     
  10. Offline

    nastasescu

    Resolved... thanks for nothing.
     
  11. nastasescu I'm just trying to be honest. You can't get far into plugin development without Java knowledge, and without reading the plugin tutorial. You're making most of the classic beginner mistakes.
     
Thread Status:
Not open for further replies.

Share This Page