From 3aad692f29e04ff91a565413bc7897b8b5b44441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:00:15 +0300 Subject: [PATCH 01/56] pre format. --- README.md | 12 ++ blank/build.gradle.kts | 10 ++ .../main/java/net/infumia/pack/BlankSlot.java | 95 +++++++++++ .../resources/pack-resources/blank_slot.png | Bin 0 -> 1983 bytes build.gradle.kts | 99 +++++------ buildSrc/build.gradle.kts | 16 ++ buildSrc/settings.gradle.kts | 7 + .../main/kotlin/net/infumia/gradle/common.kt | 41 +++++ .../main/kotlin/net/infumia/gradle/publish.kt | 52 ++++++ common/build.gradle.kts | 8 + .../pack/ArbitraryCharacterFactory.java | 14 ++ .../ArbitraryCharacterFactoryReserved.java | 83 +++++++++ .../java/net/infumia/pack/FileResource.java | 15 ++ .../net/infumia/pack/FileResourceAll.java | 20 +++ .../net/infumia/pack/FileResourceAtlas.java | 17 ++ .../infumia/pack/FileResourceCompiler.java | 16 ++ .../pack/FileResourceCompilerDefault.java | 42 +++++ .../infumia/pack/FileResourceCompilers.java | 31 ++++ .../net/infumia/pack/FileResourceFont.java | 17 ++ .../net/infumia/pack/FileResourceModel.java | 17 ++ .../net/infumia/pack/FileResourceTexture.java | 17 ++ .../java/net/infumia/pack/FileResources.java | 78 +++++++++ .../src/main/java/net/infumia/pack/Glyph.java | 24 +++ .../net/infumia/pack/GlyphAppendable.java | 7 + .../java/net/infumia/pack/GlyphColorable.java | 22 +++ .../infumia/pack/GlyphComponentBuilder.java | 160 ++++++++++++++++++ .../pack/GlyphComponentBuilderImpl.java | 85 ++++++++++ .../java/net/infumia/pack/GlyphEmpty.java | 21 +++ .../java/net/infumia/pack/GlyphImage.java | 46 +++++ .../net/infumia/pack/GlyphImageColored.java | 13 ++ .../infumia/pack/GlyphImageColoredImpl.java | 44 +++++ .../java/net/infumia/pack/GlyphImageImpl.java | 103 +++++++++++ .../net/infumia/pack/GlyphImagePrepared.java | 40 +++++ .../java/net/infumia/pack/GlyphSpaces.java | 35 ++++ .../main/java/net/infumia/pack/Glyphs.java | 28 +++ .../main/java/net/infumia/pack/Internal.java | 55 ++++++ .../src/main/java/net/infumia/pack/Kyori.java | 79 +++++++++ .../src/main/java/net/infumia/pack/Lazy.java | 32 ++++ .../src/main/java/net/infumia/pack/Pack.java | 81 +++++++++ .../java/net/infumia/pack/PackDefault.java | 63 +++++++ .../src/main/java/net/infumia/pack/Packs.java | 29 ++++ .../net/infumia/pack/ResourceIdentifier.java | 22 +++ .../infumia/pack/ResourceIdentifierImage.java | 16 ++ .../infumia/pack/ResourceIdentifierImpl.java | 21 +++ .../pack/ResourceIdentifierSpaces.java | 16 ++ .../net/infumia/pack/ResourceProducer.java | 49 ++++++ .../pack/ResourceProducerImageMultichar.java | 63 +++++++ .../ResourceProducerImageMulticharImpl.java | 134 +++++++++++++++ .../pack/ResourceProducerLanguage.java | 72 ++++++++ .../pack/ResourceProducerLanguageImpl.java | 120 +++++++++++++ .../infumia/pack/ResourceProducerSpaces.java | 17 ++ .../pack/ResourceProducerSpacesAbstract.java | 55 ++++++ .../pack/ResourceProducerSpacesDefault.java | 81 +++++++++ .../pack/ResourceProducerSpacesMojang.java | 53 ++++++ .../net/infumia/pack/ResourceProducers.java | 99 +++++++++++ .../net/infumia/pack/TextureProperties.java | 64 +++++++ .../ResourceAlreadyProducedException.java | 6 + .../ResourceNotProducedException.java | 6 + generator/build.gradle.kts | 11 ++ gradle/libs.versions.toml | 9 +- settings.gradle.kts | 2 + 61 files changed, 2528 insertions(+), 62 deletions(-) create mode 100644 blank/build.gradle.kts create mode 100644 blank/src/main/java/net/infumia/pack/BlankSlot.java create mode 100644 blank/src/main/resources/pack-resources/blank_slot.png create mode 100644 buildSrc/build.gradle.kts create mode 100644 buildSrc/settings.gradle.kts create mode 100644 buildSrc/src/main/kotlin/net/infumia/gradle/common.kt create mode 100644 buildSrc/src/main/kotlin/net/infumia/gradle/publish.kt create mode 100644 common/build.gradle.kts create mode 100644 common/src/main/java/net/infumia/pack/ArbitraryCharacterFactory.java create mode 100644 common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java create mode 100644 common/src/main/java/net/infumia/pack/FileResource.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceAll.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceAtlas.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceCompiler.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceCompilers.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceFont.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceModel.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceTexture.java create mode 100644 common/src/main/java/net/infumia/pack/FileResources.java create mode 100644 common/src/main/java/net/infumia/pack/Glyph.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphAppendable.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphColorable.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphEmpty.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphImage.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphImageColored.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphImageImpl.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphImagePrepared.java create mode 100644 common/src/main/java/net/infumia/pack/GlyphSpaces.java create mode 100644 common/src/main/java/net/infumia/pack/Glyphs.java create mode 100644 common/src/main/java/net/infumia/pack/Internal.java create mode 100644 common/src/main/java/net/infumia/pack/Kyori.java create mode 100644 common/src/main/java/net/infumia/pack/Lazy.java create mode 100644 common/src/main/java/net/infumia/pack/Pack.java create mode 100644 common/src/main/java/net/infumia/pack/PackDefault.java create mode 100644 common/src/main/java/net/infumia/pack/Packs.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceIdentifier.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceIdentifierImage.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducer.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerSpaces.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java create mode 100644 common/src/main/java/net/infumia/pack/ResourceProducers.java create mode 100644 common/src/main/java/net/infumia/pack/TextureProperties.java create mode 100644 common/src/main/java/net/infumia/pack/exception/ResourceAlreadyProducedException.java create mode 100644 common/src/main/java/net/infumia/pack/exception/ResourceNotProducedException.java create mode 100644 generator/build.gradle.kts diff --git a/README.md b/README.md index 1418ec7..c1fdeff 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,18 @@ repositories { dependencies { // Base module implementation "net.infumia:pack:VERSION" + // Required, https://mvnrepository.com/artifact/net.kyori/adventure-api/ + implementation "net.kyori:adventure-api:4.17.0" + // Required, https://mvnrepository.com/artifact/team.unnamed/creative-api/ + implementation "team.unnamed:creative-api:1.7.2" + + // Blank Slot (Optional) + implementation "net.infumia:pack-blank:VERSION" + + // Generator (Optional) + implementation "net.infumia:pack-generator:VERSION" + // Required, https://mvnrepository.com/artifact/team.unnamed/creative-serializer-minecraft/ + implementation "team.unnamed:creative-serializer-minecraft:1.7.2" } ``` ### Code diff --git a/blank/build.gradle.kts b/blank/build.gradle.kts new file mode 100644 index 0000000..8b6dc03 --- /dev/null +++ b/blank/build.gradle.kts @@ -0,0 +1,10 @@ +import net.infumia.gradle.publish + +publish("blank") + +dependencies { + compileOnly(project(":common")) + + compileOnly(libs.creative.api) + compileOnly(libs.adventure.api) +} diff --git a/blank/src/main/java/net/infumia/pack/BlankSlot.java b/blank/src/main/java/net/infumia/pack/BlankSlot.java new file mode 100644 index 0000000..5df9c4d --- /dev/null +++ b/blank/src/main/java/net/infumia/pack/BlankSlot.java @@ -0,0 +1,95 @@ +package net.infumia.pack; + +import net.kyori.adventure.key.Key; +import net.kyori.adventure.key.KeyPattern; +import team.unnamed.creative.atlas.Atlas; +import team.unnamed.creative.atlas.AtlasSource; +import team.unnamed.creative.base.Writable; +import team.unnamed.creative.model.ItemOverride; +import team.unnamed.creative.model.ItemPredicate; +import team.unnamed.creative.model.Model; +import team.unnamed.creative.model.ModelTexture; +import team.unnamed.creative.model.ModelTextures; +import team.unnamed.creative.texture.Texture; + +/** + * Utility class for creating and retrieving blank slot file resources. + */ +public final class BlankSlot { + /** + * Retrieves a pre-configured blank slot file resource. + * + * @return A {@link FileResource} representing the blank slot. + */ + public static FileResource get() { + return Internal0.BLANK_SLOT.get(); + } + + /** + * Creates a blank slot file resource with the specified parameters. + * + * @param namespace The namespace for the model. Cannot be null. + * @param itemId The id for the item. Cannot be null. + * @param baseKey The key for the base model. Cannot be null. + * @param blankSlotImage The writable image for the blank slot. Cannot be null. + * @param customModelData The custom model data value. + * @return A {@link FileResource} representing the created blank slot. + */ + public static FileResource create( + @KeyPattern.Namespace final String namespace, + @KeyPattern.Value final String itemId, + final Key baseKey, + final Writable blankSlotImage, + final int customModelData + ) { + final Key itemKey = Key.key(namespace, itemId); + final ModelTextures itemModelTexture = ModelTextures.builder() + .layers(ModelTexture.ofKey(itemKey)) + .build(); + final Model itemModel = Model.model() + .key(itemKey) + .parent(Model.ITEM_GENERATED) + .textures(itemModelTexture) + .build(); + + final ModelTextures baseModelTexture = ModelTextures.builder() + .layers(ModelTexture.ofKey(baseKey)) + .build(); + final ItemOverride baseOverride = ItemOverride.of(itemKey, ItemPredicate.customModelData(customModelData)); + final Model baseItemModel = Model.model() + .key(baseKey) + .parent(Model.ITEM_GENERATED) + .textures(baseModelTexture) + .overrides(baseOverride) + .build(); + + final Texture texture = Texture.texture(Internal.keyWithPngExtension(itemKey), blankSlotImage); + final Atlas atlas = Atlas.atlas() + .key(Atlas.BLOCKS) + .sources(AtlasSource.directory(namespace, namespace + "/")) + .build(); + + return FileResources.all( + FileResources.model(itemModel), + FileResources.model(baseItemModel), + FileResources.texture(texture), + FileResources.atlas(atlas) + ); + } + + private BlankSlot() { + throw new IllegalStateException("Utility class"); + } + + private static final class Internal0 { + private static final Lazy BLANK_SLOT = Lazy.of(() -> + BlankSlot.create( + Internal.DEFAULT_NAMESPACE, + "blank_slot", + Key.key("item/paper"), + Internal.resourceFromJar("pack-resources/blank_slot.png"), + 1 + ) + ); + } +} diff --git a/blank/src/main/resources/pack-resources/blank_slot.png b/blank/src/main/resources/pack-resources/blank_slot.png new file mode 100644 index 0000000000000000000000000000000000000000..0058d7aa78c112d2703b1e87e53f1591e2ab38e9 GIT binary patch literal 1983 zcmbVNZEO=|9KSNBW3W-hz!b^On~@Q@-b?RV?xeEO)~%C{ZmY1NhCO%pbUW7G9e202 zTQFOE;YA4q%pw{I5~ImL`Cy_17c+>0$P7`GOpFOe6aBD+iIXo9#pl|tj>(YN^m@_I|Ci_Aow3HcveNrYF$^n<)Q6hU+wD9hMd-JhT+o7HF25QK#%dK^f_QV?8oZhj zgq6$D?%u&YBMqO#?%h7JBz)@7`jQ`iFqSpNk{3?yiahla-g+ds@4ZNHP`XDu+W&`p zx3=`@@1LEstnOs(mAPAsP7OUDd|`g^%a?vXa7lRJoa-%V?7+6BZIi7>dcNL!^q0|# zixd0j3^eY4yXQzZ=3cht(~3(IB?Dv8;mh3VYyE8HI}Jru?ZZppgF8yU963L9Cv(Ra zZywz2UNA7!_#N+FS2OhLwI#7q^E%dW_Ucymrh$uApmLXRV!pY_wRY(~5Um4DMV z5g$Da+sC6J5!nTFFn5ggLgl^+W%MJ?y*Uz z9jq&3_1~(O1T$&y4%ZCqQ3Z8TK2;T=R~FKiv4@Z;Muy_hTnculsYd zW%U^p$@#Ih@i2N{f1>Iq3_FZ@%f=Utqq)m&ZfwD7e*QLrCUcGYHVeb%Ep#3i_WBzY z7*_nN8gI2*qiaQ3PkDfo1WzVqAT)-pTAeX~+y!l1f*q&M<0=)ufX_lrKmZAlcVMN|9avYxj5XhRTB*f;><9S~BbquWeojVm$*lQ0D}+d^2TfHgXGTemv(S*Q!! zHyA*njYbPLZmK1fDwweBa1U}LACQ}(t$42isb*;D-KGq~JxFGiGpy)kMV@(t_KGx}gGO~lP4v!y z3UQhdX+fm98=$CU6kvn@1S_(b(9IM;4y!590VzZ4An-yM#h{+lO=Q?nH+?;Vx{s+R z)Fj%N*@d!dqfs%USvJsQ7zqUk6dRAKDxxB>K3hM@r}(k-W?$NJ&s23H;Cpy?(^y^*dFcEhMpCwNQ$C^Y^F) zZpn|5DxR$!5y;M+2oSPU3{WBR%j!+yaa)%2=hK}Kk=~+6a@H+9VW)u!YdcWvrm{0@ z5mK{>w?NOWrQcpEbDCyI|98>;AL;q9$elpzfT+PzgyRh5w4z*6sG0f7?oDf3Q*hJ^ zonfH~qe-ESLk&@zs7aswX!L#5;L9SR+IYpF++I() - apply() - - repositories.mavenCentral() - - java { - toolchain { - languageVersion = JavaLanguageVersion.of(8) - } - } +} - tasks { - val javadocJar by creating(Jar::class) { - dependsOn("javadoc") - archiveClassifier.set("javadoc") - from(javadoc) - } +repositories.mavenCentral() - val sourcesJar by creating(Jar::class) { - dependsOn("classes") - archiveClassifier.set("sources") - from(sourceSets["main"].allSource) - } +spotless { + isEnforceCheck = false + lineEndings = com.diffplug.spotless.LineEnding.UNIX + + val prettierConfig = + mapOf( + "prettier" to "3.3.2", + "prettier-plugin-java" to "2.6.0", + ) + + yaml { + target(".github/**/*.yml") + endWithNewline() + trimTrailingWhitespace() + jackson() + .yamlFeature("LITERAL_BLOCK_STYLE", true) + .yamlFeature("SPLIT_LINES", false) } - val moduleName = project.findProperty("artifact-id") as String? - val projectName = "pack${if (moduleName == null) "" else "-$moduleName"}" - val signRequired = project.hasProperty("sign-required") - - extensions.configure { - coordinates(project.group.toString(), projectName, project.version.toString()) - publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, true) - if (signRequired) { - signAllPublications() - } - - pom { - name.set(projectName) - description.set("Minecraft resource pack generator.") - url.set("https://github.com/Infumia/pack") - licenses { - license { - name.set("MIT License") - url.set("https://mit-license.org/license.txt") - } - } - developers { - developer { - id.set("portlek") - name.set("Hasan Demirtaş") - email.set("utsukushihito@outlook.com") - } - } - scm { - connection.set("scm:git:git://github.com/infumia/pack.git") - developerConnection.set("scm:git:ssh://github.com/infumia/pack.git") - url.set("https://github.com/infumia/pack/") - } - } + java { + target( + "**/src/main/java/net/infumia/**/*.java", + ) + importOrder() + removeUnusedImports() + endWithNewline() + trimTrailingWhitespace() + prettier(prettierConfig) + .config( + mapOf( + "parser" to "java", + "tabWidth" to 4, + "useTabs" to false, + "printWidth" to 100, + "plugins" to listOf("prettier-plugin-java"), + ), + ) } } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 0000000..04e63ba --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -0,0 +1,16 @@ +plugins { + `kotlin-dsl` +} + +repositories { + mavenCentral() + gradlePluginPortal() +} + +dependencies { + implementation(libs.nexus.plugin) +} + +kotlin { + jvmToolchain(11) +} diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts new file mode 100644 index 0000000..b5a0fab --- /dev/null +++ b/buildSrc/settings.gradle.kts @@ -0,0 +1,7 @@ +dependencyResolutionManagement { + versionCatalogs { + create("libs") { + from(files("../gradle/libs.versions.toml")) + } + } +} diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt new file mode 100644 index 0000000..f2b45f8 --- /dev/null +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt @@ -0,0 +1,41 @@ +package net.infumia.gradle + +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.api.tasks.bundling.Jar +import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.kotlin.dsl.* + +fun Project.applyCommon( + javaVersion: Int = 8, + sources: Boolean = true, + javadoc: Boolean = true +) { + apply() + + repositories.mavenCentral() + + extensions.configure { + toolchain { + languageVersion = JavaLanguageVersion.of(javaVersion) + } + } + + if (javadoc) { + val javadocJar by tasks.creating(Jar::class) { + dependsOn("javadoc") + archiveClassifier.set("javadoc") + from(javadoc) + } + } + + if (sources) { + val sourceSets = extensions.getByType().sourceSets + val sourcesJar by tasks.creating(Jar::class) { + dependsOn("classes") + archiveClassifier.set("sources") + from(sourceSets["main"].allSource) + } + } +} diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/publish.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/publish.kt new file mode 100644 index 0000000..25b2cc8 --- /dev/null +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/publish.kt @@ -0,0 +1,52 @@ +package net.infumia.gradle + +import com.vanniktech.maven.publish.MavenPublishBaseExtension +import com.vanniktech.maven.publish.MavenPublishPlugin +import com.vanniktech.maven.publish.SonatypeHost +import org.gradle.api.Project +import org.gradle.kotlin.dsl.* + +fun Project.publish( + moduleName: String? = null, + javaVersion: Int = 8, + sources: Boolean = true, + javadoc: Boolean = true +) { + applyCommon(javaVersion, sources, javadoc) + apply() + + val projectName = "pack${if (moduleName == null) "" else "-$moduleName"}" + val signRequired = project.hasProperty("sign-required") + + extensions.configure { + coordinates(project.group.toString(), projectName, project.version.toString()) + publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, true) + if (signRequired) { + signAllPublications() + } + + pom { + name.set(projectName) + description.set("Minecraft resource pack generator.") + url.set("https://github.com/Infumia/pack") + licenses { + license { + name.set("MIT License") + url.set("https://mit-license.org/license.txt") + } + } + developers { + developer { + id.set("portlek") + name.set("Hasan Demirtaş") + email.set("utsukushihito@outlook.com") + } + } + scm { + connection.set("scm:git:git://github.com/infumia/pack.git") + developerConnection.set("scm:git:ssh://github.com/infumia/pack.git") + url.set("https://github.com/infumia/pack/") + } + } + } +} diff --git a/common/build.gradle.kts b/common/build.gradle.kts new file mode 100644 index 0000000..170aef2 --- /dev/null +++ b/common/build.gradle.kts @@ -0,0 +1,8 @@ +import net.infumia.gradle.publish + +publish() + +dependencies { + compileOnly(libs.creative.api) + compileOnly(libs.adventure.api) +} diff --git a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactory.java b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactory.java new file mode 100644 index 0000000..c337cdc --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactory.java @@ -0,0 +1,14 @@ +package net.infumia.pack; + +/** + * Interface for a factory that creates arbitrary characters. + */ +public interface ArbitraryCharacterFactory { + /** + * Creates and returns an arbitrary character. + * + * @return a generated character. + * @throws IllegalStateException if the character range exceeds. + */ + char create() throws IllegalStateException; +} diff --git a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java new file mode 100644 index 0000000..f5751d0 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java @@ -0,0 +1,83 @@ +package net.infumia.pack; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; + +/** + * A factory for creating arbitrary characters, ensuring that reserved characters are not produced. + */ +@SuppressWarnings("UnnecessaryUnicodeEscape") +public final class ArbitraryCharacterFactoryReserved implements ArbitraryCharacterFactory { + private final Collection reserved; + private char startPoint; + + /** + * Constructs a new instance with the specified start point and collection of reserved characters. + * + * @param startPoint the starting character for generating new characters. + * @param reserved the collection of characters that should not be produced by the factory. Cannot be null. + */ + public ArbitraryCharacterFactoryReserved(final char startPoint, final Collection reserved) { + this.startPoint = startPoint; + this.reserved = reserved; + } + + /** + * Constructs a new instance with the specified collection of reserved characters, + * starting from the default character {@literal '\uA201'}. + * + * @param reserved the collection of characters that should not be produced by the factory. Cannot be null. + */ + public ArbitraryCharacterFactoryReserved(final Collection reserved) { + this('\uA201', reserved); + } + + /** + * Constructs a new instance with a default set of reserved characters, + * starting from the default character '\uA201'. + */ + public ArbitraryCharacterFactoryReserved() { + this(Internal.reserved()); + } + + @Override + public char create() throws IllegalStateException { + if (this.startPoint == Character.MAX_VALUE) { + throw new IllegalStateException("Characters range exceeded"); + } + do { + this.startPoint++; + } while (this.reserved.contains(this.startPoint)); + return this.startPoint; + } + + private static final class Internal { + private static final Lazy> RESERVED = Lazy.of(() -> { + final Collection reserved = new HashSet<>(); + for (char c = 'a'; c <= 'z'; c++) { + reserved.add(c); + } + for (char c = 'A'; c <= 'Z'; c++) { + reserved.add(c); + } + for (char c = '0'; c <= '9'; c++) { + reserved.add(c); + } + Collections.addAll( + reserved, + '!', '?', ':', '$', ';', '#', '@', '%', '^', '&', '*', '(', ')', '_', '-', '+', '/', '\\', '"', '\'', + '{', '}', '[', ']', '~', '`', '<', '>', ',', '.', '|', '\n', '\r', '\b', '\f', '\t', ' ', '=' + ); + return reserved; + }); + + private static Collection reserved() { + return Internal.RESERVED.get(); + } + + private Internal() { + throw new IllegalStateException("Utility class"); + } + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResource.java b/common/src/main/java/net/infumia/pack/FileResource.java new file mode 100644 index 0000000..6ced467 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResource.java @@ -0,0 +1,15 @@ +package net.infumia.pack; + +import team.unnamed.creative.ResourcePack; + +/** + * Represents a file resource that can be written to a resource pack. + */ +public interface FileResource { + /** + * Writes the file resource to the specified resource pack. + * + * @param pack the resource pack to which the file resource will be written. Cannot be null. + */ + void write(ResourcePack pack); +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceAll.java b/common/src/main/java/net/infumia/pack/FileResourceAll.java new file mode 100644 index 0000000..c0037a7 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceAll.java @@ -0,0 +1,20 @@ +package net.infumia.pack; + +import team.unnamed.creative.ResourcePack; + +import java.util.Collection; + +final class FileResourceAll implements FileResource { + private final Collection resources; + + FileResourceAll(final Collection resources) { + this.resources = resources; + } + + @Override + public void write(final ResourcePack pack) { + for (final FileResource resource : this.resources) { + resource.write(pack); + } + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceAtlas.java b/common/src/main/java/net/infumia/pack/FileResourceAtlas.java new file mode 100644 index 0000000..454efdb --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceAtlas.java @@ -0,0 +1,17 @@ +package net.infumia.pack; + +import team.unnamed.creative.ResourcePack; +import team.unnamed.creative.atlas.Atlas; + +final class FileResourceAtlas implements FileResource { + private final Atlas atlas; + + FileResourceAtlas(final Atlas atlas) { + this.atlas = atlas; + } + + @Override + public void write(final ResourcePack pack) { + pack.atlas(this.atlas); + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceCompiler.java b/common/src/main/java/net/infumia/pack/FileResourceCompiler.java new file mode 100644 index 0000000..42e7c92 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceCompiler.java @@ -0,0 +1,16 @@ +package net.infumia.pack; + +import java.util.Collection; + +/** + * Interface representing a resource pack compiler that compiles resources from multiple producers into file resources. + */ +public interface FileResourceCompiler { + /** + * Compiles resources produced by the given collection of {@link ResourceProducer}s into {@link FileResource}s. + * + * @param producers the collection of resource producers. Cannot be null. + * @return an unmodifiable collection of compiled file resources. + */ + Collection compile(Collection producers); +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java new file mode 100644 index 0000000..945acb2 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java @@ -0,0 +1,42 @@ +package net.infumia.pack; + +import net.kyori.adventure.key.Key; +import team.unnamed.creative.font.Font; +import team.unnamed.creative.font.FontProvider; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +final class FileResourceCompilerDefault implements FileResourceCompiler { + private final ArbitraryCharacterFactory characterFactory; + + FileResourceCompilerDefault(final ArbitraryCharacterFactory characterFactory) { + this.characterFactory = characterFactory; + } + + @Override + public Collection compile(final Collection producers) { + final Collection resources = new HashSet<>(); + final Collection fontKeys = producers.stream() + .map(ResourceProducer::key) + .collect(Collectors.toSet()); + for (final Key fontKey : fontKeys) { + final List fontProviders = new ArrayList<>(); + for (final ResourceProducer producer : producers) { + if (producer.key().equals(fontKey)) { + producer.produce(this.characterFactory); + fontProviders.addAll(producer.fontProviders()); + final List textureResources = producer.textures().stream() + .map(FileResources::texture) + .collect(Collectors.toList()); + resources.add(FileResources.all(textureResources)); + } + } + resources.add(FileResources.font(Font.font(fontKey, fontProviders))); + } + return resources; + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceCompilers.java b/common/src/main/java/net/infumia/pack/FileResourceCompilers.java new file mode 100644 index 0000000..b3bd9ab --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceCompilers.java @@ -0,0 +1,31 @@ +package net.infumia.pack; + +/** + * Utility class providing factory methods to create instances of {@link FileResourceCompiler}. + */ +public final class FileResourceCompilers { + /** + * Creates a simple {@link FileResourceCompiler} instance with a custom character factory. + * + * @param characterFactory The character factory to be used in the compilation. Cannot be null. + * @return A newly created {@link FileResourceCompiler} instance configured with the provided character factory. + */ + public static FileResourceCompiler simple(final ArbitraryCharacterFactory characterFactory) { + return new FileResourceCompilerDefault(characterFactory); + } + + /** + * Creates a simple PackCompiler instance using a default character factory. + *

+ * Uses an instance of {@link ArbitraryCharacterFactoryReserved} as the default character factory. + * + * @return A newly created {@link FileResourceCompiler} instance configured with a default character factory. + */ + public static FileResourceCompiler simple() { + return FileResourceCompilers.simple(new ArbitraryCharacterFactoryReserved()); + } + + private FileResourceCompilers() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceFont.java b/common/src/main/java/net/infumia/pack/FileResourceFont.java new file mode 100644 index 0000000..8acd06c --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceFont.java @@ -0,0 +1,17 @@ +package net.infumia.pack; + +import team.unnamed.creative.ResourcePack; +import team.unnamed.creative.font.Font; + +final class FileResourceFont implements FileResource { + private final Font font; + + FileResourceFont(final Font font) { + this.font = font; + } + + @Override + public void write(final ResourcePack pack) { + pack.font(this.font); + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceModel.java b/common/src/main/java/net/infumia/pack/FileResourceModel.java new file mode 100644 index 0000000..905b0ee --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceModel.java @@ -0,0 +1,17 @@ +package net.infumia.pack; + +import team.unnamed.creative.ResourcePack; +import team.unnamed.creative.model.Model; + +final class FileResourceModel implements FileResource { + private final Model model; + + FileResourceModel(final Model model) { + this.model = model; + } + + @Override + public void write(final ResourcePack pack) { + pack.model(this.model); + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceTexture.java b/common/src/main/java/net/infumia/pack/FileResourceTexture.java new file mode 100644 index 0000000..77476dd --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceTexture.java @@ -0,0 +1,17 @@ +package net.infumia.pack; + +import team.unnamed.creative.ResourcePack; +import team.unnamed.creative.texture.Texture; + +final class FileResourceTexture implements FileResource { + private final Texture texture; + + FileResourceTexture(final Texture texture) { + this.texture = texture; + } + + @Override + public void write(final ResourcePack pack) { + pack.texture(this.texture); + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResources.java b/common/src/main/java/net/infumia/pack/FileResources.java new file mode 100644 index 0000000..5c8e958 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResources.java @@ -0,0 +1,78 @@ +package net.infumia.pack; + +import team.unnamed.creative.atlas.Atlas; +import team.unnamed.creative.font.Font; +import team.unnamed.creative.model.Model; +import team.unnamed.creative.texture.Texture; + +import java.util.Arrays; +import java.util.Collection; + +/** + * Utility class for creating various types of {@link FileResource} instances. + */ +public final class FileResources { + /** + * Creates a {@link FileResource} for the specified texture. + * + * @param texture the texture to create a file resource for. Cannot be null. + * @return a {@link FileResource} representing the texture. + */ + public static FileResource texture(final Texture texture) { + return new FileResourceTexture(texture); + } + + /** + * Creates a {@link FileResource} for the specified atlas. + * + * @param atlas the atlas to create a file resource for. Cannot be null. + * @return a {@link FileResource} representing the atlas. + */ + public static FileResource atlas(final Atlas atlas) { + return new FileResourceAtlas(atlas); + } + + /** + * Creates a {@link FileResource} for the specified font. + * + * @param font the font to create a file resource for. Cannot be null. + * @return a {@link FileResource} representing the font. + */ + public static FileResource font(final Font font) { + return new FileResourceFont(font); + } + + /** + * Creates a {@link FileResource} for the specified model. + * + * @param model the model to create a file resource for. Cannot be null. + * @return a {@link FileResource} representing the model. + */ + public static FileResource model(final Model model) { + return new FileResourceModel(model); + } + + /** + * Creates a {@link FileResource} for a collection of file resources. + * + * @param resources the collection of resources to create file resources for. Cannot be null. + * @return a {@link FileResource} representing all the resources. + */ + public static FileResource all(final Collection resources) { + return new FileResourceAll(resources); + } + + /** + * Creates a {@link FileResource} for a collection of file resources. + * + * @param resources the collection of resources to create file resources for. Cannot be null. + * @return a {@link FileResource} representing all the resources. + */ + public static FileResource all(final FileResource... resources) { + return FileResources.all(Arrays.asList(resources)); + } + + private FileResources() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/Glyph.java b/common/src/main/java/net/infumia/pack/Glyph.java new file mode 100644 index 0000000..d6b6414 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Glyph.java @@ -0,0 +1,24 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.text.Component; + +/** + * Represents a glyph that can be converted to an adventure component and provides width information. + */ +public interface Glyph { + /** + * Converts the glyph into an adventure component. + * + * @return The adventure component representing this glyph. + * @throws ResourceNotProducedException if the glyph cannot be converted to an adventure component. + */ + Component toAdventure() throws ResourceNotProducedException; + + /** + * Returns the width of the glyph. + * + * @return The width of the glyph as an integer. + */ + int width(); +} diff --git a/common/src/main/java/net/infumia/pack/GlyphAppendable.java b/common/src/main/java/net/infumia/pack/GlyphAppendable.java new file mode 100644 index 0000000..020f684 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphAppendable.java @@ -0,0 +1,7 @@ +package net.infumia.pack; + +/** + * Serves to mark classes that are both Glyphs and can be appended. + */ +public interface GlyphAppendable extends Glyph { +} diff --git a/common/src/main/java/net/infumia/pack/GlyphColorable.java b/common/src/main/java/net/infumia/pack/GlyphColorable.java new file mode 100644 index 0000000..13a9f98 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphColorable.java @@ -0,0 +1,22 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.format.TextColor; + +/** + * Represents an interface for objects that can be colored with text color. + */ +public interface GlyphColorable { + /** + * Retrieves the current text color of the object. + * + * @return The current text color. + */ + TextColor color(); + + /** + * Updates the text color of the object. + * + * @param color The new text color to set. + */ + void updateColor(TextColor color); +} diff --git a/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java b/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java new file mode 100644 index 0000000..facc639 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java @@ -0,0 +1,160 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.List; + +/** + * Interface for building glyph components with various positioning and appending capabilities. + */ +public interface GlyphComponentBuilder { + + /** + * Creates a universal GlyphComponentBuilder with the specified space producer. + * + * @param spacesProducer The space producer to use. + * @return A new GlyphComponentBuilder instance. + */ + static GlyphComponentBuilder universal(final ResourceProducerSpaces spacesProducer) { + return new GlyphComponentBuilderImpl(spacesProducer, 0, Component.text("")); + } + + /** + * Creates a GUI GlyphComponentBuilder with the specified space producer. + * + * @param spacesProducer The space producer to use. + * @return A new GlyphComponentBuilder instance. + */ + static GlyphComponentBuilder gui(final ResourceProducerSpaces spacesProducer) { + return new GlyphComponentBuilderImpl(spacesProducer, -8, Component.text("", NamedTextColor.WHITE)); + } + + /** + * Creates a custom GlyphComponentBuilder with the specified parameters. + * + * @param spacesProducer The space producer to use. + * @param position The initial position. + * @param baseComponent The base component. + * @return A new GlyphComponentBuilder instance. + */ + static GlyphComponentBuilder custom( + final ResourceProducerSpaces spacesProducer, + final int position, + final Component baseComponent + ) { + return new GlyphComponentBuilderImpl(spacesProducer, position, baseComponent); + } + + /** + * Appends a glyph with a specified position type and position. + * + * @param positionType The type of position (absolute or relative). + * @param position The position value. + * @param glyph The glyph to append. + * @return This GlyphComponentBuilder instance. + */ + GlyphComponentBuilder append(PositionType positionType, int position, GlyphAppendable glyph); + + /** + * Appends a glyph with specified position type and default position (0). + * + * @param positionType The type of position (absolute or relative). + * @param glyph The glyph to append. + * @return This GlyphComponentBuilder instance. + */ + default GlyphComponentBuilder append(final PositionType positionType, final GlyphAppendable glyph) { + return this.append(positionType, 0, glyph); + } + + /** + * Appends a list of glyphs with a specified position type and position. + * + * @param positionType The type of position (absolute or relative). + * @param position The position value. + * @param glyphes The list of glyphs to append. + * @return This GlyphComponentBuilder instance. + */ + GlyphComponentBuilder append(PositionType positionType, int position, List glyphes); + + /** + * Appends a list of glyphs with specified position type and default position (0). + * + * @param positionType The type of position (absolute or relative). + * @param glyphList The list of glyphs to append. + * @return This GlyphComponentBuilder instance. + */ + default GlyphComponentBuilder append( + final PositionType positionType, + final List glyphList + ) { + return this.append(positionType, 0, glyphList); + } + + /** + * Appends a glyph with an absolute position type and specified position. + * + * @param position The position value. + * @param glyph The glyph to append. + * @return This GlyphComponentBuilder instance. + */ + default GlyphComponentBuilder append(final int position, final GlyphAppendable glyph) { + return this.append(PositionType.ABSOLUTE, position, glyph); + } + + /** + * Appends a glyph with absolute position type and default position. + * + * @param glyph The glyph to append. + * @return This GlyphComponentBuilder instance. + */ + default GlyphComponentBuilder append(final GlyphAppendable glyph) { + return this.append(PositionType.ABSOLUTE, glyph); + } + + /** + * Appends a list of glyphs with an absolute position type and specified position. + * + * @param position The position value. + * @param glyphList The list of glyphs to append. + * @return This GlyphComponentBuilder instance. + */ + default GlyphComponentBuilder append(final int position, final List glyphList) { + return this.append(PositionType.ABSOLUTE, position, glyphList); + } + + /** + * Appends a list of glyphs with absolute position type and default position. + * + * @param glyphList The list of glyphs to append. + * @return This GlyphComponentBuilder instance. + */ + default GlyphComponentBuilder append(final List glyphList) { + return this.append(PositionType.ABSOLUTE, glyphList); + } + + /** + * Builds the component with an option to keep the initial position. + * + * @param keepInitialPosition Whether to keep the initial position. + * @return The built Component. + */ + Component build(boolean keepInitialPosition); + + /** + * Builds the component keeping the initial position. + * + * @return The built Component. + */ + default Component build() { + return this.build(true); + } + + /** + * Enum representing the type of position for appending glyphs. + */ + enum PositionType { + ABSOLUTE, + RELATIVE + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java b/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java new file mode 100644 index 0000000..d1aff64 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java @@ -0,0 +1,85 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.Component; + +import java.util.ArrayList; +import java.util.List; + +final class GlyphComponentBuilderImpl implements GlyphComponentBuilder { + private final ResourceProducerSpaces spacesProducer; + private final int initialPosition; + private final Component baseComponent; + + private final List glyphs = new ArrayList<>(); + + private int previousElementsWidth; + + public GlyphComponentBuilderImpl( + final ResourceProducerSpaces spacesProducer, + final int initialPosition, + final Component baseComponent + ) { + this.spacesProducer = spacesProducer; + this.initialPosition = initialPosition; + this.baseComponent = baseComponent; + } + + @Override + public GlyphComponentBuilder append( + final PositionType positionType, + final int position, + final GlyphAppendable glyph + ) { + this.preAppend(positionType, position); + + this.glyphs.add(glyph); + this.previousElementsWidth += position + glyph.width(); + + return this; + } + + @Override + public GlyphComponentBuilder append( + final PositionType positionType, + final int position, + final List glyphList + ) { + this.preAppend(positionType, position); + int width = 0; + for (final GlyphAppendable glyph : glyphList) { + this.glyphs.add(glyph); + width += glyph.width(); + } + this.previousElementsWidth += position + width; + return this; + } + + @Override + public Component build(final boolean keepInitialPosition) { + if (keepInitialPosition) { + this.previousElementsWidth += this.initialPosition; + if (this.previousElementsWidth != 0) { + this.glyphs.add(this.spacesProducer.translate((-1) * this.previousElementsWidth)); + } + } + Component component = this.baseComponent; + if (this.initialPosition != 0) { + component = + component.append(this.spacesProducer.translate(this.initialPosition).toAdventure()); + } + for (final Glyph glyph : this.glyphs) { + component = component.append(glyph.toAdventure()); + } + return component; + } + + private void preAppend(final PositionType positionType, final int position) { + if (positionType == PositionType.ABSOLUTE && this.previousElementsWidth != 0) { + this.glyphs.add(this.spacesProducer.translate((-1) * this.previousElementsWidth)); + this.previousElementsWidth = 0; + } + if (position != 0) { + this.glyphs.add(this.spacesProducer.translate(position)); + } + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphEmpty.java b/common/src/main/java/net/infumia/pack/GlyphEmpty.java new file mode 100644 index 0000000..6aa5bf8 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphEmpty.java @@ -0,0 +1,21 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.text.Component; + +final class GlyphEmpty implements GlyphAppendable { + static final GlyphEmpty INSTANCE = new GlyphEmpty(); + + private GlyphEmpty() { + } + + @Override + public Component toAdventure() throws ResourceNotProducedException { + return Component.text(""); + } + + @Override + public int width() { + return 0; + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphImage.java b/common/src/main/java/net/infumia/pack/GlyphImage.java new file mode 100644 index 0000000..0884245 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphImage.java @@ -0,0 +1,46 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import team.unnamed.creative.texture.Texture; + +/** + * Represents an interface for a glyph image that can be appended and serves as a resource producer. + */ +public interface GlyphImage extends GlyphAppendable, ResourceProducer { + /** + * Retrieves the character associated with this glyph image. + * + * @return The character of the glyph image. + * @throws ResourceNotProducedException If the glyph image is not produced. + */ + char character() throws ResourceNotProducedException; + + /** + * Retrieves the texture associated with this glyph image. + * + * @return The texture of the glyph image. + */ + Texture texture(); + + /** + * Creates a colored version of this glyph image with the specified text color. + * + * @param color The text color for the colored glyph image. Cannot be null. + * @return A colored GlyphImageColored instance. + */ + default GlyphImageColored withColor(final TextColor color) { + return new GlyphImageColoredImpl(this, color); + } + + /** + * Converts this glyph image to an Adventure Component. + * + * @return The Adventure Component representing this glyph image. + * @throws ResourceNotProducedException If the glyph image is not produced. + */ + default Component toAdventure() throws ResourceNotProducedException { + return Component.text(this.character()).font(this.key()); + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphImageColored.java b/common/src/main/java/net/infumia/pack/GlyphImageColored.java new file mode 100644 index 0000000..df8c2ce --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphImageColored.java @@ -0,0 +1,13 @@ +package net.infumia.pack; + +/** + * Represents an interface for a colored glyph image that can be appended and has color attributes. + */ +public interface GlyphImageColored extends GlyphAppendable, GlyphColorable { + /** + * Retrieves the original glyph image associated with this colored glyph image. + * + * @return The original glyph image. + */ + GlyphImage original(); +} diff --git a/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java b/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java new file mode 100644 index 0000000..7ce1223 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java @@ -0,0 +1,44 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; + +final class GlyphImageColoredImpl implements GlyphImageColored { + private final GlyphImage original; + private TextColor color; + + GlyphImageColoredImpl(final GlyphImage original, final TextColor color) { + this.original = original; + this.color = color; + } + + @Override + public Component toAdventure() throws ResourceNotProducedException { + Component component = this.original.toAdventure(); + if (this.color != null) { + component = component.color(this.color); + } + return component; + } + + @Override + public int width() { + return this.original.width(); + } + + @Override + public TextColor color() { + return this.color; + } + + @Override + public void updateColor(final TextColor color) { + this.color = color; + } + + @Override + public GlyphImage original() { + return this.original; + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java new file mode 100644 index 0000000..14894b6 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java @@ -0,0 +1,103 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import org.jetbrains.annotations.NotNull; +import team.unnamed.creative.font.BitMapFontProvider; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.texture.Texture; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.Set; + +final class GlyphImageImpl implements GlyphImage { + private final Key key; + private final Texture texture; + private final TextureProperties properties; + + private Character character; + private Set fontProviders; + + private int width = -1; + + GlyphImageImpl(final Key key, final Texture texture, final TextureProperties properties) { + this.key = key; + this.texture = texture; + this.properties = properties; + } + + @NotNull + @Override + public Key key() { + return this.key; + } + + @Override + public boolean produced() { + return this.fontProviders != null; + } + + @Override + public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { + if (this.fontProviders != null) { + throw new ResourceAlreadyProducedException(); + } + final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); + this.character = characterFactory.create(); + fontProviderBuilder.characters(String.valueOf(this.character)); + fontProviderBuilder.file(this.texture.key()); + fontProviderBuilder.ascent(this.properties.ascent()); + fontProviderBuilder.height(this.properties.height()); + this.fontProviders = Collections.singleton(fontProviderBuilder.build()); + } + + @Override + public Collection fontProviders() throws ResourceNotProducedException { + if (this.fontProviders == null) { + throw new ResourceNotProducedException(); + } + return this.fontProviders; + } + + @Override + public Collection textures() throws ResourceNotProducedException { + return Collections.singleton(this.texture); + } + + @Override + public int width() { + if (this.width != -1) { + return this.width; + } + try { + final BufferedImage image = + ImageIO.read(new ByteArrayInputStream(this.texture.data().toByteArray())); + final int fileHeight = image.getHeight(); + this.width = (int) Math.ceil( + ((double) this.properties.height() / (double) fileHeight) * Internal.calculateWidth(image) + ) + Internal.SEPARATOR_WIDTH; + } catch (final IOException e) { + throw new RuntimeException(e); + } + return this.width; + } + + @Override + public char character() throws ResourceNotProducedException { + if (this.fontProviders == null) { + throw new ResourceNotProducedException(); + } + return this.character; + } + + @Override + public Texture texture() { + return this.texture; + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java b/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java new file mode 100644 index 0000000..1358b92 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java @@ -0,0 +1,40 @@ +package net.infumia.pack; + +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; + +final class GlyphImagePrepared implements GlyphAppendable, GlyphColorable { + private final Key key; + private final char character; + private final int width; + private TextColor color; + + GlyphImagePrepared(final Key key, final char character, final int width, final TextColor color) { + this.key = key; + this.character = character; + this.width = width; + this.color = color; + } + + @Override + public Component toAdventure() { + return Component.text(this.character).font(this.key).color(this.color == null ? NamedTextColor.BLACK : this.color); + } + + @Override + public int width() { + return this.width; + } + + @Override + public TextColor color() { + return this.color; + } + + @Override + public void updateColor(final TextColor color) { + this.color = color; + } +} diff --git a/common/src/main/java/net/infumia/pack/GlyphSpaces.java b/common/src/main/java/net/infumia/pack/GlyphSpaces.java new file mode 100644 index 0000000..5460ed7 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/GlyphSpaces.java @@ -0,0 +1,35 @@ +package net.infumia.pack; + +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import team.unnamed.creative.font.Font; + +final class GlyphSpaces implements GlyphAppendable { + static final GlyphAppendable DEFAULT = new GlyphSpaces(Font.MINECRAFT_DEFAULT, " ", 4); + + private final Component component; + private final int length; + + GlyphSpaces(final Component component, final int length) { + this.component = component; + this.length = length; + } + + GlyphSpaces(final Key key, final String text, final int length) { + this(Component.text(text).font(key), length); + } + + GlyphSpaces(final Key key, final char[] text, final int length) { + this(key, new String(text), length); + } + + @Override + public Component toAdventure() { + return this.component; + } + + @Override + public int width() { + return this.length; + } +} diff --git a/common/src/main/java/net/infumia/pack/Glyphs.java b/common/src/main/java/net/infumia/pack/Glyphs.java new file mode 100644 index 0000000..aca6f7d --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Glyphs.java @@ -0,0 +1,28 @@ +package net.infumia.pack; + +/** + * Utility class for working with glyphs. + */ +public final class Glyphs { + /** + * Returns an empty glyph instance. + * + * @return The empty glyph. + */ + public static Glyph empty() { + return GlyphEmpty.INSTANCE; + } + + /** + * Returns a space glyph instance. + * + * @return The space glyph. + */ + public static GlyphAppendable space() { + return GlyphSpaces.DEFAULT; + } + + private Glyphs() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/Internal.java b/common/src/main/java/net/infumia/pack/Internal.java new file mode 100644 index 0000000..361bc42 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Internal.java @@ -0,0 +1,55 @@ +package net.infumia.pack; + +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; + +import java.awt.Color; +import java.awt.image.BufferedImage; +import java.util.List; + +final class Internal { + static final String DEFAULT_NAMESPACE = "glyphs"; + static final Key DEFAULT_SPACES_FONT_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces"); + static final int SEPARATOR_WIDTH = 1; + + static Key keyWithPngExtension(final Key key) { + //noinspection PatternValidation + return Key.key(key.namespace(), key.value().concat(".png")); + } + + static Writable resourceFromJar(final String fileName) { + return Writable.resource(Internal.class.getClassLoader(), fileName); + } + + static int calculateWidth( + final BufferedImage image, + final int fromX, final int fromY, + final int toX, final int toY + ) { + int width; + for (width = toX - 1; width > fromX; width--) { + for (int height = fromY; height < toY; height++) { + if (new Color(image.getRGB(width, height), true).getAlpha() != 0) { + return width - fromX + 1; + } + } + } + return width - fromX + 1; + } + + static int calculateWidth(final BufferedImage image) { + return Internal.calculateWidth(image, 0, 0, image.getWidth(), image.getHeight()); + } + + static char[] toCharArray(final List list) { + final char[] charArray = new char[list.size()]; + for (int i = 0; i < list.size(); i++) { + charArray[i] = list.get(i); + } + return charArray; + } + + private Internal() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/Kyori.java b/common/src/main/java/net/infumia/pack/Kyori.java new file mode 100644 index 0000000..c85a908 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Kyori.java @@ -0,0 +1,79 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.flattener.ComponentFlattener; +import net.kyori.adventure.text.flattener.FlattenerListener; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextColor; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.LinkedList; + +final class Kyori { + static Collection toColoredParts(final Component component) { + final Collection result = new ArrayList<>(); + ComponentFlattener.basic().flatten(component, new ColoredPartsFlattenerListener(result)); + return result; + } + + static final class ColoredComponentTextPart { + private final String text; + private final TextColor color; + + private ColoredComponentTextPart(final String text, final TextColor color) { + this.text = text; + this.color = color; + } + + public String text() { + return this.text; + } + + public TextColor color() { + return this.color; + } + } + + private static final class ColoredPartsFlattenerListener implements FlattenerListener { + private final Deque colors = new LinkedList<>(); + private final Collection result; + + private ColoredPartsFlattenerListener(final Collection result) { + this.result = result; + } + + @Override + public void pushStyle(final Style style) { + final TextColor color = style.color(); + if (color != null) { + this.colors.add(color); + } + } + + @Override + public void component(@NotNull final String text) { + this.result.add(new ColoredComponentTextPart(text, this.current())); + } + + @Override + public void popStyle(@NotNull final Style style) { + final TextColor color = style.color(); + if (color != null) { + this.colors.removeLast(); + } + } + + private TextColor current() { + final TextColor color = this.colors.peekLast(); + return color != null ? color : NamedTextColor.WHITE; + } + } + + private Kyori() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/Lazy.java b/common/src/main/java/net/infumia/pack/Lazy.java new file mode 100644 index 0000000..06e5199 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Lazy.java @@ -0,0 +1,32 @@ +package net.infumia.pack; + +import java.util.function.Supplier; + +final class Lazy implements Supplier { + static Lazy of(final Supplier supplier) { + return new Lazy<>(supplier); + } + + private final Supplier supplier; + + private volatile T value; + + private Lazy(final Supplier supplier) { + this.supplier = supplier; + } + + @Override + public T get() { + T val = this.value; + if (val == null) { + synchronized (this) { + val = this.value; + if (val == null) { + val = this.supplier.get(); + this.value = val; + } + } + } + return val; + } +} diff --git a/common/src/main/java/net/infumia/pack/Pack.java b/common/src/main/java/net/infumia/pack/Pack.java new file mode 100644 index 0000000..22b752c --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Pack.java @@ -0,0 +1,81 @@ +package net.infumia.pack; + +import org.jetbrains.annotations.Contract; +import team.unnamed.creative.ResourcePack; + +import java.util.Collection; + +/** + * An interface represents a collection of resources and provides methods to manage and manipulate them. + */ +public interface Pack { + /** + * Retrieves all file resources in this pack. + * + * @return An unmodifiable collection of {@link FileResource} objects. + */ + Collection all(); + + /** + * Compiles all resources in this pack, if applicable. + */ + void compileAll(); + + /** + * Adds a resource producer identified by the given ID to this pack. + * + * @param id The identifier for the resource producer. Cannot be null. + * @param producer The resource producer to add. Cannot be null. + * @param The type of the resource producer. Cannot be null. + * @return This pack instance. + */ + @Contract("_, _ -> this") + Pack with(ResourceIdentifier id, T producer); + + /** + * Adds a file resource to this pack. + * + * @param resource The file resource to add. Cannot be null. + * @return This pack instance. + */ + @Contract("_ -> this") + Pack with(FileResource resource); + + /** + * Adds Mojang-specific spaces resource to this pack. + * + * @return This pack instance. + */ + @Contract("-> this") + default Pack withMojangSpaces() { + return this.with(ResourceIdentifierSpaces.SPACES, ResourceProducers.spacesMojang()); + } + + /** + * Retrieves the resource producer associated with the given identifier. + * + * @param id The identifier for the resource producer. Cannot be null. + * @param The type of the resource producer. + * @return The resource producer instance. + * @throws IllegalArgumentException If the identifier is not found in the pack. + */ + T get(ResourceIdentifier id) throws IllegalArgumentException; + + /** + * Convenience method to retrieve the Mojang-specific spaces resource producer. + * + * @return The Mojang-specific spaces resource producer. + */ + default ResourceProducerSpaces spaces() { + return this.get(ResourceIdentifierSpaces.SPACES); + } + + /** + * Writes all file resources in this pack to the provided ResourcePack. + * + * @param resourcePack The target ResourcePack to write resources to. Cannot be null. + */ + default void writeAll(final ResourcePack resourcePack) { + this.all().forEach(resource -> resource.write(resourcePack)); + } +} diff --git a/common/src/main/java/net/infumia/pack/PackDefault.java b/common/src/main/java/net/infumia/pack/PackDefault.java new file mode 100644 index 0000000..1c109ae --- /dev/null +++ b/common/src/main/java/net/infumia/pack/PackDefault.java @@ -0,0 +1,63 @@ +package net.infumia.pack; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +final class PackDefault implements Pack { + private final FileResourceCompiler compiler; + private final Map raw = new HashMap<>(); + private final Map compiled = new HashMap<>(); + private final Set resources = new HashSet<>(); + + PackDefault(final FileResourceCompiler compiler) { + this.compiler = compiler; + } + + @Override + public Collection all() { + if (!this.raw.isEmpty()) { + this.compileAll(); + } + return this.resources; + } + + @Override + public void compileAll() { + final Collection resources = this.compiler.compile(this.raw.values()); + this.resources.addAll(resources); + this.compiled.putAll(this.raw); + this.raw.clear(); + } + + @Override + public Pack with(final ResourceIdentifier id, final T producer) { + if (this.raw.containsKey(id.key()) || this.compiled.containsKey(id.key())) { + throw new IllegalArgumentException("Producer with " + id.key() + " identifier already registered"); + } + this.raw.put(id.key(), producer); + return this; + } + + @Override + public Pack with(final FileResource resource) { + this.resources.add(resource); + return this; + } + + @Override + public T get(final ResourceIdentifier id) + throws IllegalArgumentException { + if (!this.compiled.containsKey(id.key())) { + throw new IllegalArgumentException("Producer with " + id.key() + " identifier is not compiled"); + } + final ResourceProducer producer = this.compiled.get(id.key()); + if (!id.type().isAssignableFrom(producer.getClass())) { + throw new IllegalArgumentException("Wrong producer type"); + } + //noinspection unchecked + return (T) producer; + } +} diff --git a/common/src/main/java/net/infumia/pack/Packs.java b/common/src/main/java/net/infumia/pack/Packs.java new file mode 100644 index 0000000..0db076a --- /dev/null +++ b/common/src/main/java/net/infumia/pack/Packs.java @@ -0,0 +1,29 @@ +package net.infumia.pack; + +/** + * Utility class for creating instances of {@link Pack}. + */ +public final class Packs { + /** + * Creates a new Pack instance with the specified file resource compiler. + * + * @param compiler The file resource compiler to use. Cannot be null. + * @return A new Pack instance. + */ + public static Pack create(final FileResourceCompiler compiler) { + return new PackDefault(compiler); + } + + /** + * Creates a new Pack instance with a default simple file resource compiler. + * + * @return A new Pack instance. + */ + public static Pack create() { + return Packs.create(FileResourceCompilers.simple()); + } + + private Packs() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifier.java b/common/src/main/java/net/infumia/pack/ResourceIdentifier.java new file mode 100644 index 0000000..beec92b --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifier.java @@ -0,0 +1,22 @@ +package net.infumia.pack; + +/** + * Represents a resource identifier associated with a specific type of resource producer. + * + * @param The type of resource producer associated with this identifier. + */ +public interface ResourceIdentifier { + /** + * Returns the key associated with this resource identifier. + * + * @return The key as a string. + */ + String key(); + + /** + * Returns the class object representing the type of resource producer associated with this identifier. + * + * @return The class object of type T. + */ + Class type(); +} diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifierImage.java b/common/src/main/java/net/infumia/pack/ResourceIdentifierImage.java new file mode 100644 index 0000000..0c4916c --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifierImage.java @@ -0,0 +1,16 @@ +package net.infumia.pack; + +/** + * Represents a resource identifier specifically for {@link GlyphImage} instances. + */ +public interface ResourceIdentifierImage extends ResourceIdentifier { + /** + * Retrieves the type of resource this identifier represents, which is {@link GlyphImage}. + * + * @return The class object representing {@link GlyphImage}. + */ + @Override + default Class type() { + return GlyphImage.class; + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java b/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java new file mode 100644 index 0000000..e6a2a59 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java @@ -0,0 +1,21 @@ +package net.infumia.pack; + +final class ResourceIdentifierImpl implements ResourceIdentifier { + private final String id; + private final Class type; + + ResourceIdentifierImpl(final String id, final Class type) { + this.id = id; + this.type = type; + } + + @Override + public Class type() { + return this.type; + } + + @Override + public String key() { + return this.id; + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java b/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java new file mode 100644 index 0000000..93dfad5 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java @@ -0,0 +1,16 @@ +package net.infumia.pack; + +/** + * Utility class containing resource identifiers for various resource types. + */ +public final class ResourceIdentifierSpaces { + /** + * Resource identifier for spaces resource producer. + */ + public static final ResourceIdentifier SPACES = + new ResourceIdentifierImpl<>("spaces", ResourceProducerSpaces.class); + + private ResourceIdentifierSpaces() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducer.java b/common/src/main/java/net/infumia/pack/ResourceProducer.java new file mode 100644 index 0000000..44e4224 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducer.java @@ -0,0 +1,49 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Keyed; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.part.ResourcePackPart; +import team.unnamed.creative.texture.Texture; + +import java.util.Collection; +import java.util.Collections; + +/** + * Interface representing a producer of {@link ResourcePackPart}, identified by a unique key. + */ +public interface ResourceProducer extends Keyed { + /** + * Checks if the resource has been produced. + * + * @return {@code true} if the resource has been produced, {@code false} otherwise. + */ + boolean produced(); + + /** + * Produces the resource using the provided {@link ArbitraryCharacterFactory}. + * + * @param characterFactory the character factory used to produce the resource. Cannot be null. + * @throws ResourceAlreadyProducedException if the resource has already been produced. + */ + void produce(ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException; + + /** + * Retrieves the collection of {@link FontProvider} instances associated with the produced resource. + * + * @return an unmodifiable collection of font providers. + * @throws ResourceNotProducedException if the resource has not been produced. + */ + Collection fontProviders() throws ResourceNotProducedException; + + /** + * Retrieves the collection of {@link Texture} instances associated with the produced resource. + * + * @return a unmodifiable collection of textures. + * @throws ResourceNotProducedException if the resource has not been produced. + */ + default Collection textures() throws ResourceNotProducedException { + return Collections.emptyList(); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java b/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java new file mode 100644 index 0000000..cfafc6e --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java @@ -0,0 +1,63 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.format.TextColor; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents an interface for a glyph collection that supports multi-character glyphs. + */ +public interface ResourceProducerImageMultichar extends ResourceProducer { + /** + * Translates a character to a GlyphAppendable with the specified color. + * + * @param character The character to translate. + * @param color The color of the glyph. + * @return A GlyphAppendable representing the translated character. + * @throws IllegalArgumentException If translation fails. + */ + GlyphAppendable translate(char character, TextColor color) throws IllegalArgumentException; + + /** + * Translates a string of text to a list of GlyphAppendable with the specified color. + * + * @param text The text to translate. + * @param color The color of the glyphs. + * @return A list of GlyphAppendable representing the translated text. + * @throws IllegalArgumentException If translation fails. + */ + default List translate(final String text, final TextColor color) throws IllegalArgumentException { + final List glyphs = new ArrayList<>(); + for (final char character : text.toCharArray()) { + if (character == ' ') { + glyphs.add(Glyphs.space()); + } else { + glyphs.add(this.translate(character, color)); + } + } + return glyphs; + } + + /** + * Translates a character to a GlyphAppendable without specifying a color. + * + * @param character The character to translate. + * @return A GlyphAppendable representing the translated character. + * @throws IllegalArgumentException If translation fails. + */ + default GlyphAppendable translate(final char character) throws IllegalArgumentException { + return this.translate(character, null); + } + + /** + * Translates a string of text to a list of GlyphAppendable without specifying a color. + * + * @param text The text to translate. + * @return A list of GlyphAppendable representing the translated text. + * @throws IllegalArgumentException If translation fails. + */ + default List translate(final String text) throws IllegalArgumentException { + return this.translate(text, null); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java b/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java new file mode 100644 index 0000000..028414a --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java @@ -0,0 +1,134 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.format.TextColor; +import org.jetbrains.annotations.NotNull; +import team.unnamed.creative.font.BitMapFontProvider; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.texture.Texture; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +final class ResourceProducerImageMulticharImpl implements ResourceProducerImageMultichar { + private final Map originToChar = new HashMap<>(); + + private final Key key; + private final Texture texture; + private final TextureProperties properties; + private final List charactersMapping; + + private Set fontProviders; + private BufferedImage image; + + ResourceProducerImageMulticharImpl( + final Key key, + final Texture texture, + final TextureProperties properties, + final List charactersMapping + ) { + this.key = key; + this.texture = texture; + this.properties = properties; + this.charactersMapping = charactersMapping; + } + + @NotNull + @Override + public Key key() { + return this.key; + } + + @Override + public boolean produced() { + return this.fontProviders != null; + } + + @Override + public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { + if (this.fontProviders != null) { + throw new ResourceAlreadyProducedException(); + } + final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); + fontProviderBuilder.file(this.texture.key()); + fontProviderBuilder.ascent(this.properties.ascent()); + fontProviderBuilder.height(this.properties.height()); + final List mappingLines = new ArrayList<>(); + for (final String mappingLine : this.charactersMapping) { + final StringBuilder builder = new StringBuilder(); + for (final char character : mappingLine.toCharArray()) { + final char arbitraryCharacter = characterFactory.create(); + this.originToChar.put(character, arbitraryCharacter); + builder.append(arbitraryCharacter); + } + mappingLines.add(builder.toString()); + } + fontProviderBuilder.characters(mappingLines); + this.fontProviders = Collections.singleton(fontProviderBuilder.build()); + } + + @Override + public Collection fontProviders() throws ResourceNotProducedException { + return this.fontProviders; + } + + @Override + public Collection textures() throws ResourceNotProducedException { + return Collections.singleton(this.texture); + } + + @Override + public GlyphImagePrepared translate(final char character, final TextColor color) + throws IllegalArgumentException { + if (!this.originToChar.containsKey(character)) { + throw new IllegalArgumentException("Illegal character: " + character); + } + int width = 0; + for (int lineIndex = 0; lineIndex < this.charactersMapping.size(); lineIndex++) { + final String line = this.charactersMapping.get(lineIndex); + for (int characterIndex = 0; characterIndex < line.toCharArray().length; characterIndex++) { + if (line.charAt(characterIndex) == character) { + if (this.image == null) { + this.cacheImage(); + } + if (this.image == null) { + throw new IllegalArgumentException("Image " + this.texture.key() + " not found"); + } + final int filePartWidth = + this.image.getWidth() / this.charactersMapping.get(0).length(); + final int filePartHeight = this.image.getHeight() / this.charactersMapping.size(); + width = (int) Math.ceil(((double) this.properties.height() / (double) filePartHeight) + * Internal.calculateWidth( + this.image, + filePartWidth * characterIndex, + filePartHeight * lineIndex, + filePartWidth * (characterIndex + 1), + filePartHeight * (lineIndex + 1))) + + Internal.SEPARATOR_WIDTH; + break; + } + } + } + + return new GlyphImagePrepared(this.key, this.originToChar.get(character), width, color); + } + + private void cacheImage() { + try { + this.image = ImageIO.read(new ByteArrayInputStream(this.texture.data().toByteArray())); + } catch (final IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java b/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java new file mode 100644 index 0000000..a3f10d6 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java @@ -0,0 +1,72 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; + +import java.util.List; + +/** + * Represents an interface for a glyph collection that supports language-specific glyphs. + */ +public interface ResourceProducerLanguage extends ResourceProducer { + /** + * Translates a character to a GlyphAppendable with specified height, ascent, and color. + * + * @param height The height of the glyph. + * @param ascent The ascent of the glyph. + * @param character The character to translate. + * @param color The color of the glyph. + * @return A GlyphAppendable representing the translated character. + * @throws IllegalArgumentException If translation fails. + */ + GlyphAppendable translate(int height, int ascent, char character, TextColor color) throws IllegalArgumentException; + + /** + * Translates a string of text to a list of GlyphAppendable with specified height, ascent, and color. + * + * @param height The height of the glyphs. + * @param ascent The ascent of the glyphs. + * @param text The text to translate. + * @param color The color of the glyphs. + * @return A list of GlyphAppendable representing the translated text. + * @throws IllegalArgumentException If translation fails. + */ + List translate(int height, int ascent, String text, TextColor color) throws IllegalArgumentException; + + /** + * Translates a Component to a list of GlyphAppendable with specified height and ascent. + * + * @param height The height of the glyphs. + * @param ascent The ascent of the glyphs. + * @param component The component to translate. + * @return A list of GlyphAppendable representing the translated component. + * @throws IllegalArgumentException If translation fails. + */ + List translate(int height, int ascent, Component component) throws IllegalArgumentException; + + /** + * Translates a character to a GlyphAppendable with specified height and ascent, without color. + * + * @param height The height of the glyph. + * @param ascent The ascent of the glyph. + * @param character The character to translate. + * @return A GlyphAppendable representing the translated character. + * @throws IllegalArgumentException If translation fails. + */ + default GlyphAppendable translate(final int height, final int ascent, final char character) throws IllegalArgumentException { + return this.translate(height, ascent, character, null); + } + + /** + * Translates a string of text to a list of GlyphAppendable with specified height and ascent, without color. + * + * @param height The height of the glyphs. + * @param ascent The ascent of the glyphs. + * @param text The text to translate. + * @return A list of GlyphAppendable representing the translated text. + * @throws IllegalArgumentException If translation fails. + */ + default List translate(final int height, final int ascent, final String text) throws IllegalArgumentException { + return this.translate(height, ascent, text, null); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java b/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java new file mode 100644 index 0000000..88aa895 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java @@ -0,0 +1,120 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import org.jetbrains.annotations.NotNull; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.texture.Texture; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + +final class ResourceProducerLanguageImpl implements ResourceProducerLanguage { + private final Key key; + private final Texture texture; + private final Map propertiesToMultichar; + + private Set fontProviders; + + ResourceProducerLanguageImpl( + final Key key, + final Texture texture, + final List propertiesList, + final List charactersMapping + ) { + this.key = key; + this.texture = texture; + this.propertiesToMultichar = propertiesList.stream() + .collect(Collectors.toMap( + Function.identity(), + properties -> ResourceProducers.multichar(key, texture, properties, charactersMapping)) + ); + } + + @NotNull + @Override + public Key key() { + return this.key; + } + + @Override + public boolean produced() { + return this.fontProviders != null; + } + + @Override + public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { + if (this.fontProviders != null) { + throw new ResourceAlreadyProducedException(); + } + this.fontProviders = new HashSet<>(); + this.propertiesToMultichar.values().forEach(multichar -> { + multichar.produce(characterFactory); + this.fontProviders.addAll(multichar.fontProviders()); + }); + } + + @Override + public Collection fontProviders() throws ResourceNotProducedException { + return this.fontProviders; + } + + @Override + public Collection textures() throws ResourceNotProducedException { + return Collections.singleton(this.texture); + } + + private ResourceProducerImageMultichar getGlyphCollection(final int height, final int ascent) { + final TextureProperties properties = new TextureProperties(height, ascent); + final ResourceProducerImageMultichar glyphCollection = this.propertiesToMultichar.get(properties); + if (glyphCollection == null) { + throw new IllegalArgumentException("Font with " + properties + " not found"); + } + return glyphCollection; + } + + @Override + public GlyphAppendable translate( + final int height, + final int ascent, + final char character, + final TextColor color + ) throws IllegalArgumentException { + return this.getGlyphCollection(height, ascent).translate(character, color); + } + + @Override + public List translate( + final int height, + final int ascent, + final String text, + final TextColor color + ) throws IllegalArgumentException { + return Collections.unmodifiableList(this.getGlyphCollection(height, ascent).translate(text, color)); + } + + @Override + public List translate( + final int height, + final int ascent, + final Component component + ) throws IllegalArgumentException { + final ResourceProducerImageMultichar collection = this.getGlyphCollection(height, ascent); + final Collection textAndColors = Kyori.toColoredParts(component); + return Collections.unmodifiableList( + textAndColors.stream() + .map(parts -> collection.translate(parts.text(), parts.color())) + .flatMap(Collection::stream) + .collect(Collectors.toList()) + ); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpaces.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpaces.java new file mode 100644 index 0000000..00825c7 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpaces.java @@ -0,0 +1,17 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceNotProducedException; + +/** + * Represents a resource producer that generates space glyphs. + */ +public interface ResourceProducerSpaces extends ResourceProducer { + /** + * Translates the specified length into a space glyph. + * + * @param length The length of the space glyph to produce. + * @return The space glyph of the specified length. + * @throws ResourceNotProducedException if the space glyph cannot be produced. + */ + Glyph translate(int length) throws ResourceNotProducedException; +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java new file mode 100644 index 0000000..f22fd8e --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java @@ -0,0 +1,55 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +abstract class ResourceProducerSpacesAbstract implements ResourceProducerSpaces { + private final Key fontKey; + protected Map mapping; + + protected ResourceProducerSpacesAbstract(final Key fontKey) { + this.fontKey = fontKey; + } + + @NotNull + @Override + public final Key key() { + return this.fontKey; + } + + @Override + public boolean produced() { + return this.mapping != null; + } + + @Override + public Glyph translate(final int length) throws ResourceNotProducedException { + if (this.mapping == null) { + throw new ResourceNotProducedException(); + } + if (length == 0) { + return Glyphs.empty(); + } + final int sign = length > 0 ? 1 : -1; + final String binaryString = Integer.toBinaryString(Math.abs(length)); + final List characters = new ArrayList<>(); + int currentRankLength = 1; + for (int index = 0; index < binaryString.length(); index++) { + final char digit = binaryString.charAt(binaryString.length() - index - 1); + if (digit == '1') { + final int partLength = currentRankLength * sign; + if (!this.mapping.containsKey(partLength)) { + throw new IllegalArgumentException("Too much length"); + } + characters.add(this.mapping.get(partLength)); + } + currentRankLength *= 2; + } + return new GlyphSpaces(this.fontKey, Internal.toCharArray(characters), length); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java new file mode 100644 index 0000000..e0c097b --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java @@ -0,0 +1,81 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; +import team.unnamed.creative.font.BitMapFontProvider; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.texture.Texture; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +final class ResourceProducerSpacesDefault extends ResourceProducerSpacesAbstract { + private final Key textureKey; + private final Writable writable; + + private Set textures; + private Set fontProviders; + + ResourceProducerSpacesDefault(final Key fontKey, final Key textureKey, final Writable writable) { + super(fontKey); + this.textureKey = textureKey; + this.writable = writable; + } + + @Override + public boolean produced() { + return this.textures != null; + } + + @Override + public void produce(final ArbitraryCharacterFactory characterFactory) { + if (this.textures != null) { + throw new ResourceAlreadyProducedException(); + } + this.mapping = new HashMap<>(); + final Set fontProviders = new HashSet<>(); + for (int length = 1; length <= 2048; length *= 2) { + fontProviders.add(this.prepareBuilder(characterFactory, length).build()); + fontProviders.add(this.prepareBuilder(characterFactory, length * (-1)).build()); + } + this.textures = Collections.singleton(Texture.texture(this.textureKey, this.writable)); + this.fontProviders = fontProviders; + } + + @Override + public Collection fontProviders() throws ResourceNotProducedException { + if (this.fontProviders == null) { + throw new ResourceNotProducedException(); + } + return this.fontProviders; + } + + @Override + public Collection textures() throws ResourceNotProducedException { + if (this.textures == null) { + throw new ResourceNotProducedException(); + } + return this.textures; + } + + private BitMapFontProvider.Builder prepareBuilder(final ArbitraryCharacterFactory characterFactory, final int length) { + final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); + final char character = characterFactory.create(); + fontProviderBuilder.characters(String.valueOf(character)); + fontProviderBuilder.file(this.textureKey); + if (length > 0) { + fontProviderBuilder.height(length - 1); + fontProviderBuilder.ascent(0); + } else { + fontProviderBuilder.height(length - 2); + fontProviderBuilder.ascent(-32768); + } + this.mapping.put(length, character); + return fontProviderBuilder; + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java new file mode 100644 index 0000000..64f6eec --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java @@ -0,0 +1,53 @@ +package net.infumia.pack; + +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.font.SpaceFontProvider; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; + +final class ResourceProducerSpacesMojang extends ResourceProducerSpacesAbstract { + + private Collection fontProviders; + + ResourceProducerSpacesMojang(final Key key) { + super(key); + } + + @Override + public boolean produced() { + return this.fontProviders != null; + } + + @Override + public void produce(final ArbitraryCharacterFactory characterFactory) { + if (this.fontProviders != null) { + throw new ResourceAlreadyProducedException(); + } + this.mapping = new HashMap<>(); + final SpaceFontProvider.Builder fontProviderBuilder = FontProvider.space(); + for (int length = 1; length <= 2048; length *= 2) { + fontProviderBuilder.advance(this.retrieveCharacter(characterFactory, length), length); + fontProviderBuilder.advance(this.retrieveCharacter(characterFactory, length * (-1)), length * (-1)); + } + this.fontProviders = Collections.singleton(fontProviderBuilder.build()); + } + + @Override + public Collection fontProviders() throws ResourceNotProducedException { + if (this.fontProviders == null) { + throw new ResourceNotProducedException(); + } + return this.fontProviders; + } + + private char retrieveCharacter(final ArbitraryCharacterFactory characterFactory, final int length) { + final char character = characterFactory.create(); + this.mapping.put(length, character); + return character; + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java new file mode 100644 index 0000000..56168b8 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -0,0 +1,99 @@ +package net.infumia.pack; + +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; +import team.unnamed.creative.texture.Texture; + +import java.util.List; + +/** + * Utility class for creating instances of {@link ResourceProducerSpaces}. + */ +public final class ResourceProducers { + /** + * Creates a space producer with specified font key, texture key, and writable. + * + * @param fontKey The font key. Cannot be null. + * @param textureKey The texture key. Cannot be null. + * @param spacesWritable The writable instance for spaces. Cannot be null. + * @return A {@link ResourceProducerSpaces} instance. + */ + public static ResourceProducerSpaces spaces( + final Key fontKey, + final Key textureKey, + final Writable spacesWritable + ) { + return new ResourceProducerSpacesDefault(fontKey, textureKey, spacesWritable); + } + + /** + * Creates a Mojang-specific space producer with the specified key. + * + * @param key The key for the Mojang space producer. Cannot be null. + * @return A {@link ResourceProducerSpaces} instance for Mojang. + */ + public static ResourceProducerSpaces spacesMojang(final Key key) { + return new ResourceProducerSpacesMojang(key); + } + + /** + * Creates a Mojang-specific space producer with default parameters. + * + * @return A {@link ResourceProducerSpaces} instance for Mojang. + */ + public static ResourceProducerSpaces spacesMojang() { + return ResourceProducers.spacesMojang(Internal.DEFAULT_SPACES_FONT_KEY); + } + + /** + * Creates a {@link GlyphImage} instance with the specified key, texture, and properties. + * + * @param key The key associated with the glyph image. Cannot be null. + * @param texture The texture of the glyph image. Cannot be null. + * @param properties The properties of the glyph image. Cannot be null. + * @return A {@link GlyphImage} instance. + */ + public static GlyphImage image(final Key key, final Texture texture, final TextureProperties properties) { + return new GlyphImageImpl(key, texture, properties); + } + + /** + * Creates a {@link ResourceProducerLanguage} instance with the specified parameters. + * + * @param fontKey The key associated with the font. Cannot be null. + * @param texture The texture of the glyph collection. Cannot be null. + * @param propertiesList The list of texture properties for the glyphs. Cannot be null. + * @param charactersMapping The list of character mappings for the glyphs. Cannot be null. + * @return A {@link ResourceProducerLanguage} instance. + */ + public static ResourceProducerLanguage language( + final Key fontKey, + final Texture texture, + final List propertiesList, + final List charactersMapping + ) { + return new ResourceProducerLanguageImpl(fontKey, texture, propertiesList, charactersMapping); + } + + /** + * Creates a {@link ResourceProducerImageMultichar} instance with the specified parameters. + * + * @param fontKey The key associated with the font. Cannot be null. + * @param texture The texture of the glyph collection. Cannot be null. + * @param properties The properties of the texture. Cannot be null. + * @param charactersMapping The list of character mappings for the glyphs. Cannot be null. + * @return A {@link ResourceProducerImageMultichar} instance. + */ + static ResourceProducerImageMultichar multichar( + final Key fontKey, + final Texture texture, + final TextureProperties properties, + final List charactersMapping + ) { + return new ResourceProducerImageMulticharImpl(fontKey, texture, properties, charactersMapping); + } + + private ResourceProducers() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/TextureProperties.java b/common/src/main/java/net/infumia/pack/TextureProperties.java new file mode 100644 index 0000000..fdd4209 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/TextureProperties.java @@ -0,0 +1,64 @@ +package net.infumia.pack; + +import java.util.Objects; + +/** + * Represents the properties of a texture, including its height and ascent. + */ +public final class TextureProperties { + private final int height; + private final int ascent; + + /** + * Ctor. + * + * @param height The height of the texture. + * @param ascent The ascent of the texture. + */ + public TextureProperties(final int height, final int ascent) { + this.height = height; + this.ascent = ascent; + } + + /** + * Retrieves the height of the texture. + * + * @return The height of the texture. + */ + public int height() { + return this.height; + } + + /** + * Retrieves the ascent of the texture. + * + * @return The ascent of the texture. + */ + public int ascent() { + return this.ascent; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || this.getClass() != o.getClass()) { + return false; + } + final TextureProperties that = (TextureProperties) o; + return this.height == that.height && this.ascent == that.ascent; + } + + @Override + public int hashCode() { + return Objects.hash(this.height, this.ascent); + } + + @Override + public String toString() { + return "TextureProperties[" + + "height=" + this.height + ", " + + "ascent=" + this.ascent + ']'; + } +} diff --git a/common/src/main/java/net/infumia/pack/exception/ResourceAlreadyProducedException.java b/common/src/main/java/net/infumia/pack/exception/ResourceAlreadyProducedException.java new file mode 100644 index 0000000..72290f4 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/exception/ResourceAlreadyProducedException.java @@ -0,0 +1,6 @@ +package net.infumia.pack.exception; + +/** + * Exception thrown when an attempt is made to produce a resource that has already been produced. + */ +public final class ResourceAlreadyProducedException extends RuntimeException {} diff --git a/common/src/main/java/net/infumia/pack/exception/ResourceNotProducedException.java b/common/src/main/java/net/infumia/pack/exception/ResourceNotProducedException.java new file mode 100644 index 0000000..9a4a518 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/exception/ResourceNotProducedException.java @@ -0,0 +1,6 @@ +package net.infumia.pack.exception; + +/** + * Exception thrown when an attempt is made to access a resource that has not been produced. + */ +public final class ResourceNotProducedException extends RuntimeException {} diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts new file mode 100644 index 0000000..1206b6b --- /dev/null +++ b/generator/build.gradle.kts @@ -0,0 +1,11 @@ +import net.infumia.gradle.publish + +publish("generator") + +dependencies { + compileOnly(project(":common")) + + compileOnly(libs.creative.api) + compileOnly(libs.creative.serializer) + compileOnly(libs.adventure.api) +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ddccbfa..0610992 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,13 @@ [versions] +creative = "1.7.2" +spotless = "6.25.0" [libraries] +adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0"} +creative-api = { module = "team.unnamed:creative-api", version.ref = "creative"} +creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative"} + +nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } [plugins] -nexus = { id = "com.vanniktech.maven.publish", version = "0.29.0" } +spotless = { id = "com.diffplug.spotless", version.ref = "spotless" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 5787d6e..582b73c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,3 +3,5 @@ plugins { } rootProject.name = "pack" + +include("common", "blank", "generator") From 3b189378a4e0a809fd0b6ed1972a029199bb6d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:06:15 +0300 Subject: [PATCH 02/56] format. --- .github/release-drafter.yml | 47 ++++---- .github/workflows/build.yml | 58 +++++----- .github/workflows/changelog.yml | 24 ++--- .github/workflows/release.yml | 57 +++++----- .../main/java/net/infumia/pack/BlankSlot.java | 101 +++++++++++------- .../ArbitraryCharacterFactoryReserved.java | 47 +++++++- .../net/infumia/pack/FileResourceAll.java | 4 +- .../net/infumia/pack/FileResourceAtlas.java | 1 + .../pack/FileResourceCompilerDefault.java | 15 +-- .../infumia/pack/FileResourceCompilers.java | 1 + .../net/infumia/pack/FileResourceFont.java | 1 + .../net/infumia/pack/FileResourceModel.java | 1 + .../net/infumia/pack/FileResourceTexture.java | 1 + .../java/net/infumia/pack/FileResources.java | 6 +- .../net/infumia/pack/GlyphAppendable.java | 3 +- .../infumia/pack/GlyphComponentBuilder.java | 28 +++-- .../pack/GlyphComponentBuilderImpl.java | 9 +- .../java/net/infumia/pack/GlyphEmpty.java | 4 +- .../infumia/pack/GlyphImageColoredImpl.java | 1 + .../java/net/infumia/pack/GlyphImageImpl.java | 30 +++--- .../net/infumia/pack/GlyphImagePrepared.java | 12 ++- .../java/net/infumia/pack/GlyphSpaces.java | 1 + .../main/java/net/infumia/pack/Glyphs.java | 1 + .../main/java/net/infumia/pack/Internal.java | 12 ++- .../src/main/java/net/infumia/pack/Kyori.java | 12 ++- .../src/main/java/net/infumia/pack/Lazy.java | 1 + .../src/main/java/net/infumia/pack/Pack.java | 3 +- .../java/net/infumia/pack/PackDefault.java | 14 ++- .../src/main/java/net/infumia/pack/Packs.java | 1 + .../infumia/pack/ResourceIdentifierImpl.java | 1 + .../pack/ResourceIdentifierSpaces.java | 1 + .../net/infumia/pack/ResourceProducer.java | 8 +- .../pack/ResourceProducerImageMultichar.java | 6 +- .../ResourceProducerImageMulticharImpl.java | 55 ++++++---- .../pack/ResourceProducerLanguage.java | 18 ++-- .../pack/ResourceProducerLanguageImpl.java | 56 ++++++---- .../pack/ResourceProducerSpacesAbstract.java | 8 +- .../pack/ResourceProducerSpacesDefault.java | 23 ++-- .../pack/ResourceProducerSpacesMojang.java | 17 +-- .../net/infumia/pack/ResourceProducers.java | 24 ++++- .../net/infumia/pack/TextureProperties.java | 7 +- 41 files changed, 439 insertions(+), 281 deletions(-) diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml index 14700a2..cda804b 100644 --- a/.github/release-drafter.yml +++ b/.github/release-drafter.yml @@ -1,38 +1,37 @@ -name-template: '$RESOLVED_VERSION' -tag-template: '$RESOLVED_VERSION' +--- +name-template: "$RESOLVED_VERSION" +tag-template: "$RESOLVED_VERSION" prerelease: true template: | # What's Changed $CHANGES categories: - - title: Breaking - label: breaking - - title: New - label: enhancement - - title: Bug Fixes - label: bug - - title: Maintenance - label: maintenance - - title: Documentation - label: documentation - - title: Dependency Updates - label: dependencies - +- title: "Breaking" + label: "breaking" +- title: "New" + label: "enhancement" +- title: "Bug Fixes" + label: "bug" +- title: "Maintenance" + label: "maintenance" +- title: "Documentation" + label: "documentation" +- title: "Dependency Updates" + label: "dependencies" version-resolver: major: labels: - - breaking + - "breaking" minor: labels: - - enhancement + - "enhancement" patch: labels: - - bug - - maintenance - - documentation - - dependencies - - security - + - "bug" + - "maintenance" + - "documentation" + - "dependencies" + - "security" exclude-labels: - - skip-changelog +- "skip-changelog" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a3881a9..1a7bcea 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,35 +1,31 @@ -name: Build - -on: +--- +name: "Build" +"on": pull_request: - branches: [ master ] - + branches: + - "master" jobs: build: - name: Build - runs-on: ubuntu-latest + name: "Build" + runs-on: "ubuntu-latest" steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-java@v4 - with: - distribution: 'adopt' - java-version: '11' - - - uses: actions/cache@v4 - with: - path: ~/.gradle/caches - key: ${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle.kts') }} - restore-keys: | - ${{ runner.os }}-gradle- - - - uses: actions/cache@v4 - with: - path: ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradlew- - - - run: | - chmod +x gradlew - ./gradlew build + - uses: "actions/checkout@v4" + - uses: "actions/setup-java@v4" + with: + distribution: "adopt" + java-version: "11" + - uses: "actions/cache@v4" + with: + path: "~/.gradle/caches" + key: "${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle.kts') }}" + restore-keys: | + ${{ runner.os }}-gradle- + - uses: "actions/cache@v4" + with: + path: "~/.gradle/wrapper" + key: "${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle-wrapper.properties') }}" + restore-keys: | + ${{ runner.os }}-gradlew- + - run: | + chmod +x gradlew + ./gradlew build diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml index fef8afa..1e80163 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/changelog.yml @@ -1,16 +1,16 @@ -name: Update Changelog - -on: +--- +name: "Update Changelog" +"on": push: - branches: [ master ] - + branches: + - "master" jobs: changelog: - name: Update Changelog - runs-on: ubuntu-latest - if: ${{ !contains(github.event.head_commit.message, 'skip-snapshot') }} + name: "Update Changelog" + runs-on: "ubuntu-latest" + if: "${{ !contains(github.event.head_commit.message, 'skip-snapshot') }}" steps: - - uses: release-drafter/release-drafter@master - id: release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - uses: "release-drafter/release-drafter@master" + id: "release" + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2ed5670..0e5a0f7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,35 +1,32 @@ -name: Release -on: +--- +name: "Release" +"on": release: types: - - released + - "released" jobs: build: - name: Release - runs-on: ubuntu-latest + name: "Release" + runs-on: "ubuntu-latest" steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-java@v4 - with: - distribution: 'adopt' - java-version: '11' - - - uses: actions/cache@v4 - with: - path: ~/.gradle/caches - key: ${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle.kts') }} - restore-keys: | - ${{ runner.os }}-gradle- - - - uses: actions/cache@v4 - with: - path: ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradlew- - - - run: | - [[ "${{ github.event.release.tag_name }}" =~ ^[0-9]+(\.[0-9]+)*$ ]] || exit -1 - chmod +x gradlew - ./gradlew -Psign-required=true publish -Pversion="${{ github.event.release.tag_name }}" -PmavenCentralUsername="${{ secrets.MAVEN_CENTRAL_USERNAME }}" -PmavenCentralPassword="${{ secrets.MAVEN_CENTRAL_PASSWORD }}" -PsigningInMemoryKey="${{ secrets.GPG_PRIVATE_KEY_ARMORED }}" -PsigningInMemoryKeyPassword="${{ secrets.GPG_PASSPHRASE }}" + - uses: "actions/checkout@v4" + - uses: "actions/setup-java@v4" + with: + distribution: "adopt" + java-version: "11" + - uses: "actions/cache@v4" + with: + path: "~/.gradle/caches" + key: "${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle.kts') }}" + restore-keys: | + ${{ runner.os }}-gradle- + - uses: "actions/cache@v4" + with: + path: "~/.gradle/wrapper" + key: "${{ runner.os }}-gradle-wrapper-${{ hashFiles('**/gradle-wrapper.properties') }}" + restore-keys: | + ${{ runner.os }}-gradlew- + - run: | + [[ "${{ github.event.release.tag_name }}" =~ ^[0-9]+(\.[0-9]+)*$ ]] || exit -1 + chmod +x gradlew + ./gradlew -Psign-required=true publish -Pversion="${{ github.event.release.tag_name }}" -PmavenCentralUsername="${{ secrets.MAVEN_CENTRAL_USERNAME }}" -PmavenCentralPassword="${{ secrets.MAVEN_CENTRAL_PASSWORD }}" -PsigningInMemoryKey="${{ secrets.GPG_PRIVATE_KEY_ARMORED }}" -PsigningInMemoryKeyPassword="${{ secrets.GPG_PASSPHRASE }}" diff --git a/blank/src/main/java/net/infumia/pack/BlankSlot.java b/blank/src/main/java/net/infumia/pack/BlankSlot.java index 5df9c4d..7ef46cc 100644 --- a/blank/src/main/java/net/infumia/pack/BlankSlot.java +++ b/blank/src/main/java/net/infumia/pack/BlankSlot.java @@ -16,6 +16,7 @@ * Utility class for creating and retrieving blank slot file resources. */ public final class BlankSlot { + /** * Retrieves a pre-configured blank slot file resource. * @@ -25,6 +26,51 @@ public static FileResource get() { return Internal0.BLANK_SLOT.get(); } + /** + * Creates a blank slot file resource with the specified parameters. + * + * @param itemKey The key for the item. Cannot be null. + * @param baseKey The key for the base model. Cannot be null. + * @param blankSlotImage The writable image for the blank slot. Cannot be null. + * @param customModelData The custom model data value. + * @return A {@link FileResource} representing the created blank slot. + */ + public static FileResource create( + final Key itemKey, + final Key baseKey, + final Writable blankSlotImage, + final int customModelData + ) { + return FileResources.all( + FileResources.model( + Model.model() + .key(itemKey) + .parent(Model.ITEM_GENERATED) + .textures(ModelTextures.builder().layers(ModelTexture.ofKey(itemKey)).build()) + .build() + ), + FileResources.model( + Model.model() + .key(baseKey) + .parent(Model.ITEM_GENERATED) + .textures(ModelTextures.builder().layers(ModelTexture.ofKey(baseKey)).build()) + .overrides( + ItemOverride.of(itemKey, ItemPredicate.customModelData(customModelData)) + ) + .build() + ), + FileResources.texture( + Texture.texture(Internal.keyWithPngExtension(itemKey), blankSlotImage) + ), + FileResources.atlas( + Atlas.atlas() + .key(Atlas.BLOCKS) + .sources(AtlasSource.directory(itemKey.namespace(), itemKey.namespace() + "/")) + .build() + ) + ); + } + /** * Creates a blank slot file resource with the specified parameters. * @@ -42,38 +88,11 @@ public static FileResource create( final Writable blankSlotImage, final int customModelData ) { - final Key itemKey = Key.key(namespace, itemId); - final ModelTextures itemModelTexture = ModelTextures.builder() - .layers(ModelTexture.ofKey(itemKey)) - .build(); - final Model itemModel = Model.model() - .key(itemKey) - .parent(Model.ITEM_GENERATED) - .textures(itemModelTexture) - .build(); - - final ModelTextures baseModelTexture = ModelTextures.builder() - .layers(ModelTexture.ofKey(baseKey)) - .build(); - final ItemOverride baseOverride = ItemOverride.of(itemKey, ItemPredicate.customModelData(customModelData)); - final Model baseItemModel = Model.model() - .key(baseKey) - .parent(Model.ITEM_GENERATED) - .textures(baseModelTexture) - .overrides(baseOverride) - .build(); - - final Texture texture = Texture.texture(Internal.keyWithPngExtension(itemKey), blankSlotImage); - final Atlas atlas = Atlas.atlas() - .key(Atlas.BLOCKS) - .sources(AtlasSource.directory(namespace, namespace + "/")) - .build(); - - return FileResources.all( - FileResources.model(itemModel), - FileResources.model(baseItemModel), - FileResources.texture(texture), - FileResources.atlas(atlas) + return BlankSlot.create( + Key.key(namespace, itemId), + baseKey, + blankSlotImage, + customModelData ); } @@ -82,14 +101,16 @@ private BlankSlot() { } private static final class Internal0 { - private static final Lazy BLANK_SLOT = Lazy.of(() -> - BlankSlot.create( - Internal.DEFAULT_NAMESPACE, - "blank_slot", - Key.key("item/paper"), - Internal.resourceFromJar("pack-resources/blank_slot.png"), - 1 - ) + + private static final Lazy BLANK_SLOT = Lazy.of( + () -> + BlankSlot.create( + Internal.DEFAULT_NAMESPACE, + "blank_slot", + Key.key("item/paper"), + Internal.resourceFromJar("pack-resources/blank_slot.png"), + 1 + ) ); } } diff --git a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java index f5751d0..e89a711 100644 --- a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java +++ b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java @@ -9,6 +9,7 @@ */ @SuppressWarnings("UnnecessaryUnicodeEscape") public final class ArbitraryCharacterFactoryReserved implements ArbitraryCharacterFactory { + private final Collection reserved; private char startPoint; @@ -18,7 +19,10 @@ public final class ArbitraryCharacterFactoryReserved implements ArbitraryCharact * @param startPoint the starting character for generating new characters. * @param reserved the collection of characters that should not be produced by the factory. Cannot be null. */ - public ArbitraryCharacterFactoryReserved(final char startPoint, final Collection reserved) { + public ArbitraryCharacterFactoryReserved( + final char startPoint, + final Collection reserved + ) { this.startPoint = startPoint; this.reserved = reserved; } @@ -53,6 +57,7 @@ public char create() throws IllegalStateException { } private static final class Internal { + private static final Lazy> RESERVED = Lazy.of(() -> { final Collection reserved = new HashSet<>(); for (char c = 'a'; c <= 'z'; c++) { @@ -66,8 +71,44 @@ private static final class Internal { } Collections.addAll( reserved, - '!', '?', ':', '$', ';', '#', '@', '%', '^', '&', '*', '(', ')', '_', '-', '+', '/', '\\', '"', '\'', - '{', '}', '[', ']', '~', '`', '<', '>', ',', '.', '|', '\n', '\r', '\b', '\f', '\t', ' ', '=' + '!', + '?', + ':', + '$', + ';', + '#', + '@', + '%', + '^', + '&', + '*', + '(', + ')', + '_', + '-', + '+', + '/', + '\\', + '"', + '\'', + '{', + '}', + '[', + ']', + '~', + '`', + '<', + '>', + ',', + '.', + '|', + '\n', + '\r', + '\b', + '\f', + '\t', + ' ', + '=' ); return reserved; }); diff --git a/common/src/main/java/net/infumia/pack/FileResourceAll.java b/common/src/main/java/net/infumia/pack/FileResourceAll.java index c0037a7..c3ef45b 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceAll.java +++ b/common/src/main/java/net/infumia/pack/FileResourceAll.java @@ -1,10 +1,10 @@ package net.infumia.pack; -import team.unnamed.creative.ResourcePack; - import java.util.Collection; +import team.unnamed.creative.ResourcePack; final class FileResourceAll implements FileResource { + private final Collection resources; FileResourceAll(final Collection resources) { diff --git a/common/src/main/java/net/infumia/pack/FileResourceAtlas.java b/common/src/main/java/net/infumia/pack/FileResourceAtlas.java index 454efdb..e5ca3c9 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceAtlas.java +++ b/common/src/main/java/net/infumia/pack/FileResourceAtlas.java @@ -4,6 +4,7 @@ import team.unnamed.creative.atlas.Atlas; final class FileResourceAtlas implements FileResource { + private final Atlas atlas; FileResourceAtlas(final Atlas atlas) { diff --git a/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java index 945acb2..466e01a 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java +++ b/common/src/main/java/net/infumia/pack/FileResourceCompilerDefault.java @@ -1,16 +1,16 @@ package net.infumia.pack; -import net.kyori.adventure.key.Key; -import team.unnamed.creative.font.Font; -import team.unnamed.creative.font.FontProvider; - import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.font.Font; +import team.unnamed.creative.font.FontProvider; final class FileResourceCompilerDefault implements FileResourceCompiler { + private final ArbitraryCharacterFactory characterFactory; FileResourceCompilerDefault(final ArbitraryCharacterFactory characterFactory) { @@ -20,7 +20,8 @@ final class FileResourceCompilerDefault implements FileResourceCompiler { @Override public Collection compile(final Collection producers) { final Collection resources = new HashSet<>(); - final Collection fontKeys = producers.stream() + final Collection fontKeys = producers + .stream() .map(ResourceProducer::key) .collect(Collectors.toSet()); for (final Key fontKey : fontKeys) { @@ -29,7 +30,9 @@ public Collection compile(final Collection produ if (producer.key().equals(fontKey)) { producer.produce(this.characterFactory); fontProviders.addAll(producer.fontProviders()); - final List textureResources = producer.textures().stream() + final List textureResources = producer + .textures() + .stream() .map(FileResources::texture) .collect(Collectors.toList()); resources.add(FileResources.all(textureResources)); diff --git a/common/src/main/java/net/infumia/pack/FileResourceCompilers.java b/common/src/main/java/net/infumia/pack/FileResourceCompilers.java index b3bd9ab..94cc236 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceCompilers.java +++ b/common/src/main/java/net/infumia/pack/FileResourceCompilers.java @@ -4,6 +4,7 @@ * Utility class providing factory methods to create instances of {@link FileResourceCompiler}. */ public final class FileResourceCompilers { + /** * Creates a simple {@link FileResourceCompiler} instance with a custom character factory. * diff --git a/common/src/main/java/net/infumia/pack/FileResourceFont.java b/common/src/main/java/net/infumia/pack/FileResourceFont.java index 8acd06c..9e80481 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceFont.java +++ b/common/src/main/java/net/infumia/pack/FileResourceFont.java @@ -4,6 +4,7 @@ import team.unnamed.creative.font.Font; final class FileResourceFont implements FileResource { + private final Font font; FileResourceFont(final Font font) { diff --git a/common/src/main/java/net/infumia/pack/FileResourceModel.java b/common/src/main/java/net/infumia/pack/FileResourceModel.java index 905b0ee..6f44cb6 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceModel.java +++ b/common/src/main/java/net/infumia/pack/FileResourceModel.java @@ -4,6 +4,7 @@ import team.unnamed.creative.model.Model; final class FileResourceModel implements FileResource { + private final Model model; FileResourceModel(final Model model) { diff --git a/common/src/main/java/net/infumia/pack/FileResourceTexture.java b/common/src/main/java/net/infumia/pack/FileResourceTexture.java index 77476dd..b6cddde 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceTexture.java +++ b/common/src/main/java/net/infumia/pack/FileResourceTexture.java @@ -4,6 +4,7 @@ import team.unnamed.creative.texture.Texture; final class FileResourceTexture implements FileResource { + private final Texture texture; FileResourceTexture(final Texture texture) { diff --git a/common/src/main/java/net/infumia/pack/FileResources.java b/common/src/main/java/net/infumia/pack/FileResources.java index 5c8e958..5cc6bb4 100644 --- a/common/src/main/java/net/infumia/pack/FileResources.java +++ b/common/src/main/java/net/infumia/pack/FileResources.java @@ -1,17 +1,17 @@ package net.infumia.pack; +import java.util.Arrays; +import java.util.Collection; import team.unnamed.creative.atlas.Atlas; import team.unnamed.creative.font.Font; import team.unnamed.creative.model.Model; import team.unnamed.creative.texture.Texture; -import java.util.Arrays; -import java.util.Collection; - /** * Utility class for creating various types of {@link FileResource} instances. */ public final class FileResources { + /** * Creates a {@link FileResource} for the specified texture. * diff --git a/common/src/main/java/net/infumia/pack/GlyphAppendable.java b/common/src/main/java/net/infumia/pack/GlyphAppendable.java index 020f684..e54cb4f 100644 --- a/common/src/main/java/net/infumia/pack/GlyphAppendable.java +++ b/common/src/main/java/net/infumia/pack/GlyphAppendable.java @@ -3,5 +3,4 @@ /** * Serves to mark classes that are both Glyphs and can be appended. */ -public interface GlyphAppendable extends Glyph { -} +public interface GlyphAppendable extends Glyph {} diff --git a/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java b/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java index facc639..a375a0d 100644 --- a/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java +++ b/common/src/main/java/net/infumia/pack/GlyphComponentBuilder.java @@ -1,15 +1,13 @@ package net.infumia.pack; +import java.util.List; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import java.util.List; - /** * Interface for building glyph components with various positioning and appending capabilities. */ public interface GlyphComponentBuilder { - /** * Creates a universal GlyphComponentBuilder with the specified space producer. * @@ -27,7 +25,11 @@ static GlyphComponentBuilder universal(final ResourceProducerSpaces spacesProduc * @return A new GlyphComponentBuilder instance. */ static GlyphComponentBuilder gui(final ResourceProducerSpaces spacesProducer) { - return new GlyphComponentBuilderImpl(spacesProducer, -8, Component.text("", NamedTextColor.WHITE)); + return new GlyphComponentBuilderImpl( + spacesProducer, + -8, + Component.text("", NamedTextColor.WHITE) + ); } /** @@ -63,7 +65,10 @@ static GlyphComponentBuilder custom( * @param glyph The glyph to append. * @return This GlyphComponentBuilder instance. */ - default GlyphComponentBuilder append(final PositionType positionType, final GlyphAppendable glyph) { + default GlyphComponentBuilder append( + final PositionType positionType, + final GlyphAppendable glyph + ) { return this.append(positionType, 0, glyph); } @@ -75,7 +80,11 @@ default GlyphComponentBuilder append(final PositionType positionType, final Glyp * @param glyphes The list of glyphs to append. * @return This GlyphComponentBuilder instance. */ - GlyphComponentBuilder append(PositionType positionType, int position, List glyphes); + GlyphComponentBuilder append( + PositionType positionType, + int position, + List glyphes + ); /** * Appends a list of glyphs with specified position type and default position (0). @@ -119,7 +128,10 @@ default GlyphComponentBuilder append(final GlyphAppendable glyph) { * @param glyphList The list of glyphs to append. * @return This GlyphComponentBuilder instance. */ - default GlyphComponentBuilder append(final int position, final List glyphList) { + default GlyphComponentBuilder append( + final int position, + final List glyphList + ) { return this.append(PositionType.ABSOLUTE, position, glyphList); } @@ -155,6 +167,6 @@ default Component build() { */ enum PositionType { ABSOLUTE, - RELATIVE + RELATIVE, } } diff --git a/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java b/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java index d1aff64..423b8b8 100644 --- a/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java +++ b/common/src/main/java/net/infumia/pack/GlyphComponentBuilderImpl.java @@ -1,11 +1,11 @@ package net.infumia.pack; -import net.kyori.adventure.text.Component; - import java.util.ArrayList; import java.util.List; +import net.kyori.adventure.text.Component; final class GlyphComponentBuilderImpl implements GlyphComponentBuilder { + private final ResourceProducerSpaces spacesProducer; private final int initialPosition; private final Component baseComponent; @@ -64,8 +64,9 @@ public Component build(final boolean keepInitialPosition) { } Component component = this.baseComponent; if (this.initialPosition != 0) { - component = - component.append(this.spacesProducer.translate(this.initialPosition).toAdventure()); + component = component.append( + this.spacesProducer.translate(this.initialPosition).toAdventure() + ); } for (final Glyph glyph : this.glyphs) { component = component.append(glyph.toAdventure()); diff --git a/common/src/main/java/net/infumia/pack/GlyphEmpty.java b/common/src/main/java/net/infumia/pack/GlyphEmpty.java index 6aa5bf8..b21ee5e 100644 --- a/common/src/main/java/net/infumia/pack/GlyphEmpty.java +++ b/common/src/main/java/net/infumia/pack/GlyphEmpty.java @@ -4,10 +4,10 @@ import net.kyori.adventure.text.Component; final class GlyphEmpty implements GlyphAppendable { + static final GlyphEmpty INSTANCE = new GlyphEmpty(); - private GlyphEmpty() { - } + private GlyphEmpty() {} @Override public Component toAdventure() throws ResourceNotProducedException { diff --git a/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java b/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java index 7ce1223..586ee1f 100644 --- a/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java +++ b/common/src/main/java/net/infumia/pack/GlyphImageColoredImpl.java @@ -5,6 +5,7 @@ import net.kyori.adventure.text.format.TextColor; final class GlyphImageColoredImpl implements GlyphImageColored { + private final GlyphImage original; private TextColor color; diff --git a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java index 14894b6..cdb0676 100644 --- a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java +++ b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java @@ -1,5 +1,12 @@ package net.infumia.pack; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.Set; +import javax.imageio.ImageIO; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; import net.kyori.adventure.key.Key; @@ -8,15 +15,8 @@ import team.unnamed.creative.font.FontProvider; import team.unnamed.creative.texture.Texture; -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.Set; - final class GlyphImageImpl implements GlyphImage { + private final Key key; private final Texture texture; private final TextureProperties properties; @@ -44,7 +44,8 @@ public boolean produced() { } @Override - public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { + public void produce(final ArbitraryCharacterFactory characterFactory) + throws ResourceAlreadyProducedException { if (this.fontProviders != null) { throw new ResourceAlreadyProducedException(); } @@ -76,12 +77,15 @@ public int width() { return this.width; } try { - final BufferedImage image = - ImageIO.read(new ByteArrayInputStream(this.texture.data().toByteArray())); + final BufferedImage image = ImageIO.read( + new ByteArrayInputStream(this.texture.data().toByteArray()) + ); final int fileHeight = image.getHeight(); this.width = (int) Math.ceil( - ((double) this.properties.height() / (double) fileHeight) * Internal.calculateWidth(image) - ) + Internal.SEPARATOR_WIDTH; + ((double) this.properties.height() / (double) fileHeight) * + Internal.calculateWidth(image) + ) + + Internal.SEPARATOR_WIDTH; } catch (final IOException e) { throw new RuntimeException(e); } diff --git a/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java b/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java index 1358b92..0be44e0 100644 --- a/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java +++ b/common/src/main/java/net/infumia/pack/GlyphImagePrepared.java @@ -6,12 +6,18 @@ import net.kyori.adventure.text.format.TextColor; final class GlyphImagePrepared implements GlyphAppendable, GlyphColorable { + private final Key key; private final char character; private final int width; private TextColor color; - GlyphImagePrepared(final Key key, final char character, final int width, final TextColor color) { + GlyphImagePrepared( + final Key key, + final char character, + final int width, + final TextColor color + ) { this.key = key; this.character = character; this.width = width; @@ -20,7 +26,9 @@ final class GlyphImagePrepared implements GlyphAppendable, GlyphColorable { @Override public Component toAdventure() { - return Component.text(this.character).font(this.key).color(this.color == null ? NamedTextColor.BLACK : this.color); + return Component.text(this.character) + .font(this.key) + .color(this.color == null ? NamedTextColor.BLACK : this.color); } @Override diff --git a/common/src/main/java/net/infumia/pack/GlyphSpaces.java b/common/src/main/java/net/infumia/pack/GlyphSpaces.java index 5460ed7..dd55550 100644 --- a/common/src/main/java/net/infumia/pack/GlyphSpaces.java +++ b/common/src/main/java/net/infumia/pack/GlyphSpaces.java @@ -5,6 +5,7 @@ import team.unnamed.creative.font.Font; final class GlyphSpaces implements GlyphAppendable { + static final GlyphAppendable DEFAULT = new GlyphSpaces(Font.MINECRAFT_DEFAULT, " ", 4); private final Component component; diff --git a/common/src/main/java/net/infumia/pack/Glyphs.java b/common/src/main/java/net/infumia/pack/Glyphs.java index aca6f7d..27412c9 100644 --- a/common/src/main/java/net/infumia/pack/Glyphs.java +++ b/common/src/main/java/net/infumia/pack/Glyphs.java @@ -4,6 +4,7 @@ * Utility class for working with glyphs. */ public final class Glyphs { + /** * Returns an empty glyph instance. * diff --git a/common/src/main/java/net/infumia/pack/Internal.java b/common/src/main/java/net/infumia/pack/Internal.java index 361bc42..a84f9c1 100644 --- a/common/src/main/java/net/infumia/pack/Internal.java +++ b/common/src/main/java/net/infumia/pack/Internal.java @@ -1,13 +1,13 @@ package net.infumia.pack; -import net.kyori.adventure.key.Key; -import team.unnamed.creative.base.Writable; - import java.awt.Color; import java.awt.image.BufferedImage; import java.util.List; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; final class Internal { + static final String DEFAULT_NAMESPACE = "glyphs"; static final Key DEFAULT_SPACES_FONT_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces"); static final int SEPARATOR_WIDTH = 1; @@ -23,8 +23,10 @@ static Writable resourceFromJar(final String fileName) { static int calculateWidth( final BufferedImage image, - final int fromX, final int fromY, - final int toX, final int toY + final int fromX, + final int fromY, + final int toX, + final int toY ) { int width; for (width = toX - 1; width > fromX; width--) { diff --git a/common/src/main/java/net/infumia/pack/Kyori.java b/common/src/main/java/net/infumia/pack/Kyori.java index c85a908..5d65ca8 100644 --- a/common/src/main/java/net/infumia/pack/Kyori.java +++ b/common/src/main/java/net/infumia/pack/Kyori.java @@ -1,5 +1,9 @@ package net.infumia.pack; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.LinkedList; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.flattener.FlattenerListener; @@ -8,12 +12,8 @@ import net.kyori.adventure.text.format.TextColor; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Deque; -import java.util.LinkedList; - final class Kyori { + static Collection toColoredParts(final Component component) { final Collection result = new ArrayList<>(); ComponentFlattener.basic().flatten(component, new ColoredPartsFlattenerListener(result)); @@ -21,6 +21,7 @@ static Collection toColoredParts(final Component compo } static final class ColoredComponentTextPart { + private final String text; private final TextColor color; @@ -39,6 +40,7 @@ public TextColor color() { } private static final class ColoredPartsFlattenerListener implements FlattenerListener { + private final Deque colors = new LinkedList<>(); private final Collection result; diff --git a/common/src/main/java/net/infumia/pack/Lazy.java b/common/src/main/java/net/infumia/pack/Lazy.java index 06e5199..fad6e54 100644 --- a/common/src/main/java/net/infumia/pack/Lazy.java +++ b/common/src/main/java/net/infumia/pack/Lazy.java @@ -3,6 +3,7 @@ import java.util.function.Supplier; final class Lazy implements Supplier { + static Lazy of(final Supplier supplier) { return new Lazy<>(supplier); } diff --git a/common/src/main/java/net/infumia/pack/Pack.java b/common/src/main/java/net/infumia/pack/Pack.java index 22b752c..1d31c87 100644 --- a/common/src/main/java/net/infumia/pack/Pack.java +++ b/common/src/main/java/net/infumia/pack/Pack.java @@ -1,10 +1,9 @@ package net.infumia.pack; +import java.util.Collection; import org.jetbrains.annotations.Contract; import team.unnamed.creative.ResourcePack; -import java.util.Collection; - /** * An interface represents a collection of resources and provides methods to manage and manipulate them. */ diff --git a/common/src/main/java/net/infumia/pack/PackDefault.java b/common/src/main/java/net/infumia/pack/PackDefault.java index 1c109ae..46c2a14 100644 --- a/common/src/main/java/net/infumia/pack/PackDefault.java +++ b/common/src/main/java/net/infumia/pack/PackDefault.java @@ -7,6 +7,7 @@ import java.util.Set; final class PackDefault implements Pack { + private final FileResourceCompiler compiler; private final Map raw = new HashMap<>(); private final Map compiled = new HashMap<>(); @@ -33,9 +34,14 @@ public void compileAll() { } @Override - public Pack with(final ResourceIdentifier id, final T producer) { + public Pack with( + final ResourceIdentifier id, + final T producer + ) { if (this.raw.containsKey(id.key()) || this.compiled.containsKey(id.key())) { - throw new IllegalArgumentException("Producer with " + id.key() + " identifier already registered"); + throw new IllegalArgumentException( + "Producer with " + id.key() + " identifier already registered" + ); } this.raw.put(id.key(), producer); return this; @@ -51,7 +57,9 @@ public Pack with(final FileResource resource) { public T get(final ResourceIdentifier id) throws IllegalArgumentException { if (!this.compiled.containsKey(id.key())) { - throw new IllegalArgumentException("Producer with " + id.key() + " identifier is not compiled"); + throw new IllegalArgumentException( + "Producer with " + id.key() + " identifier is not compiled" + ); } final ResourceProducer producer = this.compiled.get(id.key()); if (!id.type().isAssignableFrom(producer.getClass())) { diff --git a/common/src/main/java/net/infumia/pack/Packs.java b/common/src/main/java/net/infumia/pack/Packs.java index 0db076a..50d8221 100644 --- a/common/src/main/java/net/infumia/pack/Packs.java +++ b/common/src/main/java/net/infumia/pack/Packs.java @@ -4,6 +4,7 @@ * Utility class for creating instances of {@link Pack}. */ public final class Packs { + /** * Creates a new Pack instance with the specified file resource compiler. * diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java b/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java index e6a2a59..82fbed1 100644 --- a/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifierImpl.java @@ -1,6 +1,7 @@ package net.infumia.pack; final class ResourceIdentifierImpl implements ResourceIdentifier { + private final String id; private final Class type; diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java b/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java index 93dfad5..78225c6 100644 --- a/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java @@ -4,6 +4,7 @@ * Utility class containing resource identifiers for various resource types. */ public final class ResourceIdentifierSpaces { + /** * Resource identifier for spaces resource producer. */ diff --git a/common/src/main/java/net/infumia/pack/ResourceProducer.java b/common/src/main/java/net/infumia/pack/ResourceProducer.java index 44e4224..2ae3be3 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducer.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducer.java @@ -1,5 +1,7 @@ package net.infumia.pack; +import java.util.Collection; +import java.util.Collections; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; import net.kyori.adventure.key.Keyed; @@ -7,9 +9,6 @@ import team.unnamed.creative.part.ResourcePackPart; import team.unnamed.creative.texture.Texture; -import java.util.Collection; -import java.util.Collections; - /** * Interface representing a producer of {@link ResourcePackPart}, identified by a unique key. */ @@ -27,7 +26,8 @@ public interface ResourceProducer extends Keyed { * @param characterFactory the character factory used to produce the resource. Cannot be null. * @throws ResourceAlreadyProducedException if the resource has already been produced. */ - void produce(ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException; + void produce(ArbitraryCharacterFactory characterFactory) + throws ResourceAlreadyProducedException; /** * Retrieves the collection of {@link FontProvider} instances associated with the produced resource. diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java b/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java index cfafc6e..850cfec 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java @@ -1,9 +1,8 @@ package net.infumia.pack; -import net.kyori.adventure.text.format.TextColor; - import java.util.ArrayList; import java.util.List; +import net.kyori.adventure.text.format.TextColor; /** * Represents an interface for a glyph collection that supports multi-character glyphs. @@ -27,7 +26,8 @@ public interface ResourceProducerImageMultichar extends ResourceProducer { * @return A list of GlyphAppendable representing the translated text. * @throws IllegalArgumentException If translation fails. */ - default List translate(final String text, final TextColor color) throws IllegalArgumentException { + default List translate(final String text, final TextColor color) + throws IllegalArgumentException { final List glyphs = new ArrayList<>(); for (final char character : text.toCharArray()) { if (character == ' ') { diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java b/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java index 028414a..7415e3c 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java @@ -1,15 +1,5 @@ package net.infumia.pack; -import net.infumia.pack.exception.ResourceAlreadyProducedException; -import net.infumia.pack.exception.ResourceNotProducedException; -import net.kyori.adventure.key.Key; -import net.kyori.adventure.text.format.TextColor; -import org.jetbrains.annotations.NotNull; -import team.unnamed.creative.font.BitMapFontProvider; -import team.unnamed.creative.font.FontProvider; -import team.unnamed.creative.texture.Texture; - -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -20,8 +10,18 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.imageio.ImageIO; +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.format.TextColor; +import org.jetbrains.annotations.NotNull; +import team.unnamed.creative.font.BitMapFontProvider; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.texture.Texture; final class ResourceProducerImageMulticharImpl implements ResourceProducerImageMultichar { + private final Map originToChar = new HashMap<>(); private final Key key; @@ -56,7 +56,8 @@ public boolean produced() { } @Override - public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { + public void produce(final ArbitraryCharacterFactory characterFactory) + throws ResourceAlreadyProducedException { if (this.fontProviders != null) { throw new ResourceAlreadyProducedException(); } @@ -97,25 +98,35 @@ public GlyphImagePrepared translate(final char character, final TextColor color) int width = 0; for (int lineIndex = 0; lineIndex < this.charactersMapping.size(); lineIndex++) { final String line = this.charactersMapping.get(lineIndex); - for (int characterIndex = 0; characterIndex < line.toCharArray().length; characterIndex++) { + for ( + int characterIndex = 0; + characterIndex < line.toCharArray().length; + characterIndex++ + ) { if (line.charAt(characterIndex) == character) { if (this.image == null) { this.cacheImage(); } if (this.image == null) { - throw new IllegalArgumentException("Image " + this.texture.key() + " not found"); + throw new IllegalArgumentException( + "Image " + this.texture.key() + " not found" + ); } final int filePartWidth = this.image.getWidth() / this.charactersMapping.get(0).length(); - final int filePartHeight = this.image.getHeight() / this.charactersMapping.size(); - width = (int) Math.ceil(((double) this.properties.height() / (double) filePartHeight) - * Internal.calculateWidth( - this.image, - filePartWidth * characterIndex, - filePartHeight * lineIndex, - filePartWidth * (characterIndex + 1), - filePartHeight * (lineIndex + 1))) - + Internal.SEPARATOR_WIDTH; + final int filePartHeight = + this.image.getHeight() / this.charactersMapping.size(); + width = (int) Math.ceil( + ((double) this.properties.height() / (double) filePartHeight) * + Internal.calculateWidth( + this.image, + filePartWidth * characterIndex, + filePartHeight * lineIndex, + filePartWidth * (characterIndex + 1), + filePartHeight * (lineIndex + 1) + ) + ) + + Internal.SEPARATOR_WIDTH; break; } } diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java b/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java index a3f10d6..68145ff 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java @@ -1,10 +1,9 @@ package net.infumia.pack; +import java.util.List; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; -import java.util.List; - /** * Represents an interface for a glyph collection that supports language-specific glyphs. */ @@ -19,7 +18,8 @@ public interface ResourceProducerLanguage extends ResourceProducer { * @return A GlyphAppendable representing the translated character. * @throws IllegalArgumentException If translation fails. */ - GlyphAppendable translate(int height, int ascent, char character, TextColor color) throws IllegalArgumentException; + GlyphAppendable translate(int height, int ascent, char character, TextColor color) + throws IllegalArgumentException; /** * Translates a string of text to a list of GlyphAppendable with specified height, ascent, and color. @@ -31,7 +31,8 @@ public interface ResourceProducerLanguage extends ResourceProducer { * @return A list of GlyphAppendable representing the translated text. * @throws IllegalArgumentException If translation fails. */ - List translate(int height, int ascent, String text, TextColor color) throws IllegalArgumentException; + List translate(int height, int ascent, String text, TextColor color) + throws IllegalArgumentException; /** * Translates a Component to a list of GlyphAppendable with specified height and ascent. @@ -42,7 +43,8 @@ public interface ResourceProducerLanguage extends ResourceProducer { * @return A list of GlyphAppendable representing the translated component. * @throws IllegalArgumentException If translation fails. */ - List translate(int height, int ascent, Component component) throws IllegalArgumentException; + List translate(int height, int ascent, Component component) + throws IllegalArgumentException; /** * Translates a character to a GlyphAppendable with specified height and ascent, without color. @@ -53,7 +55,8 @@ public interface ResourceProducerLanguage extends ResourceProducer { * @return A GlyphAppendable representing the translated character. * @throws IllegalArgumentException If translation fails. */ - default GlyphAppendable translate(final int height, final int ascent, final char character) throws IllegalArgumentException { + default GlyphAppendable translate(final int height, final int ascent, final char character) + throws IllegalArgumentException { return this.translate(height, ascent, character, null); } @@ -66,7 +69,8 @@ default GlyphAppendable translate(final int height, final int ascent, final char * @return A list of GlyphAppendable representing the translated text. * @throws IllegalArgumentException If translation fails. */ - default List translate(final int height, final int ascent, final String text) throws IllegalArgumentException { + default List translate(final int height, final int ascent, final String text) + throws IllegalArgumentException { return this.translate(height, ascent, text, null); } } diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java b/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java index 88aa895..af000c7 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java @@ -1,14 +1,5 @@ package net.infumia.pack; -import net.infumia.pack.exception.ResourceAlreadyProducedException; -import net.infumia.pack.exception.ResourceNotProducedException; -import net.kyori.adventure.key.Key; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.TextColor; -import org.jetbrains.annotations.NotNull; -import team.unnamed.creative.font.FontProvider; -import team.unnamed.creative.texture.Texture; - import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -17,8 +8,17 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import net.infumia.pack.exception.ResourceAlreadyProducedException; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import org.jetbrains.annotations.NotNull; +import team.unnamed.creative.font.FontProvider; +import team.unnamed.creative.texture.Texture; final class ResourceProducerLanguageImpl implements ResourceProducerLanguage { + private final Key key; private final Texture texture; private final Map propertiesToMultichar; @@ -33,10 +33,14 @@ final class ResourceProducerLanguageImpl implements ResourceProducerLanguage { ) { this.key = key; this.texture = texture; - this.propertiesToMultichar = propertiesList.stream() - .collect(Collectors.toMap( - Function.identity(), - properties -> ResourceProducers.multichar(key, texture, properties, charactersMapping)) + this.propertiesToMultichar = propertiesList + .stream() + .collect( + Collectors.toMap( + Function.identity(), + properties -> + ResourceProducers.multichar(key, texture, properties, charactersMapping) + ) ); } @@ -52,15 +56,17 @@ public boolean produced() { } @Override - public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { + public void produce(final ArbitraryCharacterFactory characterFactory) + throws ResourceAlreadyProducedException { if (this.fontProviders != null) { throw new ResourceAlreadyProducedException(); } this.fontProviders = new HashSet<>(); - this.propertiesToMultichar.values().forEach(multichar -> { - multichar.produce(characterFactory); - this.fontProviders.addAll(multichar.fontProviders()); - }); + this.propertiesToMultichar.values() + .forEach(multichar -> { + multichar.produce(characterFactory); + this.fontProviders.addAll(multichar.fontProviders()); + }); } @Override @@ -75,7 +81,8 @@ public Collection textures() throws ResourceNotProducedException { private ResourceProducerImageMultichar getGlyphCollection(final int height, final int ascent) { final TextureProperties properties = new TextureProperties(height, ascent); - final ResourceProducerImageMultichar glyphCollection = this.propertiesToMultichar.get(properties); + final ResourceProducerImageMultichar glyphCollection = + this.propertiesToMultichar.get(properties); if (glyphCollection == null) { throw new IllegalArgumentException("Font with " + properties + " not found"); } @@ -99,7 +106,9 @@ public List translate( final String text, final TextColor color ) throws IllegalArgumentException { - return Collections.unmodifiableList(this.getGlyphCollection(height, ascent).translate(text, color)); + return Collections.unmodifiableList( + this.getGlyphCollection(height, ascent).translate(text, color) + ); } @Override @@ -109,9 +118,12 @@ public List translate( final Component component ) throws IllegalArgumentException { final ResourceProducerImageMultichar collection = this.getGlyphCollection(height, ascent); - final Collection textAndColors = Kyori.toColoredParts(component); + final Collection textAndColors = Kyori.toColoredParts( + component + ); return Collections.unmodifiableList( - textAndColors.stream() + textAndColors + .stream() .map(parts -> collection.translate(parts.text(), parts.color())) .flatMap(Collection::stream) .collect(Collectors.toList()) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java index f22fd8e..2f339e9 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java @@ -1,14 +1,14 @@ package net.infumia.pack; -import net.infumia.pack.exception.ResourceNotProducedException; -import net.kyori.adventure.key.Key; -import org.jetbrains.annotations.NotNull; - import java.util.ArrayList; import java.util.List; import java.util.Map; +import net.infumia.pack.exception.ResourceNotProducedException; +import net.kyori.adventure.key.Key; +import org.jetbrains.annotations.NotNull; abstract class ResourceProducerSpacesAbstract implements ResourceProducerSpaces { + private final Key fontKey; protected Map mapping; diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java index e0c097b..6269bb2 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java @@ -1,5 +1,10 @@ package net.infumia.pack; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; import net.kyori.adventure.key.Key; @@ -8,20 +13,19 @@ import team.unnamed.creative.font.FontProvider; import team.unnamed.creative.texture.Texture; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; - final class ResourceProducerSpacesDefault extends ResourceProducerSpacesAbstract { + private final Key textureKey; private final Writable writable; private Set textures; private Set fontProviders; - ResourceProducerSpacesDefault(final Key fontKey, final Key textureKey, final Writable writable) { + ResourceProducerSpacesDefault( + final Key fontKey, + final Key textureKey, + final Writable writable + ) { super(fontKey); this.textureKey = textureKey; this.writable = writable; @@ -63,7 +67,10 @@ public Collection textures() throws ResourceNotProducedException { return this.textures; } - private BitMapFontProvider.Builder prepareBuilder(final ArbitraryCharacterFactory characterFactory, final int length) { + private BitMapFontProvider.Builder prepareBuilder( + final ArbitraryCharacterFactory characterFactory, + final int length + ) { final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); final char character = characterFactory.create(); fontProviderBuilder.characters(String.valueOf(character)); diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java index 64f6eec..a265988 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java @@ -1,15 +1,14 @@ package net.infumia.pack; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; import net.kyori.adventure.key.Key; import team.unnamed.creative.font.FontProvider; import team.unnamed.creative.font.SpaceFontProvider; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; - final class ResourceProducerSpacesMojang extends ResourceProducerSpacesAbstract { private Collection fontProviders; @@ -32,7 +31,10 @@ public void produce(final ArbitraryCharacterFactory characterFactory) { final SpaceFontProvider.Builder fontProviderBuilder = FontProvider.space(); for (int length = 1; length <= 2048; length *= 2) { fontProviderBuilder.advance(this.retrieveCharacter(characterFactory, length), length); - fontProviderBuilder.advance(this.retrieveCharacter(characterFactory, length * (-1)), length * (-1)); + fontProviderBuilder.advance( + this.retrieveCharacter(characterFactory, length * (-1)), + length * (-1) + ); } this.fontProviders = Collections.singleton(fontProviderBuilder.build()); } @@ -45,7 +47,10 @@ public Collection fontProviders() throws ResourceNotProducedExcept return this.fontProviders; } - private char retrieveCharacter(final ArbitraryCharacterFactory characterFactory, final int length) { + private char retrieveCharacter( + final ArbitraryCharacterFactory characterFactory, + final int length + ) { final char character = characterFactory.create(); this.mapping.put(length, character); return character; diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 56168b8..2c00993 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -1,15 +1,15 @@ package net.infumia.pack; +import java.util.List; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; import team.unnamed.creative.texture.Texture; -import java.util.List; - /** * Utility class for creating instances of {@link ResourceProducerSpaces}. */ public final class ResourceProducers { + /** * Creates a space producer with specified font key, texture key, and writable. * @@ -53,7 +53,11 @@ public static ResourceProducerSpaces spacesMojang() { * @param properties The properties of the glyph image. Cannot be null. * @return A {@link GlyphImage} instance. */ - public static GlyphImage image(final Key key, final Texture texture, final TextureProperties properties) { + public static GlyphImage image( + final Key key, + final Texture texture, + final TextureProperties properties + ) { return new GlyphImageImpl(key, texture, properties); } @@ -72,7 +76,12 @@ public static ResourceProducerLanguage language( final List propertiesList, final List charactersMapping ) { - return new ResourceProducerLanguageImpl(fontKey, texture, propertiesList, charactersMapping); + return new ResourceProducerLanguageImpl( + fontKey, + texture, + propertiesList, + charactersMapping + ); } /** @@ -90,7 +99,12 @@ static ResourceProducerImageMultichar multichar( final TextureProperties properties, final List charactersMapping ) { - return new ResourceProducerImageMulticharImpl(fontKey, texture, properties, charactersMapping); + return new ResourceProducerImageMulticharImpl( + fontKey, + texture, + properties, + charactersMapping + ); } private ResourceProducers() { diff --git a/common/src/main/java/net/infumia/pack/TextureProperties.java b/common/src/main/java/net/infumia/pack/TextureProperties.java index fdd4209..c2e29b6 100644 --- a/common/src/main/java/net/infumia/pack/TextureProperties.java +++ b/common/src/main/java/net/infumia/pack/TextureProperties.java @@ -6,6 +6,7 @@ * Represents the properties of a texture, including its height and ascent. */ public final class TextureProperties { + private final int height; private final int ascent; @@ -57,8 +58,8 @@ public int hashCode() { @Override public String toString() { - return "TextureProperties[" + - "height=" + this.height + ", " + - "ascent=" + this.ascent + ']'; + return ( + "TextureProperties[" + "height=" + this.height + ", " + "ascent=" + this.ascent + ']' + ); } } From 52ea09c0c56015702a41e8681547e79a21b036f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:21:01 +0300 Subject: [PATCH 03/56] format *.kts files. --- build.gradle.kts | 26 ++++++++++++++++++++++---- buildSrc/build.gradle.kts | 12 +++--------- buildSrc/settings.gradle.kts | 4 +--- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d629562..ed31e73 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,9 +3,7 @@ plugins { alias(libs.plugins.spotless) } -subprojects { - apply() -} +subprojects { apply() } repositories.mavenCentral() @@ -28,9 +26,29 @@ spotless { .yamlFeature("SPLIT_LINES", false) } + kotlinGradle { + target( + "buildSrc/**/*.gradle.kts", + "*.gradle.kts", + *subprojects + .map { it.name } + .map { "$it/*.gradle.kts" } + .toTypedArray(), + ) + ktfmt().kotlinlangStyle().configure { + it.setMaxWidth(80) + it.setBlockIndent(4) + it.setContinuationIndent(4) + it.setRemoveUnusedImport(true) + } + } + java { target( - "**/src/main/java/net/infumia/**/*.java", + *subprojects + .map { it.name } + .map { "$it/src/main/java/**/*.java" } + .toTypedArray(), ) importOrder() removeUnusedImports() diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 04e63ba..1eaffc6 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,16 +1,10 @@ -plugins { - `kotlin-dsl` -} +plugins { `kotlin-dsl` } repositories { mavenCentral() gradlePluginPortal() } -dependencies { - implementation(libs.nexus.plugin) -} +dependencies { implementation(libs.nexus.plugin) } -kotlin { - jvmToolchain(11) -} +kotlin { jvmToolchain(11) } diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts index b5a0fab..0edc037 100644 --- a/buildSrc/settings.gradle.kts +++ b/buildSrc/settings.gradle.kts @@ -1,7 +1,5 @@ dependencyResolutionManagement { versionCatalogs { - create("libs") { - from(files("../gradle/libs.versions.toml")) - } + create("libs") { from(files("../gradle/libs.versions.toml")) } } } From f83b62e67214ebbb9b7c9f36fcafefe847364974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:23:08 +0300 Subject: [PATCH 04/56] format *.kt files. --- build.gradle.kts | 12 +++++++ .../main/kotlin/net/infumia/gradle/common.kt | 32 ++++++++----------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ed31e73..fb21a93 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,6 +43,18 @@ spotless { } } + kotlin { + target( + "buildSrc/src/main/kotlin/**/*.kt", + ) + ktfmt().kotlinlangStyle().configure { + it.setMaxWidth(100) + it.setBlockIndent(4) + it.setContinuationIndent(4) + it.setRemoveUnusedImport(true) + } + } + java { target( *subprojects diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt index f2b45f8..abc779c 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt @@ -7,35 +7,31 @@ import org.gradle.api.tasks.bundling.Jar import org.gradle.jvm.toolchain.JavaLanguageVersion import org.gradle.kotlin.dsl.* -fun Project.applyCommon( - javaVersion: Int = 8, - sources: Boolean = true, - javadoc: Boolean = true -) { +fun Project.applyCommon(javaVersion: Int = 8, sources: Boolean = true, javadoc: Boolean = true) { apply() repositories.mavenCentral() extensions.configure { - toolchain { - languageVersion = JavaLanguageVersion.of(javaVersion) - } + toolchain { languageVersion = JavaLanguageVersion.of(javaVersion) } } if (javadoc) { - val javadocJar by tasks.creating(Jar::class) { - dependsOn("javadoc") - archiveClassifier.set("javadoc") - from(javadoc) - } + val javadocJar by + tasks.creating(Jar::class) { + dependsOn("javadoc") + archiveClassifier.set("javadoc") + from(javadoc) + } } if (sources) { val sourceSets = extensions.getByType().sourceSets - val sourcesJar by tasks.creating(Jar::class) { - dependsOn("classes") - archiveClassifier.set("sources") - from(sourceSets["main"].allSource) - } + val sourcesJar by + tasks.creating(Jar::class) { + dependsOn("classes") + archiveClassifier.set("sources") + from(sourceSets["main"].allSource) + } } } From 0fd32237f6433f38214e043f2a8ebcbe091bb3ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:28:58 +0300 Subject: [PATCH 05/56] move spotless. --- build.gradle.kts | 81 +--------------- buildSrc/build.gradle.kts | 5 +- .../kotlin/net/infumia/gradle/spotless.kt | 92 +++++++++++++++++++ gradle/libs.versions.toml | 4 +- 4 files changed, 101 insertions(+), 81 deletions(-) create mode 100644 buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt diff --git a/build.gradle.kts b/build.gradle.kts index fb21a93..ff3dd55 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,80 +1,7 @@ -plugins { - java - alias(libs.plugins.spotless) -} +import net.infumia.gradle.spotless -subprojects { apply() } - -repositories.mavenCentral() - -spotless { - isEnforceCheck = false - lineEndings = com.diffplug.spotless.LineEnding.UNIX - - val prettierConfig = - mapOf( - "prettier" to "3.3.2", - "prettier-plugin-java" to "2.6.0", - ) +plugins { java } - yaml { - target(".github/**/*.yml") - endWithNewline() - trimTrailingWhitespace() - jackson() - .yamlFeature("LITERAL_BLOCK_STYLE", true) - .yamlFeature("SPLIT_LINES", false) - } - - kotlinGradle { - target( - "buildSrc/**/*.gradle.kts", - "*.gradle.kts", - *subprojects - .map { it.name } - .map { "$it/*.gradle.kts" } - .toTypedArray(), - ) - ktfmt().kotlinlangStyle().configure { - it.setMaxWidth(80) - it.setBlockIndent(4) - it.setContinuationIndent(4) - it.setRemoveUnusedImport(true) - } - } - - kotlin { - target( - "buildSrc/src/main/kotlin/**/*.kt", - ) - ktfmt().kotlinlangStyle().configure { - it.setMaxWidth(100) - it.setBlockIndent(4) - it.setContinuationIndent(4) - it.setRemoveUnusedImport(true) - } - } +subprojects { apply() } - java { - target( - *subprojects - .map { it.name } - .map { "$it/src/main/java/**/*.java" } - .toTypedArray(), - ) - importOrder() - removeUnusedImports() - endWithNewline() - trimTrailingWhitespace() - prettier(prettierConfig) - .config( - mapOf( - "parser" to "java", - "tabWidth" to 4, - "useTabs" to false, - "printWidth" to 100, - "plugins" to listOf("prettier-plugin-java"), - ), - ) - } -} +spotless() diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1eaffc6..a622070 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -5,6 +5,9 @@ repositories { gradlePluginPortal() } -dependencies { implementation(libs.nexus.plugin) } +dependencies { + implementation(libs.nexus.plugin) + implementation(libs.spotless.plugin) +} kotlin { jvmToolchain(11) } diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt new file mode 100644 index 0000000..a965574 --- /dev/null +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt @@ -0,0 +1,92 @@ +package net.infumia.gradle + +import com.diffplug.gradle.spotless.SpotlessExtension +import com.diffplug.gradle.spotless.SpotlessPlugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.* + +fun Project.spotless() { + repositories.mavenCentral() + + apply() + + extensions.configure { + isEnforceCheck = false + lineEndings = com.diffplug.spotless.LineEnding.UNIX + + val prettierConfig = + mapOf( + "prettier" to "3.3.2", + "prettier-plugin-java" to "2.6.0", + "prettier-plugin-toml" to "2.0.1", + ) + + yaml { + target(".github/**/*.yml") + endWithNewline() + trimTrailingWhitespace() + jackson().yamlFeature("LITERAL_BLOCK_STYLE", true).yamlFeature("SPLIT_LINES", false) + } + + format("toml") { + target("gradle/libs.versions.toml") + prettier() + .config( + mapOf( + "parser" to "java", + "tabWidth" to 4, + "useTabs" to false, + "printWidth" to 100, + "plugins" to listOf("prettier-plugin-toml"), + ), + ) + + } + + kotlinGradle { + target( + "buildSrc/**/*.gradle.kts", + "*.gradle.kts", + *subprojects.map { it.name }.map { "$it/*.gradle.kts" }.toTypedArray(), + ) + ktfmt().kotlinlangStyle().configure { + it.setMaxWidth(80) + it.setBlockIndent(4) + it.setContinuationIndent(4) + it.setRemoveUnusedImport(true) + } + } + + kotlin { + target( + "buildSrc/src/main/kotlin/**/*.kt", + ) + ktfmt().kotlinlangStyle().configure { + it.setMaxWidth(100) + it.setBlockIndent(4) + it.setContinuationIndent(4) + it.setRemoveUnusedImport(true) + } + } + + java { + target( + *subprojects.map { it.name }.map { "$it/src/main/java/**/*.java" }.toTypedArray(), + ) + importOrder() + removeUnusedImports() + endWithNewline() + trimTrailingWhitespace() + prettier(prettierConfig) + .config( + mapOf( + "parser" to "java", + "tabWidth" to 4, + "useTabs" to false, + "printWidth" to 100, + "plugins" to listOf("prettier-plugin-java"), + ), + ) + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0610992..d12c09d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,6 +8,4 @@ creative-api = { module = "team.unnamed:creative-api", version.ref = "creative"} creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative"} nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } - -[plugins] -spotless = { id = "com.diffplug.spotless", version.ref = "spotless" } +spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless" } From 90f87b2fef4df33b499802c9a0eb7d13cd20bffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:33:12 +0300 Subject: [PATCH 06/56] format *.toml files. --- buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt | 5 ++--- gradle/libs.versions.toml | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt index a965574..fdaaf5d 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt @@ -30,17 +30,16 @@ fun Project.spotless() { format("toml") { target("gradle/libs.versions.toml") - prettier() + prettier(prettierConfig) .config( mapOf( - "parser" to "java", + "parser" to "toml", "tabWidth" to 4, "useTabs" to false, "printWidth" to 100, "plugins" to listOf("prettier-plugin-toml"), ), ) - } kotlinGradle { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d12c09d..5205f96 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,9 +3,9 @@ creative = "1.7.2" spotless = "6.25.0" [libraries] -adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0"} -creative-api = { module = "team.unnamed:creative-api", version.ref = "creative"} -creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative"} +adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0" } +creative-api = { module = "team.unnamed:creative-api", version.ref = "creative" } +creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" } nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless" } From 943c0a7be27cf50e7fe9fbf79837f64f54c596c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:33:35 +0300 Subject: [PATCH 07/56] Update spotless.kt --- buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt index fdaaf5d..52fa499 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt @@ -34,9 +34,6 @@ fun Project.spotless() { .config( mapOf( "parser" to "toml", - "tabWidth" to 4, - "useTabs" to false, - "printWidth" to 100, "plugins" to listOf("prettier-plugin-toml"), ), ) From 66eb77b3f67d1893c4396148b53b282ecc03c6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:36:17 +0300 Subject: [PATCH 08/56] format. --- buildSrc/settings.gradle.kts | 4 +--- .../kotlin/net/infumia/gradle/spotless.kt | 19 ++++++------------- settings.gradle.kts | 4 +--- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts index 0edc037..65a9695 100644 --- a/buildSrc/settings.gradle.kts +++ b/buildSrc/settings.gradle.kts @@ -1,5 +1,3 @@ dependencyResolutionManagement { - versionCatalogs { - create("libs") { from(files("../gradle/libs.versions.toml")) } - } + versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } } } diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt index 52fa499..d3d5004 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt @@ -30,6 +30,8 @@ fun Project.spotless() { format("toml") { target("gradle/libs.versions.toml") + endWithNewline() + trimTrailingWhitespace() prettier(prettierConfig) .config( mapOf( @@ -39,24 +41,15 @@ fun Project.spotless() { ) } - kotlinGradle { + kotlin { target( + "buildSrc/src/main/kotlin/**/*.kt", "buildSrc/**/*.gradle.kts", "*.gradle.kts", *subprojects.map { it.name }.map { "$it/*.gradle.kts" }.toTypedArray(), ) - ktfmt().kotlinlangStyle().configure { - it.setMaxWidth(80) - it.setBlockIndent(4) - it.setContinuationIndent(4) - it.setRemoveUnusedImport(true) - } - } - - kotlin { - target( - "buildSrc/src/main/kotlin/**/*.kt", - ) + endWithNewline() + trimTrailingWhitespace() ktfmt().kotlinlangStyle().configure { it.setMaxWidth(100) it.setBlockIndent(4) diff --git a/settings.gradle.kts b/settings.gradle.kts index 582b73c..709752b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,6 +1,4 @@ -plugins { - id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" -} +plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" } rootProject.name = "pack" From 78441ab27055f7103b6c4c1f7880bfc7bc301e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:39:23 +0300 Subject: [PATCH 09/56] format *.json files. --- .../kotlin/net/infumia/gradle/spotless.kt | 7 +++++ renovate.json | 26 +++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt index d3d5004..8b19cee 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt @@ -28,6 +28,13 @@ fun Project.spotless() { jackson().yamlFeature("LITERAL_BLOCK_STYLE", true).yamlFeature("SPLIT_LINES", false) } + json { + target("renovate.json") + endWithNewline() + trimTrailingWhitespace() + jackson() + } + format("toml") { target("gradle/libs.versions.toml") endWithNewline() diff --git a/renovate.json b/renovate.json index 8f82b0a..2e0f7b2 100644 --- a/renovate.json +++ b/renovate.json @@ -1,18 +1,10 @@ { - "extends": [ - "config:base" - ], - "labels": ["dependencies"], - "schedule": [ - "after 9am" - ], - "packageRules": [ - { - "matchUpdateTypes": ["major", "minor", "patch", "pin", "digest", "lockFileMaintenance", "rollback", "bump"], - "automerge": true - } - ], - "ignorePaths": [ - "example" - ] -} + "extends": [ "config:base" ], + "labels": [ "dependencies" ], + "schedule": [ "after 9am" ], + "packageRules": [ { + "matchUpdateTypes": [ "major", "minor", "patch", "pin", "digest", "lockFileMaintenance", "rollback", "bump" ], + "automerge": true + } ], + "ignorePaths": [ "example" ] +} \ No newline at end of file From 8acb939540b49a176b0ac3ad8aa42b6f1c6854a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:41:44 +0300 Subject: [PATCH 10/56] Update libs.versions.toml --- gradle/libs.versions.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5205f96..0ed8119 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,5 @@ [versions] creative = "1.7.2" -spotless = "6.25.0" [libraries] adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0" } @@ -8,4 +7,4 @@ creative-api = { module = "team.unnamed:creative-api", version.ref = "creative" creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" } nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } -spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless" } +spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.25.0" } From 8405830b2f150bca20253b109f1ecc55edd566db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 16:57:45 +0300 Subject: [PATCH 11/56] include more things. --- buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt index 8b19cee..95553c9 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/spotless.kt @@ -6,6 +6,8 @@ import org.gradle.api.Project import org.gradle.kotlin.dsl.* fun Project.spotless() { + val subProjects = subprojects.map { it.projectDir.toRelativeString(projectDir) } + repositories.mavenCentral() apply() @@ -53,7 +55,8 @@ fun Project.spotless() { "buildSrc/src/main/kotlin/**/*.kt", "buildSrc/**/*.gradle.kts", "*.gradle.kts", - *subprojects.map { it.name }.map { "$it/*.gradle.kts" }.toTypedArray(), + *subProjects.map { "$it/*.gradle.kts" }.toTypedArray(), + *subProjects.map { "$it/src/main/kotlin/**/*.kt" }.toTypedArray(), ) endWithNewline() trimTrailingWhitespace() @@ -67,7 +70,7 @@ fun Project.spotless() { java { target( - *subprojects.map { it.name }.map { "$it/src/main/java/**/*.java" }.toTypedArray(), + *subProjects.map { "$it/src/main/java/**/*.java" }.toTypedArray(), ) importOrder() removeUnusedImports() From abe6394e004887b6b5ff3f73c49eeb2604e5d34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 17:20:09 +0300 Subject: [PATCH 12/56] Update ResourceProducers.java --- common/src/main/java/net/infumia/pack/ResourceProducers.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 2c00993..e03674a 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -6,7 +6,7 @@ import team.unnamed.creative.texture.Texture; /** - * Utility class for creating instances of {@link ResourceProducerSpaces}. + * Utility class for creating instances of {@link ResourceProducer}. */ public final class ResourceProducers { From f83663c0a900c9dd0cbaaca09d4070377f65037e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 18:56:37 +0300 Subject: [PATCH 13/56] move things. --- README.md | 5 ++- .../pack/{GlyphSpaces.java => GlyphImpl.java} | 10 ++--- .../main/java/net/infumia/pack/Glyphs.java | 2 +- .../src/main/java/net/infumia/pack/Pack.java | 4 +- ...erSpaces.java => ResourceIdentifiers.java} | 4 +- .../pack/ResourceProducerSpacesAbstract.java | 2 +- .../net/infumia/pack/ResourceProducers.java | 23 ----------- .../main/resources/pack-resources/space.png | Bin 0 -> 70 bytes language/build.gradle.kts | 10 +++++ .../main/java/net/infumia/pack/Language.java | 36 ++++++++++++++++++ .../pack/ResourceProducerLanguage.java | 0 .../pack/ResourceProducerLanguageImpl.java | 0 settings.gradle.kts | 2 +- 13 files changed, 62 insertions(+), 36 deletions(-) rename common/src/main/java/net/infumia/pack/{GlyphSpaces.java => GlyphImpl.java} (62%) rename common/src/main/java/net/infumia/pack/{ResourceIdentifierSpaces.java => ResourceIdentifiers.java} (82%) create mode 100644 common/src/main/resources/pack-resources/space.png create mode 100644 language/build.gradle.kts create mode 100644 language/src/main/java/net/infumia/pack/Language.java rename {common => language}/src/main/java/net/infumia/pack/ResourceProducerLanguage.java (100%) rename {common => language}/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java (100%) diff --git a/README.md b/README.md index c1fdeff..275d6d1 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,10 @@ dependencies { // Blank Slot (Optional) implementation "net.infumia:pack-blank:VERSION" - + + // Language (Optional) + implementation "net.infumia:pack-language:VERSION" + // Generator (Optional) implementation "net.infumia:pack-generator:VERSION" // Required, https://mvnrepository.com/artifact/team.unnamed/creative-serializer-minecraft/ diff --git a/common/src/main/java/net/infumia/pack/GlyphSpaces.java b/common/src/main/java/net/infumia/pack/GlyphImpl.java similarity index 62% rename from common/src/main/java/net/infumia/pack/GlyphSpaces.java rename to common/src/main/java/net/infumia/pack/GlyphImpl.java index dd55550..92437e1 100644 --- a/common/src/main/java/net/infumia/pack/GlyphSpaces.java +++ b/common/src/main/java/net/infumia/pack/GlyphImpl.java @@ -4,23 +4,23 @@ import net.kyori.adventure.text.Component; import team.unnamed.creative.font.Font; -final class GlyphSpaces implements GlyphAppendable { +final class GlyphImpl implements GlyphAppendable { - static final GlyphAppendable DEFAULT = new GlyphSpaces(Font.MINECRAFT_DEFAULT, " ", 4); + static final GlyphAppendable DEFAULT_SPACE = new GlyphImpl(Font.MINECRAFT_DEFAULT, " ", 4); private final Component component; private final int length; - GlyphSpaces(final Component component, final int length) { + GlyphImpl(final Component component, final int length) { this.component = component; this.length = length; } - GlyphSpaces(final Key key, final String text, final int length) { + GlyphImpl(final Key key, final String text, final int length) { this(Component.text(text).font(key), length); } - GlyphSpaces(final Key key, final char[] text, final int length) { + GlyphImpl(final Key key, final char[] text, final int length) { this(key, new String(text), length); } diff --git a/common/src/main/java/net/infumia/pack/Glyphs.java b/common/src/main/java/net/infumia/pack/Glyphs.java index 27412c9..2c4b8c0 100644 --- a/common/src/main/java/net/infumia/pack/Glyphs.java +++ b/common/src/main/java/net/infumia/pack/Glyphs.java @@ -20,7 +20,7 @@ public static Glyph empty() { * @return The space glyph. */ public static GlyphAppendable space() { - return GlyphSpaces.DEFAULT; + return GlyphImpl.DEFAULT_SPACE; } private Glyphs() { diff --git a/common/src/main/java/net/infumia/pack/Pack.java b/common/src/main/java/net/infumia/pack/Pack.java index 1d31c87..fa57314 100644 --- a/common/src/main/java/net/infumia/pack/Pack.java +++ b/common/src/main/java/net/infumia/pack/Pack.java @@ -47,7 +47,7 @@ public interface Pack { */ @Contract("-> this") default Pack withMojangSpaces() { - return this.with(ResourceIdentifierSpaces.SPACES, ResourceProducers.spacesMojang()); + return this.with(ResourceIdentifiers.SPACES, ResourceProducers.spacesMojang()); } /** @@ -66,7 +66,7 @@ default Pack withMojangSpaces() { * @return The Mojang-specific spaces resource producer. */ default ResourceProducerSpaces spaces() { - return this.get(ResourceIdentifierSpaces.SPACES); + return this.get(ResourceIdentifiers.SPACES); } /** diff --git a/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java b/common/src/main/java/net/infumia/pack/ResourceIdentifiers.java similarity index 82% rename from common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java rename to common/src/main/java/net/infumia/pack/ResourceIdentifiers.java index 78225c6..cb28305 100644 --- a/common/src/main/java/net/infumia/pack/ResourceIdentifierSpaces.java +++ b/common/src/main/java/net/infumia/pack/ResourceIdentifiers.java @@ -3,7 +3,7 @@ /** * Utility class containing resource identifiers for various resource types. */ -public final class ResourceIdentifierSpaces { +public final class ResourceIdentifiers { /** * Resource identifier for spaces resource producer. @@ -11,7 +11,7 @@ public final class ResourceIdentifierSpaces { public static final ResourceIdentifier SPACES = new ResourceIdentifierImpl<>("spaces", ResourceProducerSpaces.class); - private ResourceIdentifierSpaces() { + private ResourceIdentifiers() { throw new IllegalStateException("Utility class"); } } diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java index 2f339e9..e08b70c 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesAbstract.java @@ -50,6 +50,6 @@ public Glyph translate(final int length) throws ResourceNotProducedException { } currentRankLength *= 2; } - return new GlyphSpaces(this.fontKey, Internal.toCharArray(characters), length); + return new GlyphImpl(this.fontKey, Internal.toCharArray(characters), length); } } diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index e03674a..eabf557 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -61,29 +61,6 @@ public static GlyphImage image( return new GlyphImageImpl(key, texture, properties); } - /** - * Creates a {@link ResourceProducerLanguage} instance with the specified parameters. - * - * @param fontKey The key associated with the font. Cannot be null. - * @param texture The texture of the glyph collection. Cannot be null. - * @param propertiesList The list of texture properties for the glyphs. Cannot be null. - * @param charactersMapping The list of character mappings for the glyphs. Cannot be null. - * @return A {@link ResourceProducerLanguage} instance. - */ - public static ResourceProducerLanguage language( - final Key fontKey, - final Texture texture, - final List propertiesList, - final List charactersMapping - ) { - return new ResourceProducerLanguageImpl( - fontKey, - texture, - propertiesList, - charactersMapping - ); - } - /** * Creates a {@link ResourceProducerImageMultichar} instance with the specified parameters. * diff --git a/common/src/main/resources/pack-resources/space.png b/common/src/main/resources/pack-resources/space.png new file mode 100644 index 0000000000000000000000000000000000000000..099055fc14de65b2cd991ea64f1f33ba766a059b GIT binary patch literal 70 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}1TpU9 propertiesList, + final List charactersMapping + ) { + return new ResourceProducerLanguageImpl( + fontKey, + texture, + propertiesList, + charactersMapping + ); + } + + private Language() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java b/language/src/main/java/net/infumia/pack/ResourceProducerLanguage.java similarity index 100% rename from common/src/main/java/net/infumia/pack/ResourceProducerLanguage.java rename to language/src/main/java/net/infumia/pack/ResourceProducerLanguage.java diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java similarity index 100% rename from common/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java rename to language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java diff --git a/settings.gradle.kts b/settings.gradle.kts index 709752b..c0670b6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,4 +2,4 @@ plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" rootProject.name = "pack" -include("common", "blank", "generator") +include("common", "blank", "language", "generator") From 147843f1ab62a9e93deee742d431bc61bf2bf46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 19:03:08 +0300 Subject: [PATCH 14/56] Update Language.java --- language/src/main/java/net/infumia/pack/Language.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/language/src/main/java/net/infumia/pack/Language.java b/language/src/main/java/net/infumia/pack/Language.java index a477fcb..9e60619 100644 --- a/language/src/main/java/net/infumia/pack/Language.java +++ b/language/src/main/java/net/infumia/pack/Language.java @@ -1,10 +1,9 @@ package net.infumia.pack; +import java.util.List; import net.kyori.adventure.key.Key; import team.unnamed.creative.texture.Texture; -import java.util.List; - public final class Language { /** From 056a3ff8ea83074322f85b9cf5e8ec78f67d057f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 20:05:37 +0300 Subject: [PATCH 15/56] default spaces. --- blank/src/main/java/net/infumia/pack/BlankSlot.java | 2 +- common/src/main/java/net/infumia/pack/Internal.java | 7 ++++++- common/src/main/java/net/infumia/pack/Pack.java | 10 ++++++++++ .../java/net/infumia/pack/ResourceProducers.java | 13 +++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/blank/src/main/java/net/infumia/pack/BlankSlot.java b/blank/src/main/java/net/infumia/pack/BlankSlot.java index 7ef46cc..8aacc14 100644 --- a/blank/src/main/java/net/infumia/pack/BlankSlot.java +++ b/blank/src/main/java/net/infumia/pack/BlankSlot.java @@ -108,7 +108,7 @@ private static final class Internal0 { Internal.DEFAULT_NAMESPACE, "blank_slot", Key.key("item/paper"), - Internal.resourceFromJar("pack-resources/blank_slot.png"), + Internal.resourceFromJar("blank_slot.png"), 1 ) ); diff --git a/common/src/main/java/net/infumia/pack/Internal.java b/common/src/main/java/net/infumia/pack/Internal.java index a84f9c1..ca23007 100644 --- a/common/src/main/java/net/infumia/pack/Internal.java +++ b/common/src/main/java/net/infumia/pack/Internal.java @@ -8,8 +8,10 @@ final class Internal { + private static final String RESOURCES_FOLDER = "pack-resources"; static final String DEFAULT_NAMESPACE = "glyphs"; static final Key DEFAULT_SPACES_FONT_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces"); + static final Key DEFAULT_SPACES_TEXTURE_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces.png"); static final int SEPARATOR_WIDTH = 1; static Key keyWithPngExtension(final Key key) { @@ -18,7 +20,10 @@ static Key keyWithPngExtension(final Key key) { } static Writable resourceFromJar(final String fileName) { - return Writable.resource(Internal.class.getClassLoader(), fileName); + return Writable.resource( + Internal.class.getClassLoader(), + Internal.RESOURCES_FOLDER + "/" + fileName + ); } static int calculateWidth( diff --git a/common/src/main/java/net/infumia/pack/Pack.java b/common/src/main/java/net/infumia/pack/Pack.java index fa57314..8a0340a 100644 --- a/common/src/main/java/net/infumia/pack/Pack.java +++ b/common/src/main/java/net/infumia/pack/Pack.java @@ -50,6 +50,16 @@ default Pack withMojangSpaces() { return this.with(ResourceIdentifiers.SPACES, ResourceProducers.spacesMojang()); } + /** + * Adds the default spaces resource producer to the pack. + * + * @return This Pack instance with the default spaces added. + */ + @Contract("-> this") + default Pack withDefaultSpaces() { + return this.with(ResourceIdentifiers.SPACES, ResourceProducers.spaces()); + } + /** * Retrieves the resource producer associated with the given identifier. * diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index eabf557..51f54f7 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -26,6 +26,19 @@ public static ResourceProducerSpaces spaces( return new ResourceProducerSpacesDefault(fontKey, textureKey, spacesWritable); } + /** + * Creates a ResourceProducerSpaces instance with default parameters. + * + * @return A ResourceProducerSpaces instance. + */ + public static ResourceProducerSpaces spaces() { + return ResourceProducers.spaces( + Internal.DEFAULT_SPACES_FONT_KEY, + Internal.DEFAULT_SPACES_TEXTURE_KEY, + Internal.resourceFromJar("space.png") + ); + } + /** * Creates a Mojang-specific space producer with the specified key. * From aa2ea4d74e9027d2a160fe69f3ba568cdedbd1cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 20:29:23 +0300 Subject: [PATCH 16/56] fix. --- .../src/main/java/net/infumia/pack/Internal.java | 3 +-- .../pack/ResourceProducerSpacesDefault.java | 14 +++++++------- .../infumia/pack/ResourceProducerSpacesMojang.java | 4 ++-- .../java/net/infumia/pack/ResourceProducers.java | 11 ++++++----- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/Internal.java b/common/src/main/java/net/infumia/pack/Internal.java index ca23007..eb41676 100644 --- a/common/src/main/java/net/infumia/pack/Internal.java +++ b/common/src/main/java/net/infumia/pack/Internal.java @@ -10,8 +10,7 @@ final class Internal { private static final String RESOURCES_FOLDER = "pack-resources"; static final String DEFAULT_NAMESPACE = "glyphs"; - static final Key DEFAULT_SPACES_FONT_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces"); - static final Key DEFAULT_SPACES_TEXTURE_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces.png"); + static final Key DEFAULT_SPACES_TEXTURE_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces"); static final int SEPARATOR_WIDTH = 1; static Key keyWithPngExtension(final Key key) { diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java index 6269bb2..bfb2deb 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java @@ -1,10 +1,10 @@ package net.infumia.pack; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; +import java.util.List; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; import net.kyori.adventure.key.Key; @@ -18,8 +18,8 @@ final class ResourceProducerSpacesDefault extends ResourceProducerSpacesAbstract private final Key textureKey; private final Writable writable; - private Set textures; - private Set fontProviders; + private List textures; + private List fontProviders; ResourceProducerSpacesDefault( final Key fontKey, @@ -27,7 +27,7 @@ final class ResourceProducerSpacesDefault extends ResourceProducerSpacesAbstract final Writable writable ) { super(fontKey); - this.textureKey = textureKey; + this.textureKey = Internal.keyWithPngExtension(textureKey); this.writable = writable; } @@ -42,12 +42,12 @@ public void produce(final ArbitraryCharacterFactory characterFactory) { throw new ResourceAlreadyProducedException(); } this.mapping = new HashMap<>(); - final Set fontProviders = new HashSet<>(); + final List fontProviders = new ArrayList<>(); for (int length = 1; length <= 2048; length *= 2) { fontProviders.add(this.prepareBuilder(characterFactory, length).build()); fontProviders.add(this.prepareBuilder(characterFactory, length * (-1)).build()); } - this.textures = Collections.singleton(Texture.texture(this.textureKey, this.writable)); + this.textures = Collections.singletonList(Texture.texture(this.textureKey, this.writable)); this.fontProviders = fontProviders; } diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java index a265988..e596d5a 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java @@ -13,8 +13,8 @@ final class ResourceProducerSpacesMojang extends ResourceProducerSpacesAbstract private Collection fontProviders; - ResourceProducerSpacesMojang(final Key key) { - super(key); + ResourceProducerSpacesMojang(final Key fontKey) { + super(fontKey); } @Override diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 51f54f7..bfd7aac 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -3,6 +3,7 @@ import java.util.List; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; +import team.unnamed.creative.font.Font; import team.unnamed.creative.texture.Texture; /** @@ -33,7 +34,7 @@ public static ResourceProducerSpaces spaces( */ public static ResourceProducerSpaces spaces() { return ResourceProducers.spaces( - Internal.DEFAULT_SPACES_FONT_KEY, + Font.MINECRAFT_DEFAULT, Internal.DEFAULT_SPACES_TEXTURE_KEY, Internal.resourceFromJar("space.png") ); @@ -42,11 +43,11 @@ public static ResourceProducerSpaces spaces() { /** * Creates a Mojang-specific space producer with the specified key. * - * @param key The key for the Mojang space producer. Cannot be null. + * @param fontKey The font key for the Mojang space producer. Cannot be null. * @return A {@link ResourceProducerSpaces} instance for Mojang. */ - public static ResourceProducerSpaces spacesMojang(final Key key) { - return new ResourceProducerSpacesMojang(key); + public static ResourceProducerSpaces spacesMojang(final Key fontKey) { + return new ResourceProducerSpacesMojang(fontKey); } /** @@ -55,7 +56,7 @@ public static ResourceProducerSpaces spacesMojang(final Key key) { * @return A {@link ResourceProducerSpaces} instance for Mojang. */ public static ResourceProducerSpaces spacesMojang() { - return ResourceProducers.spacesMojang(Internal.DEFAULT_SPACES_FONT_KEY); + return ResourceProducers.spacesMojang(Font.MINECRAFT_DEFAULT); } /** From 4fc1bb76e09f6e3069a36b1b1540cc1ca2fda4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 20:43:44 +0300 Subject: [PATCH 17/56] desc. --- common/src/main/java/net/infumia/pack/Pack.java | 6 ++++-- ...efault.java => ResourceProducerSpacesBitmap.java} | 8 ++------ .../java/net/infumia/pack/ResourceProducers.java | 12 ++++++------ 3 files changed, 12 insertions(+), 14 deletions(-) rename common/src/main/java/net/infumia/pack/{ResourceProducerSpacesDefault.java => ResourceProducerSpacesBitmap.java} (92%) diff --git a/common/src/main/java/net/infumia/pack/Pack.java b/common/src/main/java/net/infumia/pack/Pack.java index 8a0340a..bb15c2f 100644 --- a/common/src/main/java/net/infumia/pack/Pack.java +++ b/common/src/main/java/net/infumia/pack/Pack.java @@ -42,6 +42,8 @@ public interface Pack { /** * Adds Mojang-specific spaces resource to this pack. + *

+ * Available after pack format 9. * * @return This pack instance. */ @@ -56,8 +58,8 @@ default Pack withMojangSpaces() { * @return This Pack instance with the default spaces added. */ @Contract("-> this") - default Pack withDefaultSpaces() { - return this.with(ResourceIdentifiers.SPACES, ResourceProducers.spaces()); + default Pack withBitmapSpaces() { + return this.with(ResourceIdentifiers.SPACES, ResourceProducers.spacesBitmap()); } /** diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java similarity index 92% rename from common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java rename to common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java index bfb2deb..c5f607f 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesDefault.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java @@ -13,7 +13,7 @@ import team.unnamed.creative.font.FontProvider; import team.unnamed.creative.texture.Texture; -final class ResourceProducerSpacesDefault extends ResourceProducerSpacesAbstract { +final class ResourceProducerSpacesBitmap extends ResourceProducerSpacesAbstract { private final Key textureKey; private final Writable writable; @@ -21,11 +21,7 @@ final class ResourceProducerSpacesDefault extends ResourceProducerSpacesAbstract private List textures; private List fontProviders; - ResourceProducerSpacesDefault( - final Key fontKey, - final Key textureKey, - final Writable writable - ) { + ResourceProducerSpacesBitmap(final Key fontKey, final Key textureKey, final Writable writable) { super(fontKey); this.textureKey = Internal.keyWithPngExtension(textureKey); this.writable = writable; diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index bfd7aac..0296402 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -19,21 +19,21 @@ public final class ResourceProducers { * @param spacesWritable The writable instance for spaces. Cannot be null. * @return A {@link ResourceProducerSpaces} instance. */ - public static ResourceProducerSpaces spaces( + public static ResourceProducerSpaces spacesBitmap( final Key fontKey, final Key textureKey, final Writable spacesWritable ) { - return new ResourceProducerSpacesDefault(fontKey, textureKey, spacesWritable); + return new ResourceProducerSpacesBitmap(fontKey, textureKey, spacesWritable); } /** - * Creates a ResourceProducerSpaces instance with default parameters. + * Creates a {@link ResourceProducerSpaces} instance with default parameters. * - * @return A ResourceProducerSpaces instance. + * @return A {@link ResourceProducerSpaces} instance. */ - public static ResourceProducerSpaces spaces() { - return ResourceProducers.spaces( + public static ResourceProducerSpaces spacesBitmap() { + return ResourceProducers.spacesBitmap( Font.MINECRAFT_DEFAULT, Internal.DEFAULT_SPACES_TEXTURE_KEY, Internal.resourceFromJar("space.png") From 59a56684628b1d487715db8dff7497e600e6f5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 21:49:59 +0300 Subject: [PATCH 18/56] Update ResourceProducers.java --- .../main/java/net/infumia/pack/ResourceProducers.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 0296402..e616b87 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -1,11 +1,12 @@ package net.infumia.pack; -import java.util.List; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; import team.unnamed.creative.font.Font; import team.unnamed.creative.texture.Texture; +import java.util.List; + /** * Utility class for creating instances of {@link ResourceProducer}. */ @@ -62,23 +63,23 @@ public static ResourceProducerSpaces spacesMojang() { /** * Creates a {@link GlyphImage} instance with the specified key, texture, and properties. * - * @param key The key associated with the glyph image. Cannot be null. + * @param fontKey The font key associated with the glyph image. Cannot be null. * @param texture The texture of the glyph image. Cannot be null. * @param properties The properties of the glyph image. Cannot be null. * @return A {@link GlyphImage} instance. */ public static GlyphImage image( - final Key key, + final Key fontKey, final Texture texture, final TextureProperties properties ) { - return new GlyphImageImpl(key, texture, properties); + return new GlyphImageImpl(fontKey, texture, properties); } /** * Creates a {@link ResourceProducerImageMultichar} instance with the specified parameters. * - * @param fontKey The key associated with the font. Cannot be null. + * @param fontKey The font key associated with the font. Cannot be null. * @param texture The texture of the glyph collection. Cannot be null. * @param properties The properties of the texture. Cannot be null. * @param charactersMapping The list of character mappings for the glyphs. Cannot be null. From 7a1743a80cb8f78844d7acbfbc6af7ce113041ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 21:50:15 +0300 Subject: [PATCH 19/56] Update ResourceProducers.java --- common/src/main/java/net/infumia/pack/ResourceProducers.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index e616b87..149d130 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -1,12 +1,11 @@ package net.infumia.pack; +import java.util.List; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; import team.unnamed.creative.font.Font; import team.unnamed.creative.texture.Texture; -import java.util.List; - /** * Utility class for creating instances of {@link ResourceProducer}. */ From 6528a2c6e8b2635b220cb66752c58f22d18e15a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 22:36:13 +0300 Subject: [PATCH 20/56] move things. --- .../main/java/net/infumia/pack/Internal.java | 2 +- .../net/infumia/pack/ResourceProducers.java | 23 ------------------- .../main/java/net/infumia/pack/Language.java | 23 +++++++++++++++++++ .../pack/ResourceProducerImageMultichar.java | 0 .../ResourceProducerImageMulticharImpl.java | 0 .../pack/ResourceProducerLanguageImpl.java | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) rename {common => language}/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java (100%) rename {common => language}/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java (100%) diff --git a/common/src/main/java/net/infumia/pack/Internal.java b/common/src/main/java/net/infumia/pack/Internal.java index eb41676..b5f7a6e 100644 --- a/common/src/main/java/net/infumia/pack/Internal.java +++ b/common/src/main/java/net/infumia/pack/Internal.java @@ -9,7 +9,7 @@ final class Internal { private static final String RESOURCES_FOLDER = "pack-resources"; - static final String DEFAULT_NAMESPACE = "glyphs"; + static final String DEFAULT_NAMESPACE = "pack"; static final Key DEFAULT_SPACES_TEXTURE_KEY = Key.key(Internal.DEFAULT_NAMESPACE, "spaces"); static final int SEPARATOR_WIDTH = 1; diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 149d130..916f7ba 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -75,29 +75,6 @@ public static GlyphImage image( return new GlyphImageImpl(fontKey, texture, properties); } - /** - * Creates a {@link ResourceProducerImageMultichar} instance with the specified parameters. - * - * @param fontKey The font key associated with the font. Cannot be null. - * @param texture The texture of the glyph collection. Cannot be null. - * @param properties The properties of the texture. Cannot be null. - * @param charactersMapping The list of character mappings for the glyphs. Cannot be null. - * @return A {@link ResourceProducerImageMultichar} instance. - */ - static ResourceProducerImageMultichar multichar( - final Key fontKey, - final Texture texture, - final TextureProperties properties, - final List charactersMapping - ) { - return new ResourceProducerImageMulticharImpl( - fontKey, - texture, - properties, - charactersMapping - ); - } - private ResourceProducers() { throw new IllegalStateException("Utility class"); } diff --git a/language/src/main/java/net/infumia/pack/Language.java b/language/src/main/java/net/infumia/pack/Language.java index 9e60619..695bff3 100644 --- a/language/src/main/java/net/infumia/pack/Language.java +++ b/language/src/main/java/net/infumia/pack/Language.java @@ -29,6 +29,29 @@ public static ResourceProducerLanguage language( ); } + /** + * Creates a {@link ResourceProducerImageMultichar} instance with the specified parameters. + * + * @param fontKey The font key associated with the font. Cannot be null. + * @param texture The texture of the glyph collection. Cannot be null. + * @param properties The properties of the texture. Cannot be null. + * @param charactersMapping The list of character mappings for the glyphs. Cannot be null. + * @return A {@link ResourceProducerImageMultichar} instance. + */ + static ResourceProducerImageMultichar multichar( + final Key fontKey, + final Texture texture, + final TextureProperties properties, + final List charactersMapping + ) { + return new ResourceProducerImageMulticharImpl( + fontKey, + texture, + properties, + charactersMapping + ); + } + private Language() { throw new IllegalStateException("Utility class"); } diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java b/language/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java similarity index 100% rename from common/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java rename to language/src/main/java/net/infumia/pack/ResourceProducerImageMultichar.java diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java b/language/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java similarity index 100% rename from common/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java rename to language/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java diff --git a/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java index af000c7..34263a4 100644 --- a/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java +++ b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java @@ -39,7 +39,7 @@ final class ResourceProducerLanguageImpl implements ResourceProducerLanguage { Collectors.toMap( Function.identity(), properties -> - ResourceProducers.multichar(key, texture, properties, charactersMapping) + Language.multichar(key, texture, properties, charactersMapping) ) ); } From ed81e09c9a278d73fe30f60e8a4eb6ff0e01eaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 22:37:36 +0300 Subject: [PATCH 21/56] doc. --- common/src/main/java/net/infumia/pack/ResourceProducers.java | 1 - language/src/main/java/net/infumia/pack/Language.java | 3 +++ .../java/net/infumia/pack/ResourceProducerLanguageImpl.java | 3 +-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 916f7ba..0833990 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -1,6 +1,5 @@ package net.infumia.pack; -import java.util.List; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; import team.unnamed.creative.font.Font; diff --git a/language/src/main/java/net/infumia/pack/Language.java b/language/src/main/java/net/infumia/pack/Language.java index 695bff3..11978cf 100644 --- a/language/src/main/java/net/infumia/pack/Language.java +++ b/language/src/main/java/net/infumia/pack/Language.java @@ -4,6 +4,9 @@ import net.kyori.adventure.key.Key; import team.unnamed.creative.texture.Texture; +/** + * Utility class for creating resource producers related to languages and multi-character glyphs. + */ public final class Language { /** diff --git a/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java index 34263a4..2d340ae 100644 --- a/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java +++ b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java @@ -38,8 +38,7 @@ final class ResourceProducerLanguageImpl implements ResourceProducerLanguage { .collect( Collectors.toMap( Function.identity(), - properties -> - Language.multichar(key, texture, properties, charactersMapping) + properties -> Language.multichar(key, texture, properties, charactersMapping) ) ); } From 7d8b9655c598aac61772233f7a4e72bf2b037815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 22:42:02 +0300 Subject: [PATCH 22/56] lazy load. --- .../infumia/pack/FileResourceCompilers.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/FileResourceCompilers.java b/common/src/main/java/net/infumia/pack/FileResourceCompilers.java index 94cc236..e0a3935 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceCompilers.java +++ b/common/src/main/java/net/infumia/pack/FileResourceCompilers.java @@ -16,17 +16,25 @@ public static FileResourceCompiler simple(final ArbitraryCharacterFactory charac } /** - * Creates a simple PackCompiler instance using a default character factory. - *

- * Uses an instance of {@link ArbitraryCharacterFactoryReserved} as the default character factory. + * Creates a default {@link FileResourceCompiler} instance using a reserved character factory. * - * @return A newly created {@link FileResourceCompiler} instance configured with a default character factory. + * @return A newly created {@link FileResourceCompiler} instance configured with the default character factory. */ public static FileResourceCompiler simple() { - return FileResourceCompilers.simple(new ArbitraryCharacterFactoryReserved()); + return Internal.DEFAULT_COMPILER.get(); } private FileResourceCompilers() { throw new IllegalStateException("Utility class"); } + + private static final class Internal { + + private static final Lazy CHARACTER_FACTORY = Lazy.of( + ArbitraryCharacterFactoryReserved::new + ); + private static final Lazy DEFAULT_COMPILER = Lazy.of( + () -> FileResourceCompilers.simple(Internal.CHARACTER_FACTORY.get()) + ); + } } From 375a08b2a1bd902f54ab9c7f9f4857dda118f616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sun, 23 Jun 2024 22:43:15 +0300 Subject: [PATCH 23/56] Update ArbitraryCharacterFactoryReserved.java --- .../net/infumia/pack/ArbitraryCharacterFactoryReserved.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java index e89a711..5798b11 100644 --- a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java +++ b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java @@ -42,7 +42,7 @@ public ArbitraryCharacterFactoryReserved(final Collection reserved) { * starting from the default character '\uA201'. */ public ArbitraryCharacterFactoryReserved() { - this(Internal.reserved()); + this(Internal.RESERVED.get()); } @Override @@ -113,10 +113,6 @@ private static final class Internal { return reserved; }); - private static Collection reserved() { - return Internal.RESERVED.get(); - } - private Internal() { throw new IllegalStateException("Utility class"); } From c443479aa79a1bc4e6735f5f28a15dd93a305dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 15:18:42 +0300 Subject: [PATCH 24/56] start working on pack generator. --- README.md | 4 ++++ generator/build.gradle.kts | 4 ++++ .../src/main/java/net/infumia/pack/PackGenerator.java | 8 ++++++++ gradle/libs.versions.toml | 4 ++++ 4 files changed, 20 insertions(+) create mode 100644 generator/src/main/java/net/infumia/pack/PackGenerator.java diff --git a/README.md b/README.md index 275d6d1..365b268 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ dependencies { implementation "net.infumia:pack-generator:VERSION" // Required, https://mvnrepository.com/artifact/team.unnamed/creative-serializer-minecraft/ implementation "team.unnamed:creative-serializer-minecraft:1.7.2" + // Required, https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/ + implementation "com.fasterxml.jackson.core:jackson-databind:" + // Required, https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/ + implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" } ``` ### Code diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts index 1206b6b..ce21382 100644 --- a/generator/build.gradle.kts +++ b/generator/build.gradle.kts @@ -4,8 +4,12 @@ publish("generator") dependencies { compileOnly(project(":common")) + compileOnly(project(":blank")) + compileOnly(project(":language")) compileOnly(libs.creative.api) compileOnly(libs.creative.serializer) compileOnly(libs.adventure.api) + compileOnly(libs.jackson.databind) + compileOnly(libs.jackson.yaml) } diff --git a/generator/src/main/java/net/infumia/pack/PackGenerator.java b/generator/src/main/java/net/infumia/pack/PackGenerator.java new file mode 100644 index 0000000..ea26a74 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackGenerator.java @@ -0,0 +1,8 @@ +package net.infumia.pack; + +public final class PackGenerator { + + private PackGenerator() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0ed8119..69e8f5e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,10 +1,14 @@ [versions] creative = "1.7.2" +jackson = "2.17.1" [libraries] adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0" } creative-api = { module = "team.unnamed:creative-api", version.ref = "creative" } creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" } +jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" } +jackson-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" } + nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.25.0" } From d5cef5bb37fff637c96b5027749429344e638901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 16:55:41 +0300 Subject: [PATCH 25/56] pack writer. --- README.md | 2 - generator/build.gradle.kts | 4 +- .../infumia/pack/PackGeneratedContext.java | 71 ++++++++++ .../java/net/infumia/pack/PackGenerator.java | 41 ++++++ .../infumia/pack/PackGeneratorContext.java | 113 +++++++++++++++ .../java/net/infumia/pack/PackParser.java | 43 ++++++ .../net/infumia/pack/PackPartReference.java | 3 + .../java/net/infumia/pack/PackReader.java | 107 ++++++++++++++ .../net/infumia/pack/PackReaderSettings.java | 132 ++++++++++++++++++ .../java/net/infumia/pack/PackReference.java | 100 +++++++++++++ .../java/net/infumia/pack/PackWriter.java | 48 +++++++ .../net/infumia/pack/PackWriterSettings.java | 17 +++ gradle/libs.versions.toml | 11 +- 13 files changed, 678 insertions(+), 14 deletions(-) create mode 100644 generator/src/main/java/net/infumia/pack/PackGeneratedContext.java create mode 100644 generator/src/main/java/net/infumia/pack/PackGeneratorContext.java create mode 100644 generator/src/main/java/net/infumia/pack/PackParser.java create mode 100644 generator/src/main/java/net/infumia/pack/PackPartReference.java create mode 100644 generator/src/main/java/net/infumia/pack/PackReader.java create mode 100644 generator/src/main/java/net/infumia/pack/PackReaderSettings.java create mode 100644 generator/src/main/java/net/infumia/pack/PackReference.java create mode 100644 generator/src/main/java/net/infumia/pack/PackWriter.java create mode 100644 generator/src/main/java/net/infumia/pack/PackWriterSettings.java diff --git a/README.md b/README.md index 365b268..47a796f 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,6 @@ dependencies { // Generator (Optional) implementation "net.infumia:pack-generator:VERSION" - // Required, https://mvnrepository.com/artifact/team.unnamed/creative-serializer-minecraft/ - implementation "team.unnamed:creative-serializer-minecraft:1.7.2" // Required, https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/ implementation "com.fasterxml.jackson.core:jackson-databind:" // Required, https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/ diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts index ce21382..19838e9 100644 --- a/generator/build.gradle.kts +++ b/generator/build.gradle.kts @@ -7,9 +7,7 @@ dependencies { compileOnly(project(":blank")) compileOnly(project(":language")) - compileOnly(libs.creative.api) - compileOnly(libs.creative.serializer) compileOnly(libs.adventure.api) + compileOnly(libs.creative.api) compileOnly(libs.jackson.databind) - compileOnly(libs.jackson.yaml) } diff --git a/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java b/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java new file mode 100644 index 0000000..1c647a1 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java @@ -0,0 +1,71 @@ +package net.infumia.pack; + +import java.nio.file.Path; +import team.unnamed.creative.ResourcePack; + +/** + * Context for generated resource pack. + */ +public final class PackGeneratedContext { + + private final ResourcePack resourcePack; + private final Pack pack; + private final Path outputDirectory; + private final Path outputFile; + + /** + * Ctor. + * + * @param resourcePack the resource pack. Cannot be null. + * @param pack the pack. Cannot be null. + * @param outputDirectory the output directory. Can be null. + * @param outputFile the output file. Can be null. + */ + PackGeneratedContext( + final ResourcePack resourcePack, + final Pack pack, + final Path outputDirectory, + final Path outputFile + ) { + this.resourcePack = resourcePack; + this.pack = pack; + this.outputDirectory = outputDirectory; + this.outputFile = outputFile; + } + + /** + * Returns the resource pack. + * + * @return the resource pack. + */ + public ResourcePack resourcePack() { + return this.resourcePack; + } + + /** + * Returns the pack. + * + * @return the pack. + */ + public Pack pack() { + return this.pack; + } + + /** + * Returns the output directory. + * + * @return the output directory. Can be null. + */ + public Path outputDirectory() { + return this.outputDirectory; + } + + /** + * Returns the output file. + * + * @return the output file. Can be null. + */ + public Path outputFile() { + return this.outputFile; + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackGenerator.java b/generator/src/main/java/net/infumia/pack/PackGenerator.java index ea26a74..167cc67 100644 --- a/generator/src/main/java/net/infumia/pack/PackGenerator.java +++ b/generator/src/main/java/net/infumia/pack/PackGenerator.java @@ -1,7 +1,48 @@ package net.infumia.pack; +import java.io.IOException; + +/** + * Utility class for generating resource packs. + */ public final class PackGenerator { + /** + * Generates a resource pack based on the provided settings and base pack. + * + * @param readerSettings the pack reader settings. Cannot be null. + * @param writerSettings the pack writer settings. Cannot be null. + * @param base the base pack. Cannot be null. + * @return the generated pack context. + * @throws IOException if an I/ O error is thrown when accessing the starting file. + */ + public static PackGeneratedContext generate( + final PackReaderSettings readerSettings, + final PackWriterSettings writerSettings, + final Pack base + ) throws IOException { + final PackReader reader = new PackReader(readerSettings, base); + final PackWriter writer = new PackWriter(writerSettings); + final PackGeneratorContext context = reader.read(); + final PackGeneratorContext parsed = PackParser.parse(context); + return writer.write(parsed); + } + + /** + * Generates a resource pack based on the provided settings with a default base pack. + * + * @param readerSettings the pack reader settings. Cannot be null. + * @param writerSettings the pack writer settings. Cannot be null. + * @return the generated pack context. + * @throws IOException if an I/ O error is thrown when accessing the starting file. + */ + public static PackGeneratedContext generate( + final PackReaderSettings readerSettings, + final PackWriterSettings writerSettings + ) throws IOException { + return PackGenerator.generate(readerSettings, writerSettings, Packs.create()); + } + private PackGenerator() { throw new IllegalStateException("Utility class"); } diff --git a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java new file mode 100644 index 0000000..994c455 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java @@ -0,0 +1,113 @@ +package net.infumia.pack; + +import java.nio.file.Path; +import java.util.Collection; +import java.util.Collections; +import net.kyori.adventure.text.serializer.ComponentSerializer; +import team.unnamed.creative.ResourcePack; + +/** + * Context for resource pack generator. + */ +public final class PackGeneratorContext { + + private final ResourcePack resourcePack; + private final Pack pack; + private final PackReference packReference; + private final Collection packPartReferences; + private final Path outputDirectory; + private final Path outputFile; + private final ComponentSerializer serializer; + + /** + * Ctor. + * + * @param resourcePack the resource pack. Cannot be null. + * @param pack the pack. Cannot be null. + * @param packReference the pack file reference. Cannot be null. + * @param packPartReferences the pack part references. Cannot be null. + * @param serializer the serializer to serialize components when needed. Cannot be null. + * @param outputDirectory the output directory. Can be null. + * @param outputFile the output file. Can be null. + */ + PackGeneratorContext( + final ResourcePack resourcePack, + final Pack pack, + final PackReference packReference, + final Collection packPartReferences, + final ComponentSerializer serializer, + final Path outputDirectory, + final Path outputFile + ) { + this.resourcePack = resourcePack; + this.pack = pack; + this.packReference = packReference; + this.packPartReferences = Collections.unmodifiableCollection(packPartReferences); + this.serializer = serializer; + this.outputDirectory = outputDirectory; + this.outputFile = outputFile; + } + + /** + * Returns the resource pack. + * + * @return the resource pack. + */ + public ResourcePack resourcePack() { + return this.resourcePack; + } + + /** + * Returns the pack. + * + * @return the pack. + */ + public Pack pack() { + return this.pack; + } + + /** + * Returns the pack reference. + * + * @return the pack reference. + */ + public PackReference packReference() { + return this.packReference; + } + + /** + * Returns the pack part references. + * + * @return the pack part references. + */ + public Collection packPartReferences() { + return this.packPartReferences; + } + + /** + * Returns the component serializer. + * + * @return the component serializer. + */ + public ComponentSerializer serializer() { + return this.serializer; + } + + /** + * Returns the output directory. + * + * @return the output directory. Can be null. + */ + public Path outputDirectory() { + return this.outputDirectory; + } + + /** + * Returns the output file. + * + * @return the output file. Can be null. + */ + public Path outputFile() { + return this.outputFile; + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java new file mode 100644 index 0000000..3504062 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -0,0 +1,43 @@ +package net.infumia.pack; + +import java.util.Collection; +import team.unnamed.creative.ResourcePack; +import team.unnamed.creative.metadata.pack.PackMeta; + +/** + * Utility class for parsing packs. + */ +public final class PackParser { + + /** + * Parses the given pack generator context. + * + * @param context the pack generator context. Cannot be null. + * @return the updated pack generator context. + */ + public static PackGeneratorContext parse(final PackGeneratorContext context) { + final PackReference packReference = context.packReference(); + final Collection partReferences = context.packPartReferences(); + final ResourcePack resourcePack = context.resourcePack(); + final PackMeta packMeta = packReference.parsePackMeta(context.serializer()); + resourcePack.packMeta(packMeta); + final Pack pack = context.pack(); + // TODO: portlek, Parse partReferences here. + if (packReference.addBlankSlot()) { + pack.with(BlankSlot.get()); + } + if (packReference.addSpaces()) { + if (packMeta.formats().isInRange(9)) { + pack.withMojangSpaces(); + } else { + pack.withBitmapSpaces(); + } + } + pack.writeAll(resourcePack); + return context; + } + + private PackParser() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackPartReference.java b/generator/src/main/java/net/infumia/pack/PackPartReference.java new file mode 100644 index 0000000..9d0e13a --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackPartReference.java @@ -0,0 +1,3 @@ +package net.infumia.pack; + +public final class PackPartReference {} diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java new file mode 100644 index 0000000..ddafedd --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -0,0 +1,107 @@ +package net.infumia.pack; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitOption; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; +import team.unnamed.creative.ResourcePack; + +final class PackReader { + + private final PackReaderSettings settings; + private final Pack base; + + private File packReferenceFile; + private Path outputDirectory; + private Path outputFile; + private ObjectReader packReader; + private ObjectReader packPartReader; + + PackReader(final PackReaderSettings settings, final Pack base) { + this.settings = settings; + this.base = base; + } + + PackGeneratorContext read() throws IOException { + this.prepare(); + + FileVisitOption[] visitOptions = this.settings.visitOptions(); + if (visitOptions == null) { + visitOptions = new FileVisitOption[0]; + } + try (final Stream walking = Files.walk(this.settings.root(), visitOptions)) { + return this.read0(walking); + } + } + + private PackGeneratorContext read0(@NotNull final Stream walking) throws IOException { + final PackReference packReference = this.packReader.readValue(this.packReferenceFile); + final Collection packPartReferences = walking + .map(Path::toFile) + .map(file -> { + try ( + final MappingIterator iterator = + this.packPartReader.readValues(file) + ) { + return iterator.readAll(); + } catch (final IOException e) { + throw new RuntimeException(e); + } + }) + .flatMap(Collection::stream) + .collect(Collectors.toSet()); + return new PackGeneratorContext( + ResourcePack.resourcePack(), + this.base, + packReference, + packPartReferences, + this.settings.serializer(), + this.outputDirectory, + this.outputFile + ); + } + + private void prepare() { + final Path root = this.settings.root(); + final Path packReferenceFile = root.resolve(this.settings.packReferenceFileName()); + if (Files.notExists(packReferenceFile)) { + throw new IllegalStateException( + "Pack reference file does not exist: " + packReferenceFile + ); + } + this.packReferenceFile = packReferenceFile.toFile(); + + final String directoryName = this.settings.directoryName(); + if (directoryName != null) { + this.outputDirectory = root.resolve(directoryName); + } + + final String zipFileName = this.settings.zipFileName(); + if (zipFileName != null) { + this.outputFile = root.resolve(zipFileName); + } + + final ObjectMapper mapper = this.settings.mapper(); + this.packReader = mapper.readerFor(Internal.PACK_TYPE); + this.packPartReader = mapper.readerFor(Internal.PACK_PART_TYPE); + } + + private static final class Internal { + + private static final TypeReference PACK_TYPE = new TypeReference< + PackReference + >() {}; + private static final TypeReference PACK_PART_TYPE = new TypeReference< + PackPartReference + >() {}; + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java new file mode 100644 index 0000000..608c14e --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java @@ -0,0 +1,132 @@ +package net.infumia.pack; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.nio.file.FileVisitOption; +import java.nio.file.Path; +import net.kyori.adventure.text.serializer.ComponentSerializer; + +/** + * Settings for reading a pack. + */ +public final class PackReaderSettings { + + private final Path root; + private final FileVisitOption[] visitOptions; + private final String packReferenceFileName; + private final String directoryName; + private final String zipFileName; + private final ObjectMapper mapper; + private final ComponentSerializer serializer; + + /** + * Constructs a new PackReaderSettings. + * + * @param root the root path. + * @param visitOptions the visit options. Can be null. + * @param packReferenceFileName the pack reference file name. Cannot be null. + * @param directoryName the directory name. Can be null. + * @param zipFileName the zip file name. Can be null. + * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. + * @param serializer the serializer to serialize components when needed. Cannot be null. + */ + public PackReaderSettings( + final Path root, + final FileVisitOption[] visitOptions, + final String packReferenceFileName, + final String directoryName, + final String zipFileName, + final ObjectMapper mapper, + final ComponentSerializer serializer + ) { + this.root = root; + this.visitOptions = visitOptions; + this.packReferenceFileName = packReferenceFileName; + this.directoryName = directoryName; + this.zipFileName = zipFileName; + this.mapper = mapper; + this.serializer = serializer; + } + + /** + * Constructs a new PackReaderSettings. + * + * @param root the root path. + * @param packReferenceFileName the pack reference file name. Cannot be null. + * @param directoryName the directory name. Can be null. + * @param zipFileName the zip file name. Can be null. + * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. + */ + public PackReaderSettings( + final Path root, + final String packReferenceFileName, + final String directoryName, + final String zipFileName, + final ObjectMapper mapper, + final ComponentSerializer serializer + ) { + this(root, null, packReferenceFileName, directoryName, zipFileName, mapper, serializer); + } + + /** + * Returns the root path. + * + * @return the root path. + */ + public Path root() { + return this.root; + } + + /** + * Returns the visit options. + * + * @return the visit options. + */ + public FileVisitOption[] visitOptions() { + return this.visitOptions; + } + + /** + * Returns the pack reference file name. + * + * @return the pack reference file name. + */ + public String packReferenceFileName() { + return this.packReferenceFileName; + } + + /** + * Returns the directory name. + * + * @return the directory name. Can be null. + */ + public String directoryName() { + return this.directoryName; + } + + /** + * Returns the zip file name. + * + * @return the zip file name. Can be null. + */ + public String zipFileName() { + return this.zipFileName; + } + + /** + * Returns the object mapper. + * + * @return the object mapper. + */ + public ObjectMapper mapper() { + return this.mapper; + } + + /** + * Returns the component serializer. + * + * @return the component serializer. + */ + public ComponentSerializer serializer() { + return this.serializer; + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackReference.java b/generator/src/main/java/net/infumia/pack/PackReference.java new file mode 100644 index 0000000..036884d --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReference.java @@ -0,0 +1,100 @@ +package net.infumia.pack; + +import net.kyori.adventure.text.serializer.ComponentSerializer; +import team.unnamed.creative.metadata.pack.PackFormat; +import team.unnamed.creative.metadata.pack.PackMeta; + +/** + * Represents a reference to a pack with format constraints and a description. + */ +public final class PackReference { + + private final Integer format; + private final Integer minimumFormat; + private final Integer maximumFormat; + private final String description; + private final boolean addBlankSlot; + private final boolean addSpaces; + + /** + * Constructs a new PackReference. + * + * @param format the pack format. Can be null + * @param minimumFormat the minimum pack format. Can be null + * @param maximumFormat the maximum pack format. Can be null + * @param description the description of the pack + * @param addBlankSlot adds the {@link BlankSlot} resources. + * @param addSpaces adds the {@link ResourceProducers#spacesBitmap()} or {@link ResourceProducers#spacesMojang()} based on the pack format. + */ + public PackReference( + final Integer format, + final Integer minimumFormat, + final Integer maximumFormat, + final String description, + final boolean addBlankSlot, + final boolean addSpaces + ) { + this.format = format; + this.minimumFormat = minimumFormat; + this.maximumFormat = maximumFormat; + this.description = description; + this.addBlankSlot = addBlankSlot; + this.addSpaces = addSpaces; + } + + /** + * Returns whether the {@link BlankSlot} resources should be added. + * + * @return {@code true} if the {@link BlankSlot} resources should be added, {@code false} otherwise + */ + public boolean addBlankSlot() { + return this.addBlankSlot; + } + + /** + * Returns whether spaces should be added. + * + * @return {@code true} if spaces should be added, {@code false} otherwise + */ + public boolean addSpaces() { + return this.addSpaces; + } + + /** + * Parses the pack formats to a {@link PackMeta} object. + * + * @param serializer the component serializer. Cannot be null. + * @return the generated {@link PackMeta}. + * @throws IllegalStateException if none of the {@link #format},{@link #minimumFormat}, and {@link #maximumFormat} are provided. + */ + public PackMeta parsePackMeta(final ComponentSerializer serializer) { + if (this.format == null && this.minimumFormat == null && this.maximumFormat == null) { + throw new IllegalStateException( + "At least one of format, minimumFormat and maximumFormat must be provided!" + ); + } + + final int minimumFormat; + if (this.format != null) { + minimumFormat = this.format; + } else if (this.minimumFormat != null) { + minimumFormat = this.minimumFormat; + } else { + minimumFormat = this.maximumFormat; + } + + final int maximumFormat; + if (this.format != null) { + maximumFormat = this.format; + } else if (this.maximumFormat != null) { + maximumFormat = this.maximumFormat; + } else { + maximumFormat = minimumFormat; + } + + return PackMeta.of( + PackFormat.format(minimumFormat, minimumFormat, maximumFormat), + serializer.deserialize(this.description) + ); + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackWriter.java b/generator/src/main/java/net/infumia/pack/PackWriter.java new file mode 100644 index 0000000..07f478b --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackWriter.java @@ -0,0 +1,48 @@ +package net.infumia.pack; + +import java.io.BufferedOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.zip.ZipOutputStream; +import team.unnamed.creative.ResourcePack; +import team.unnamed.creative.serialize.ResourcePackWriter; +import team.unnamed.creative.serialize.minecraft.fs.FileTreeWriter; + +final class PackWriter { + + private final PackWriterSettings settings; + + PackWriter(final PackWriterSettings settings) { + this.settings = settings; + } + + PackGeneratedContext write(final PackGeneratorContext context) { + final ResourcePackWriter writer = this.settings.writer(); + final Path outputDirectory = context.outputDirectory(); + final ResourcePack resourcePack = context.resourcePack(); + if (outputDirectory != null) { + writer.write(FileTreeWriter.directory(outputDirectory.toFile()), resourcePack); + } + final Path outputFile = context.outputFile(); + if (outputFile != null) { + try ( + final ZipOutputStream outputStream = new ZipOutputStream( + new BufferedOutputStream(Files.newOutputStream(outputFile)) + ) + ) { + writer.write(FileTreeWriter.zip(outputStream), resourcePack); + } catch (final FileNotFoundException e) { + throw new IllegalStateException( + "Failed to write resource pack to zip file: File not found: " + outputFile, + e + ); + } catch (final IOException e) { + throw new UncheckedIOException(e); + } + } + return new PackGeneratedContext(resourcePack, context.pack(), outputDirectory, outputFile); + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java new file mode 100644 index 0000000..9266270 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java @@ -0,0 +1,17 @@ +package net.infumia.pack; + +import team.unnamed.creative.serialize.ResourcePackWriter; +import team.unnamed.creative.serialize.minecraft.fs.FileTreeWriter; + +public final class PackWriterSettings { + + private final ResourcePackWriter writer; + + public PackWriterSettings(final ResourcePackWriter writer) { + this.writer = writer; + } + + public ResourcePackWriter writer() { + return this.writer; + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 69e8f5e..9127424 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,14 +1,7 @@ -[versions] -creative = "1.7.2" -jackson = "2.17.1" - [libraries] adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0" } -creative-api = { module = "team.unnamed:creative-api", version.ref = "creative" } -creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" } - -jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" } -jackson-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" } +creative-api = { module = "team.unnamed:creative-api", version = "1.7.2" } +jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version = "2.17.1" } nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.25.0" } From 7b40af245e270a3f2ecb520f148fe1d22433101a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 16:58:30 +0300 Subject: [PATCH 26/56] fix. --- README.md | 2 ++ generator/build.gradle.kts | 1 + .../java/net/infumia/pack/PackReaderSettings.java | 4 ++-- .../main/java/net/infumia/pack/PackReference.java | 2 +- .../java/net/infumia/pack/PackWriterSettings.java | 13 +++++++++++++ gradle/libs.versions.toml | 6 +++++- 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 47a796f..3ea8abb 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ dependencies { // Generator (Optional) implementation "net.infumia:pack-generator:VERSION" + // Required, https://mvnrepository.com/artifact/team.unnamed/team.unnamed:creative-serializer-minecraft/ + implementation "team.unnamed:team.unnamed:creative-serializer-minecraft:1.7.2" // Required, https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/ implementation "com.fasterxml.jackson.core:jackson-databind:" // Required, https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/ diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts index 19838e9..2594ee0 100644 --- a/generator/build.gradle.kts +++ b/generator/build.gradle.kts @@ -9,5 +9,6 @@ dependencies { compileOnly(libs.adventure.api) compileOnly(libs.creative.api) + compileOnly(libs.creative.serializer) compileOnly(libs.jackson.databind) } diff --git a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java index 608c14e..b112660 100644 --- a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java @@ -19,7 +19,7 @@ public final class PackReaderSettings { private final ComponentSerializer serializer; /** - * Constructs a new PackReaderSettings. + * Ctor. * * @param root the root path. * @param visitOptions the visit options. Can be null. @@ -48,7 +48,7 @@ public PackReaderSettings( } /** - * Constructs a new PackReaderSettings. + * Ctor. * * @param root the root path. * @param packReferenceFileName the pack reference file name. Cannot be null. diff --git a/generator/src/main/java/net/infumia/pack/PackReference.java b/generator/src/main/java/net/infumia/pack/PackReference.java index 036884d..fb68d9c 100644 --- a/generator/src/main/java/net/infumia/pack/PackReference.java +++ b/generator/src/main/java/net/infumia/pack/PackReference.java @@ -17,7 +17,7 @@ public final class PackReference { private final boolean addSpaces; /** - * Constructs a new PackReference. + * Ctor. * * @param format the pack format. Can be null * @param minimumFormat the minimum pack format. Can be null diff --git a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java index 9266270..a92492d 100644 --- a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java @@ -3,14 +3,27 @@ import team.unnamed.creative.serialize.ResourcePackWriter; import team.unnamed.creative.serialize.minecraft.fs.FileTreeWriter; +/** + * Settings for writing a resource pack. + */ public final class PackWriterSettings { private final ResourcePackWriter writer; + /** + * Ctor. + * + * @param writer the resource pack writer. Cannot be null. + */ public PackWriterSettings(final ResourcePackWriter writer) { this.writer = writer; } + /** + * Returns the resource pack writer. + * + * @return the resource pack writer. + */ public ResourcePackWriter writer() { return this.writer; } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9127424..f131868 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,10 @@ +[versions] +creative = "1.7.2" + [libraries] adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0" } -creative-api = { module = "team.unnamed:creative-api", version = "1.7.2" } +creative-api = { module = "team.unnamed:creative-api", version.ref = "creative" } +creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" } jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version = "2.17.1" } nexus-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.29.0" } From 708da2663ea6fb4fffe2226e28117c854d4e930d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:01:01 +0300 Subject: [PATCH 27/56] simplify. --- .../java/net/infumia/pack/PackWriter.java | 28 +++---------------- .../net/infumia/pack/PackWriterSettings.java | 9 +++--- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackWriter.java b/generator/src/main/java/net/infumia/pack/PackWriter.java index 07f478b..d16f7a0 100644 --- a/generator/src/main/java/net/infumia/pack/PackWriter.java +++ b/generator/src/main/java/net/infumia/pack/PackWriter.java @@ -1,15 +1,8 @@ package net.infumia.pack; -import java.io.BufferedOutputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Files; import java.nio.file.Path; -import java.util.zip.ZipOutputStream; import team.unnamed.creative.ResourcePack; -import team.unnamed.creative.serialize.ResourcePackWriter; -import team.unnamed.creative.serialize.minecraft.fs.FileTreeWriter; +import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter; final class PackWriter { @@ -20,28 +13,15 @@ final class PackWriter { } PackGeneratedContext write(final PackGeneratorContext context) { - final ResourcePackWriter writer = this.settings.writer(); + final MinecraftResourcePackWriter writer = this.settings.writer(); final Path outputDirectory = context.outputDirectory(); final ResourcePack resourcePack = context.resourcePack(); if (outputDirectory != null) { - writer.write(FileTreeWriter.directory(outputDirectory.toFile()), resourcePack); + writer.writeToDirectory(outputDirectory.toFile(), resourcePack); } final Path outputFile = context.outputFile(); if (outputFile != null) { - try ( - final ZipOutputStream outputStream = new ZipOutputStream( - new BufferedOutputStream(Files.newOutputStream(outputFile)) - ) - ) { - writer.write(FileTreeWriter.zip(outputStream), resourcePack); - } catch (final FileNotFoundException e) { - throw new IllegalStateException( - "Failed to write resource pack to zip file: File not found: " + outputFile, - e - ); - } catch (final IOException e) { - throw new UncheckedIOException(e); - } + writer.writeToZipFile(outputFile, resourcePack); } return new PackGeneratedContext(resourcePack, context.pack(), outputDirectory, outputFile); } diff --git a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java index a92492d..5368c0b 100644 --- a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java @@ -1,21 +1,20 @@ package net.infumia.pack; -import team.unnamed.creative.serialize.ResourcePackWriter; -import team.unnamed.creative.serialize.minecraft.fs.FileTreeWriter; +import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter; /** * Settings for writing a resource pack. */ public final class PackWriterSettings { - private final ResourcePackWriter writer; + private final MinecraftResourcePackWriter writer; /** * Ctor. * * @param writer the resource pack writer. Cannot be null. */ - public PackWriterSettings(final ResourcePackWriter writer) { + public PackWriterSettings(final MinecraftResourcePackWriter writer) { this.writer = writer; } @@ -24,7 +23,7 @@ public PackWriterSettings(final ResourcePackWriter writer) { * * @return the resource pack writer. */ - public ResourcePackWriter writer() { + public MinecraftResourcePackWriter writer() { return this.writer; } } From edfb382aa35410216072d8a28c9ca2de5b317eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:17:16 +0300 Subject: [PATCH 28/56] fix. --- .../infumia/pack/PackGeneratedContext.java | 19 ++++-- .../infumia/pack/PackGeneratorContext.java | 26 ++++++-- .../java/net/infumia/pack/PackParser.java | 4 +- .../net/infumia/pack/PackPartReference.java | 3 - .../java/net/infumia/pack/PackReader.java | 30 +++------- .../net/infumia/pack/PackReaderSettings.java | 59 ++++++++++++------- ...kReference.java => PackReferenceMeta.java} | 32 +++++++--- .../net/infumia/pack/PackReferencePart.java | 3 + .../net/infumia/pack/PackWriterSettings.java | 8 +++ 9 files changed, 118 insertions(+), 66 deletions(-) delete mode 100644 generator/src/main/java/net/infumia/pack/PackPartReference.java rename generator/src/main/java/net/infumia/pack/{PackReference.java => PackReferenceMeta.java} (74%) create mode 100644 generator/src/main/java/net/infumia/pack/PackReferencePart.java diff --git a/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java b/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java index 1c647a1..7643eee 100644 --- a/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java +++ b/generator/src/main/java/net/infumia/pack/PackGeneratedContext.java @@ -1,6 +1,7 @@ package net.infumia.pack; import java.nio.file.Path; +import java.util.StringJoiner; import team.unnamed.creative.ResourcePack; /** @@ -16,10 +17,10 @@ public final class PackGeneratedContext { /** * Ctor. * - * @param resourcePack the resource pack. Cannot be null. - * @param pack the pack. Cannot be null. - * @param outputDirectory the output directory. Can be null. - * @param outputFile the output file. Can be null. + * @param resourcePack the resource pack. Cannot be null. + * @param pack the pack. Cannot be null. + * @param outputDirectory the output directory. Can be null. + * @param outputFile the output file. Can be null. */ PackGeneratedContext( final ResourcePack resourcePack, @@ -68,4 +69,14 @@ public Path outputDirectory() { public Path outputFile() { return this.outputFile; } + + @Override + public String toString() { + return new StringJoiner(", ", PackGeneratedContext.class.getSimpleName() + "[", "]") + .add("resourcePack=" + this.resourcePack) + .add("pack=" + this.pack) + .add("outputDirectory=" + this.outputDirectory) + .add("outputFile=" + this.outputFile) + .toString(); + } } diff --git a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java index 994c455..33b9abd 100644 --- a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java +++ b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java @@ -3,6 +3,7 @@ import java.nio.file.Path; import java.util.Collection; import java.util.Collections; +import java.util.StringJoiner; import net.kyori.adventure.text.serializer.ComponentSerializer; import team.unnamed.creative.ResourcePack; @@ -13,8 +14,8 @@ public final class PackGeneratorContext { private final ResourcePack resourcePack; private final Pack pack; - private final PackReference packReference; - private final Collection packPartReferences; + private final PackReferenceMeta packReference; + private final Collection packPartReferences; private final Path outputDirectory; private final Path outputFile; private final ComponentSerializer serializer; @@ -33,8 +34,8 @@ public final class PackGeneratorContext { PackGeneratorContext( final ResourcePack resourcePack, final Pack pack, - final PackReference packReference, - final Collection packPartReferences, + final PackReferenceMeta packReference, + final Collection packPartReferences, final ComponentSerializer serializer, final Path outputDirectory, final Path outputFile @@ -71,7 +72,7 @@ public Pack pack() { * * @return the pack reference. */ - public PackReference packReference() { + public PackReferenceMeta packReference() { return this.packReference; } @@ -80,7 +81,7 @@ public PackReference packReference() { * * @return the pack part references. */ - public Collection packPartReferences() { + public Collection packPartReferences() { return this.packPartReferences; } @@ -110,4 +111,17 @@ public Path outputDirectory() { public Path outputFile() { return this.outputFile; } + + @Override + public String toString() { + return new StringJoiner(", ", PackGeneratorContext.class.getSimpleName() + "[", "]") + .add("resourcePack=" + this.resourcePack) + .add("pack=" + this.pack) + .add("packReference=" + this.packReference) + .add("packPartReferences=" + this.packPartReferences) + .add("outputDirectory=" + this.outputDirectory) + .add("outputFile=" + this.outputFile) + .add("serializer=" + this.serializer) + .toString(); + } } diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java index 3504062..4e7626c 100644 --- a/generator/src/main/java/net/infumia/pack/PackParser.java +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -16,8 +16,8 @@ public final class PackParser { * @return the updated pack generator context. */ public static PackGeneratorContext parse(final PackGeneratorContext context) { - final PackReference packReference = context.packReference(); - final Collection partReferences = context.packPartReferences(); + final PackReferenceMeta packReference = context.packReference(); + final Collection partReferences = context.packPartReferences(); final ResourcePack resourcePack = context.resourcePack(); final PackMeta packMeta = packReference.parsePackMeta(context.serializer()); resourcePack.packMeta(packMeta); diff --git a/generator/src/main/java/net/infumia/pack/PackPartReference.java b/generator/src/main/java/net/infumia/pack/PackPartReference.java deleted file mode 100644 index 9d0e13a..0000000 --- a/generator/src/main/java/net/infumia/pack/PackPartReference.java +++ /dev/null @@ -1,3 +0,0 @@ -package net.infumia.pack; - -public final class PackPartReference {} diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index ddafedd..492beb3 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -21,8 +21,6 @@ final class PackReader { private final Pack base; private File packReferenceFile; - private Path outputDirectory; - private Path outputFile; private ObjectReader packReader; private ObjectReader packPartReader; @@ -44,12 +42,12 @@ PackGeneratorContext read() throws IOException { } private PackGeneratorContext read0(@NotNull final Stream walking) throws IOException { - final PackReference packReference = this.packReader.readValue(this.packReferenceFile); - final Collection packPartReferences = walking + final PackReferenceMeta packReference = this.packReader.readValue(this.packReferenceFile); + final Collection packPartReferences = walking .map(Path::toFile) .map(file -> { try ( - final MappingIterator iterator = + final MappingIterator iterator = this.packPartReader.readValues(file) ) { return iterator.readAll(); @@ -65,8 +63,8 @@ private PackGeneratorContext read0(@NotNull final Stream walking) throws I packReference, packPartReferences, this.settings.serializer(), - this.outputDirectory, - this.outputFile + this.settings.outputDirectory(), + this.settings.outputFile() ); } @@ -80,16 +78,6 @@ private void prepare() { } this.packReferenceFile = packReferenceFile.toFile(); - final String directoryName = this.settings.directoryName(); - if (directoryName != null) { - this.outputDirectory = root.resolve(directoryName); - } - - final String zipFileName = this.settings.zipFileName(); - if (zipFileName != null) { - this.outputFile = root.resolve(zipFileName); - } - final ObjectMapper mapper = this.settings.mapper(); this.packReader = mapper.readerFor(Internal.PACK_TYPE); this.packPartReader = mapper.readerFor(Internal.PACK_PART_TYPE); @@ -97,11 +85,11 @@ private void prepare() { private static final class Internal { - private static final TypeReference PACK_TYPE = new TypeReference< - PackReference + private static final TypeReference PACK_TYPE = new TypeReference< + PackReferenceMeta >() {}; - private static final TypeReference PACK_PART_TYPE = new TypeReference< - PackPartReference + private static final TypeReference PACK_PART_TYPE = new TypeReference< + PackReferencePart >() {}; } } diff --git a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java index b112660..3ed0d63 100644 --- a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java @@ -3,6 +3,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.nio.file.FileVisitOption; import java.nio.file.Path; +import java.util.Arrays; +import java.util.StringJoiner; import net.kyori.adventure.text.serializer.ComponentSerializer; /** @@ -13,8 +15,8 @@ public final class PackReaderSettings { private final Path root; private final FileVisitOption[] visitOptions; private final String packReferenceFileName; - private final String directoryName; - private final String zipFileName; + private final Path outputDirectory; + private final Path outputFile; private final ObjectMapper mapper; private final ComponentSerializer serializer; @@ -24,25 +26,25 @@ public final class PackReaderSettings { * @param root the root path. * @param visitOptions the visit options. Can be null. * @param packReferenceFileName the pack reference file name. Cannot be null. - * @param directoryName the directory name. Can be null. - * @param zipFileName the zip file name. Can be null. + * @param outputDirectory the directory name. Can be null. + * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. - * @param serializer the serializer to serialize components when needed. Cannot be null. + * @param serializer the serializer to serialize components when needed. Cannot be null. */ public PackReaderSettings( final Path root, final FileVisitOption[] visitOptions, final String packReferenceFileName, - final String directoryName, - final String zipFileName, + final Path outputDirectory, + final Path outputFile, final ObjectMapper mapper, final ComponentSerializer serializer ) { this.root = root; this.visitOptions = visitOptions; this.packReferenceFileName = packReferenceFileName; - this.directoryName = directoryName; - this.zipFileName = zipFileName; + this.outputDirectory = outputDirectory; + this.outputFile = outputFile; this.mapper = mapper; this.serializer = serializer; } @@ -52,19 +54,19 @@ public PackReaderSettings( * * @param root the root path. * @param packReferenceFileName the pack reference file name. Cannot be null. - * @param directoryName the directory name. Can be null. - * @param zipFileName the zip file name. Can be null. + * @param outputDirectory the directory name. Can be null. + * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. */ public PackReaderSettings( final Path root, final String packReferenceFileName, - final String directoryName, - final String zipFileName, + final Path outputDirectory, + final Path outputFile, final ObjectMapper mapper, final ComponentSerializer serializer ) { - this(root, null, packReferenceFileName, directoryName, zipFileName, mapper, serializer); + this(root, null, packReferenceFileName, outputDirectory, outputFile, mapper, serializer); } /** @@ -95,21 +97,21 @@ public String packReferenceFileName() { } /** - * Returns the directory name. + * Returns the output directory. * - * @return the directory name. Can be null. + * @return the directory output. Can be null. */ - public String directoryName() { - return this.directoryName; + public Path outputDirectory() { + return this.outputDirectory; } /** - * Returns the zip file name. + * Returns the zip file. * - * @return the zip file name. Can be null. + * @return the zip file. Can be null. */ - public String zipFileName() { - return this.zipFileName; + public Path outputFile() { + return this.outputFile; } /** @@ -129,4 +131,17 @@ public ObjectMapper mapper() { public ComponentSerializer serializer() { return this.serializer; } + + @Override + public String toString() { + return new StringJoiner(", ", PackReaderSettings.class.getSimpleName() + "[", "]") + .add("root=" + this.root) + .add("visitOptions=" + Arrays.toString(this.visitOptions)) + .add("packReferenceFileName='" + this.packReferenceFileName + "'") + .add("outputDirectory=" + this.outputDirectory) + .add("outputFile=" + this.outputFile) + .add("mapper=" + this.mapper) + .add("serializer=" + this.serializer) + .toString(); + } } diff --git a/generator/src/main/java/net/infumia/pack/PackReference.java b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java similarity index 74% rename from generator/src/main/java/net/infumia/pack/PackReference.java rename to generator/src/main/java/net/infumia/pack/PackReferenceMeta.java index fb68d9c..e9a7c83 100644 --- a/generator/src/main/java/net/infumia/pack/PackReference.java +++ b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java @@ -1,5 +1,8 @@ package net.infumia.pack; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.StringJoiner; import net.kyori.adventure.text.serializer.ComponentSerializer; import team.unnamed.creative.metadata.pack.PackFormat; import team.unnamed.creative.metadata.pack.PackMeta; @@ -7,7 +10,7 @@ /** * Represents a reference to a pack with format constraints and a description. */ -public final class PackReference { +public final class PackReferenceMeta { private final Integer format; private final Integer minimumFormat; @@ -26,13 +29,14 @@ public final class PackReference { * @param addBlankSlot adds the {@link BlankSlot} resources. * @param addSpaces adds the {@link ResourceProducers#spacesBitmap()} or {@link ResourceProducers#spacesMojang()} based on the pack format. */ - public PackReference( - final Integer format, - final Integer minimumFormat, - final Integer maximumFormat, - final String description, - final boolean addBlankSlot, - final boolean addSpaces + @JsonCreator + public PackReferenceMeta( + @JsonProperty("format") final Integer format, + @JsonProperty("minimum-format") final Integer minimumFormat, + @JsonProperty("maximum-format") final Integer maximumFormat, + @JsonProperty("description") final String description, + @JsonProperty("add-blank-slot") final boolean addBlankSlot, + @JsonProperty("add-spaces") final boolean addSpaces ) { this.format = format; this.minimumFormat = minimumFormat; @@ -97,4 +101,16 @@ public PackMeta parsePackMeta(final ComponentSerializer serializer serializer.deserialize(this.description) ); } + + @Override + public String toString() { + return new StringJoiner(", ", PackReferenceMeta.class.getSimpleName() + "[", "]") + .add("format=" + this.format) + .add("minimumFormat=" + this.minimumFormat) + .add("maximumFormat=" + this.maximumFormat) + .add("description='" + this.description + "'") + .add("addBlankSlot=" + this.addBlankSlot) + .add("addSpaces=" + this.addSpaces) + .toString(); + } } diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java new file mode 100644 index 0000000..6296f99 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -0,0 +1,3 @@ +package net.infumia.pack; + +public final class PackReferencePart {} diff --git a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java index 5368c0b..f3f3d1f 100644 --- a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java @@ -1,5 +1,6 @@ package net.infumia.pack; +import java.util.StringJoiner; import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter; /** @@ -26,4 +27,11 @@ public PackWriterSettings(final MinecraftResourcePackWriter writer) { public MinecraftResourcePackWriter writer() { return this.writer; } + + @Override + public String toString() { + return new StringJoiner(", ", PackWriterSettings.class.getSimpleName() + "[", "]") + .add("writer=" + this.writer) + .toString(); + } } From b333868b8c5e5bd7db5df864da2f4002786847eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:26:55 +0300 Subject: [PATCH 29/56] fix. --- .../net/infumia/pack/PackReadFilters.java | 24 +++++++++++++++ .../java/net/infumia/pack/PackReader.java | 3 ++ .../net/infumia/pack/PackReaderSettings.java | 29 ++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 generator/src/main/java/net/infumia/pack/PackReadFilters.java diff --git a/generator/src/main/java/net/infumia/pack/PackReadFilters.java b/generator/src/main/java/net/infumia/pack/PackReadFilters.java new file mode 100644 index 0000000..d67edb7 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReadFilters.java @@ -0,0 +1,24 @@ +package net.infumia.pack; + +import java.nio.file.Path; +import java.util.function.Predicate; + +/** + * Utility class for creating read filters for pack reading. + */ +public final class PackReadFilters { + + /** + * Creates a predicate that filters paths by the specified file extension. + * + * @param extension the file extension to filter by. Cannot be null. + * @return a predicate that returns true for paths with the specified extension. + */ + public static Predicate withExtension(final String extension) { + return path -> path.getFileName().toString().endsWith(extension); + } + + private PackReadFilters() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index 492beb3..3d4d21c 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -44,7 +44,10 @@ PackGeneratorContext read() throws IOException { private PackGeneratorContext read0(@NotNull final Stream walking) throws IOException { final PackReferenceMeta packReference = this.packReader.readValue(this.packReferenceFile); final Collection packPartReferences = walking + .filter(Files::isRegularFile) + .filter(this.settings.readFilter()) .map(Path::toFile) + .filter(file -> !this.packReferenceFile.equals(file)) .map(file -> { try ( final MappingIterator iterator = diff --git a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java index 3ed0d63..f0f4f22 100644 --- a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java @@ -2,9 +2,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.nio.file.FileVisitOption; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.StringJoiner; +import java.util.function.Predicate; import net.kyori.adventure.text.serializer.ComponentSerializer; /** @@ -18,6 +20,7 @@ public final class PackReaderSettings { private final Path outputDirectory; private final Path outputFile; private final ObjectMapper mapper; + private final Predicate readFilter; private final ComponentSerializer serializer; /** @@ -29,6 +32,7 @@ public final class PackReaderSettings { * @param outputDirectory the directory name. Can be null. * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. + * @param readFilter the read filter for {@link Files#walk(Path, FileVisitOption...)}. * @param serializer the serializer to serialize components when needed. Cannot be null. */ public PackReaderSettings( @@ -38,6 +42,7 @@ public PackReaderSettings( final Path outputDirectory, final Path outputFile, final ObjectMapper mapper, + final Predicate readFilter, final ComponentSerializer serializer ) { this.root = root; @@ -46,6 +51,7 @@ public PackReaderSettings( this.outputDirectory = outputDirectory; this.outputFile = outputFile; this.mapper = mapper; + this.readFilter = readFilter; this.serializer = serializer; } @@ -57,6 +63,7 @@ public PackReaderSettings( * @param outputDirectory the directory name. Can be null. * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. + * @param readFilter the read filter for {@link Files#walk(Path, FileVisitOption...)}. */ public PackReaderSettings( final Path root, @@ -64,9 +71,19 @@ public PackReaderSettings( final Path outputDirectory, final Path outputFile, final ObjectMapper mapper, + final Predicate readFilter, final ComponentSerializer serializer ) { - this(root, null, packReferenceFileName, outputDirectory, outputFile, mapper, serializer); + this( + root, + null, + packReferenceFileName, + outputDirectory, + outputFile, + mapper, + readFilter, + serializer + ); } /** @@ -123,6 +140,15 @@ public ObjectMapper mapper() { return this.mapper; } + /** + * Returns the read filter. + * + * @return the read filter. + */ + public Predicate readFilter() { + return this.readFilter; + } + /** * Returns the component serializer. * @@ -141,6 +167,7 @@ public String toString() { .add("outputDirectory=" + this.outputDirectory) .add("outputFile=" + this.outputFile) .add("mapper=" + this.mapper) + .add("readFilter=" + this.readFilter) .add("serializer=" + this.serializer) .toString(); } From 75bb35eb6b105e12be68363c1caf1737c36171e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:35:42 +0300 Subject: [PATCH 30/56] simplify. --- .../java/net/infumia/pack/PackParser.java | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java index 4e7626c..f5fd8e4 100644 --- a/generator/src/main/java/net/infumia/pack/PackParser.java +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -1,7 +1,6 @@ package net.infumia.pack; import java.util.Collection; -import team.unnamed.creative.ResourcePack; import team.unnamed.creative.metadata.pack.PackMeta; /** @@ -16,25 +15,33 @@ public final class PackParser { * @return the updated pack generator context. */ public static PackGeneratorContext parse(final PackGeneratorContext context) { - final PackReferenceMeta packReference = context.packReference(); - final Collection partReferences = context.packPartReferences(); - final ResourcePack resourcePack = context.resourcePack(); - final PackMeta packMeta = packReference.parsePackMeta(context.serializer()); - resourcePack.packMeta(packMeta); + PackParser.parseMeta(context); + PackParser.parseParts(context); + context.pack().writeAll(context.resourcePack()); + return context; + } + + private static void parseMeta(final PackGeneratorContext context) { final Pack pack = context.pack(); - // TODO: portlek, Parse partReferences here. - if (packReference.addBlankSlot()) { + final PackReferenceMeta meta = context.packReference(); + final PackMeta packMeta = meta.parsePackMeta(context.serializer()); + context.resourcePack().packMeta(packMeta); + if (meta.addBlankSlot()) { pack.with(BlankSlot.get()); } - if (packReference.addSpaces()) { + if (meta.addSpaces()) { if (packMeta.formats().isInRange(9)) { pack.withMojangSpaces(); } else { pack.withBitmapSpaces(); } } - pack.writeAll(resourcePack); - return context; + } + + private static void parseParts(final PackGeneratorContext context) { + final Pack pack = context.pack(); + final Collection parts = context.packPartReferences(); + for (final PackReferencePart part : parts) {} } private PackParser() { From 10d99ed51da56e149ffe3408d2e017a7229585dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:43:29 +0300 Subject: [PATCH 31/56] start part parser. --- .../java/net/infumia/pack/PackParser.java | 4 ++- .../net/infumia/pack/PackReferenceMeta.java | 4 +-- .../net/infumia/pack/PackReferencePart.java | 27 ++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java index f5fd8e4..274c3ec 100644 --- a/generator/src/main/java/net/infumia/pack/PackParser.java +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -41,7 +41,9 @@ private static void parseMeta(final PackGeneratorContext context) { private static void parseParts(final PackGeneratorContext context) { final Pack pack = context.pack(); final Collection parts = context.packPartReferences(); - for (final PackReferencePart part : parts) {} + for (final PackReferencePart part : parts) { + pack.with(part.toResource()); + } } private PackParser() { diff --git a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java index e9a7c83..94382b7 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java +++ b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java @@ -31,10 +31,10 @@ public final class PackReferenceMeta { */ @JsonCreator public PackReferenceMeta( - @JsonProperty("format") final Integer format, + @JsonProperty final Integer format, @JsonProperty("minimum-format") final Integer minimumFormat, @JsonProperty("maximum-format") final Integer maximumFormat, - @JsonProperty("description") final String description, + @JsonProperty final String description, @JsonProperty("add-blank-slot") final boolean addBlankSlot, @JsonProperty("add-spaces") final boolean addSpaces ) { diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index 6296f99..6992f97 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -1,3 +1,28 @@ package net.infumia.pack; -public final class PackReferencePart {} +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public final class PackReferencePart { + + private final String key; + private final String image; + + @JsonCreator + public PackReferencePart(@JsonProperty final String key, @JsonProperty final String image) { + this.key = key; + this.image = image; + } + + public String key() { + return this.key; + } + + public String image() { + return this.image; + } + + public FileResource toResource() { + return FileResources.all(); + } +} From 403206c46fca4b71941d3c3661d8ac599d73075c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:43:54 +0300 Subject: [PATCH 32/56] rename. --- generator/src/main/java/net/infumia/pack/PackParser.java | 2 +- generator/src/main/java/net/infumia/pack/PackReferencePart.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java index 274c3ec..724f61b 100644 --- a/generator/src/main/java/net/infumia/pack/PackParser.java +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -42,7 +42,7 @@ private static void parseParts(final PackGeneratorContext context) { final Pack pack = context.pack(); final Collection parts = context.packPartReferences(); for (final PackReferencePart part : parts) { - pack.with(part.toResource()); + pack.with(part.parseResource()); } } diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index 6992f97..a4dc4ca 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -22,7 +22,7 @@ public String image() { return this.image; } - public FileResource toResource() { + public FileResource parseResource() { return FileResources.all(); } } From 3c44f8580644b4c23877fc7802cf14152a0336a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:46:08 +0300 Subject: [PATCH 33/56] fix. --- .../src/main/java/net/infumia/pack/PackReferenceMeta.java | 4 ++-- .../src/main/java/net/infumia/pack/PackReferencePart.java | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java index 94382b7..e9a7c83 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java +++ b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java @@ -31,10 +31,10 @@ public final class PackReferenceMeta { */ @JsonCreator public PackReferenceMeta( - @JsonProperty final Integer format, + @JsonProperty("format") final Integer format, @JsonProperty("minimum-format") final Integer minimumFormat, @JsonProperty("maximum-format") final Integer maximumFormat, - @JsonProperty final String description, + @JsonProperty("description") final String description, @JsonProperty("add-blank-slot") final boolean addBlankSlot, @JsonProperty("add-spaces") final boolean addSpaces ) { diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index a4dc4ca..a801ee1 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -9,7 +9,10 @@ public final class PackReferencePart { private final String image; @JsonCreator - public PackReferencePart(@JsonProperty final String key, @JsonProperty final String image) { + public PackReferencePart( + @JsonProperty("key") final String key, + @JsonProperty("image") final String image + ) { this.key = key; this.image = image; } From 2272692bb9c02d4dd2517fe82d74bb5fc4444c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:47:48 +0300 Subject: [PATCH 34/56] Update PackReferencePart.java --- .../src/main/java/net/infumia/pack/PackReferencePart.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index a801ee1..572ee38 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -17,14 +17,6 @@ public PackReferencePart( this.image = image; } - public String key() { - return this.key; - } - - public String image() { - return this.image; - } - public FileResource parseResource() { return FileResources.all(); } From dd94ed82a11159178bb11d19e7289e2c9b4b5581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 18:03:59 +0300 Subject: [PATCH 35/56] add images first. --- .../infumia/pack/PackGeneratorContext.java | 29 ++++++++++++----- .../java/net/infumia/pack/PackParser.java | 3 +- .../java/net/infumia/pack/PackReader.java | 5 +-- .../net/infumia/pack/PackReferencePart.java | 32 +++++++++++++++++-- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java index 33b9abd..6a1698e 100644 --- a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java +++ b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java @@ -16,6 +16,7 @@ public final class PackGeneratorContext { private final Pack pack; private final PackReferenceMeta packReference; private final Collection packPartReferences; + private final Path rootDirectory; private final Path outputDirectory; private final Path outputFile; private final ComponentSerializer serializer; @@ -27,26 +28,29 @@ public final class PackGeneratorContext { * @param pack the pack. Cannot be null. * @param packReference the pack file reference. Cannot be null. * @param packPartReferences the pack part references. Cannot be null. - * @param serializer the serializer to serialize components when needed. Cannot be null. + * @param rootDirectory the root directory of the pack. Cannot be null. * @param outputDirectory the output directory. Can be null. * @param outputFile the output file. Can be null. + * @param serializer the serializer to serialize components when needed. Cannot be null. */ PackGeneratorContext( final ResourcePack resourcePack, final Pack pack, final PackReferenceMeta packReference, final Collection packPartReferences, - final ComponentSerializer serializer, + final Path rootDirectory, final Path outputDirectory, - final Path outputFile + final Path outputFile, + final ComponentSerializer serializer ) { this.resourcePack = resourcePack; this.pack = pack; this.packReference = packReference; this.packPartReferences = Collections.unmodifiableCollection(packPartReferences); - this.serializer = serializer; + this.rootDirectory = rootDirectory; this.outputDirectory = outputDirectory; this.outputFile = outputFile; + this.serializer = serializer; } /** @@ -86,12 +90,12 @@ public Collection packPartReferences() { } /** - * Returns the component serializer. + * Returns the root directory. * - * @return the component serializer. + * @return the root directory. Can be null. */ - public ComponentSerializer serializer() { - return this.serializer; + public Path rootDirectory() { + return this.rootDirectory; } /** @@ -112,6 +116,15 @@ public Path outputFile() { return this.outputFile; } + /** + * Returns the component serializer. + * + * @return the component serializer. + */ + public ComponentSerializer serializer() { + return this.serializer; + } + @Override public String toString() { return new StringJoiner(", ", PackGeneratorContext.class.getSimpleName() + "[", "]") diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java index 724f61b..3e1c96c 100644 --- a/generator/src/main/java/net/infumia/pack/PackParser.java +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -39,10 +39,9 @@ private static void parseMeta(final PackGeneratorContext context) { } private static void parseParts(final PackGeneratorContext context) { - final Pack pack = context.pack(); final Collection parts = context.packPartReferences(); for (final PackReferencePart part : parts) { - pack.with(part.parseResource()); + part.add(context); } } diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index 3d4d21c..df8f8ce 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -65,9 +65,10 @@ private PackGeneratorContext read0(@NotNull final Stream walking) throws I this.base, packReference, packPartReferences, - this.settings.serializer(), + this.settings.root(), this.settings.outputDirectory(), - this.settings.outputFile() + this.settings.outputFile(), + this.settings.serializer() ); } diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index 572ee38..c5eec2e 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -2,22 +2,48 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import java.nio.file.Path; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; +import team.unnamed.creative.font.Font; +import team.unnamed.creative.texture.Texture; public final class PackReferencePart { + private final String namespace; private final String key; private final String image; + private final int height; + private final int ascent; @JsonCreator public PackReferencePart( + @JsonProperty("namespace") final String namespace, @JsonProperty("key") final String key, - @JsonProperty("image") final String image + @JsonProperty("image") final String image, + @JsonProperty("height") final int height, + @JsonProperty("ascent") final int ascent ) { + this.namespace = namespace; this.key = key; this.image = image; + this.height = height; + this.ascent = ascent; } - public FileResource parseResource() { - return FileResources.all(); + public void add(final PackGeneratorContext context) { + final Path root = context.rootDirectory(); + final Pack pack = context.pack(); + pack.with( + (ResourceIdentifierImage) () -> this.key, + ResourceProducers.image( + Font.MINECRAFT_DEFAULT, + Texture.texture( + Key.key(this.namespace, this.key + ".png"), + Writable.path(root.resolve(this.image)) + ), + new TextureProperties(this.height, this.ascent) + ) + ); } } From d7dbe0cc12792346d9c422eca1153ef5de260650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 18:12:31 +0300 Subject: [PATCH 36/56] subtypes. --- .../net/infumia/pack/PackReferencePart.java | 50 +++---------------- .../infumia/pack/PackReferencePartImage.java | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 generator/src/main/java/net/infumia/pack/PackReferencePartImage.java diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index c5eec2e..8adb6f6 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -1,49 +1,11 @@ package net.infumia.pack; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; -import java.nio.file.Path; -import net.kyori.adventure.key.Key; -import team.unnamed.creative.base.Writable; -import team.unnamed.creative.font.Font; -import team.unnamed.creative.texture.Texture; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; -public final class PackReferencePart { +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") +@JsonSubTypes({ @JsonSubTypes.Type(value = PackReferencePartImage.class, name = "image") }) +public abstract class PackReferencePart { - private final String namespace; - private final String key; - private final String image; - private final int height; - private final int ascent; - - @JsonCreator - public PackReferencePart( - @JsonProperty("namespace") final String namespace, - @JsonProperty("key") final String key, - @JsonProperty("image") final String image, - @JsonProperty("height") final int height, - @JsonProperty("ascent") final int ascent - ) { - this.namespace = namespace; - this.key = key; - this.image = image; - this.height = height; - this.ascent = ascent; - } - - public void add(final PackGeneratorContext context) { - final Path root = context.rootDirectory(); - final Pack pack = context.pack(); - pack.with( - (ResourceIdentifierImage) () -> this.key, - ResourceProducers.image( - Font.MINECRAFT_DEFAULT, - Texture.texture( - Key.key(this.namespace, this.key + ".png"), - Writable.path(root.resolve(this.image)) - ), - new TextureProperties(this.height, this.ascent) - ) - ); - } + public abstract void add(PackGeneratorContext context); } diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java new file mode 100644 index 0000000..aec5f81 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -0,0 +1,50 @@ +package net.infumia.pack; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.nio.file.Path; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; +import team.unnamed.creative.font.Font; +import team.unnamed.creative.texture.Texture; + +public final class PackReferencePartImage extends PackReferencePart { + + private final String namespace; + private final String key; + private final String image; + private final int height; + private final int ascent; + + @JsonCreator + public PackReferencePartImage( + @JsonProperty("namespace") final String namespace, + @JsonProperty("key") final String key, + @JsonProperty("image") final String image, + @JsonProperty("height") final int height, + @JsonProperty("ascent") final int ascent + ) { + this.namespace = namespace; + this.key = key; + this.image = image; + this.height = height; + this.ascent = ascent; + } + + @Override + public void add(final PackGeneratorContext context) { + final Path root = context.rootDirectory(); + final Pack pack = context.pack(); + pack.with( + (ResourceIdentifierImage) () -> this.key, + ResourceProducers.image( + Font.MINECRAFT_DEFAULT, + Texture.texture( + Key.key(this.namespace, this.key + ".png"), + Writable.path(root.resolve(this.image)) + ), + new TextureProperties(this.height, this.ascent) + ) + ); + } +} From 392a79df8b2476381d2a8c1c34cb1d27012e6af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 18:31:03 +0300 Subject: [PATCH 37/56] use parent folder names. --- .../java/net/infumia/pack/PackReader.java | 11 +++++++- .../net/infumia/pack/PackReferencePart.java | 17 ++++++++++++ .../infumia/pack/PackReferencePartImage.java | 26 +++++++++++++++++-- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index df8f8ce..d328856 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -53,7 +53,16 @@ private PackGeneratorContext read0(@NotNull final Stream walking) throws I final MappingIterator iterator = this.packPartReader.readValues(file) ) { - return iterator.readAll(); + final Path path = file.getParentFile().toPath(); + if (path.equals(this.settings.root())) { + return iterator.readAll(); + } else { + return iterator + .readAll() + .stream() + .map(part -> part.directory(path)) + .collect(Collectors.toSet()); + } } catch (final IOException e) { throw new RuntimeException(e); } diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index 8adb6f6..f5d47a5 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -2,10 +2,27 @@ import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import java.nio.file.Path; +/** + * Abstract base class for a pack part reference. + */ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonSubTypes({ @JsonSubTypes.Type(value = PackReferencePartImage.class, name = "image") }) public abstract class PackReferencePart { + /** + * Adds this part to the given pack generation context. + * + * @param context the pack generation context + */ public abstract void add(PackGeneratorContext context); + + /** + * Sets the directory for this pack part reference. + * + * @param directory the directory path. Cannot be null. + * @return the updated pack part reference. + */ + abstract PackReferencePart directory(Path directory); } diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index aec5f81..73991f6 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import java.nio.file.Path; +import java.util.Locale; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; import team.unnamed.creative.font.Font; @@ -16,6 +17,8 @@ public final class PackReferencePartImage extends PackReferencePart { private final int height; private final int ascent; + private Path directory; + @JsonCreator public PackReferencePartImage( @JsonProperty("namespace") final String namespace, @@ -34,17 +37,36 @@ public PackReferencePartImage( @Override public void add(final PackGeneratorContext context) { final Path root = context.rootDirectory(); + + final String parent; + if (this.directory == null) { + parent = ""; + } else { + parent = root + .relativize(this.directory) + .toString() + .toLowerCase(Locale.ROOT) + .replace(" ", "_") + + "/"; + } + final Pack pack = context.pack(); pack.with( (ResourceIdentifierImage) () -> this.key, ResourceProducers.image( Font.MINECRAFT_DEFAULT, Texture.texture( - Key.key(this.namespace, this.key + ".png"), - Writable.path(root.resolve(this.image)) + Key.key(this.namespace, parent + this.key + ".png"), + Writable.path(root.resolve(parent + this.image)) ), new TextureProperties(this.height, this.ascent) ) ); } + + @Override + PackReferencePartImage directory(final Path directory) { + this.directory = directory; + return this; + } } From 96ebdffdf8419cf08ee78c907cbabc9dd54277bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 25 Jun 2024 16:16:33 +0300 Subject: [PATCH 38/56] default namespace. --- .../net/infumia/pack/PackReferenceMeta.java | 31 ++++++++++++++----- .../infumia/pack/PackReferencePartImage.java | 7 ++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java index e9a7c83..60aa85e 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java +++ b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java @@ -2,11 +2,13 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.StringJoiner; +import net.kyori.adventure.key.KeyPattern; import net.kyori.adventure.text.serializer.ComponentSerializer; import team.unnamed.creative.metadata.pack.PackFormat; import team.unnamed.creative.metadata.pack.PackMeta; +import java.util.StringJoiner; + /** * Represents a reference to a pack with format constraints and a description. */ @@ -18,16 +20,18 @@ public final class PackReferenceMeta { private final String description; private final boolean addBlankSlot; private final boolean addSpaces; + private final String defaultNamespace; /** * Ctor. * - * @param format the pack format. Can be null - * @param minimumFormat the minimum pack format. Can be null - * @param maximumFormat the maximum pack format. Can be null - * @param description the description of the pack - * @param addBlankSlot adds the {@link BlankSlot} resources. - * @param addSpaces adds the {@link ResourceProducers#spacesBitmap()} or {@link ResourceProducers#spacesMojang()} based on the pack format. + * @param format the pack format. Can be null + * @param minimumFormat the minimum pack format. Can be null + * @param maximumFormat the maximum pack format. Can be null + * @param description the description of the pack + * @param addBlankSlot adds the {@link BlankSlot} resources. + * @param addSpaces adds the {@link ResourceProducers#spacesBitmap()} or {@link ResourceProducers#spacesMojang()} based on the pack format. + * @param defaultNamespace the default namespace that will be used when a {@link PackReferencePart} does not have a namespace. Can be null. */ @JsonCreator public PackReferenceMeta( @@ -36,7 +40,8 @@ public PackReferenceMeta( @JsonProperty("maximum-format") final Integer maximumFormat, @JsonProperty("description") final String description, @JsonProperty("add-blank-slot") final boolean addBlankSlot, - @JsonProperty("add-spaces") final boolean addSpaces + @JsonProperty("add-spaces") final boolean addSpaces, + @JsonProperty("default-namespace") final String defaultNamespace ) { this.format = format; this.minimumFormat = minimumFormat; @@ -44,6 +49,7 @@ public PackReferenceMeta( this.description = description; this.addBlankSlot = addBlankSlot; this.addSpaces = addSpaces; + this.defaultNamespace = defaultNamespace; } /** @@ -64,6 +70,15 @@ public boolean addSpaces() { return this.addSpaces; } + /** + * Returns the default namespace. + * + * @return the default namespace. Can be null. + */ + public String defaultNamespace() { + return this.defaultNamespace; + } + /** * Parses the pack formats to a {@link PackMeta} object. * diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index 73991f6..e0dc6ec 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -36,6 +36,11 @@ public PackReferencePartImage( @Override public void add(final PackGeneratorContext context) { + final String namespace = this.namespace == null ? context.packReference().defaultNamespace() : this.namespace; + if (namespace == null) { + throw new IllegalStateException("Pack reference namespace cannot be null!"); + } + final Path root = context.rootDirectory(); final String parent; @@ -56,7 +61,7 @@ public void add(final PackGeneratorContext context) { ResourceProducers.image( Font.MINECRAFT_DEFAULT, Texture.texture( - Key.key(this.namespace, parent + this.key + ".png"), + Key.key(namespace, parent + this.key + ".png"), Writable.path(root.resolve(parent + this.image)) ), new TextureProperties(this.height, this.ascent) From d362d9953d51f320f649e7c1231a6db10a52a275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 25 Jun 2024 16:17:12 +0300 Subject: [PATCH 39/56] format. --- .../src/main/java/net/infumia/pack/PackReferenceMeta.java | 4 +--- .../main/java/net/infumia/pack/PackReferencePartImage.java | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java index 60aa85e..098d892 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java +++ b/generator/src/main/java/net/infumia/pack/PackReferenceMeta.java @@ -2,13 +2,11 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import net.kyori.adventure.key.KeyPattern; +import java.util.StringJoiner; import net.kyori.adventure.text.serializer.ComponentSerializer; import team.unnamed.creative.metadata.pack.PackFormat; import team.unnamed.creative.metadata.pack.PackMeta; -import java.util.StringJoiner; - /** * Represents a reference to a pack with format constraints and a description. */ diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index e0dc6ec..a402b14 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -36,7 +36,9 @@ public PackReferencePartImage( @Override public void add(final PackGeneratorContext context) { - final String namespace = this.namespace == null ? context.packReference().defaultNamespace() : this.namespace; + final String namespace = this.namespace == null + ? context.packReference().defaultNamespace() + : this.namespace; if (namespace == null) { throw new IllegalStateException("Pack reference namespace cannot be null!"); } From c8accbf5c90aced288924f8052e311ea9900f118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 25 Jun 2024 16:35:25 +0300 Subject: [PATCH 40/56] Update PackReferencePartImage.java --- .../src/main/java/net/infumia/pack/PackReferencePartImage.java | 1 + 1 file changed, 1 insertion(+) diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index a402b14..00574c3 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -53,6 +53,7 @@ public void add(final PackGeneratorContext context) { .relativize(this.directory) .toString() .toLowerCase(Locale.ROOT) + .replace("\\", "/") .replace(" ", "_") + "/"; } From 067024262d75c4adf47beb9c91c079dd88247f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 25 Jun 2024 16:37:05 +0300 Subject: [PATCH 41/56] Update PackReferencePartImage.java --- .../main/java/net/infumia/pack/PackReferencePartImage.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index 00574c3..7674a3d 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -59,12 +59,13 @@ public void add(final PackGeneratorContext context) { } final Pack pack = context.pack(); + final String key = parent + this.key; pack.with( - (ResourceIdentifierImage) () -> this.key, + (ResourceIdentifierImage) () -> key, ResourceProducers.image( Font.MINECRAFT_DEFAULT, Texture.texture( - Key.key(namespace, parent + this.key + ".png"), + Key.key(namespace, key + ".png"), Writable.path(root.resolve(parent + this.image)) ), new TextureProperties(this.height, this.ascent) From 8eda643c3f76451b4ae59380c1adaa707c16a1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 25 Jun 2024 17:00:30 +0300 Subject: [PATCH 42/56] split. --- .../java/net/infumia/pack/PackGenerator.java | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackGenerator.java b/generator/src/main/java/net/infumia/pack/PackGenerator.java index 167cc67..441d521 100644 --- a/generator/src/main/java/net/infumia/pack/PackGenerator.java +++ b/generator/src/main/java/net/infumia/pack/PackGenerator.java @@ -21,11 +21,10 @@ public static PackGeneratedContext generate( final PackWriterSettings writerSettings, final Pack base ) throws IOException { - final PackReader reader = new PackReader(readerSettings, base); - final PackWriter writer = new PackWriter(writerSettings); - final PackGeneratorContext context = reader.read(); - final PackGeneratorContext parsed = PackParser.parse(context); - return writer.write(parsed); + return PackGenerator.write( + writerSettings, + PackParser.parse(PackGenerator.read(readerSettings, base)) + ); } /** @@ -43,6 +42,35 @@ public static PackGeneratedContext generate( return PackGenerator.generate(readerSettings, writerSettings, Packs.create()); } + /** + * Reads the pack based on the provided settings and base pack. + * + * @param readerSettings the pack reader settings. Cannot be null. + * @param base the base pack. Cannot be null. + * @return the pack generation context. + * @throws IOException if an I/O error is thrown when accessing the starting file. + */ + public static PackGeneratorContext read( + final PackReaderSettings readerSettings, + final Pack base + ) throws IOException { + return new PackReader(readerSettings, base).read(); + } + + /** + * Writes the pack based on the provided settings and context. + * + * @param writerSettings the pack writer settings. Cannot be null. + * @param context the pack generator context. Cannot be null. + * @return the generated pack context. + */ + public static PackGeneratedContext write( + final PackWriterSettings writerSettings, + final PackGeneratorContext context + ) { + return new PackWriter(writerSettings).write(context); + } + private PackGenerator() { throw new IllegalStateException("Utility class"); } From 681986d135968b19d2597276e69dca21725403ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 25 Jun 2024 17:23:09 +0300 Subject: [PATCH 43/56] Update PackReferencePartImage.java --- .../infumia/pack/PackReferencePartImage.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index 7674a3d..7285773 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -4,11 +4,15 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.nio.file.Path; import java.util.Locale; +import java.util.StringJoiner; import net.kyori.adventure.key.Key; import team.unnamed.creative.base.Writable; import team.unnamed.creative.font.Font; import team.unnamed.creative.texture.Texture; +/** + * Represents an image part of a pack reference. + */ public final class PackReferencePartImage extends PackReferencePart { private final String namespace; @@ -19,13 +23,22 @@ public final class PackReferencePartImage extends PackReferencePart { private Path directory; + /** + * Ctor. + * + * @param namespace the namespace of the image. Can be null + * @param key the key for the image. Cannot be null. + * @param image the image file name. Cannot be null. + * @param height the height of the image. + * @param ascent the ascent value of the image. + */ @JsonCreator public PackReferencePartImage( @JsonProperty("namespace") final String namespace, - @JsonProperty("key") final String key, - @JsonProperty("image") final String image, - @JsonProperty("height") final int height, - @JsonProperty("ascent") final int ascent + @JsonProperty(value = "key", required = true) final String key, + @JsonProperty(value = "image", required = true) final String image, + @JsonProperty(value = "height", required = true) final int height, + @JsonProperty(value = "ascent", required = true) final int ascent ) { this.namespace = namespace; this.key = key; @@ -78,4 +91,16 @@ PackReferencePartImage directory(final Path directory) { this.directory = directory; return this; } + + @Override + public String toString() { + return new StringJoiner(", ", PackReferencePartImage.class.getSimpleName() + "[", "]") + .add("namespace='" + this.namespace + "'") + .add("key='" + this.key + "'") + .add("image='" + this.image + "'") + .add("height=" + this.height) + .add("ascent=" + this.ascent) + .add("directory=" + this.directory) + .toString(); + } } From b176bfec6c265664a9a9a41f91572a3f5c3b1f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 1 Jul 2024 19:58:47 +0300 Subject: [PATCH 44/56] move. --- buildSrc/build.gradle.kts | 7 ++++++- buildSrc/settings.gradle.kts | 2 ++ buildSrc/src/main/kotlin/net/infumia/gradle/common.kt | 6 +++++- gradlew | 0 settings.gradle.kts | 2 -- 5 files changed, 13 insertions(+), 4 deletions(-) mode change 100644 => 100755 gradlew diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index a622070..910a66b 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -10,4 +10,9 @@ dependencies { implementation(libs.spotless.plugin) } -kotlin { jvmToolchain(11) } +kotlin { + jvmToolchain { + languageVersion = JavaLanguageVersion.of(11) + vendor = JvmVendorSpec.ADOPTIUM + } +} diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts index 65a9695..245f39a 100644 --- a/buildSrc/settings.gradle.kts +++ b/buildSrc/settings.gradle.kts @@ -1,3 +1,5 @@ +plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" } + dependencyResolutionManagement { versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } } } diff --git a/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt b/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt index abc779c..7ef5b89 100644 --- a/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt +++ b/buildSrc/src/main/kotlin/net/infumia/gradle/common.kt @@ -5,6 +5,7 @@ import org.gradle.api.plugins.JavaPlugin import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.tasks.bundling.Jar import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.jvm.toolchain.JvmVendorSpec import org.gradle.kotlin.dsl.* fun Project.applyCommon(javaVersion: Int = 8, sources: Boolean = true, javadoc: Boolean = true) { @@ -13,7 +14,10 @@ fun Project.applyCommon(javaVersion: Int = 8, sources: Boolean = true, javadoc: repositories.mavenCentral() extensions.configure { - toolchain { languageVersion = JavaLanguageVersion.of(javaVersion) } + toolchain { + languageVersion = JavaLanguageVersion.of(javaVersion) + vendor = JvmVendorSpec.ADOPTIUM + } } if (javadoc) { diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/settings.gradle.kts b/settings.gradle.kts index c0670b6..142f3fd 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,5 +1,3 @@ -plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" } - rootProject.name = "pack" include("common", "blank", "language", "generator") From b9a2ffc1c9a61a8d4373c2314c30af28f2e1bac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Wed, 3 Jul 2024 19:13:10 +0300 Subject: [PATCH 45/56] change the space icon. --- .../src/main/resources/pack-resources/space.png | Bin 70 -> 120 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/common/src/main/resources/pack-resources/space.png b/common/src/main/resources/pack-resources/space.png index 099055fc14de65b2cd991ea64f1f33ba766a059b..d59960add1673db0a0dbe658b3c7aa0f828a00e1 100644 GIT binary patch literal 120 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K585o&?RN5XZRUpL{;1lBNUf=^{&ev)Q1X4^T zL4Lsuf-=f)PXT%Io-U3d6}R3VWaI?NE%;TxOSVx8$OM6gd|5?C2ByRZE_ooXr>mdK II;Vst07+gMo&W#< literal 70 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}1TpU9 Date: Sun, 7 Jul 2024 20:13:31 +0300 Subject: [PATCH 46/56] simplify dependencies. --- blank/build.gradle.kts | 1 - common/build.gradle.kts | 1 - generator/build.gradle.kts | 2 -- language/build.gradle.kts | 1 - 4 files changed, 5 deletions(-) diff --git a/blank/build.gradle.kts b/blank/build.gradle.kts index 8b6dc03..05d7dad 100644 --- a/blank/build.gradle.kts +++ b/blank/build.gradle.kts @@ -6,5 +6,4 @@ dependencies { compileOnly(project(":common")) compileOnly(libs.creative.api) - compileOnly(libs.adventure.api) } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 170aef2..2527dbc 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -4,5 +4,4 @@ publish() dependencies { compileOnly(libs.creative.api) - compileOnly(libs.adventure.api) } diff --git a/generator/build.gradle.kts b/generator/build.gradle.kts index 2594ee0..acbe1ba 100644 --- a/generator/build.gradle.kts +++ b/generator/build.gradle.kts @@ -7,8 +7,6 @@ dependencies { compileOnly(project(":blank")) compileOnly(project(":language")) - compileOnly(libs.adventure.api) - compileOnly(libs.creative.api) compileOnly(libs.creative.serializer) compileOnly(libs.jackson.databind) } diff --git a/language/build.gradle.kts b/language/build.gradle.kts index 2d7859f..0caefcb 100644 --- a/language/build.gradle.kts +++ b/language/build.gradle.kts @@ -6,5 +6,4 @@ dependencies { compileOnly(project(":common")) compileOnly(libs.creative.api) - compileOnly(libs.adventure.api) } From 8903cf942a383e31d4158d5cf2a81cedcd63180e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 12 Jul 2024 21:20:34 +0300 Subject: [PATCH 47/56] Refactor code to merge duplicate resources in a pack --- .../main/java/net/infumia/pack/BlankSlot.java | 55 +---------- common/build.gradle.kts | 4 +- .../net/infumia/pack/FileResourceAll.java | 2 +- .../net/infumia/pack/FileResourceAtlas.java | 2 +- .../net/infumia/pack/FileResourceFont.java | 2 +- .../net/infumia/pack/FileResourceMerger.java | 21 ++++ .../pack/FileResourceMergerDefault.java | 97 +++++++++++++++++++ .../net/infumia/pack/FileResourceMergers.java | 20 ++++ .../net/infumia/pack/FileResourceModel.java | 2 +- .../net/infumia/pack/FileResourceTexture.java | 2 +- .../main/java/net/infumia/pack/MultiMap.java | 41 ++++++++ .../java/net/infumia/pack/PackDefault.java | 7 +- .../src/main/java/net/infumia/pack/Packs.java | 10 +- .../net/infumia/pack/ResourceProducers.java | 56 +++++++++++ .../java/net/infumia/pack/PackParser.java | 2 +- .../net/infumia/pack/PackReferencePart.java | 7 +- .../infumia/pack/PackReferencePartImage.java | 69 ++++++------- .../infumia/pack/PackReferencePartItem.java | 96 ++++++++++++++++++ 18 files changed, 385 insertions(+), 110 deletions(-) create mode 100644 common/src/main/java/net/infumia/pack/FileResourceMerger.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java create mode 100644 common/src/main/java/net/infumia/pack/FileResourceMergers.java create mode 100644 common/src/main/java/net/infumia/pack/MultiMap.java create mode 100644 generator/src/main/java/net/infumia/pack/PackReferencePartItem.java diff --git a/blank/src/main/java/net/infumia/pack/BlankSlot.java b/blank/src/main/java/net/infumia/pack/BlankSlot.java index 8aacc14..f50daf8 100644 --- a/blank/src/main/java/net/infumia/pack/BlankSlot.java +++ b/blank/src/main/java/net/infumia/pack/BlankSlot.java @@ -2,15 +2,7 @@ import net.kyori.adventure.key.Key; import net.kyori.adventure.key.KeyPattern; -import team.unnamed.creative.atlas.Atlas; -import team.unnamed.creative.atlas.AtlasSource; import team.unnamed.creative.base.Writable; -import team.unnamed.creative.model.ItemOverride; -import team.unnamed.creative.model.ItemPredicate; -import team.unnamed.creative.model.Model; -import team.unnamed.creative.model.ModelTexture; -import team.unnamed.creative.model.ModelTextures; -import team.unnamed.creative.texture.Texture; /** * Utility class for creating and retrieving blank slot file resources. @@ -26,51 +18,6 @@ public static FileResource get() { return Internal0.BLANK_SLOT.get(); } - /** - * Creates a blank slot file resource with the specified parameters. - * - * @param itemKey The key for the item. Cannot be null. - * @param baseKey The key for the base model. Cannot be null. - * @param blankSlotImage The writable image for the blank slot. Cannot be null. - * @param customModelData The custom model data value. - * @return A {@link FileResource} representing the created blank slot. - */ - public static FileResource create( - final Key itemKey, - final Key baseKey, - final Writable blankSlotImage, - final int customModelData - ) { - return FileResources.all( - FileResources.model( - Model.model() - .key(itemKey) - .parent(Model.ITEM_GENERATED) - .textures(ModelTextures.builder().layers(ModelTexture.ofKey(itemKey)).build()) - .build() - ), - FileResources.model( - Model.model() - .key(baseKey) - .parent(Model.ITEM_GENERATED) - .textures(ModelTextures.builder().layers(ModelTexture.ofKey(baseKey)).build()) - .overrides( - ItemOverride.of(itemKey, ItemPredicate.customModelData(customModelData)) - ) - .build() - ), - FileResources.texture( - Texture.texture(Internal.keyWithPngExtension(itemKey), blankSlotImage) - ), - FileResources.atlas( - Atlas.atlas() - .key(Atlas.BLOCKS) - .sources(AtlasSource.directory(itemKey.namespace(), itemKey.namespace() + "/")) - .build() - ) - ); - } - /** * Creates a blank slot file resource with the specified parameters. * @@ -88,7 +35,7 @@ public static FileResource create( final Writable blankSlotImage, final int customModelData ) { - return BlankSlot.create( + return ResourceProducers.item( Key.key(namespace, itemId), baseKey, blankSlotImage, diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 2527dbc..a9b4c70 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -2,6 +2,4 @@ import net.infumia.gradle.publish publish() -dependencies { - compileOnly(libs.creative.api) -} +dependencies { compileOnly(libs.creative.api) } diff --git a/common/src/main/java/net/infumia/pack/FileResourceAll.java b/common/src/main/java/net/infumia/pack/FileResourceAll.java index c3ef45b..8c77e83 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceAll.java +++ b/common/src/main/java/net/infumia/pack/FileResourceAll.java @@ -5,7 +5,7 @@ final class FileResourceAll implements FileResource { - private final Collection resources; + final Collection resources; FileResourceAll(final Collection resources) { this.resources = resources; diff --git a/common/src/main/java/net/infumia/pack/FileResourceAtlas.java b/common/src/main/java/net/infumia/pack/FileResourceAtlas.java index e5ca3c9..faacf41 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceAtlas.java +++ b/common/src/main/java/net/infumia/pack/FileResourceAtlas.java @@ -5,7 +5,7 @@ final class FileResourceAtlas implements FileResource { - private final Atlas atlas; + final Atlas atlas; FileResourceAtlas(final Atlas atlas) { this.atlas = atlas; diff --git a/common/src/main/java/net/infumia/pack/FileResourceFont.java b/common/src/main/java/net/infumia/pack/FileResourceFont.java index 9e80481..b3ca93c 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceFont.java +++ b/common/src/main/java/net/infumia/pack/FileResourceFont.java @@ -5,7 +5,7 @@ final class FileResourceFont implements FileResource { - private final Font font; + final Font font; FileResourceFont(final Font font) { this.font = font; diff --git a/common/src/main/java/net/infumia/pack/FileResourceMerger.java b/common/src/main/java/net/infumia/pack/FileResourceMerger.java new file mode 100644 index 0000000..2374230 --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceMerger.java @@ -0,0 +1,21 @@ +package net.infumia.pack; + +import java.util.Collection; +import team.unnamed.creative.atlas.Atlas; +import team.unnamed.creative.model.Model; + +/** + * Represents a file resource merger that can merge a collection of file resources. + */ +public interface FileResourceMerger { + /** + * Merges a collection of {@link FileResources} into a unified resource. + *

+ * Tries to merge duplicate {@link Atlas}s and {@link Model}s. + * + * @param resources the collection of {@link FileResources} to merge. Cannot be null. + * + * @return the merged collection of {@link FileResource}s. Cannot be null. + */ + Collection merge(Collection resources); +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java new file mode 100644 index 0000000..1dbe5fa --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java @@ -0,0 +1,97 @@ +package net.infumia.pack; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.stream.Collectors; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.atlas.Atlas; +import team.unnamed.creative.model.Model; + +final class FileResourceMergerDefault implements FileResourceMerger { + + static final FileResourceMerger INSTANCE = new FileResourceMergerDefault(); + + private FileResourceMergerDefault() {} + + @Override + public Collection merge(final Collection resources) { + final Collection simplified = new HashSet<>(resources.size()); + for (final FileResource resource : resources) { + simplified.addAll(this.simplify(resource)); + } + + final MultiMap atlases = new MultiMap<>(); + final MultiMap models = new MultiMap<>(); + for (final FileResource resource : simplified) { + if (resource instanceof FileResourceAtlas) { + final Atlas atlas = ((FileResourceAtlas) resource).atlas; + atlases.put(atlas.key(), atlas); + } else if (resource instanceof FileResourceModel) { + final Model model = ((FileResourceModel) resource).model; + models.put(model.key(), model); + } + // TODO: portlek, Merge more things. + } + + final Collection mergedAtlases = new HashSet<>(atlases.keys().size()); + for (final Key key : atlases.keys()) { + final Collection duplicates = atlases.get(key); + final Atlas merged = Atlas.atlas() + .key(key) + .sources( + duplicates + .stream() + .map(Atlas::sources) + .flatMap(Collection::stream) + .collect(Collectors.toList()) + ) + .build(); + mergedAtlases.add(merged); + } + + final Collection mergedModels = new HashSet<>(models.keys().size()); + for (final Key key : models.keys()) { + final Collection duplicates = models.get(key); + final Model.Builder builder = Model.model().key(key); + // TODO: portlek, Find a way to merge these too. + for (final Model duplicate : duplicates) { + builder + .guiLight(duplicate.guiLight()) + .elements(duplicate.elements()) + .parent(duplicate.parent()) + .ambientOcclusion(duplicate.ambientOcclusion()) + .display(duplicate.display()) + .textures(duplicate.textures()); + } + builder.overrides( + duplicates + .stream() + .map(Model::overrides) + .flatMap(Collection::stream) + .collect(Collectors.toList()) + ); + mergedModels.add(builder.build()); + } + + final Collection mergedResources = mergedAtlases + .stream() + .map(FileResources::atlas) + .collect(Collectors.toSet()); + mergedResources.addAll( + mergedModels.stream().map(FileResources::model).collect(Collectors.toSet()) + ); + return mergedResources; + } + + private Collection simplify(final FileResource resource) { + if (resource instanceof FileResourceAll) { + return ((FileResourceAll) resource).resources.stream() + .map(this::simplify) + .flatMap(Collection::stream) + .collect(Collectors.toSet()); + } else { + return Collections.singleton(resource); + } + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceMergers.java b/common/src/main/java/net/infumia/pack/FileResourceMergers.java new file mode 100644 index 0000000..d60d6ad --- /dev/null +++ b/common/src/main/java/net/infumia/pack/FileResourceMergers.java @@ -0,0 +1,20 @@ +package net.infumia.pack; + +/** + * Utility class for providing various implementations of {@link FileResourceMerger}. + */ +public final class FileResourceMergers { + + /** + * Returns a simple file resource merger implementation. + * + * @return A {@link FileResourceMerger} representing the simple file resource merger. + */ + public static FileResourceMerger simple() { + return FileResourceMergerDefault.INSTANCE; + } + + private FileResourceMergers() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/common/src/main/java/net/infumia/pack/FileResourceModel.java b/common/src/main/java/net/infumia/pack/FileResourceModel.java index 6f44cb6..6682de9 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceModel.java +++ b/common/src/main/java/net/infumia/pack/FileResourceModel.java @@ -5,7 +5,7 @@ final class FileResourceModel implements FileResource { - private final Model model; + final Model model; FileResourceModel(final Model model) { this.model = model; diff --git a/common/src/main/java/net/infumia/pack/FileResourceTexture.java b/common/src/main/java/net/infumia/pack/FileResourceTexture.java index b6cddde..2aa828b 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceTexture.java +++ b/common/src/main/java/net/infumia/pack/FileResourceTexture.java @@ -5,7 +5,7 @@ final class FileResourceTexture implements FileResource { - private final Texture texture; + final Texture texture; FileResourceTexture(final Texture texture) { this.texture = texture; diff --git a/common/src/main/java/net/infumia/pack/MultiMap.java b/common/src/main/java/net/infumia/pack/MultiMap.java new file mode 100644 index 0000000..5c9841f --- /dev/null +++ b/common/src/main/java/net/infumia/pack/MultiMap.java @@ -0,0 +1,41 @@ +package net.infumia.pack; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +final class MultiMap { + + private final Map> map = new HashMap<>(); + + MultiMap() {} + + void put(final K key, final V value) { + this.map.computeIfAbsent(key, __ -> new HashSet<>()).add(value); + } + + Collection get(final K key) { + return this.map.get(key); + } + + boolean remove(final K key, final V value) { + final Collection values = this.map.get(key); + if (values == null) { + return false; + } + final boolean removed = values.remove(value); + if (values.isEmpty()) { + this.map.remove(key); + } + return removed; + } + + void clear() { + this.map.clear(); + } + + Collection keys() { + return this.map.keySet(); + } +} diff --git a/common/src/main/java/net/infumia/pack/PackDefault.java b/common/src/main/java/net/infumia/pack/PackDefault.java index 46c2a14..a50849b 100644 --- a/common/src/main/java/net/infumia/pack/PackDefault.java +++ b/common/src/main/java/net/infumia/pack/PackDefault.java @@ -9,12 +9,14 @@ final class PackDefault implements Pack { private final FileResourceCompiler compiler; + private final FileResourceMerger merger; private final Map raw = new HashMap<>(); private final Map compiled = new HashMap<>(); private final Set resources = new HashSet<>(); - PackDefault(final FileResourceCompiler compiler) { + PackDefault(final FileResourceCompiler compiler, final FileResourceMerger merger) { this.compiler = compiler; + this.merger = merger; } @Override @@ -29,6 +31,9 @@ public Collection all() { public void compileAll() { final Collection resources = this.compiler.compile(this.raw.values()); this.resources.addAll(resources); + final Collection mergedResources = this.merger.merge(this.resources); + this.resources.clear(); + this.resources.addAll(mergedResources); this.compiled.putAll(this.raw); this.raw.clear(); } diff --git a/common/src/main/java/net/infumia/pack/Packs.java b/common/src/main/java/net/infumia/pack/Packs.java index 50d8221..9cb8e21 100644 --- a/common/src/main/java/net/infumia/pack/Packs.java +++ b/common/src/main/java/net/infumia/pack/Packs.java @@ -9,10 +9,14 @@ public final class Packs { * Creates a new Pack instance with the specified file resource compiler. * * @param compiler The file resource compiler to use. Cannot be null. + * @param merger The merger to be used to merge same file resources. Cannot be null. * @return A new Pack instance. */ - public static Pack create(final FileResourceCompiler compiler) { - return new PackDefault(compiler); + public static Pack create( + final FileResourceCompiler compiler, + final FileResourceMerger merger + ) { + return new PackDefault(compiler, merger); } /** @@ -21,7 +25,7 @@ public static Pack create(final FileResourceCompiler compiler) { * @return A new Pack instance. */ public static Pack create() { - return Packs.create(FileResourceCompilers.simple()); + return Packs.create(FileResourceCompilers.simple(), FileResourceMergers.simple()); } private Packs() { diff --git a/common/src/main/java/net/infumia/pack/ResourceProducers.java b/common/src/main/java/net/infumia/pack/ResourceProducers.java index 0833990..2b63aee 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducers.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducers.java @@ -1,8 +1,15 @@ package net.infumia.pack; import net.kyori.adventure.key.Key; +import team.unnamed.creative.atlas.Atlas; +import team.unnamed.creative.atlas.AtlasSource; import team.unnamed.creative.base.Writable; import team.unnamed.creative.font.Font; +import team.unnamed.creative.model.ItemOverride; +import team.unnamed.creative.model.ItemPredicate; +import team.unnamed.creative.model.Model; +import team.unnamed.creative.model.ModelTexture; +import team.unnamed.creative.model.ModelTextures; import team.unnamed.creative.texture.Texture; /** @@ -74,6 +81,55 @@ public static GlyphImage image( return new GlyphImageImpl(fontKey, texture, properties); } + /** + * Creates an item resource with the specified parameters. + * + * @param itemKey The key for the item. Cannot be null. + * @param overriddenItemKey The key for the base model. Cannot be null. + * @param itemImage The writable image for the blank slot. Cannot be null. + * @param customModelData The custom model data value. + * @return A {@link FileResource} representing the created item. + */ + public static FileResource item( + final Key itemKey, + final Key overriddenItemKey, + final Writable itemImage, + final int customModelData + ) { + return FileResources.all( + FileResources.model( + Model.model() + .key(itemKey) + .parent(Model.ITEM_GENERATED) + .textures(ModelTextures.builder().layers(ModelTexture.ofKey(itemKey)).build()) + .build() + ), + FileResources.model( + Model.model() + .key(overriddenItemKey) + .parent(Model.ITEM_GENERATED) + .textures( + ModelTextures.builder() + .layers(ModelTexture.ofKey(overriddenItemKey)) + .build() + ) + .overrides( + ItemOverride.of(itemKey, ItemPredicate.customModelData(customModelData)) + ) + .build() + ), + FileResources.texture( + Texture.texture(Internal.keyWithPngExtension(itemKey), itemImage) + ), + FileResources.atlas( + Atlas.atlas() + .key(Atlas.BLOCKS) + .sources(AtlasSource.directory(itemKey.namespace(), itemKey.namespace() + "/")) + .build() + ) + ); + } + private ResourceProducers() { throw new IllegalStateException("Utility class"); } diff --git a/generator/src/main/java/net/infumia/pack/PackParser.java b/generator/src/main/java/net/infumia/pack/PackParser.java index 3e1c96c..e3e89ea 100644 --- a/generator/src/main/java/net/infumia/pack/PackParser.java +++ b/generator/src/main/java/net/infumia/pack/PackParser.java @@ -30,7 +30,7 @@ private static void parseMeta(final PackGeneratorContext context) { pack.with(BlankSlot.get()); } if (meta.addSpaces()) { - if (packMeta.formats().isInRange(9)) { + if (packMeta.formats().min() >= 9) { pack.withMojangSpaces(); } else { pack.withBitmapSpaces(); diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePart.java b/generator/src/main/java/net/infumia/pack/PackReferencePart.java index f5d47a5..61fe1ef 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePart.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePart.java @@ -8,7 +8,12 @@ * Abstract base class for a pack part reference. */ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") -@JsonSubTypes({ @JsonSubTypes.Type(value = PackReferencePartImage.class, name = "image") }) +@JsonSubTypes( + { + @JsonSubTypes.Type(value = PackReferencePartImage.class, name = "image"), + @JsonSubTypes.Type(value = PackReferencePartItem.class, name = "item"), + } +) public abstract class PackReferencePart { /** diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java index 7285773..9c58b28 100644 --- a/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartImage.java @@ -1,6 +1,5 @@ package net.infumia.pack; -import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import java.nio.file.Path; import java.util.Locale; @@ -15,37 +14,22 @@ */ public final class PackReferencePartImage extends PackReferencePart { - private final String namespace; - private final String key; - private final String image; - private final int height; - private final int ascent; + @JsonProperty + private String namespace; - private Path directory; + @JsonProperty(required = true) + private String key; - /** - * Ctor. - * - * @param namespace the namespace of the image. Can be null - * @param key the key for the image. Cannot be null. - * @param image the image file name. Cannot be null. - * @param height the height of the image. - * @param ascent the ascent value of the image. - */ - @JsonCreator - public PackReferencePartImage( - @JsonProperty("namespace") final String namespace, - @JsonProperty(value = "key", required = true) final String key, - @JsonProperty(value = "image", required = true) final String image, - @JsonProperty(value = "height", required = true) final int height, - @JsonProperty(value = "ascent", required = true) final int ascent - ) { - this.namespace = namespace; - this.key = key; - this.image = image; - this.height = height; - this.ascent = ascent; - } + @JsonProperty(required = true) + private String image; + + @JsonProperty(required = true) + private int height; + + @JsonProperty(required = true) + private int ascent; + + private Path directory; @Override public void add(final PackGeneratorContext context) { @@ -71,19 +55,20 @@ public void add(final PackGeneratorContext context) { "/"; } - final Pack pack = context.pack(); final String key = parent + this.key; - pack.with( - (ResourceIdentifierImage) () -> key, - ResourceProducers.image( - Font.MINECRAFT_DEFAULT, - Texture.texture( - Key.key(namespace, key + ".png"), - Writable.path(root.resolve(parent + this.image)) - ), - new TextureProperties(this.height, this.ascent) - ) - ); + context + .pack() + .with( + (ResourceIdentifierImage) () -> key, + ResourceProducers.image( + Font.MINECRAFT_DEFAULT, + Texture.texture( + Key.key(namespace, key + ".png"), + Writable.path(root.resolve(parent + this.image)) + ), + new TextureProperties(this.height, this.ascent) + ) + ); } @Override diff --git a/generator/src/main/java/net/infumia/pack/PackReferencePartItem.java b/generator/src/main/java/net/infumia/pack/PackReferencePartItem.java new file mode 100644 index 0000000..27a64cf --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReferencePartItem.java @@ -0,0 +1,96 @@ +package net.infumia.pack; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.nio.file.Path; +import java.util.Locale; +import java.util.StringJoiner; +import net.kyori.adventure.key.Key; +import team.unnamed.creative.base.Writable; + +/** + * Represents an item part of a pack reference. + */ +public final class PackReferencePartItem extends PackReferencePart { + + @JsonProperty + private String namespace; + + @JsonProperty(required = true) + private String key; + + @JsonProperty(value = "custom-model-data", required = true) + private int customModelData; + + @JsonProperty(required = true) + private String image; + + @JsonProperty("overridden-namespace") + private String overriddenNamespace; + + @JsonProperty(value = "overridden-key", required = true) + private String overriddenKey; + + private Path directory; + + @Override + public void add(final PackGeneratorContext context) { + final String namespace = this.namespace == null + ? context.packReference().defaultNamespace() + : this.namespace; + if (namespace == null) { + throw new IllegalStateException("Pack reference namespace cannot be null!"); + } + + final Path root = context.rootDirectory(); + + final String parent; + if (this.directory == null) { + parent = ""; + } else { + parent = root + .relativize(this.directory) + .toString() + .toLowerCase(Locale.ROOT) + .replace("\\", "/") + .replace(" ", "_") + + "/"; + } + + final Key overriddenItemKey; + if (this.overriddenNamespace == null) { + overriddenItemKey = Key.key(this.overriddenKey); + } else { + overriddenItemKey = Key.key(this.overriddenNamespace, this.overriddenKey); + } + + context + .pack() + .with( + ResourceProducers.item( + Key.key(namespace, parent + this.key), + overriddenItemKey, + Writable.path(root.resolve(parent + this.image)), + this.customModelData + ) + ); + } + + @Override + PackReferencePartItem directory(final Path directory) { + this.directory = directory; + return this; + } + + @Override + public String toString() { + return new StringJoiner(", ", PackReferencePartItem.class.getSimpleName() + "[", "]") + .add("namespace='" + this.namespace + "'") + .add("key='" + this.key + "'") + .add("customModelData=" + this.customModelData) + .add("image='" + this.image + "'") + .add("overriddenNamespace='" + this.overriddenNamespace + "'") + .add("overriddenKey='" + this.overriddenKey + "'") + .add("directory=" + this.directory) + .toString(); + } +} From 2e779d14daa3d2d3bb13e6c6bbedb64320dda591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 12 Jul 2024 21:23:33 +0300 Subject: [PATCH 48/56] Add unmerged resources to 'remainings' --- .../java/net/infumia/pack/FileResourceMergerDefault.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java index 1dbe5fa..a9ec2fe 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java +++ b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java @@ -23,6 +23,7 @@ public Collection merge(final Collection resources) final MultiMap atlases = new MultiMap<>(); final MultiMap models = new MultiMap<>(); + final HashSet remainings = new HashSet<>(); for (final FileResource resource : simplified) { if (resource instanceof FileResourceAtlas) { final Atlas atlas = ((FileResourceAtlas) resource).atlas; @@ -32,6 +33,9 @@ public Collection merge(final Collection resources) models.put(model.key(), model); } // TODO: portlek, Merge more things. + else { + remainings.add(resource); + } } final Collection mergedAtlases = new HashSet<>(atlases.keys().size()); @@ -81,6 +85,7 @@ public Collection merge(final Collection resources) mergedResources.addAll( mergedModels.stream().map(FileResources::model).collect(Collectors.toSet()) ); + mergedResources.addAll(remainings); return mergedResources; } From 974575fa30683518d78c47c6b2773c2b2a860b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 12 Jul 2024 21:38:55 +0300 Subject: [PATCH 49/56] Add sorting for ItemOverride in FileResourceMerger --- .../pack/FileResourceMergerDefault.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java index a9ec2fe..5215e5c 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java +++ b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java @@ -2,15 +2,30 @@ import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; +import java.util.Optional; import java.util.stream.Collectors; import net.kyori.adventure.key.Key; import team.unnamed.creative.atlas.Atlas; +import team.unnamed.creative.model.ItemOverride; +import team.unnamed.creative.model.ItemPredicate; import team.unnamed.creative.model.Model; final class FileResourceMergerDefault implements FileResourceMerger { static final FileResourceMerger INSTANCE = new FileResourceMergerDefault(); + static final Comparator OVERRIDE_COMPARATOR = Comparator.comparingInt(value -> { + final Optional first = value.predicate().stream().findFirst(); + if (!first.isPresent()) { + return 0; + } + final ItemPredicate predicate = first.get(); + if (predicate.name().equals("custom_model_data")) { + return (int) predicate.value(); + } + return 0; + }); private FileResourceMergerDefault() {} @@ -23,7 +38,7 @@ public Collection merge(final Collection resources) final MultiMap atlases = new MultiMap<>(); final MultiMap models = new MultiMap<>(); - final HashSet remainings = new HashSet<>(); + final HashSet remaining = new HashSet<>(); for (final FileResource resource : simplified) { if (resource instanceof FileResourceAtlas) { final Atlas atlas = ((FileResourceAtlas) resource).atlas; @@ -34,7 +49,7 @@ public Collection merge(final Collection resources) } // TODO: portlek, Merge more things. else { - remainings.add(resource); + remaining.add(resource); } } @@ -73,6 +88,7 @@ public Collection merge(final Collection resources) .stream() .map(Model::overrides) .flatMap(Collection::stream) + .sorted(FileResourceMergerDefault.OVERRIDE_COMPARATOR) .collect(Collectors.toList()) ); mergedModels.add(builder.build()); @@ -85,7 +101,7 @@ public Collection merge(final Collection resources) mergedResources.addAll( mergedModels.stream().map(FileResources::model).collect(Collectors.toSet()) ); - mergedResources.addAll(remainings); + mergedResources.addAll(remaining); return mergedResources; } From a59a820f9aae7ba222dfc3532a69d75611eb09cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Sat, 13 Jul 2024 00:12:20 +0300 Subject: [PATCH 50/56] Move output directory/file from PackGeneratorContext to PackWriterSettings --- .../infumia/pack/PackGeneratorContext.java | 28 ------------ .../java/net/infumia/pack/PackReader.java | 2 - .../net/infumia/pack/PackReaderSettings.java | 43 +------------------ .../java/net/infumia/pack/PackWriter.java | 4 +- .../net/infumia/pack/PackWriterSettings.java | 31 ++++++++++++- 5 files changed, 33 insertions(+), 75 deletions(-) diff --git a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java index 6a1698e..c29fb65 100644 --- a/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java +++ b/generator/src/main/java/net/infumia/pack/PackGeneratorContext.java @@ -17,8 +17,6 @@ public final class PackGeneratorContext { private final PackReferenceMeta packReference; private final Collection packPartReferences; private final Path rootDirectory; - private final Path outputDirectory; - private final Path outputFile; private final ComponentSerializer serializer; /** @@ -29,8 +27,6 @@ public final class PackGeneratorContext { * @param packReference the pack file reference. Cannot be null. * @param packPartReferences the pack part references. Cannot be null. * @param rootDirectory the root directory of the pack. Cannot be null. - * @param outputDirectory the output directory. Can be null. - * @param outputFile the output file. Can be null. * @param serializer the serializer to serialize components when needed. Cannot be null. */ PackGeneratorContext( @@ -39,8 +35,6 @@ public final class PackGeneratorContext { final PackReferenceMeta packReference, final Collection packPartReferences, final Path rootDirectory, - final Path outputDirectory, - final Path outputFile, final ComponentSerializer serializer ) { this.resourcePack = resourcePack; @@ -48,8 +42,6 @@ public final class PackGeneratorContext { this.packReference = packReference; this.packPartReferences = Collections.unmodifiableCollection(packPartReferences); this.rootDirectory = rootDirectory; - this.outputDirectory = outputDirectory; - this.outputFile = outputFile; this.serializer = serializer; } @@ -98,24 +90,6 @@ public Path rootDirectory() { return this.rootDirectory; } - /** - * Returns the output directory. - * - * @return the output directory. Can be null. - */ - public Path outputDirectory() { - return this.outputDirectory; - } - - /** - * Returns the output file. - * - * @return the output file. Can be null. - */ - public Path outputFile() { - return this.outputFile; - } - /** * Returns the component serializer. * @@ -132,8 +106,6 @@ public String toString() { .add("pack=" + this.pack) .add("packReference=" + this.packReference) .add("packPartReferences=" + this.packPartReferences) - .add("outputDirectory=" + this.outputDirectory) - .add("outputFile=" + this.outputFile) .add("serializer=" + this.serializer) .toString(); } diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index d328856..c59ab6a 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -75,8 +75,6 @@ private PackGeneratorContext read0(@NotNull final Stream walking) throws I packReference, packPartReferences, this.settings.root(), - this.settings.outputDirectory(), - this.settings.outputFile(), this.settings.serializer() ); } diff --git a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java index f0f4f22..c5e0e8b 100644 --- a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java @@ -17,8 +17,6 @@ public final class PackReaderSettings { private final Path root; private final FileVisitOption[] visitOptions; private final String packReferenceFileName; - private final Path outputDirectory; - private final Path outputFile; private final ObjectMapper mapper; private final Predicate readFilter; private final ComponentSerializer serializer; @@ -29,8 +27,6 @@ public final class PackReaderSettings { * @param root the root path. * @param visitOptions the visit options. Can be null. * @param packReferenceFileName the pack reference file name. Cannot be null. - * @param outputDirectory the directory name. Can be null. - * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. * @param readFilter the read filter for {@link Files#walk(Path, FileVisitOption...)}. * @param serializer the serializer to serialize components when needed. Cannot be null. @@ -39,8 +35,6 @@ public PackReaderSettings( final Path root, final FileVisitOption[] visitOptions, final String packReferenceFileName, - final Path outputDirectory, - final Path outputFile, final ObjectMapper mapper, final Predicate readFilter, final ComponentSerializer serializer @@ -48,8 +42,6 @@ public PackReaderSettings( this.root = root; this.visitOptions = visitOptions; this.packReferenceFileName = packReferenceFileName; - this.outputDirectory = outputDirectory; - this.outputFile = outputFile; this.mapper = mapper; this.readFilter = readFilter; this.serializer = serializer; @@ -60,30 +52,17 @@ public PackReaderSettings( * * @param root the root path. * @param packReferenceFileName the pack reference file name. Cannot be null. - * @param outputDirectory the directory name. Can be null. - * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. * @param readFilter the read filter for {@link Files#walk(Path, FileVisitOption...)}. */ public PackReaderSettings( final Path root, final String packReferenceFileName, - final Path outputDirectory, - final Path outputFile, final ObjectMapper mapper, final Predicate readFilter, final ComponentSerializer serializer ) { - this( - root, - null, - packReferenceFileName, - outputDirectory, - outputFile, - mapper, - readFilter, - serializer - ); + this(root, null, packReferenceFileName, mapper, readFilter, serializer); } /** @@ -113,24 +92,6 @@ public String packReferenceFileName() { return this.packReferenceFileName; } - /** - * Returns the output directory. - * - * @return the directory output. Can be null. - */ - public Path outputDirectory() { - return this.outputDirectory; - } - - /** - * Returns the zip file. - * - * @return the zip file. Can be null. - */ - public Path outputFile() { - return this.outputFile; - } - /** * Returns the object mapper. * @@ -164,8 +125,6 @@ public String toString() { .add("root=" + this.root) .add("visitOptions=" + Arrays.toString(this.visitOptions)) .add("packReferenceFileName='" + this.packReferenceFileName + "'") - .add("outputDirectory=" + this.outputDirectory) - .add("outputFile=" + this.outputFile) .add("mapper=" + this.mapper) .add("readFilter=" + this.readFilter) .add("serializer=" + this.serializer) diff --git a/generator/src/main/java/net/infumia/pack/PackWriter.java b/generator/src/main/java/net/infumia/pack/PackWriter.java index d16f7a0..9b39eba 100644 --- a/generator/src/main/java/net/infumia/pack/PackWriter.java +++ b/generator/src/main/java/net/infumia/pack/PackWriter.java @@ -14,12 +14,12 @@ final class PackWriter { PackGeneratedContext write(final PackGeneratorContext context) { final MinecraftResourcePackWriter writer = this.settings.writer(); - final Path outputDirectory = context.outputDirectory(); + final Path outputDirectory = this.settings.outputDirectory(); final ResourcePack resourcePack = context.resourcePack(); if (outputDirectory != null) { writer.writeToDirectory(outputDirectory.toFile(), resourcePack); } - final Path outputFile = context.outputFile(); + final Path outputFile = this.settings.outputFile(); if (outputFile != null) { writer.writeToZipFile(outputFile, resourcePack); } diff --git a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java index f3f3d1f..9b489a6 100644 --- a/generator/src/main/java/net/infumia/pack/PackWriterSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackWriterSettings.java @@ -1,5 +1,6 @@ package net.infumia.pack; +import java.nio.file.Path; import java.util.StringJoiner; import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter; @@ -9,14 +10,24 @@ public final class PackWriterSettings { private final MinecraftResourcePackWriter writer; + private final Path outputDirectory; + private final Path outputFile; /** * Ctor. * * @param writer the resource pack writer. Cannot be null. + * @param outputDirectory the output directory. Can be null. + * @param outputFile the output file. Can be null. */ - public PackWriterSettings(final MinecraftResourcePackWriter writer) { + public PackWriterSettings( + final MinecraftResourcePackWriter writer, + final Path outputDirectory, + final Path outputFile + ) { this.writer = writer; + this.outputDirectory = outputDirectory; + this.outputFile = outputFile; } /** @@ -28,6 +39,24 @@ public MinecraftResourcePackWriter writer() { return this.writer; } + /** + * Returns the output directory. + * + * @return the output directory. Can be null. + */ + public Path outputDirectory() { + return this.outputDirectory; + } + + /** + * Returns the output file. + * + * @return the output file. Can be null. + */ + public Path outputFile() { + return this.outputFile; + } + @Override public String toString() { return new StringJoiner(", ", PackWriterSettings.class.getSimpleName() + "[", "]") From d7e923d3a0e37246381e78ac35f91f83159f8bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 16 Jul 2024 18:25:42 +0300 Subject: [PATCH 51/56] fix. --- common/src/main/java/net/infumia/pack/GlyphImageImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java index cdb0676..9bee8b1 100644 --- a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java +++ b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java @@ -46,11 +46,11 @@ public boolean produced() { @Override public void produce(final ArbitraryCharacterFactory characterFactory) throws ResourceAlreadyProducedException { - if (this.fontProviders != null) { + if (this.fontProviders != null || this.character != null) { throw new ResourceAlreadyProducedException(); } - final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); this.character = characterFactory.create(); + final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); fontProviderBuilder.characters(String.valueOf(this.character)); fontProviderBuilder.file(this.texture.key()); fontProviderBuilder.ascent(this.properties.ascent()); @@ -94,7 +94,7 @@ public int width() { @Override public char character() throws ResourceNotProducedException { - if (this.fontProviders == null) { + if (this.character == null) { throw new ResourceNotProducedException(); } return this.character; From 8de92396696e5584c1198ea8d77a6cf1682b0276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 16 Jul 2024 18:30:50 +0300 Subject: [PATCH 52/56] simplify. --- .../java/net/infumia/pack/ResourceProducerSpacesBitmap.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java index c5f607f..cbdb790 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java @@ -73,10 +73,9 @@ private BitMapFontProvider.Builder prepareBuilder( fontProviderBuilder.file(this.textureKey); if (length > 0) { fontProviderBuilder.height(length - 1); - fontProviderBuilder.ascent(0); } else { fontProviderBuilder.height(length - 2); - fontProviderBuilder.ascent(-32768); + fontProviderBuilder.ascent(Short.MIN_VALUE); } this.mapping.put(length, character); return fontProviderBuilder; From 986940d57006e42ab47c008a6f0b3322b8e19705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 16 Jul 2024 18:31:31 +0300 Subject: [PATCH 53/56] simplify. --- .../infumia/pack/ResourceProducerSpacesBitmap.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java index cbdb790..e98b172 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesBitmap.java @@ -67,17 +67,17 @@ private BitMapFontProvider.Builder prepareBuilder( final ArbitraryCharacterFactory characterFactory, final int length ) { - final BitMapFontProvider.Builder fontProviderBuilder = FontProvider.bitMap(); + final BitMapFontProvider.Builder builder = FontProvider.bitMap(); final char character = characterFactory.create(); - fontProviderBuilder.characters(String.valueOf(character)); - fontProviderBuilder.file(this.textureKey); + builder.characters(String.valueOf(character)); + builder.file(this.textureKey); if (length > 0) { - fontProviderBuilder.height(length - 1); + builder.height(length - 1); } else { - fontProviderBuilder.height(length - 2); - fontProviderBuilder.ascent(Short.MIN_VALUE); + builder.height(length - 2); + builder.ascent(Short.MIN_VALUE); } this.mapping.put(length, character); - return fontProviderBuilder; + return builder; } } From 153aca51ab32a2dbf5ec3d71e25f1dcb38be0eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 16 Jul 2024 18:39:56 +0300 Subject: [PATCH 54/56] use list instead of set. --- .../java/net/infumia/pack/FileResourceMergerDefault.java | 2 +- .../src/main/java/net/infumia/pack/GlyphImageImpl.java | 8 ++++---- common/src/main/java/net/infumia/pack/PackDefault.java | 6 +++--- .../net/infumia/pack/ResourceProducerSpacesMojang.java | 2 +- .../infumia/pack/ResourceProducerImageMulticharImpl.java | 7 +++---- .../net/infumia/pack/ResourceProducerLanguageImpl.java | 9 ++++----- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java index 5215e5c..f4a0bd1 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java +++ b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java @@ -112,7 +112,7 @@ private Collection simplify(final FileResource resource) { .flatMap(Collection::stream) .collect(Collectors.toSet()); } else { - return Collections.singleton(resource); + return Collections.singletonList(resource); } } } diff --git a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java index 9bee8b1..388f443 100644 --- a/common/src/main/java/net/infumia/pack/GlyphImageImpl.java +++ b/common/src/main/java/net/infumia/pack/GlyphImageImpl.java @@ -5,7 +5,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; -import java.util.Set; +import java.util.List; import javax.imageio.ImageIO; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; @@ -22,7 +22,7 @@ final class GlyphImageImpl implements GlyphImage { private final TextureProperties properties; private Character character; - private Set fontProviders; + private List fontProviders; private int width = -1; @@ -55,7 +55,7 @@ public void produce(final ArbitraryCharacterFactory characterFactory) fontProviderBuilder.file(this.texture.key()); fontProviderBuilder.ascent(this.properties.ascent()); fontProviderBuilder.height(this.properties.height()); - this.fontProviders = Collections.singleton(fontProviderBuilder.build()); + this.fontProviders = Collections.singletonList(fontProviderBuilder.build()); } @Override @@ -68,7 +68,7 @@ public Collection fontProviders() throws ResourceNotProducedExcept @Override public Collection textures() throws ResourceNotProducedException { - return Collections.singleton(this.texture); + return Collections.singletonList(this.texture); } @Override diff --git a/common/src/main/java/net/infumia/pack/PackDefault.java b/common/src/main/java/net/infumia/pack/PackDefault.java index a50849b..e42b9a2 100644 --- a/common/src/main/java/net/infumia/pack/PackDefault.java +++ b/common/src/main/java/net/infumia/pack/PackDefault.java @@ -1,10 +1,10 @@ package net.infumia.pack; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; +import java.util.List; import java.util.Map; -import java.util.Set; final class PackDefault implements Pack { @@ -12,7 +12,7 @@ final class PackDefault implements Pack { private final FileResourceMerger merger; private final Map raw = new HashMap<>(); private final Map compiled = new HashMap<>(); - private final Set resources = new HashSet<>(); + private final List resources = new ArrayList<>(); PackDefault(final FileResourceCompiler compiler, final FileResourceMerger merger) { this.compiler = compiler; diff --git a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java index e596d5a..b3f0b22 100644 --- a/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java +++ b/common/src/main/java/net/infumia/pack/ResourceProducerSpacesMojang.java @@ -36,7 +36,7 @@ public void produce(final ArbitraryCharacterFactory characterFactory) { length * (-1) ); } - this.fontProviders = Collections.singleton(fontProviderBuilder.build()); + this.fontProviders = Collections.singletonList(fontProviderBuilder.build()); } @Override diff --git a/language/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java b/language/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java index 7415e3c..6d6963d 100644 --- a/language/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java +++ b/language/src/main/java/net/infumia/pack/ResourceProducerImageMulticharImpl.java @@ -9,7 +9,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import javax.imageio.ImageIO; import net.infumia.pack.exception.ResourceAlreadyProducedException; import net.infumia.pack.exception.ResourceNotProducedException; @@ -29,7 +28,7 @@ final class ResourceProducerImageMulticharImpl implements ResourceProducerImageM private final TextureProperties properties; private final List charactersMapping; - private Set fontProviders; + private List fontProviders; private BufferedImage image; ResourceProducerImageMulticharImpl( @@ -76,7 +75,7 @@ public void produce(final ArbitraryCharacterFactory characterFactory) mappingLines.add(builder.toString()); } fontProviderBuilder.characters(mappingLines); - this.fontProviders = Collections.singleton(fontProviderBuilder.build()); + this.fontProviders = Collections.singletonList(fontProviderBuilder.build()); } @Override @@ -86,7 +85,7 @@ public Collection fontProviders() throws ResourceNotProducedExcept @Override public Collection textures() throws ResourceNotProducedException { - return Collections.singleton(this.texture); + return Collections.singletonList(this.texture); } @Override diff --git a/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java index 2d340ae..518ceb8 100644 --- a/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java +++ b/language/src/main/java/net/infumia/pack/ResourceProducerLanguageImpl.java @@ -1,11 +1,10 @@ package net.infumia.pack; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import net.infumia.pack.exception.ResourceAlreadyProducedException; @@ -23,7 +22,7 @@ final class ResourceProducerLanguageImpl implements ResourceProducerLanguage { private final Texture texture; private final Map propertiesToMultichar; - private Set fontProviders; + private List fontProviders; ResourceProducerLanguageImpl( final Key key, @@ -60,7 +59,7 @@ public void produce(final ArbitraryCharacterFactory characterFactory) if (this.fontProviders != null) { throw new ResourceAlreadyProducedException(); } - this.fontProviders = new HashSet<>(); + this.fontProviders = new ArrayList<>(); this.propertiesToMultichar.values() .forEach(multichar -> { multichar.produce(characterFactory); @@ -75,7 +74,7 @@ public Collection fontProviders() throws ResourceNotProducedExcept @Override public Collection textures() throws ResourceNotProducedException { - return Collections.singleton(this.texture); + return Collections.singletonList(this.texture); } private ResourceProducerImageMultichar getGlyphCollection(final int height, final int ascent) { From 071e16dc113c251a156f9324a75a393311797206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 16 Jul 2024 18:43:51 +0300 Subject: [PATCH 55/56] use list instead of set. --- .../pack/ArbitraryCharacterFactoryReserved.java | 4 ++-- .../infumia/pack/FileResourceMergerDefault.java | 16 ++++++++-------- .../src/main/java/net/infumia/pack/MultiMap.java | 4 ++-- .../main/java/net/infumia/pack/PackReader.java | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java index 5798b11..a0625a2 100644 --- a/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java +++ b/common/src/main/java/net/infumia/pack/ArbitraryCharacterFactoryReserved.java @@ -1,8 +1,8 @@ package net.infumia.pack; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; /** * A factory for creating arbitrary characters, ensuring that reserved characters are not produced. @@ -59,7 +59,7 @@ public char create() throws IllegalStateException { private static final class Internal { private static final Lazy> RESERVED = Lazy.of(() -> { - final Collection reserved = new HashSet<>(); + final Collection reserved = new ArrayList<>(); for (char c = 'a'; c <= 'z'; c++) { reserved.add(c); } diff --git a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java index f4a0bd1..1a56f79 100644 --- a/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java +++ b/common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java @@ -1,9 +1,9 @@ package net.infumia.pack; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; -import java.util.HashSet; import java.util.Optional; import java.util.stream.Collectors; import net.kyori.adventure.key.Key; @@ -31,14 +31,14 @@ private FileResourceMergerDefault() {} @Override public Collection merge(final Collection resources) { - final Collection simplified = new HashSet<>(resources.size()); + final Collection simplified = new ArrayList<>(resources.size()); for (final FileResource resource : resources) { simplified.addAll(this.simplify(resource)); } final MultiMap atlases = new MultiMap<>(); final MultiMap models = new MultiMap<>(); - final HashSet remaining = new HashSet<>(); + final Collection remaining = new ArrayList<>(); for (final FileResource resource : simplified) { if (resource instanceof FileResourceAtlas) { final Atlas atlas = ((FileResourceAtlas) resource).atlas; @@ -53,7 +53,7 @@ public Collection merge(final Collection resources) } } - final Collection mergedAtlases = new HashSet<>(atlases.keys().size()); + final Collection mergedAtlases = new ArrayList<>(atlases.keys().size()); for (final Key key : atlases.keys()) { final Collection duplicates = atlases.get(key); final Atlas merged = Atlas.atlas() @@ -69,7 +69,7 @@ public Collection merge(final Collection resources) mergedAtlases.add(merged); } - final Collection mergedModels = new HashSet<>(models.keys().size()); + final Collection mergedModels = new ArrayList<>(models.keys().size()); for (final Key key : models.keys()) { final Collection duplicates = models.get(key); final Model.Builder builder = Model.model().key(key); @@ -97,9 +97,9 @@ public Collection merge(final Collection resources) final Collection mergedResources = mergedAtlases .stream() .map(FileResources::atlas) - .collect(Collectors.toSet()); + .collect(Collectors.toList()); mergedResources.addAll( - mergedModels.stream().map(FileResources::model).collect(Collectors.toSet()) + mergedModels.stream().map(FileResources::model).collect(Collectors.toList()) ); mergedResources.addAll(remaining); return mergedResources; @@ -110,7 +110,7 @@ private Collection simplify(final FileResource resource) { return ((FileResourceAll) resource).resources.stream() .map(this::simplify) .flatMap(Collection::stream) - .collect(Collectors.toSet()); + .collect(Collectors.toList()); } else { return Collections.singletonList(resource); } diff --git a/common/src/main/java/net/infumia/pack/MultiMap.java b/common/src/main/java/net/infumia/pack/MultiMap.java index 5c9841f..0cfec3f 100644 --- a/common/src/main/java/net/infumia/pack/MultiMap.java +++ b/common/src/main/java/net/infumia/pack/MultiMap.java @@ -1,8 +1,8 @@ package net.infumia.pack; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; final class MultiMap { @@ -12,7 +12,7 @@ final class MultiMap { MultiMap() {} void put(final K key, final V value) { - this.map.computeIfAbsent(key, __ -> new HashSet<>()).add(value); + this.map.computeIfAbsent(key, __ -> new ArrayList<>()).add(value); } Collection get(final K key) { diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index c59ab6a..a8da00e 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -61,14 +61,14 @@ private PackGeneratorContext read0(@NotNull final Stream walking) throws I .readAll() .stream() .map(part -> part.directory(path)) - .collect(Collectors.toSet()); + .collect(Collectors.toList()); } } catch (final IOException e) { throw new RuntimeException(e); } }) .flatMap(Collection::stream) - .collect(Collectors.toSet()); + .collect(Collectors.toList()); return new PackGeneratorContext( ResourcePack.resourcePack(), this.base, From f0cfab0d141dbcc79b04e252ff9048cb9eb8607f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 29 Jul 2024 15:00:51 +0300 Subject: [PATCH 56/56] move. --- buildSrc/settings.gradle.kts | 2 +- settings.gradle.kts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts index 245f39a..c8d9270 100644 --- a/buildSrc/settings.gradle.kts +++ b/buildSrc/settings.gradle.kts @@ -1,4 +1,4 @@ -plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" } +plugins { id("org.gradle.toolchains.foojay-resolver-convention") } dependencyResolutionManagement { versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 142f3fd..c0670b6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,5 @@ +plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" } + rootProject.name = "pack" include("common", "blank", "language", "generator")