.split(" ");

Discussion in 'Plugin Development' started by SkyleTyler1337, Apr 4, 2014.

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

    SkyleTyler1337

    Code:java
    1. import net.vividnetwork.VividNetwork;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class WarnCommand implements CommandExecutor{
    10.  
    11. public VividNetwork plugin;
    12.  
    13.  
    14. public WarnCommand(VividNetwork plugin){
    15. this.plugin = plugin;
    16. }
    17.  
    18.  
    19. @Override
    20. public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
    21. if(sender instanceof Player){
    22. Player player = (Player) sender;
    23. if(label.equalsIgnoreCase("warn")){
    24. if(args.length < 2){
    25. player.sendMessage(ChatColor.RED + "/warn <player> <reason>");
    26. return false;
    27. }
    28. Player targetplayer = plugin.getServer().getPlayer(args[0]);
    29.  
    30. if(args.length == 1){
    31. if(args[0].equalsIgnoreCase(args[0])){
    32. if(targetplayer == null){
    33. player.sendMessage(ChatColor.RED + "The Player " + ChatColor.DARK_RED + args[0] + ChatColor.RED + " is not online!");
    34. return false;
    35. }
    36. }
    37. }
    38. String reason = args[1];
    39. if(args.length == 2){
    40. if(args[1].equalsIgnoreCase(args[1])){
    41. if(reason == null){
    42. player.sendMessage(ChatColor.RED + "please provide a reason!");
    43. return false;
    44. }
    45. }
    46.  
    47.  
    48. if(targetplayer == null){
    49. player.sendMessage(ChatColor.RED + "The Player " + ChatColor.DARK_RED + args[0] + ChatColor.RED + " is not online!");
    50. return false;
    51. }
    52.  
    53.  
    54. if(targetplayer.isOnline()){
    55. targetplayer.sendMessage(player.getDisplayName() + ChatColor.GOLD + " warned you for " +
    56. ChatColor.RED + ChatColor.BOLD.toString() + args[1]);
    57. }else{
    58. player.sendMessage(ChatColor.RED+"The Player" + ChatColor.DARK_RED + args[0] + ChatColor.RED + " is not online!");
    59. }
    60. }
    61. }
    62. }
    63. return false;
    64. }
    65. }


    I'm trying to do a space when i try to give them a reason.
    For Example: "/warn SkylerTyler1337 hacking click aimbot"
     
  2. Offline

    br456

    SkyleTyler1337
    You would want to iterate through all of the arguments except for args[0], and add them to one string, not split
     
    PogoStick29 likes this.
  3. Offline

    PogoStick29

    Split is used to split a String. If I called split(" ") on "Hello there", it would return a String[] that contains "Hello" and "there". Instead, you want to concatenate all of the arguments except the first.

    Code:java
    1. String str;
    2. for (int i = 1; i < args.length(); i++) {
    3. str += args[ i ] + " ";
    4. }
    5. str.trim();


    I do this in a Warn plugin (coincidental) here:

     
    br456 likes this.
  4. Offline

    SkyleTyler1337

    thanks i will see if this works!

    and i watch your videos xD
     
    PogoStick29 likes this.
  5. Offline

    mazentheamazin

    PogoStick29 SkyleTyler1337
    You could use StringBuilder, I find it more understandable than the method you have shown above:
    Code:java
    1. public String buildString(String[] args, int startLocation) {
    2. StringBuilder sb = new StringBuilder("");
    3. for(int i = startLocation; i < args.length(); i++) {
    4. sb.append(args[i]).append(" ");
    5. }
    6. return sb.toString();
    7. }[/i]
     
  6. Offline

    PogoStick29

    mazentheamazin If you're using a StringBuilder, why are you still using String concatenation? (sb.append(args[0] + " "))
     
    Garris0n likes this.
  7. Offline

    Garris0n

    One of the bajillion features of IntelliJ is to warn you if you accidentally do this, always handy.
     
    number1_Master and PogoStick29 like this.
  8. Offline

    mazentheamazin

    PogoStick29
    My mistake, I updated it. Thank you for informing me
     
  9. Offline

    PogoStick29

    That's exactly where I learned to catch those mistakes ;)

    Now it will append each String without a space so "Hello world" would be Helloworld. You want to do

    Code:java
    1. sb.append(args[ i ]).append(" ");


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page