Solved Sending list to player

Discussion in 'Plugin Development' started by yPedx, May 19, 2017.

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

    yPedx

    Eh, I've tried a bit, and this is the only thing that pops up;
    Well, it's being spammed all the time until the server crashes...
    What did I do wrong though?
    upload_2017-5-20_0-45-1.png
    Code;
    Code:
    if (args.length >= 1 && args[0].equalsIgnoreCase("check")) {
                                if (!sender.hasPermission("glamreports.checknotes")){
                                    sender.sendMessage(notePrefix+notesNoPermission);
                            }
                                if (sender.hasPermission("glamreports.checknotes")) {
                                    if (args.length == 0 || args.length == 1) {
                                        sender.sendMessage(notePrefix+removeNoteUsage);
                                    }
                                    if(args.length >= 2){
                                        Player target = Bukkit.getPlayer(args[1]);
                                    if (target == null && args.length == 2){
                                        sender.sendMessage(notePrefix+notesPlayerNotFound);
                                    }
                                    List<String> notes = this.getConfig().getStringList("Note-List."+target+".notes");
                                    this.getConfig().set("Note-List", notes);
                                    if (target != null && args.length == 2){
                                        sender.sendMessage(notePrefix+noteListHeader.replaceAll("%player%", target.getName()));
                                        if(notes == null){
                                            sender.sendMessage(notePrefix+"§cThis player does not have any notes!");
                                        }
                                        for(int i = notes.size(); i >= 0; ++i){
                                            String IDPrefix = idPrefix.replaceAll("%ID%", String.valueOf(i+1));
                                                for(String gray : notes){
                                            gray.replaceAll("-", ChatColor.DARK_GRAY+"-");
                                            gray.replaceAll(""+Bukkit.getPlayer(gray)+" ", ChatColor.RED+""+Bukkit.getPlayer(gray)+" ");
                                                }
                                            player.sendMessage(idPrefix+((Object[])notes.toArray()));
                                    }}}}}}
    I know there's a lot of if statements, I'll change that later.
     
  2. Offline

    Zombie_Striker

    @yPedx
    • You are still referencing idprefix instead of IDprefix.
    • You are just printing out the Instance of the object. What you need to do is print out the values of each string in the list.
    • Replaceall does not actually replace the instance. It creates a New instance of the string that has those bits replaced. You need to continually update the gray in the notes list in order for the changes to be applied.
     
  3. Offline

    yPedx

    @Zombie_Striker
    idPrefix has a value at the top of the code, not shown though.
    Ehm, I'm kind of new to 'lists'. Any pages you'd recommend looking at?
     
  4. Offline

    Zombie_Striker

    @yPedx
    But idPrefix is not the one where you replace the ID. IDPrefix changes %ID% to the actual Id. This is most likely what you meant.

    Instead of doing just a replace all, send gray after you are done looping through it. Also, Remove the for int-loop at above the enhanced foreach loop. What that does is loop through the whole list for every object in the list. That is squaring the amount of looped needed.
     
  5. Offline

    yPedx

    @Zombie_Striker
    Ah, I see now. Little typo.

    So, remove this,
    Code:
    for(int i = notes.size(); i >= 0; ++i){
    But how would I replace the id?
    Example how I tried it to be;
    Code:
    // In-game chat
    // the numbers just means the first note, second etc.. in the list.
    // "sender" is the one who added the note.
    // "note" is of course the note.
    
    [1] sender - note
    [2] sender - note
    [3] sender - note
    etc..
     
  6. Offline

    Zombie_Striker

    @yPedx
    My mistake. If you still need the ID of each string, then remove the string for-each loop. When you need to instantiate "gray", set it equal to notes.get(i);
     
  7. Offline

    yPedx

    Don't really understand what you're trying to tell me >.<'

    @Zombie_Striker
    Alright, so I got the numbers to work.. Now I still have the weird messages;
    [​IMG]

    I can't seem to get rid of them.
    Here's the code;
    Code:
    List<String> notes = this.getConfig().getStringList("Note-List."+target.getName()+".notes");
                                    this.getConfig().set("Note-List."+target.getName()+".notes", notes);
                                    if (target != null && args.length == 2){
                                        if(notes == null || notes.size() == 0){
                                            sender.sendMessage(notePrefix+"§cThis player does not have any notes!");
                                        }
                                        sender.sendMessage(notePrefix+noteListHeader.replaceAll("%player%", target.getName()));
                                        for(int i = 0; i<notes.size(); ++i){
                                            String IDPrefix = idPrefix.replaceAll("%ID%", String.valueOf(i+1));
                                            player.sendMessage(IDPrefix+(notes.toArray()));
                                    }}}}}}
     
    Last edited: May 25, 2017
  8. Offline

    yPedx

    bump ? xD
     
  9. Offline

    timtower Administrator Administrator Moderator

    @yPedx Don't send notes.toArray(), loop through the values instead. You already have the loop for it.
     
  10. Offline

    yPedx

    @timtower
    How to loop through the values is what I don't understand properly how to do. I've searched for hours to find an answer but everything comes up as list.toArray();
     
  11. Offline

    Zombie_Striker

  12. Offline

    Horsey

    Here's some code:
    Code:java
    1. int i = 1;
    2. for (String note : notes){
    3. sender.sendMessage("["+ i + "] " + note)
    4. i++;
    5. }
     
  13. Offline

    yPedx

Thread Status:
Not open for further replies.

Share This Page