Commits

md_5 authored a25e56c4b61
SPIGOT-2646: MapFont.getWidth with coloured text
No tags

src/main/java/org/bukkit/map/MapFont.java

Modified
1 1 package org.bukkit.map;
2 2
3 3 import java.util.HashMap;
4 +import org.bukkit.ChatColor;
4 5
5 6 /**
6 7 * Represents a bitmap font drawable to a map.
7 8 */
8 9 public class MapFont {
9 10
10 11 private final HashMap<Character, CharacterSprite> chars = new HashMap<Character, CharacterSprite>();
11 12 private int height = 0;
12 13 protected boolean malleable = true;
13 14
51 52 if (!isValid(text)) {
52 53 throw new IllegalArgumentException("text contains invalid characters");
53 54 }
54 55
55 56 if (text.length() == 0){
56 57 return 0;
57 58 }
58 59
59 60 int result = 0;
60 61 for (int i = 0; i < text.length(); ++i) {
61 - result += chars.get(text.charAt(i)).getWidth();
62 + char ch = text.charAt(i);
63 + if (ch == ChatColor.COLOR_CHAR) continue;
64 + result += chars.get(ch).getWidth();
62 65 }
63 66 result += text.length() - 1; // Account for 1px spacing between characters
64 67
65 68 return result;
66 69 }
67 70
68 71 /**
69 72 * Get the height of this font.
70 73 *
71 74 * @return The height of the font.
77 80 /**
78 81 * Check whether the given text is valid.
79 82 *
80 83 * @param text The text.
81 84 * @return True if the string contains only defined characters, false
82 85 * otherwise.
83 86 */
84 87 public boolean isValid(String text) {
85 88 for (int i = 0; i < text.length(); ++i) {
86 89 char ch = text.charAt(i);
87 - if (ch == '\u00A7' || ch == '\n') continue;
90 + if (ch == ChatColor.COLOR_CHAR || ch == '\n') continue;
88 91 if (chars.get(ch) == null) return false;
89 92 }
90 93 return true;
91 94 }
92 95
93 96 /**
94 97 * Represents the graphics for a single character in a MapFont.
95 98 */
96 99 public static class CharacterSprite {
97 100

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut