Skip to content

Commit

Permalink
add empty item
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Feb 26, 2024
1 parent f9d74a4 commit 1155628
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ItemMeta> metaConsumer);
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ allprojects {
apply(plugin = "kotlin")

group = "kr.toxicity.inventory"
version = "1.0"
version = "1.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ 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

class InventoryFrameworkImpl: InventoryFramework() {

private val info = mutableListOf<PluginInfo>()
private val managers = listOf(
ConfigManager,
BackgroundManager,
AnimationManager,
TextManager,
Expand Down Expand Up @@ -96,6 +97,11 @@ class InventoryFrameworkImpl: InventoryFramework() {
}

override fun builder(): GuiBuilder = GuiBuilderImpl()
override fun getEmptyItem(metaConsumer: Consumer<ItemMeta>): ItemStack {
return ConfigManager.getEmptyItem {
metaConsumer.accept(it)
}
}

override fun loadAssets(prefix: String, dir: File) {
JarFile(file).use {
Expand Down
15 changes: 13 additions & 2 deletions dist/src/main/kotlin/kr/toxicity/inventory/data/GlobalResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -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<PluginInfo>) {
}

override fun reload(pluginResources: List<PluginResource>, 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<PluginInfo>) {
}
}
1 change: 1 addition & 0 deletions dist/src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
empty-material: BARRIER
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1155628

Please sign in to comment.