From 1155628e33c7d7e624992dd34b210f5d7c20ac73 Mon Sep 17 00:00:00 2001 From: toxicity Date: Mon, 26 Feb 2024 22:55:32 +0900 Subject: [PATCH] add empty item --- .../inventory/api/InventoryFramework.java | 4 ++ build.gradle.kts | 2 +- .../inventory/InventoryFrameworkImpl.kt | 14 ++-- .../toxicity/inventory/data/GlobalResource.kt | 15 +++- .../inventory/manager/ConfigManager.kt | 65 ++++++++++++++++++ dist/src/main/resources/config.yml | 1 + .../textures/item/empty.png | Bin 0 -> 336 bytes 7 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 dist/src/main/kotlin/kr/toxicity/inventory/manager/ConfigManager.kt create mode 100644 dist/src/main/resources/config.yml create mode 100644 dist/src/main/resources/pack/assets/inventoryframework/textures/item/empty.png diff --git a/api/src/main/java/kr/toxicity/inventory/api/InventoryFramework.java b/api/src/main/java/kr/toxicity/inventory/api/InventoryFramework.java index a30bc62..1c10fd5 100644 --- a/api/src/main/java/kr/toxicity/inventory/api/InventoryFramework.java +++ b/api/src/main/java/kr/toxicity/inventory/api/InventoryFramework.java @@ -1,11 +1,14 @@ package kr.toxicity.inventory.api; import kr.toxicity.inventory.api.gui.GuiBuilder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.Objects; +import java.util.function.Consumer; public abstract class InventoryFramework extends JavaPlugin { private static InventoryFramework instance; @@ -21,4 +24,5 @@ public final void onLoad() { public abstract long reload(); public abstract @NotNull GuiBuilder builder(); public abstract void loadAssets(@NotNull String prefix, @NotNull File dir); + public abstract @NotNull ItemStack getEmptyItem(@NotNull Consumer metaConsumer); } diff --git a/build.gradle.kts b/build.gradle.kts index a5c0943..3d63f75 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ allprojects { apply(plugin = "kotlin") group = "kr.toxicity.inventory" - version = "1.0" + version = "1.2" repositories { mavenCentral() diff --git a/dist/src/main/kotlin/kr/toxicity/inventory/InventoryFrameworkImpl.kt b/dist/src/main/kotlin/kr/toxicity/inventory/InventoryFrameworkImpl.kt index 886d8b0..b8f8031 100644 --- a/dist/src/main/kotlin/kr/toxicity/inventory/InventoryFrameworkImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/inventory/InventoryFrameworkImpl.kt @@ -7,16 +7,16 @@ import kr.toxicity.inventory.data.GlobalResource import kr.toxicity.inventory.data.PluginInfo import kr.toxicity.inventory.data.PluginResource import kr.toxicity.inventory.gui.GuiBuilderImpl -import kr.toxicity.inventory.manager.AnimationManager -import kr.toxicity.inventory.manager.BackgroundManager -import kr.toxicity.inventory.manager.InventoryManager -import kr.toxicity.inventory.manager.TextManager +import kr.toxicity.inventory.manager.* import kr.toxicity.inventory.util.* import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.NamedTextColor import org.bukkit.Bukkit +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.ItemMeta import org.bukkit.plugin.java.JavaPlugin import java.io.File +import java.util.function.Consumer import java.util.jar.JarEntry import java.util.jar.JarFile @@ -24,6 +24,7 @@ class InventoryFrameworkImpl: InventoryFramework() { private val info = mutableListOf() private val managers = listOf( + ConfigManager, BackgroundManager, AnimationManager, TextManager, @@ -96,6 +97,11 @@ class InventoryFrameworkImpl: InventoryFramework() { } override fun builder(): GuiBuilder = GuiBuilderImpl() + override fun getEmptyItem(metaConsumer: Consumer): ItemStack { + return ConfigManager.getEmptyItem { + metaConsumer.accept(it) + } + } override fun loadAssets(prefix: String, dir: File) { JarFile(file).use { diff --git a/dist/src/main/kotlin/kr/toxicity/inventory/data/GlobalResource.kt b/dist/src/main/kotlin/kr/toxicity/inventory/data/GlobalResource.kt index ba79ef9..59ac827 100644 --- a/dist/src/main/kotlin/kr/toxicity/inventory/data/GlobalResource.kt +++ b/dist/src/main/kotlin/kr/toxicity/inventory/data/GlobalResource.kt @@ -12,14 +12,25 @@ class GlobalResource { init { PLUGIN.loadAssets("pack", build) } - - val assets = build + private val parent = build .subFolder("assets") + + private val assets = parent .subFolder("inventoryframework") + private val minecraftAssets = parent + .subFolder("minecraft") val font = assets .subFolder("font") val textures = assets .subFolder("textures") + + val models = assets + .subFolder("models") + .subFolder("item") + + val minecraftModels = minecraftAssets + .subFolder("models") + .subFolder("item") } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/inventory/manager/ConfigManager.kt b/dist/src/main/kotlin/kr/toxicity/inventory/manager/ConfigManager.kt new file mode 100644 index 0000000..75f782e --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/inventory/manager/ConfigManager.kt @@ -0,0 +1,65 @@ +package kr.toxicity.inventory.manager + +import com.google.gson.JsonArray +import com.google.gson.JsonObject +import kr.toxicity.inventory.data.GlobalResource +import kr.toxicity.inventory.data.PluginInfo +import kr.toxicity.inventory.data.PluginResource +import kr.toxicity.inventory.util.PLUGIN +import kr.toxicity.inventory.util.save +import kr.toxicity.inventory.util.warn +import org.bukkit.Material +import org.bukkit.configuration.file.YamlConfiguration +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.ItemMeta +import java.io.File + +object ConfigManager: FrameworkManager { + + private var emptyMaterial = Material.BARRIER + override fun start(pluginInfo: List) { + } + + override fun reload(pluginResources: List, globalResource: GlobalResource) { + val configFile = File(PLUGIN.dataFolder, "config.yml") + if (!configFile.exists()) PLUGIN.saveResource("config.yml", false) + runCatching { + val yaml = YamlConfiguration.loadConfiguration(configFile) + yaml.getString("empty-material")?.let { + emptyMaterial = Material.valueOf(it.uppercase()) + } + JsonObject().apply { + addProperty("parent", "minecraft:item/generated") + add("textures", JsonObject().apply { + addProperty("layer0", "minecraft:item/${emptyMaterial.name.lowercase()}") + }) + add("overrides", JsonArray().apply { + add(JsonObject().apply { + add("predicate", JsonObject().apply { + addProperty("custom_model_data", 1) + }) + addProperty("model", "inventoryframework:item/empty") + }) + }) + }.save(File(globalResource.minecraftModels, "${emptyMaterial.name.lowercase()}.json")) + JsonObject().apply { + add("textures", JsonObject().apply { + addProperty("0", "inventoryframework:item/empty") + }) + }.save(File(globalResource.models, "empty.json")) + }.onFailure { e -> + PLUGIN.warn("Unable to load config.yml") + PLUGIN.warn("Reason: ${e.message}") + } + } + + fun getEmptyItem(block: (ItemMeta) -> Unit) = ItemStack(emptyMaterial).apply { + itemMeta = itemMeta.apply { + setCustomModelData(1) + block(this) + } + } + + override fun end(pluginInfo: List) { + } +} \ No newline at end of file diff --git a/dist/src/main/resources/config.yml b/dist/src/main/resources/config.yml new file mode 100644 index 0000000..164d2d1 --- /dev/null +++ b/dist/src/main/resources/config.yml @@ -0,0 +1 @@ +empty-material: BARRIER \ No newline at end of file diff --git a/dist/src/main/resources/pack/assets/inventoryframework/textures/item/empty.png b/dist/src/main/resources/pack/assets/inventoryframework/textures/item/empty.png new file mode 100644 index 0000000000000000000000000000000000000000..6e705432a15ce9f3d6f3b237016c1382a105a691 GIT binary patch literal 336 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$2RZ)5S5Q;?~=PiXb^A1^plY k*IX5JU;=VRE;#U#fzg*iXj