Solved How to get every arg as 1 message

Discussion in 'Plugin Development' started by KarimAKL, Apr 5, 2018.

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

    KarimAKL

    How would i make every arg as 1 message? And also how would i do it after like arg 2? Like /ban (player) (reason) where reason can be arg 2 and everything after arg 2?
     
  2. Offline

    Zombie_Striker

    @KarimAKL
    1. Create a StringBuilder. This will hold the 'reason'
    2. Create a for int loop. Start at "1" (the second arg) and loop up to the length of the args.
    3. For each loop, add the arg to the stringbuilder. Also, if there is another arg after this, add a space to the string builder
    4. Outside of the for loop, call StringBuilder.toString to get the reason as a string.
     
  3. Offline

    KarimAKL

    @Zombie_Striker I don't know how to do any of this. :/ Could you explain the steps more?
     
  4. @KarimAKL
    StringBuilder sb = new StringBuilder();

    Im not gonna explain the for loop, you should know how to make one of those
    but inside that for loop add
    sb.append(args).append(" ");
    and then out side of the for loop do
    String msg = sb.toString();
     
  5. Offline

    Zombie_Striker

    @KarimAKL
    Just remember that if you copy @Blackwing_Forged 's psudo-code, you will need to call .trim() for the toString(), because there will be an added space at the end of the string.
     
  6. Offline

    KarimAKL

    @Blackwing_Forged I have this:
    Code:
    for (Player p : Bukkit.getOnlinePlayers()) {
    
    That's a loop for online players, right?
    Then what would i want to loop for here?
    @Zombie_Striker 1. What is a "psudo-code"? 2. What do you mean added space at the end of the string?
     
  7. Offline

    Zombie_Striker

    @KarimAKL
    1. Psudo code is a way of explaining what you need to do with code without actually writing the code.
    2. Because at loop always appends a " " at the end of each arg, there will be an added space at the end of the string because it is still 'expecting' another word.
    3. Yes, that is an enhanced for loop. You will need to do that, but convert it into a for int loop.
     
  8. Offline

    KarimAKL

    @Zombie_Striker
    1. Okay, thanks for that information. :)
    2. Okay i get why not but i still don't know how.
    3. Okay, now how would i do that? Maybe: "for int (whatever i need to write)"?
     
  9. @KarimAKL
    Maybe click the link he posted and read it
     
  10. Offline

    KarimAKL

  11. Offline

    Zombie_Striker

    @KarimAKL
    For 2. Just add ".trim()" after "toString()"
     
  12. Offline

    KarimAKL

    @Zombie_Striker Okay, thanks. :)
    EDIT: Okay, i just read that link but i still don't get what i should do. :/ Maybe something like this?:
    Code:
    for (int whatever : args) {
    
    If not then what should i do?
     
    Last edited by a moderator: Apr 6, 2018
  13. Offline

    KarimAKL

    bump, i still need this. :/
     
  14. Offline

    Zombie_Striker

    @KarimAKL
    Please do not edit old posts. I am not notified when this happens.

    A for int loop looks like:
    Code:
    for(int i = 0; i < XXX; i++){
    Where "XXX" is the highest number it should go to (in this case, args.length)
     
  15. Offline

    KarimAKL

    @Zombie_Striker Oh, my bad but i’m not supposed to post 2 or more messages within 24 hours, right? If not then what? Anyway so if i want the reason in arg 2+ i would do this?:
    Code:
    for (int i = 2; i =< args.length; i++) {
        //Code
    }
    
    Or:
    Code:
    for (int i = 1; i < args.length; i++) {
        //Code
    }
    
    Or is that wrong?
    Also how would i get the message? Like if i want to broadcast the reason the player was banned.
     
    Last edited by a moderator: Apr 12, 2018
  16. Offline

    Zombie_Striker

    @KarimAKL
    #2 is correct.

    To get the reason, do what @Blackwing_Forged posted
    And then 'msg' will be equal to the reason, which you could broadcast.
     
  17. Offline

    KarimAKL

    @Zombie_Striker Okay so like this?:
    Code:
    for (int i = 1; i < args.length;) {
                            StringBuilder sb = new StringBuilder();
                            sb.append(args).append(" ");
                            String msg = sb.toString();
                            Bukkit.broadcastMessage(ChatColorUtil.chat(plugin.getConfig().getString("ban-message")) + " " + msg);
                            return true;
    
    Or is that wrong aswell? It doesn't come with any errors tho.
    EDIT: I removed the "i++" because it said "dead code" :/ Also what does the "i++" even do? I'm a little confused about that. :p

    Also, this doesn't have anything to do with this but do you know how to do that "[ Code ]" thing with Java instead? I've tried "[JavaCode]" or "[CodeJava]" but that's not the way. :p
     
  18. Offline

    Zombie_Striker

    @KarimAKL
    1. i++ is a shorthand for increasing i by 1. That is what allows use to get all of the args.
    2. The reason it is noting that it is "dead code" is because you are returning true inside the for loop. Re-add i++, and move the broadcastMessage and return true; lines out of the for loop.
    3. Also, move the Stringbuilder above the for loop. Putting it inside the for loop means a new string builder (without any of the other args) will be created.
    4. [ code ] is just the syntax provided by bukkit to provide that box to appear. If you need to specify a programming language, use [ code=java]
     
  19. Offline

    KarimAKL

    @Zombie_Striker 1. Okay, thanks for the information. :D 2. Oh okay, i'll do that now then. :p 3. Another "oh okay" :p 4. I see, thanks for the information. :)
    EDIT: Where should i move the broadcast message and return true? ._.
    Code:
    if (args.length >= 2) {
                        StringBuilder sb = new StringBuilder();
                        sb.append(args).append(" ");
                        String msg = sb.toString();
                        Bukkit.broadcastMessage(ChatColorUtil.chat(plugin.getConfig().getString("ban-message")) + " " + msg);
                        return true;
                        for (int i = 1; i < args.length; i++) {}
    
    This code has an error and i thought it would have but i don't know what you meant? :p (By what you meant i mean that i don't know how i should do it)

    I just tried the [ code=java] without space at the start but it just used the normal "code" and wrote "[ code=java]" inside
     
    Last edited by a moderator: Apr 12, 2018
  20. Offline

    Zombie_Striker

    @KarimAKL
    This is the correct arrangement for those lines.
     
  21. Offline

    KarimAKL

    @Zombie_Striker Okay, thanks alot. xD I'll do that now then. :p
    EDIT: Now i have the dead code problem again.. :(
    Code:
    if (args.length >= 2) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 1; i < args.length; i++) {
                            sb.append(args).append(" ");
                            String msg = sb.toString().trim();
                            Bukkit.broadcastMessage(ChatColorUtil.chat(plugin.getConfig().getString("ban-message")) + " " + msg);
                            return true;
    
    Nvm. I fixed it by ending the for int loop at the "sb.append(args).append(" ");" part like you did. (i didn't think that was the problem, anyway thanks alot for all the help. :))
    Current code:
    Code:
    if (args.length >= 2) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 1; i < args.length; i++) {
                            sb.append(args).append(" ");
                        }
                            String msg = sb.toString().trim();
                            Bukkit.broadcastMessage(ChatColorUtil.chat(plugin.getConfig().getString("ban-message")) + " " + msg);
                            return true;
    
    I'll test this now and see if it works, if it does i'll change this to solved.
    It's just returning my plugin.yml usage. :( I'm having the same problem with all the other commands i make, i must be doing something wrong with all of them. :/ Anyway i trust this'll work so i'll change it to solved now. Thanks for all the help @Zombie_Striker and @Blackwing_Forged :D
     
    Last edited by a moderator: Apr 12, 2018
Thread Status:
Not open for further replies.

Share This Page