Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* This class is generated in a later step.
* Don't add any kind of logic here.
*/
public sealed interface FontSymbols permits FontSymbol {
public interface FontSymbols {
}
71 changes: 0 additions & 71 deletions src/main/java/net/onelitefeather/vulpes/font/BitFontSymbol.java

This file was deleted.

71 changes: 58 additions & 13 deletions src/main/java/net/onelitefeather/vulpes/font/FontSymbol.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
package net.onelitefeather.vulpes.font;

import net.onelitefeather.vulpes.registries.VulpesKey;
import org.jetbrains.annotations.UnmodifiableView;
import net.minestom.server.codec.Codec;
import net.minestom.server.codec.StructCodec;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public sealed interface FontSymbol extends FontSymbols, VulpesKey permits BitFontSymbol {
/**
* Represents a single font provider entry in a Minecraft resource pack font definition.
* <p>
* Each instance describes how a set of characters is rendered using a specific font provider
* (e.g. bitmap or unicode) as defined in a {@code font.json}.
*
* @param file the resource location of the font texture (e.g. "namespace:path/to/file.png")
* @param type the type of the font provider (e.g. "bitmap" or "unicode")
* @param ascent the ascent of the glyphs, defining the distance from the baseline to the top
* @param height the default height of the glyphs
* @param chars the characters mapped to this provider, typically defining the glyph layout
*/
public record FontSymbol(
String file,
String type,
int ascent,
int height,
List<String> chars
) {

/**
* Returns the symbol ascent.
* Codec for serializing and deserializing {@link FontSymbol} instances to and from JSON,
* following the structure defined in Minecraft's font provider format.
*/
public static final Codec<FontSymbol> CODEC = StructCodec.struct(
"file", Codec.STRING, FontSymbol::file,
"type", Codec.STRING, FontSymbol::type,
"ascent", Codec.INT, FontSymbol::ascent,
"height", Codec.INT, FontSymbol::height,
"chars", Codec.STRING.list(), FontSymbol::chars,
FontSymbol::new
);

/**
* Checks if this font symbol has any characters defined.
*
* @return the ascent
* @return true if the chars list is not empty, false otherwise
*/
int ascent();
public boolean hasChars() {
return !chars.isEmpty();
}

/**
* Returns the symbol height.
* Returns the character at the given index.
*
* @return the height
* @param index the index of the character
* @return the character at the given index or null if the index is out of bounds
*/
int height();
public @Nullable String getChar(int index) {
if (index < 0 || index >= chars.size()) {
return null;
}
return chars.get(index);
}

/**
* Returns all symbols for the font.
* Returns the overwritten character based on the index, otherwise it returns the default value.
*
* @return the symbols
* @param index the index of the character
* @param defaultValue the default value
* @return the overwritten character or the default value
*/
@UnmodifiableView
List<String> symbols();
public String getCharOr(int index, String defaultValue) {
return (index >= 0 && index < this.chars.size())
? this.chars.get(index)
: defaultValue;
}
}
103 changes: 0 additions & 103 deletions src/main/java/net/onelitefeather/vulpes/gson/KeyGsonAdapter.java

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions src/test/java/net/onelitefeather/vulpes/font/FontRegistryTest.java

This file was deleted.

74 changes: 62 additions & 12 deletions src/test/java/net/onelitefeather/vulpes/font/FontSymbolTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.onelitefeather.vulpes.font;

import net.kyori.adventure.key.Key;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand All @@ -9,17 +10,66 @@

class FontSymbolTest {

private static final String TEST_JSON =
"""
{
"file": "manis:global/player_ranks/admin.png",
"type": "bitmap",
"ascent": 10,
"height": 10,
"chars": [
""
]
}
""";

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

@Test
void testNameTagRead() {
FontSymbol tag = new FontSymbol(
"manis:global/player_ranks/admin.png",
"bitmap",
10,
10,
List.of("\uE120")
);

FontSymbol loadedText = GSON.fromJson(TEST_JSON, FontSymbol.class);
assertNotNull(loadedText);

assertEquals(tag.file(), loadedText.file());
assertEquals(tag.type(), loadedText.type());
assertEquals(tag.ascent(), loadedText.ascent());
assertEquals(tag.height(), loadedText.height());
assertEquals(1, tag.chars().size());
assertEquals(tag.chars(), loadedText.chars());
}

@Test
void testNameTagMethod() {
FontSymbol loadedText = GSON.fromJson(TEST_JSON, FontSymbol.class);
assertNotNull(loadedText);

assertTrue(loadedText.hasChars());
assertEquals("\uE120", loadedText.getChar(0));
assertEquals("\uE120", loadedText.chars().getFirst());

assertNull(loadedText.getChar(1));
assertNull(loadedText.getChar(-1));
assertEquals("\uE000", loadedText.getCharOr(1, "\uE000"));
assertEquals("\uE000", loadedText.getCharOr(-1, "\uE000"));
}

@Test
void testFontCreationWithoutFactory() {
Key key = Key.key("vulpes", "test");
FontSymbol fontSymbol = new BitFontSymbol(key, 10, 20, List.of("\u12ca"));
assertNotNull(fontSymbol);
assertInstanceOf(BitFontSymbol.class, fontSymbol);

assertEquals(key, fontSymbol.key());
assertEquals(10, fontSymbol.ascent());
assertEquals(20, fontSymbol.height());
assertEquals(1, fontSymbol.symbols().size());
assertEquals(List.of("\u12ca"), fontSymbol.symbols());
void testEmptyChars() {
FontSymbol tag = new FontSymbol(
"manis:global/player_ranks/admin.png",
"bitmap",
10,
10,
List.of()
);
assertFalse(tag.hasChars());
}
}
Loading