Skip to content

Commit 86e4aaa

Browse files
refactor: align custom font class with resource pack definition (#62)
Co-authored-by: theEvilReaper <theevilreaper@users.noreply.github.com>
1 parent f94e777 commit 86e4aaa

7 files changed

Lines changed: 121 additions & 220 deletions

File tree

src/autogenerated/java/net/onelitefeather/vulpes/font/FontSymbols.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* This class is generated in a later step.
55
* Don't add any kind of logic here.
66
*/
7-
public sealed interface FontSymbols permits FontSymbol {
7+
public interface FontSymbols {
88
}

src/main/java/net/onelitefeather/vulpes/font/BitFontSymbol.java

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,76 @@
11
package net.onelitefeather.vulpes.font;
22

3-
import net.onelitefeather.vulpes.registries.VulpesKey;
4-
import org.jetbrains.annotations.UnmodifiableView;
3+
import net.minestom.server.codec.Codec;
4+
import net.minestom.server.codec.StructCodec;
5+
import org.jetbrains.annotations.Nullable;
56

67
import java.util.List;
78

8-
public sealed interface FontSymbol extends FontSymbols, VulpesKey permits BitFontSymbol {
9+
/**
10+
* Represents a single font provider entry in a Minecraft resource pack font definition.
11+
* <p>
12+
* Each instance describes how a set of characters is rendered using a specific font provider
13+
* (e.g. bitmap or unicode) as defined in a {@code font.json}.
14+
*
15+
* @param file the resource location of the font texture (e.g. "namespace:path/to/file.png")
16+
* @param type the type of the font provider (e.g. "bitmap" or "unicode")
17+
* @param ascent the ascent of the glyphs, defining the distance from the baseline to the top
18+
* @param height the default height of the glyphs
19+
* @param chars the characters mapped to this provider, typically defining the glyph layout
20+
*/
21+
public record FontSymbol(
22+
String file,
23+
String type,
24+
int ascent,
25+
int height,
26+
List<String> chars
27+
) {
928

1029
/**
11-
* Returns the symbol ascent.
30+
* Codec for serializing and deserializing {@link FontSymbol} instances to and from JSON,
31+
* following the structure defined in Minecraft's font provider format.
32+
*/
33+
public static final Codec<FontSymbol> CODEC = StructCodec.struct(
34+
"file", Codec.STRING, FontSymbol::file,
35+
"type", Codec.STRING, FontSymbol::type,
36+
"ascent", Codec.INT, FontSymbol::ascent,
37+
"height", Codec.INT, FontSymbol::height,
38+
"chars", Codec.STRING.list(), FontSymbol::chars,
39+
FontSymbol::new
40+
);
41+
42+
/**
43+
* Checks if this font symbol has any characters defined.
1244
*
13-
* @return the ascent
45+
* @return true if the chars list is not empty, false otherwise
1446
*/
15-
int ascent();
47+
public boolean hasChars() {
48+
return !chars.isEmpty();
49+
}
1650

1751
/**
18-
* Returns the symbol height.
52+
* Returns the character at the given index.
1953
*
20-
* @return the height
54+
* @param index the index of the character
55+
* @return the character at the given index or null if the index is out of bounds
2156
*/
22-
int height();
57+
public @Nullable String getChar(int index) {
58+
if (index < 0 || index >= chars.size()) {
59+
return null;
60+
}
61+
return chars.get(index);
62+
}
2363

2464
/**
25-
* Returns all symbols for the font.
65+
* Returns the overwritten character based on the index, otherwise it returns the default value.
2666
*
27-
* @return the symbols
67+
* @param index the index of the character
68+
* @param defaultValue the default value
69+
* @return the overwritten character or the default value
2870
*/
29-
@UnmodifiableView
30-
List<String> symbols();
71+
public String getCharOr(int index, String defaultValue) {
72+
return (index >= 0 && index < this.chars.size())
73+
? this.chars.get(index)
74+
: defaultValue;
75+
}
3176
}

src/main/java/net/onelitefeather/vulpes/gson/KeyGsonAdapter.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

src/main/java/net/onelitefeather/vulpes/gson/package-info.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/test/java/net/onelitefeather/vulpes/font/FontRegistryTest.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.onelitefeather.vulpes.font;
22

3-
import net.kyori.adventure.key.Key;
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45
import org.junit.jupiter.api.Test;
56

67
import java.util.List;
@@ -9,17 +10,66 @@
910

1011
class FontSymbolTest {
1112

13+
private static final String TEST_JSON =
14+
"""
15+
{
16+
"file": "manis:global/player_ranks/admin.png",
17+
"type": "bitmap",
18+
"ascent": 10,
19+
"height": 10,
20+
"chars": [
21+
""
22+
]
23+
}
24+
""";
25+
26+
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
27+
28+
@Test
29+
void testNameTagRead() {
30+
FontSymbol tag = new FontSymbol(
31+
"manis:global/player_ranks/admin.png",
32+
"bitmap",
33+
10,
34+
10,
35+
List.of("\uE120")
36+
);
37+
38+
FontSymbol loadedText = GSON.fromJson(TEST_JSON, FontSymbol.class);
39+
assertNotNull(loadedText);
40+
41+
assertEquals(tag.file(), loadedText.file());
42+
assertEquals(tag.type(), loadedText.type());
43+
assertEquals(tag.ascent(), loadedText.ascent());
44+
assertEquals(tag.height(), loadedText.height());
45+
assertEquals(1, tag.chars().size());
46+
assertEquals(tag.chars(), loadedText.chars());
47+
}
48+
49+
@Test
50+
void testNameTagMethod() {
51+
FontSymbol loadedText = GSON.fromJson(TEST_JSON, FontSymbol.class);
52+
assertNotNull(loadedText);
53+
54+
assertTrue(loadedText.hasChars());
55+
assertEquals("\uE120", loadedText.getChar(0));
56+
assertEquals("\uE120", loadedText.chars().getFirst());
57+
58+
assertNull(loadedText.getChar(1));
59+
assertNull(loadedText.getChar(-1));
60+
assertEquals("\uE000", loadedText.getCharOr(1, "\uE000"));
61+
assertEquals("\uE000", loadedText.getCharOr(-1, "\uE000"));
62+
}
63+
1264
@Test
13-
void testFontCreationWithoutFactory() {
14-
Key key = Key.key("vulpes", "test");
15-
FontSymbol fontSymbol = new BitFontSymbol(key, 10, 20, List.of("\u12ca"));
16-
assertNotNull(fontSymbol);
17-
assertInstanceOf(BitFontSymbol.class, fontSymbol);
18-
19-
assertEquals(key, fontSymbol.key());
20-
assertEquals(10, fontSymbol.ascent());
21-
assertEquals(20, fontSymbol.height());
22-
assertEquals(1, fontSymbol.symbols().size());
23-
assertEquals(List.of("\u12ca"), fontSymbol.symbols());
65+
void testEmptyChars() {
66+
FontSymbol tag = new FontSymbol(
67+
"manis:global/player_ranks/admin.png",
68+
"bitmap",
69+
10,
70+
10,
71+
List.of()
72+
);
73+
assertFalse(tag.hasChars());
2474
}
2575
}

0 commit comments

Comments
 (0)