Files
src
build.gradle.kts
manifest.kod
Code
31 2,227
src/main/kotlin/com/weaponsmp/managers/RecipeManager.kt
package com.weaponsmp.managers
import com.weaponsmp.WeaponsMP
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.ShapedRecipe
import java.io.File
class RecipeManager(private val plugin: WeaponsMP) {
private val recipesFile = File(plugin.dataFolder, "recipes.yml")
private lateinit var recipesConfig: YamlConfiguration
val craftingGrids: MutableMap<String, Array<ItemStack?>> = HashMap()
val firstCrafts: MutableSet<String> = HashSet()
fun load() {
if (!recipesFile.exists()) {
plugin.saveResource("recipes.yml", false)
}
recipesConfig = YamlConfiguration.loadConfiguration(recipesFile)
val recipeSection = recipesConfig.getConfigurationSection("recipes")
if (recipeSection != null) {
for (weaponId in recipeSection.getKeys(false)) {
val grid = arrayOfNulls<ItemStack>(9)
for (i in 0 until 9) {
val matName = recipeSection.getString("$weaponId.slot$i") ?: continue
val material = Material.matchMaterial(matName) ?: continue
grid[i] = ItemStack(material)
}
craftingGrids[weaponId] = grid
registerBukkitRecipe(weaponId, grid)
}
}
val firstCraftSection = recipesConfig.getStringList("first-crafts")
firstCrafts.addAll(firstCraftSection)
}
fun save() {
val cfg = YamlConfiguration()
for ((weaponId, grid) in craftingGrids) {
for (i in 0 until 9) {
val item = grid[i]
if (item != null && item.type != Material.AIR) {
cfg.set("recipes.$weaponId.slot$i", item.type.name)
}
}
}
cfg.set("first-crafts", firstCrafts.toList())
cfg.save(recipesFile)
}
fun setRecipe(weaponId: String, grid: Array<ItemStack?>) {
craftingGrids[weaponId] = grid
unregisterBukkitRecipe(weaponId)
registerBukkitRecipe(weaponId, grid)
save()
}
fun clearRecipe(weaponId: String) {
craftingGrids.remove(weaponId)
unregisterBukkitRecipe(weaponId)
save()
}
fun hasBeenCrafted(weaponId: String) = firstCrafts.contains(weaponId)
fun markCrafted(weaponId: String) {
firstCrafts.add(weaponId)
save()
}
fun matchesRecipe(weaponId: String, matrix: Array<ItemStack?>): Boolean {
val expected = craftingGrids[weaponId] ?: return false
for (i in 0 until 9) {
val exp = expected[i]
val got = matrix.getOrNull(i)
if (exp == null || exp.type == Material.AIR) {
if (got != null && got.type != Material.AIR) return false
} else {
if (got == null || got.type == Material.AIR) return false
if (got.type != exp.type) return false
}
}
return true
}
private fun registerBukkitRecipe(weaponId: String, grid: Array<ItemStack?>) {
val weapon = plugin.weaponManager.weapons[weaponId] ?: return
if (grid.all { it == null || it.type == Material.AIR }) return
val key = NamespacedKey(plugin, weaponId)
val resultItem = weapon.buildItem()
val recipe = ShapedRecipe(key, resultItem)
val rows = arrayOf(
slotChar(grid[0]) + slotChar(grid[1]) + slotChar(grid[2]),
slotChar(grid[3]) + slotChar(grid[4]) + slotChar(grid[5]),
slotChar(grid[6]) + slotChar(grid[7]) + slotChar(grid[8])
)
recipe.shape(*rows)
val usedChars = mutableSetOf<Char>()
for (i in 0 until 9) {
val item = grid[i]
if (item != null && item.type != Material.AIR) {
val ch = charForSlot(i)
if (usedChars.add(ch)) recipe.setIngredient(ch, item.type)
}
}
Bukkit.addRecipe(recipe)
}
private fun unregisterBukkitRecipe(weaponId: String) {
val key = NamespacedKey(plugin, weaponId)
Bukkit.removeRecipe(key)
}
private fun slotChar(item: ItemStack?): String {
return if (item == null || item.type == Material.AIR) " " else item.type.name.first().toString()
}
private fun charForSlot(i: Int): Char {
return ('A' + i)
}
}