[v1.13.2] What's wrong in my Scoreboard code?

Discussion in 'Spigot Plugin Development' started by Leodur2006, Apr 17, 2019.

  1. How can I debug it????
     
  2. Print prefix before for loop, every loop print x and prefix and after loop print prefix.
     
  3. Maximvdw

    Benefactor

    prefix = prefix + a substring starting from 0 to x (every itt)
    [​IMG]
     
  4. Another problem is the Scoreboard don't update?

    EDIT: I found a solution: instead of doing a line.substring... just do a line.charAt(x)
     
    #25 Leodur2006, Apr 18, 2019
    Last edited by a moderator: Apr 20, 2019
  5. Another problem: now when I put placeholders, for exemple:
    "§b§lDiamonds: §e%talvorgames_diamonds%"
    And you wait 5 seconds:
    "§b§lDiamonds: §e791§b§lDiamonds: §e747§b§lDiamonds: §e%talvorgames_diamonds%"

    How can I fix that?
     
  6. Maximvdw

    Benefactor

    Thats what I said.. you were adding a substring that allways starts at the beginning
    I suggest debugging it, these are some basic mistakes. You won't be able to learn if we just keep fixing every problem you have
     
  7. Ok I debugged:

    Initial setup:

    Line:
    "§b§lDiamonds: §e%talvorgames_diamonds%"

    In for loop, before placeholders set:

    Line: "§b§lDiamonds: §e%talvorgames_diamonds%§b§lDiamonds: §e%talvorgames_diamonds%"

    In for loop, after placeholders set:

    Line: "§b§lDiamonds: §e17§b§lDiamonds: §e%talvorgames_diamonds%"

    Prefix: "§b§lDiamonds: §e17§b§lDiamonds: §e%talvorgames_diamonds%"

    Suffix: ""
     
  8. Maximvdw

    Benefactor

    Ok since you debugged, you now know the line where this problem happens. Now you know where to fix your issue
     
    • Like Like x 1
  9. No I don't know what is the line where this problem happens...
     
  10. Maximvdw

    Benefactor

    Then you did not debug it

    It's like the joke with the Girl who goes to the doctor with the problem:
    "Every time I drink tea, my left eye hurts. I don't know how it happens"
    You have a inital state "drinking tea"
    and a final state "eye hurts"
    But she forgot to mention that there is another state where there is still a teaspoon in the cup.

    Your debugging is totally wrong, you clearly see that the problem is already there BEFORE you replace the placeholders.. so your debugging should go before that
     
    • Funny Funny x 1
  11. I did INITIAL SETUP, which is BEFORE I replace the placeholders...
     
  12. New code:

    Code (Text):
    // The ScoreboardManager allows us to create Scoreboards
        private static ScoreboardManager manager = Bukkit.getScoreboardManager();

        // The Scoreboard from Bukkit API
        private Scoreboard scoreboard;

        // Each Scoreboard has an "objective", so we can set the Scoreboard title,
        // position on screen...
        private Objective objective;

        // List of players that need an update from Scoreboard
        private List<Player> toUpdate = new ArrayList<>();

        // The update task stored in a variable so may cancel it later
        private int taskId;

        // The original lines of the Sidebar without placeholders set
        private String[] originalLines;

        @SuppressWarnings("deprecation")
        public Sidebar(String title, String... lines) {
            scoreboard = manager.getNewScoreboard(); // Create a new Scoreboard
            objective = this.scoreboard.registerNewObjective("test", "dummy"); // Register the Scoreboard objective
            objective.setDisplaySlot(DisplaySlot.SIDEBAR); // Show the Scoreboard on the side of screen
            objective.setDisplayName(title); // Set title of Scoreboard
            originalLines = lines; // Keep lines with placeholders in a variable

            for (int i = lines.length; i > 0; i--) {
                String line = lines[(i - 1)]; // Current line

                // Register the team associated to this line
                // We use teams for performance reasons and anti-flickering
                Team team = scoreboard.registerNewTeam("team" + i);

                // Set the content of the team to the current line
                team.addPlayer(Bukkit.getOfflinePlayer(line));

                // Get an objective of current line
                Score score = objective.getScore(line);

                // Set the number of the objective which is used to sort the Scoreboard
                score.setScore((lines.length - i) + 1);
            }

            // Put the Bukkit task in a variable so we can cancel it later
            taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(TalvorGames.getInstance(), new Runnable() {

                public void run() {

                    // Iterate through all players that needs a Scoreboard update
                    for (Player player : toUpdate) {

                        // Iterate through original lines with placeholders kept
                        for (int i = originalLines.length; i > 0; i--) {
                            // Get team associated with this line
                            Team team = scoreboard.getTeam("team" + i);

                            // Get a copy of the current line
                            String line = new String(originalLines[i - 1]);

                            try {
                                line = Placeholders.setPlaceholders(line, player);
                            } catch (Exception placeholderApiNotFound) {
                                // PlaceholderAPI has not been found on this server
                                // We don't use PluginManager#isPluginEnabled because of this reason:
                                // - A plugin can have the same name as PlaceholderAPI but not be the
                                // PlaceholderAPI that we want to use here
                            }

                            // We need to split the line into prefixes and suffixes for no flickering
                            String prefix = "";

                            // Cut the line at 64 characters
                            for (int x = 0; (x < 64) && (x < line.length()); x++) {
                                prefix += line.charAt(x);
                            }

                            String suffix = "";

                            if (line.length() > 64) {

                                // Cut the line from 64 characters to 128 characters
                                for (int b = 64; (b < 128) && (b < line.length()); b++) {
                                    suffix += line.charAt(b);
                                }

                            }

                            // Set team prefixes and suffixes
                            team.setPrefix(prefix);
                            team.setSuffix(suffix);
                        }

                    }

                }

            }, 10L, 10L);
     
    #33 Leodur2006, Apr 18, 2019
    Last edited: Apr 18, 2019
  13. Maximvdw

    Benefactor

    String line = new String(originalLines[i - 1]);

    always copy a line if you are going to edit it
     
  14. @Maximvdw uhh why?

    Okay I did this but it don't change anything, still the same bug
     
    #35 Leodur2006, Apr 18, 2019
    Last edited by a moderator: Apr 20, 2019
  15. Maximvdw

    Benefactor

    at what locations did you debug. Can you post your code with some simple system outs
     
  16. Initial setup (before for loop):

    Line:
    "§b§lDiamonds: §e%talvorgames_diamonds%"

    Prefix: "§b§lDiamonds: §e%talvorgames_diamonds%"

    Suffix: ""

    In for loop, before placeholders set:

    Line: "§b§lDiamonds: §e%talvorgames_diamonds%§b§lDiamonds: §e%talvorgames_diamonds%"

    Prefix: "§b§lDiamonds: §e%talvorgames_diamonds%§b§lDiamonds: §e%talvorgames_diamonds%"

    Suffix: ""

    In for loop, after placeholders set:

    Line: "§b§lDiamonds: §e17§b§lDiamonds: §e%talvorgames_diamonds%"

    Prefix: "§b§lDiamonds: §e17§b§lDiamonds: §e%talvorgames_diamonds%"

    Suffix: ""
     
  17. Maximvdw

    Benefactor

    the code,not the output so we know exactly where it is
     
  18. Initial setup:
    1 line before for loop

    Before placeholders set:
    1 line before try/catch statement

    After placeholders set:
    1 line after try/catch statement
     
  19. We need the code... Not a pseudo code or debugging without debugged code.
     

Share This Page