mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add custom recipe manager.
This commit is contained in:
parent
748781fd50
commit
39ae8845b5
5 changed files with 190 additions and 0 deletions
|
|
@ -0,0 +1,84 @@
|
|||
package xyz.alexcrea.cuanvil.recipe
|
||||
|
||||
import org.bukkit.configuration.ConfigurationSection
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
|
||||
|
||||
class AnvilCustomRecipe(
|
||||
val name: String,
|
||||
var exactCount: Boolean,
|
||||
var exactLeft: Boolean,
|
||||
var exactRight: Boolean,
|
||||
|
||||
var xpCostPerCraft: Int,
|
||||
|
||||
var leftItem: ItemStack?,
|
||||
var rightItem: ItemStack?,
|
||||
var resultItem: ItemStack?,
|
||||
) {
|
||||
|
||||
// Static config name
|
||||
companion object {
|
||||
const val EXACT_COUNT_CONFIG = "exact_count"
|
||||
const val EXACT_LEFT_CONFIG = "exact_left"
|
||||
const val EXACT_RIGHT_CONFIG = "exact_right"
|
||||
|
||||
const val XP_COST_CONFIG = "xp_cost"
|
||||
|
||||
const val LEFT_ITEM_CONFIG = "left_item"
|
||||
const val RIGHT_ITEM_CONFIG = "right_item"
|
||||
const val RESULT_ITEM_CONFIG = "result_item"
|
||||
|
||||
fun getFromConfig(name: String, configSection: ConfigurationSection?): AnvilCustomRecipe? {
|
||||
if(configSection == null) return null;
|
||||
return AnvilCustomRecipe(
|
||||
name,
|
||||
configSection.getBoolean(EXACT_COUNT_CONFIG, true),
|
||||
configSection.getBoolean(EXACT_LEFT_CONFIG, true),
|
||||
configSection.getBoolean(EXACT_RIGHT_CONFIG, true),
|
||||
|
||||
configSection.getInt(XP_COST_CONFIG, 10),
|
||||
|
||||
configSection.getItemStack(LEFT_ITEM_CONFIG, null),
|
||||
configSection.getItemStack(RIGHT_ITEM_CONFIG, null),
|
||||
configSection.getItemStack(RESULT_ITEM_CONFIG, null),
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
fun getFromConfig(name: String): AnvilCustomRecipe? {
|
||||
return getFromConfig(name, ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getConfigurationSection(name))
|
||||
}
|
||||
}
|
||||
|
||||
fun validate(): Boolean {
|
||||
return (leftItem != null) && !(leftItem!!.type.isAir) && (leftItem!!.amount > 0) &&
|
||||
//(rightItem != null) && !(rightItem!!.type.isAir) && (rightItem!!.amount > 0) &&
|
||||
((rightItem == null) || (!(rightItem!!.type.isAir) && (rightItem!!.amount > 0))) &&
|
||||
(resultItem != null) && !(resultItem!!.type.isAir) && (resultItem!!.amount > 0)
|
||||
|
||||
}
|
||||
|
||||
fun saveToFile(){
|
||||
val fileConfig = ConfigHolder.CUSTOM_RECIPE_HOLDER.config
|
||||
|
||||
fileConfig.set("$name.$EXACT_COUNT_CONFIG", exactCount)
|
||||
fileConfig.set("$name.$EXACT_LEFT_CONFIG", exactLeft)
|
||||
fileConfig.set("$name.$EXACT_RIGHT_CONFIG", exactRight)
|
||||
|
||||
fileConfig.set("$name.$XP_COST_CONFIG", xpCostPerCraft)
|
||||
|
||||
fileConfig.set("$name.$LEFT_ITEM_CONFIG", leftItem)
|
||||
fileConfig.set("$name.$RIGHT_ITEM_CONFIG", rightItem)
|
||||
fileConfig.set("$name.$RESULT_ITEM_CONFIG", resultItem)
|
||||
|
||||
|
||||
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
|
||||
ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package xyz.alexcrea.cuanvil.recipe
|
||||
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.configuration.file.FileConfiguration
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import java.util.LinkedHashMap
|
||||
|
||||
class CustomAnvilRecipeManager {
|
||||
|
||||
lateinit var recipeMap: LinkedHashMap<String, AnvilCustomRecipe>
|
||||
|
||||
lateinit var recipeByMat: LinkedHashMap<Material, ArrayList<AnvilCustomRecipe>>
|
||||
|
||||
fun prepareRecipes(config: FileConfiguration) {
|
||||
recipeMap = LinkedHashMap()
|
||||
recipeByMat = LinkedHashMap()
|
||||
|
||||
// read all configs
|
||||
val keys = config.getKeys(false)
|
||||
for (key in keys) {
|
||||
if (recipeMap.containsKey(key))
|
||||
continue
|
||||
val recipe = AnvilCustomRecipe.getFromConfig(key)
|
||||
if(recipe == null){
|
||||
CustomAnvil.log("Can't load recipe $key")
|
||||
continue
|
||||
}
|
||||
|
||||
recipeMap[key] = recipe
|
||||
val leftItem = recipe.leftItem
|
||||
if(leftItem != null){
|
||||
addToMap(recipe, leftItem)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun cleanSetLeftItem(recipe: AnvilCustomRecipe, leftItem: ItemStack?){
|
||||
// Remove left item mat if exist
|
||||
val oldLeftItem = recipe.leftItem
|
||||
if(oldLeftItem != null){
|
||||
val oldMat = oldLeftItem.type
|
||||
|
||||
val test = recipeByMat[oldMat]
|
||||
test!!.remove(recipe)
|
||||
}
|
||||
if(leftItem != null){
|
||||
addToMap(recipe, leftItem)
|
||||
}
|
||||
|
||||
recipe.leftItem = leftItem
|
||||
}
|
||||
|
||||
fun addToMap(recipe: AnvilCustomRecipe, leftItem: ItemStack){
|
||||
var recipeList = recipeByMat[leftItem.type]
|
||||
if(recipeList == null){
|
||||
recipeList = ArrayList()
|
||||
recipeByMat[leftItem.type] = recipeList
|
||||
}
|
||||
recipeList.add(recipe)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -13,18 +13,21 @@ object MetricsUtil {
|
|||
private const val enchantConflictConfigHash = 1406650190
|
||||
private const val itemGroupsConfigHash = 1406650190
|
||||
private const val unitRepairItemConfigHash = 536871958
|
||||
private const val customAnvilCraftConfigHash = 0
|
||||
private const val baseConfigPieName = "isDefaultBaseConfig"
|
||||
private const val enchantLimitsConfigPieName = "isDefaultEnchantLimitsConfig"
|
||||
private const val enchantValuesConfigPieName = "isDefaultEnchantValuesConfig"
|
||||
private const val enchantConflictConfigPieName = "isDefaultEnchantConflictConfig"
|
||||
private const val itemGroupsConfigPieName = "isDefaultItemGroupsConfig"
|
||||
private const val unitRepairItemConfigPieName = "isDefaultUnitRepairItemConfig"
|
||||
private const val customAnvilCraftConfigPieName = "isDefaultCustomAnvilCraftConfig"
|
||||
private var isDefaultBaseConfig = true
|
||||
private var isDefaultEnchantLimitsConfig = true
|
||||
private var isDefaultEnchantValuesConfig = true
|
||||
private var isDefaultEnchantConflictConfig = true
|
||||
private var isDefaultItemGroupsConfig = true
|
||||
private var isDefaultUnitRepairItemConfig = true
|
||||
private var isDefaultCustomAnvilCraftConfig = true
|
||||
|
||||
/**
|
||||
* Get hash of a key, value a pair of a configuration section
|
||||
|
|
@ -73,6 +76,7 @@ object MetricsUtil {
|
|||
val enchantConflictConfig = getConfigurationHash(ConfigHolder.CONFLICT_HOLDER.config)
|
||||
val itemGroupConfig = getConfigurationHash(ConfigHolder.ITEM_GROUP_HOLDER.config)
|
||||
val unitRepairConfig = getConfigurationHash(ConfigHolder.UNIT_REPAIR_HOLDER.config)
|
||||
val customRecipeConfig = getConfigurationHash(ConfigHolder.CUSTOM_RECIPE_HOLDER.config)
|
||||
// Test if default
|
||||
isDefaultBaseConfig = baseConfigHash == baseConfig
|
||||
isDefaultEnchantLimitsConfig = enchantLimitsConfigHash == limitEnchantConfig
|
||||
|
|
@ -80,6 +84,7 @@ object MetricsUtil {
|
|||
isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig
|
||||
isDefaultItemGroupsConfig = itemGroupsConfigHash == itemGroupConfig
|
||||
isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig
|
||||
isDefaultCustomAnvilCraftConfig = customAnvilCraftConfigHash == customRecipeConfig
|
||||
// If not default and debug flag active, print the hash.
|
||||
if (ConfigOptions.debugLog) {
|
||||
if (!isDefaultBaseConfig) {
|
||||
|
|
@ -100,6 +105,9 @@ object MetricsUtil {
|
|||
if (!isDefaultUnitRepairItemConfig) {
|
||||
CustomAnvil.log("unitRepairConfig: $unitRepairConfig")
|
||||
}
|
||||
if (!isDefaultCustomAnvilCraftConfig) {
|
||||
CustomAnvil.log("customRecipeConfig: $customRecipeConfig")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -141,6 +149,9 @@ object MetricsUtil {
|
|||
metric.addCustomChart(Metrics.SimplePie(unitRepairItemConfigPieName) {
|
||||
isDefaultUnitRepairItemConfig.toString()
|
||||
})
|
||||
metric.addCustomChart(Metrics.SimplePie(customAnvilCraftConfigPieName) {
|
||||
isDefaultCustomAnvilCraftConfig.toString()
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue