diff --git a/src/main/kotlin/net/mcbrawls/packin/resource/provider/FontProvider.kt b/src/main/kotlin/net/mcbrawls/packin/resource/provider/FontProvider.kt index 264ce7a..73ce2ee 100644 --- a/src/main/kotlin/net/mcbrawls/packin/resource/provider/FontProvider.kt +++ b/src/main/kotlin/net/mcbrawls/packin/resource/provider/FontProvider.kt @@ -8,15 +8,16 @@ import net.mcbrawls.packin.resource.PackResource import net.mcbrawls.packin.resource.pack.PackinResourcePack import net.mcbrawls.packin.resource.pack.ResourceCollector import net.minecraft.util.Identifier +import net.minecraft.util.math.Vec2f -// TODO: add support for a list of shifts /** * A resource provider to add a font. */ class FontProvider( - fontId: Identifier, + val fontId: Identifier, val size: Float, val oversample: Float, + vararg val shifts: Vec2f, ) : ResourceProvider { /** * The file path of the font JSON file. @@ -45,12 +46,19 @@ class FontProvider( val json = createJson() collector.collect(fontJsonPath, json.toString().encodeToByteArray()) + + shifts.forEach { shift -> + val shiftedJson = createJson(shift) + val shiftedFontId = createShiftedFontId(fontId, shift) + val shiftedFontJsonPath = shiftedFontId.withPath { "font/$it.json" } + collector.collect(shiftedFontJsonPath, shiftedJson.toString().encodeToByteArray()) + } } /** * Creates the font JSON file as an object. */ - fun createJson(): JsonObject { + fun createJson(shift: Vec2f? = null): JsonObject { return JsonObject().apply { add("providers", JsonArray().apply { add(JsonObject().apply { @@ -58,8 +66,25 @@ class FontProvider( addProperty("file", fontFilePathInJson) addProperty("size", size) addProperty("oversample", oversample) + + shift?.also { shift -> + add( + "shift", + JsonArray().apply { + add(shift.x) + add(shift.y) + } + ) + } }) }) } } + + companion object { + fun createShiftedFontId(fontId: Identifier, shift: Vec2f): Identifier { + val shiftString = "${shift.x}_${shift.y}" + return fontId.withPath { "${it}_shift_$shiftString" } + } + } } diff --git a/src/test/kotlin/net/mcbrawls/packin/test/PackinTest.kt b/src/test/kotlin/net/mcbrawls/packin/test/PackinTest.kt index 194de8c..c4b89c2 100644 --- a/src/test/kotlin/net/mcbrawls/packin/test/PackinTest.kt +++ b/src/test/kotlin/net/mcbrawls/packin/test/PackinTest.kt @@ -24,6 +24,7 @@ import net.minecraft.command.argument.IdentifierArgumentType import net.minecraft.server.command.CommandManager import net.minecraft.text.Text import net.minecraft.util.Identifier +import net.minecraft.util.math.Vec2f import kotlin.io.path.Path import kotlin.io.path.writeBytes import kotlin.math.floor @@ -48,7 +49,7 @@ object PackinTest : ModInitializer { val packBytes = PackinResourcePack.create(PackMetadata("Test", Text.literal("Test"))) { addProvider(FontProvider(Identifier.of("brawls", "pinch"), 7.0f, 4.0f)) addProvider(FontProvider(Identifier.of("brawls", "love_bug"), 9.0f, 8.0f)) - addProvider(FontProvider(Identifier.of("brawls", "chocolate"), 11.0f, 8.0f)) + addProvider(FontProvider(Identifier.of("brawls", "chocolate"), 11.0f, 8.0f, Vec2f(0.0f, 1.0f))) addProvider( SoundProvider(