Solved Args problem!

Discussion in 'Plugin Development' started by BearProgrammer, Aug 7, 2014.

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

    BearProgrammer

    I try to do sendmessage system, that u can send messages to other plays in the server. Every think is okay with the code but the is one problem.. i cant send messages that have more then 1 word. Please help me im new in Java.
    Code:java
    1. package me.sahar.com;
    2.  
    3. import java.util.Scanner;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin {
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16. public static Main plugin;
    17.  
    18. @Override
    19. public void onEnable() {
    20. PluginDescriptionFile pdf = this.getDescription();
    21. this.logger.info(pdf.getName() + " Version" + pdf.getVersion()
    22. + " Has been enabled!");
    23. }
    24.  
    25. @Override
    26. public void onDisable() {
    27. PluginDescriptionFile pdf = this.getDescription();
    28. this.logger.info(pdf.getName() + " Version" + pdf.getVersion()
    29. + " Has been disabled!");
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command cmd,
    33. String commandLabel, String[] args) {
    34. if (commandLabel.equalsIgnoreCase("sendmessage")) {
    35. if (args.length == 0) {
    36. sender.sendMessage("Enter a player name.");
    37. return true;
    38. }
    39. if (args.length == 1) {
    40. Player target = Bukkit.getServer().getPlayer(args[0]);
    41. if (target == null) {
    42. sender.sendMessage("Player didnt found! " + args[0] + ".");
    43. return true;
    44. }
    45. sender.sendMessage("Please enter a message.");
    46. return true;
    47. }
    48. if (args.length >= 2) {
    49. Player target = Bukkit.getServer().getPlayer(args[0]);
    50. target.sendMessage(args[1]);
    51. sender.sendMessage("Message sends sucsfully!");
    52. return true;
    53. }
    54. }
    55. return true;
    56. }
    57. }
     
  2. Offline

    Necrodoom

    Well, instead of only sending args[1] message, use string builder and append all other args to it to create a message.
     
    Gnat008 likes this.
  3. Offline

    Gnat008

    BearProgrammer
    You only send one argument, you will have to loop through all of the arguments and send them as a String.
     
  4. Offline

    BearProgrammer

    And how to do that? :p
     
  5. Offline

    xXMaTTHDXx

    Well it helps if you are a bit more experienced with java haha.
     
  6. Offline

    BearProgrammer

    lol im starter :p can you ignore it and help me please?
     
  7. Offline

    iBecameALoaf

    You can use a StringBuilder.
    BearProgrammer

    Everyone is a starter at one point. No need to be rude :/

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

    BearProgrammer

    And what is that?
     
  9. Offline

    Gnat008

    BearProgrammer
    You could use a for-loop, such as:
    Code:
    for(int i = 1; i < args.length; i++) { // Start the loop at index 1, so it doesn't send the name
        // Do stuff
    }
    Here is the documentation for the StringBuilder class.
     
  10. Offline

    BearProgrammer

    Thank. i'll check it.
     
  11. Offline

    iBecameALoaf

    Alright, here's an example.

    Code:
    for(int i = 1; i<args.length; i++){  // Change this if the message is cut off (make it higher/lower by 1)
        sb.append(args[i]).append(" ");
    }
     
    String s = sb.toString();
    The string 's' is the message it would send to another player.
     
  12. Offline

    BearProgrammer

    Thanks. but what is sb?
     
  13. Offline

    Flamedek

    BearProgrammer sb is a Stringbuilder, which you can initialize with 'new StringBuilder();'
     
  14. Offline

    iBecameALoaf

    It's a StringBuilder.
    Create one like this:
    Code:
    StringBuilder sb = new StringBuilder("");
    Ninja'd me ;3

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

    BearProgrammer

    I do:
    Code:java
    1. StringBuilder sb = new StringBuilder("");
    2. for(int i = 1; i<args.length; i++){
    3. sb.append(args).append(" ");
    4. String s = sb.toString();

    and this is just sending the first word to the player. again.
     
  16. Offline

    Gnat008

    BearProgrammer
    You want to do
    Code:
    sb.append(args[i])
    As that gets the argument at that index.
     
  17. Offline

    iBecameALoaf

    Can you update your code so I can see what is looks like with the StringBuilder in it?
    Edit: Maybe change
    Code:
    sb.append(args)
    to
    Code:
    sb.append(args[i])
     
  18. Offline

    Necrodoom

  19. Offline

    BearProgrammer

    And my mistake this is even not sending the message to the player.
    And Giving the [/i] did not helped.
    iBecameALoaf
    Code:java
    1. package me.sahar.com;
    2.  
    3. import java.util.Scanner;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin {
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16. public static Main plugin;
    17.  
    18. @Override
    19. public void onEnable() {
    20. PluginDescriptionFile pdf = this.getDescription();
    21. this.logger.info(pdf.getName() + " Version" + pdf.getVersion()
    22. + " Has been enabled!");
    23. }
    24.  
    25. @Override
    26. public void onDisable() {
    27. PluginDescriptionFile pdf = this.getDescription();
    28. this.logger.info(pdf.getName() + " Version" + pdf.getVersion()
    29. + " Has been disabled!");
    30. }
    31.  
    32. @SuppressWarnings("unused")
    33. public boolean onCommand(CommandSender sender, Command cmd,
    34. String commandLabel, String[] args) {
    35. if (commandLabel.equalsIgnoreCase("sendmessage")) {
    36. if (args.length == 0) {
    37. sender.sendMessage("Enter a player name.");
    38. return true;
    39. }
    40. if (args.length == 1) {
    41. Player target = Bukkit.getServer().getPlayer(args[0]);
    42. if (target == null) {
    43. sender.sendMessage("Player didnt found! " + args[0] + ".");
    44. return true;
    45. }
    46. sender.sendMessage("Please enter a message.");
    47. return true;
    48. }
    49. if (args.length >= 2) {
    50. Player target = Bukkit.getServer().getPlayer(args[0]);
    51. StringBuilder sb = new StringBuilder("");
    52. for(int i = 1; i<args.length; i++){
    53. sb.append(args[/i]).append(" ");
    54. String s = sb.toString();
    55. sender.sendMessage("Message sends sucsfully!");
    56. return true;
    57. }
    58. }
    59. }
    60. return true;
    61. }
    62. }
    63.  
     
  20. Offline

    Necrodoom

    BearProgrammer you put the result to 's' but never do anything with it.
     
  21. Offline

    iBecameALoaf

    You put a /i, you need to put just an i.
    Also, you never actually did anything with the stringbuilder...
     
  22. Offline

    BearProgrammer

    I know becuse if i do [/i] its do Italic on bukkit fourms, its isnt really in the code.
     
  23. Offline

    iBecameALoaf

    oh... well that changes things... The problem is you're not actually doing anything with the StringBuilder.
     
  24. Offline

    BearProgrammer

    I try it i added
    Code:java
    1. target.sendMessage(args[/i]);

    and that only send the first word... idk why.
     
  25. Offline

    Gnat008

    BearProgrammer
    Because you only sent the first word. You append all the words to the StringBuilder in the for-loop, and send it to the player after it.
     
  26. Offline

    Necrodoom

    BearProgrammer why aren't you using the string builder you just made?

    Args is made from a split message string, its not going to have spaces in an arg.
     
  27. Offline

    BearProgrammer

    How to use it?
     
  28. Offline

    iBecameALoaf

    You don't use args[/i] when you send the message, you use 's'.
     
  29. Offline

    Necrodoom

    BearProgrammer if you have no idea what your own code you just wrote does, you may have to go back and learn some basic Java.

    All you did is copy-paste iBecameALoaf example without actually reading it.
     
  30. Offline

    BearProgrammer

    I tryed that alot of time and this is still sending the first word only.
    Do you want image to improve it?

    I know basic java but this is dosent work.

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

Share This Page