Holographic Displays

Holographic Displays

3M Downloads

Bug when String from hologram reached x characters

Rndstad opened this issue · 15 comments

commented

Hello :)

I’m struggling with a problem.
When I create a hologram with the HolographicDisplays API, the following will happen.

After the String reached x characters there will be added an white r. Do you know how that comes?

Server is running on Spigot 1.8.8

commented

Code?

commented

Hello filoghost!

This is my code:

 public void loadHologram(String gateName, Location hologram, me.rndstad.gate.enumeration.Hologram type, Gate gate, boolean startUp) {
        System.out.println("Loading hologram method! ");
        Bukkit.getScheduler().runTask(GatePlugin.getInstance(), () -> {
            Hologram holo = null;
            if (!startUp) {
                for (Hologram h : HologramsAPI.getHolograms(GatePlugin.getInstance())) {
                    String gn = h.getLine(0).toString();
                    gn = gn.replace(" gate (voorkant)", "");
                    gn = gn.replace(" gate (achterkant)", "");
                    gn = gn.replace("CraftTextLine [text=", "");
                    gn = gn.replace("]", "");
                    if (gn.contains(StringUtils.format(gateName))) {
                        holo = h;
                        h.removeLine(4);
                    }
                }
            }
            if (holo == null) {
                System.out.println("Try to load hologram because hologram == null...");
                holo = HologramsAPI.createHologram(GatePlugin.getInstance(), hologram);

                if (type == me.rndstad.gate.enumeration.Hologram.A) {
                    holo.insertTextLine(0, StringUtils.format(gateName + " gate &7(voorkant)"));
                } else if (type == me.rndstad.gate.enumeration.Hologram.B) {
                    holo.insertTextLine(0, StringUtils.format(gateName + " gate &7(achterkant)"));
                }
                holo.insertTextLine(1, " ");
                holo.insertTextLine(2, StringUtils.format(BarUtils.getHealthBar(gate)));
                holo.insertTextLine(3, " ");
            }

            if (gate.getCooldown() == -1) {
                holo.insertTextLine(4, StringUtils.format(StringUtils.format("&e&l&nUpgraden/openen")));
            } else {
                holo.insertTextLine(4, StringUtils.format(StringUtils.format("&c&l&nRepareren!")));
            }
        });
    }
commented

Any ideas?
When I create the hologram without the API but in-game with the command it works fine, why is that?

commented

I tried to reproduce the bug (I don't have the full code), but I didn't see any white "r".

Is the plugin updated?

Can you give me a precise string that works when used with commands, but produces a white "r" when used with the API?

Side note: instead of using toString, there is a convenient getText method, just cast the line to TextLine

commented

Hello @filoghost,
This is the code of setting the health bar:

    public static String getHealthBar(Gate gate) {
        StringBuilder fin = new StringBuilder();

        int health = gate.getHP();
        int maxHealth = (int) LevelManager.getInstance().calculateMaxHealth(gate.getLevel());

        String healthUsed = "&c|";
        String healthFree = "&a|";

        for (int i = 0; i < health; i++) {
            fin.append(healthFree);
        }

        int used = maxHealth - health;
        for (int i = 0; i < used; i++) {
            fin.append(healthUsed);
        }
        return "&7HP: " + fin.toString();
    }
}
commented

Still unable to reproduce the bug. Check your StringUtils.format method, and make sure you're using the latest version of the plugin.

Please give me a single string, without any code, that produces the white "r". Maybe print to console the full line before inserting the text into the hologram.

commented

This is the StringUtils class:
`import org.bukkit.ChatColor;

public class StringUtils {

public static String format(String text) {
    return ChatColor.translateAlternateColorCodes('&', text);
}

}
`

commented

Try to do by
int health = 150;
int maxHealth = 150;

That will show the bug if I'm not wrong.

commented

https://imgur.com/kbMJ2cj
This is the problem

commented

But you can't see anything in the string:
[19:23:06 INFO]: Jup true. [19:23:06 INFO]: Final: &a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a| [19:23:06 INFO]: Jup true. [19:23:06 INFO]: Final: &a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a| [19:23:18 INFO]: Jup true. [19:23:18 INFO]: Final: &a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a| [19:23:18 INFO]: Jup true. [19:23:18 INFO]: Final: &a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a|&a| [19:23:45 INFO]:

commented

I found the bug. Your text is too long and is cut to 300 characters max. A single "§" is left at the end, without the color char, and for some reason Minecraft displays a white "r" (the "reset" color char probably) instead of displaying the symbol or nothing.

A fix to this small bug still wouldn't fix the the health bar being too long, you need to add the color at the start of the string instead of before every single vertical line.

commented

I did this something like this but it won't work:
`
public static String getHealthBar(Gate gate) {
StringBuilder fin = new StringBuilder();

    int health = gate.getHP();
    int maxHealth = (int) LevelManager.getInstance().calculateMaxHealth(gate.getLevel());

    String healthUsed = "|";
    String healthFree = "|";

    for (int i = 0; i < health; i++) {
        fin.append("&a" + healthFree);
    }

    int used = maxHealth - health;
    for (int i = 0; i < used; i++) {
        fin.append("&c" + healthUsed);
    }

    System.out.println("Final: " + fin.toString());
    return "&7HP: " + fin.toString();
}

`

commented

I've tested it! Thank you very much man, now it works! ❤️

commented

Oh thumb fault. I’ll try with doing the color once I see now that what I’m doing makes no sense.

commented

No problem 😄