Spaces in an argument

Discussion in 'Plugin Development' started by Camaflicks, Jul 20, 2014.

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

    Camaflicks

    I am making a report plugin like the one on OCTC but I run into a problem whenever I try to make the reason have spaces.

    Code:java
    1. package com.invictuspvp.PotatoReports;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    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.java.JavaPlugin;
    12.  
    13. public final class PotatoReports extends JavaPlugin {
    14. public final ArrayList<String> reports = new ArrayList<String>();
    15.  
    16. public final List<String> getReports() {
    17. return this.reports;
    18. }
    19.  
    20. public void onEnable() {
    21. }
    22.  
    23. @SuppressWarnings("deprecation")
    24. public boolean onCommand(CommandSender sender, Command cmd,
    25. String commandLabel, String[] args) {
    26. if (!(sender instanceof Player)) {
    27. sender.sendMessage("You must be in-game to use this command.");
    28. return true;
    29. }
    30. Player p = (Player) sender;
    31. if (cmd.getName().equalsIgnoreCase("report")) {
    32. if (args.length == 0) {
    33. p.sendMessage(ChatColor.RED + "Too few arguments.\n"
    34. + ChatColor.RED
    35. + "Correct Usage: /report <player> <reason> [Forcefield, Flying, Spamming, etc.]");
    36. } else if (args.length == 1) {
    37. p.sendMessage(ChatColor.RED + "Too few arguments.\n"
    38. + ChatColor.RED
    39. + "Correct Usage: /report <player> <reason> [Forcefield, Flying, Spamming, etc.]");
    40. } else if (args.length == 2) {
    41. Player target = Bukkit.getServer().getPlayer(args[0]);
    42. String reason = args[1];
    43. if (target != null) {
    44. if (!getReports().contains(target.getName())) {
    45. getReports().add(
    46. ChatColor.WHITE + "[" + ChatColor.GOLD + "R"
    47. + ChatColor.WHITE + "] "
    48. + ChatColor.DARK_AQUA + p.getName()
    49. + ChatColor.YELLOW + " reports "
    50. + ChatColor.DARK_AQUA
    51. + target.getName() + ChatColor.GOLD
    52. + ": " + ChatColor.WHITE + reason);
    53. p.sendMessage(ChatColor.GREEN + "Thank you."
    54. + ChatColor.GOLD
    55. + " The issue will be dealt with shortly.");
    56. for (Player players : Bukkit.getOnlinePlayers()) {
    57. String report = ChatColor.WHITE + "["
    58. + ChatColor.GOLD + "R" + ChatColor.WHITE
    59. + "] " + ChatColor.DARK_AQUA + p.getName()
    60. + ChatColor.YELLOW + " reports "
    61. + ChatColor.DARK_AQUA + target.getName()
    62. + ChatColor.GOLD + ": " + ChatColor.WHITE + reason;
    63. if (players.hasPermission("reports.view")) {
    64. players.sendMessage(report);
    65. }
    66. }
    67. }
    68. } else {
    69. p.sendMessage(ChatColor.RED + "No players match your query.");
    70. }
    71. }
    72. }
    73. return true;
    74. }
    75.  
    76. public void onDisable() {
    77. }
    78. }
    79.  


    I might type /report PistonGaming Hacking - Forcefield but it will just return blank. Nothing happens in the chat. But if I do /report PistonGaming Hacking it works! I want to be able to have multi word reasons, not just one!
     
  2. Offline

    izarooni

    Camaflicks
    Code:java
    1. public static String joinStringFrom(String arr[], int start, String sep) {
    2. StringBuilder builder = new StringBuilder();
    3. for (int i = start; i < arr.length; i++) {
    4. builder.append(arr[i]);
    5. if (i != arr.length - 1) {
    6. builder.append(sep);
    7. }
    8. }
    9. return builder.toString();
    10. }[/i]


    you would use it like this

    Code:java
    1. String reason = joinStringFrom(args, 1, " ");
     
  3. Offline

    Flegyas

    The command is tokenized by spaces, for example, this command:
    will return 4 arguments! ( ["first", "second", "-", "third"]).
    So, if you want the "-" in your commands, you should ignore the third arg ( in the string array the one obtainable with args[2]) and take a command for right formatted only if it has an args array with length of 4.
     
Thread Status:
Not open for further replies.

Share This Page