diff --git a/build.gradle.kts b/build.gradle.kts index 5d574a5..d1bfc84 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "io.github.solid-resourcepack" -version = "1.0.5" +version = "1.0.6" repositories { mavenCentral() @@ -22,6 +22,7 @@ dependencies { testImplementation(rootProject.libs.kotlinTest) implementation(rootProject.libs.kotlinJvm) compileOnly(libs.paper) + api(libs.soldiMaterial) api(libs.bundles.creative) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5df7d1f..df22272 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,6 +5,7 @@ shadow = "8.1.1" paper = "1.21-R0.1-SNAPSHOT" creative = "1.7.2" sonatypeCentralPortalPublisher = "1.2.3" +solidMaterial = "1.21" [libraries] kotlinJvm = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" } @@ -14,6 +15,7 @@ paper = { module = "io.papermc.paper:paper-api", version.ref = "paper" } creativeApi = { module = "team.unnamed:creative-api", version.ref = "creative" } creativeSerializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" } +soldiMaterial = { module = "io.github.solid-resourcepack:material-api", version.ref = "solidMaterial" } [bundles] creative = [ diff --git a/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/PredicateBuilder.kt b/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/PredicateBuilder.kt index d7bcdad..e06345a 100644 --- a/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/PredicateBuilder.kt +++ b/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/PredicateBuilder.kt @@ -1,11 +1,11 @@ package io.github.solid.resourcepack.api.builder.config import io.github.solid.resourcepack.api.builder.feature.PredicateConfig -import io.github.solid.resourcepack.api.material.SolidMaterial import io.github.solid.resourcepack.api.predicate.PredicateIncrementor import io.github.solid.resourcepack.api.predicate.PredicateIncrementorType +import io.github.solid.resourcepack.material.SolidBlockMaterial +import io.github.solid.resourcepack.material.SolidMaterial import net.kyori.adventure.key.Key -import org.bukkit.Material object Predicate { @@ -35,7 +35,7 @@ object Predicate { } fun noteBlock(): PredicateBuilder { - return builder().type(PredicateIncrementorType.NOTE_BLOCK).target(SolidMaterial.from(Material.NOTE_BLOCK)!!) + return builder().type(PredicateIncrementorType.NOTE_BLOCK).target(SolidBlockMaterial.NOTE_BLOCK.toGeneric()) } } @@ -90,7 +90,7 @@ class PredicateBuilder : ConfigBuilder { override fun build(): PredicateConfig { if (target == null) { - target = SolidMaterial.from(key!!, parent, textures) + target = SolidMaterial(key!!, parent, textures) } return PredicateConfig( diff --git a/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/SolidModelBuilder.kt b/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/SolidModelBuilder.kt index 73c8810..f42244a 100644 --- a/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/SolidModelBuilder.kt +++ b/src/main/kotlin/io/github/solid/resourcepack/api/builder/config/SolidModelBuilder.kt @@ -1,26 +1,27 @@ package io.github.solid.resourcepack.api.builder.config import io.github.solid.resourcepack.api.builder.feature.SolidModelConfig -import io.github.solid.resourcepack.api.material.SolidMaterial import io.github.solid.resourcepack.api.predicate.PredicateIncrementor import io.github.solid.resourcepack.api.predicate.PredicateIncrementorType +import io.github.solid.resourcepack.material.SolidItemMaterial +import io.github.solid.resourcepack.material.SolidMaterial import net.kyori.adventure.key.Key import team.unnamed.creative.base.Writable import team.unnamed.creative.texture.Texture object SolidModel { - fun itemModel(target: SolidMaterial, model: Key): SolidModelBuilder { - return SolidModelBuilder().target(target).key(model).incrementor(PredicateIncrementorType.CUSTOM_MODEL_DATA) + fun itemModel(target: SolidItemMaterial, model: Key): SolidModelBuilder { + return SolidModelBuilder().target(target.toGeneric()).key(model).incrementor(PredicateIncrementorType.CUSTOM_MODEL_DATA) } - fun itemModel(target: SolidMaterial, model: Key, writable: Writable): SolidModelBuilder { - return SolidModelBuilder().target(target).key(model).data(writable) + fun itemModel(target: SolidItemMaterial, model: Key, writable: Writable): SolidModelBuilder { + return SolidModelBuilder().target(target.toGeneric()).key(model).data(writable) .incrementor(PredicateIncrementorType.CUSTOM_MODEL_DATA) } - fun itemModel(target: SolidMaterial, vararg textures: Texture): SolidModelBuilder { - return SolidModelBuilder().target(target).variants(*textures) + fun itemModel(target: SolidItemMaterial, vararg textures: Texture): SolidModelBuilder { + return SolidModelBuilder().target(target.toGeneric()).variants(*textures) .incrementor(PredicateIncrementorType.CUSTOM_MODEL_DATA) } @@ -122,7 +123,7 @@ class SolidModelBuilder : ConfigBuilder { override fun build(): SolidModelConfig { if (this.target == null) { - this.target = SolidMaterial.from(this.key, this.targetParent, this.targetTextures) + this.target = SolidMaterial(this.key, this.targetParent, this.targetTextures) } return SolidModelConfig( writable = writable, diff --git a/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/PredicateFeature.kt b/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/PredicateFeature.kt index 171136c..f96bf50 100644 --- a/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/PredicateFeature.kt +++ b/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/PredicateFeature.kt @@ -1,9 +1,9 @@ package io.github.solid.resourcepack.api.builder.feature import io.github.solid.resourcepack.api.builder.ResourcePackFeature -import io.github.solid.resourcepack.api.material.SolidMaterial import io.github.solid.resourcepack.api.predicate.PredicateGenerator import io.github.solid.resourcepack.api.predicate.PredicateIncrementor +import io.github.solid.resourcepack.material.SolidMaterial import net.kyori.adventure.key.Key import team.unnamed.creative.ResourcePack import team.unnamed.creative.model.Model @@ -12,10 +12,10 @@ import team.unnamed.creative.model.ModelTextures class PredicateFeature : ResourcePackFeature { override fun apply(config: PredicateConfig, pack: ResourcePack) { - val model = pack.model(config.target.minecraftKey) ?: Model.model().parent(config.target.minecraftParent) - .key(config.target.minecraftKey).textures( + val model = pack.model(config.target.key) ?: Model.model().parent(config.target.parent) + .key(config.target.key).textures( ModelTextures.builder().let { - config.target.minecraftTextures.forEach { texture -> + config.target.textures.forEach { texture -> it.addVariable( texture.key, ModelTexture.ofKey(texture.value) diff --git a/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/SolidModelFeature.kt b/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/SolidModelFeature.kt index c9af13c..0b0f05b 100644 --- a/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/SolidModelFeature.kt +++ b/src/main/kotlin/io/github/solid/resourcepack/api/builder/feature/SolidModelFeature.kt @@ -4,8 +4,8 @@ import io.github.solid.resourcepack.api.builder.ResourcePackFeature import io.github.solid.resourcepack.api.builder.config.ModelModifier import io.github.solid.resourcepack.api.builder.config.ModelVariant import io.github.solid.resourcepack.api.builder.config.Predicate -import io.github.solid.resourcepack.api.material.SolidMaterial import io.github.solid.resourcepack.api.predicate.PredicateIncrementor +import io.github.solid.resourcepack.material.SolidMaterial import net.kyori.adventure.key.Key import team.unnamed.creative.ResourcePack import team.unnamed.creative.base.Writable diff --git a/src/main/kotlin/io/github/solid/resourcepack/api/material/SolidMaterial.kt b/src/main/kotlin/io/github/solid/resourcepack/api/material/SolidMaterial.kt deleted file mode 100644 index ccbd25d..0000000 --- a/src/main/kotlin/io/github/solid/resourcepack/api/material/SolidMaterial.kt +++ /dev/null @@ -1,118 +0,0 @@ -package io.github.solid.resourcepack.api.material - -import net.kyori.adventure.key.Key -import org.bukkit.Material -import org.bukkit.block.data.Orientable -import org.jetbrains.annotations.ApiStatus.Experimental - -/** - * Represents a material that provides all data necessary to be used in a resource pack - */ -data class SolidMaterial( - val material: Material, - val minecraftKey: Key, - val minecraftParent: Key?, - val minecraftTextures: Map -) { - - /** - * Used to fine tune wrong results from the from(material: Material) method - * @return A new [SolidMaterial] with updated [SolidMaterial.minecraftParent] - * */ - fun minecraftParent(key: Key?): SolidMaterial { - return SolidMaterial(material, minecraftKey, key, minecraftTextures) - } - - /** - * Used to fine tune wrong results from the from(material: Material) method - * @return A new [SolidMaterial] with updated [SolidMaterial.minecraftTextures] - * */ - fun minecraftTextures(textures: Map): SolidMaterial { - return SolidMaterial(material, minecraftKey, minecraftParent, textures) - } - - companion object { - - /** - * Used to create very specific resource pack materials from scratch - * @return A [SolidMaterial] - * */ - fun from(minecraftKey: Key, minecraftParent: Key?, textureVariables: Map): SolidMaterial { - val material = Material.matchMaterial(minecraftKey.value().substringAfterLast("/").uppercase()) - ?: throw IllegalArgumentException("Material not found for key: $minecraftKey") - return SolidMaterial(material, minecraftKey, minecraftParent, textureVariables) - } - - /** - * Used to create a resource pack material from a Bukkit material - * @return A [SolidMaterial] or null if the material is not supported - * ONLY WORKS WHEN USED IN A MINECRAFT SERVER CONTEXT - */ - - @Experimental - fun from(material: Material): SolidMaterial? { - return try { - fromUnsafe(material) - } catch (e: Exception) { - null - } - } - - private fun fromUnsafe(material: Material): SolidMaterial { - var prefix: String? = null - var parent: Key? = null - val textures = mutableMapOf() - if (material.isBlock) { - prefix = "block" - parent = parseBlockParent(material) - textures.putAll(parseBlockTexture(material)) - } else if (material.isItem) { - prefix = "item" - parent = parseItemParent(material) - textures.putAll(parseItemTexture(material)) - } - if (prefix == null) { - throw IllegalArgumentException("Material is not a block or item") - } - - return SolidMaterial( - material, - Key.key(Key.MINECRAFT_NAMESPACE, "$prefix/${material.name.lowercase()}"), - parent, - textures - ) - } - - private fun parseBlockTexture(material: Material): Map { - val key = Key.key(Key.MINECRAFT_NAMESPACE, "block/${material.name.lowercase()}") - if (material.isSolid && material.asBlockType()!!.createBlockData() !is Orientable) { - return mapOf( - "all" to key, - "particle" to key - ) - } - throw IllegalArgumentException("Textures not found for material: $material! This material is not yet supported, please use SolidMaterial#from(Material, Key, Map)") - } - - private fun parseBlockParent(material: Material): Key? { - if (material.isSolid && material.asBlockType()!!.createBlockData() !is Orientable) { - return Key.key(Key.MINECRAFT_NAMESPACE, "block/cube_all") - } - - throw IllegalArgumentException("Parent not found for material: $material! This material is not yet supported, please use SolidMaterial#from(Material, Key, Map)") - } - - private fun parseItemTexture(material: Material): Map { - val key = Key.key(Key.MINECRAFT_NAMESPACE, "item/${material.name.lowercase()}") - return mapOf("layer0" to key) - } - - private fun parseItemParent(material: Material): Key { - if (material.maxDurability > 1) { - return Key.key(Key.MINECRAFT_NAMESPACE, "item/handheld") - } - return Key.key(Key.MINECRAFT_NAMESPACE, "item/generated") - } - - } -} \ No newline at end of file diff --git a/src/main/kotlin/io/github/solid/resourcepack/api/material/SolidMaterialMapper.kt b/src/main/kotlin/io/github/solid/resourcepack/api/material/SolidMaterialMapper.kt new file mode 100644 index 0000000..279b6d4 --- /dev/null +++ b/src/main/kotlin/io/github/solid/resourcepack/api/material/SolidMaterialMapper.kt @@ -0,0 +1,51 @@ +package io.github.solid.resourcepack.api.material + +import io.github.solid.resourcepack.material.SolidBlockMaterial +import io.github.solid.resourcepack.material.SolidItemMaterial +import io.github.solid.resourcepack.material.SolidMaterial +import org.bukkit.Material +import org.jetbrains.annotations.ApiStatus.Experimental + +object SolidMaterialMapper { + + /** + * Converts a [Material] to a [SolidMaterial] + * IMPORTANT: Only works in a server context + */ + @Experimental + fun from(material: Material): SolidMaterial { + if (material.isItem) { + return SolidItemMaterial.valueOf(material.name).toGeneric() + } + if (material.isBlock) { + return SolidBlockMaterial.valueOf(material.name).toGeneric() + } + throw IllegalArgumentException("Material $material is not supported") + } + + fun from(material: Material, type: MaterialType): SolidMaterial { + val returned = mutableListOf() + if (type == MaterialType.ITEM || type == MaterialType.BOTH) { + try { + returned.add(SolidItemMaterial.valueOf(material.name).toGeneric()) + } catch (ignored: Exception) { + } + } + + if (type == MaterialType.BLOCK || type == MaterialType.BOTH) { + try { + returned.add(SolidBlockMaterial.valueOf(material.name).toGeneric()) + } catch (ignored: Exception) { + } + } + + if (returned.size != 1) throw IllegalArgumentException("Material $material is not supported") + return returned.first() + } +} + +enum class MaterialType { + ITEM, + BLOCK, + BOTH; +} \ No newline at end of file diff --git a/src/test/kotlin/PredicateTest.kt b/src/test/kotlin/PredicateTest.kt index b1aed28..619dce0 100644 --- a/src/test/kotlin/PredicateTest.kt +++ b/src/test/kotlin/PredicateTest.kt @@ -3,7 +3,7 @@ import io.github.solid.resourcepack.api.builder.config.ModelVariant import io.github.solid.resourcepack.api.builder.config.Predicate import io.github.solid.resourcepack.api.key.KeyType import io.github.solid.resourcepack.api.key.SolidKey -import io.github.solid.resourcepack.api.material.SolidMaterial +import io.github.solid.resourcepack.material.SolidMaterial import net.kyori.adventure.key.Key import org.bukkit.Material import team.unnamed.creative.ResourcePack @@ -13,7 +13,7 @@ fun main() { val pack = AdvancedResourcePack(ResourcePack.resourcePack()) pack.apply( Predicate.customModelData( - SolidMaterial.from( + SolidMaterial( SolidKey.from(Material.POPPED_CHORUS_FRUIT, KeyType.ITEM), SolidKey.ITEM_GENERATED, ModelVariant.Textures.generic(SolidKey.from(Material.POPPED_CHORUS_FRUIT, KeyType.ITEM))