How to get the number of lines from a Stringlist and apply?

Asked

Viewed 175 times

2

I have a StringList inside a file mensagens.txt, I’d like to count how many lines there are StringList contained within that file. For each line of the string 1 code will be executed within the plugin.

If the StringList has 10 lines the code will execute:

web.setScore(1) para a linha 1
web.setScore(2) para a linha 2.....
web.setScore(10) para a linha 10

The code I created is this:

public class ScoreBoard implements Listener {

    @EventHandler
    public void pj(PlayerJoinEvent e) {
        for (String list : Main.getMensagens().getStringList("Scoreboard")) {;

        Scoreboard scoreboard = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
        Objective obj = scoreboard.registerNewObjective(Main.getMensagens().getString("Titulo"), "dummy");
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);
          Score web = obj.getScore(list);
          web.setScore(1);

        Player p = e.getPlayer();
        p.setScoreboard(scoreboard);
    }
  }
}

My problem is with web.setScore, I need him to take the number of lines StringList and run automatically.

1 answer

2


Turn into a for and have control over the index.

public class ScoreBoard implements Listener {
    @EventHandler
    public void pj(PlayerJoinEvent e) {
        /*tipo de retorno do getStringList*/ list = Main.getMensagens().getStringList("Scoreboard");
        for (int i = 0;  i < list.length; i++) {    
             Scoreboard scoreboard = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
             Objective obj = scoreboard.registerNewObjective(Main.getMensagens().getString("Titulo"), "dummy");
             obj.setDisplaySlot(DisplaySlot.SIDEBAR);
             Score web = obj.getScore(list.get(i));
             web.setScore(i);
             Player p = e.getPlayer();
             p.setScoreboard(scoreboard);
        }
    }
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.