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
|
|
@ -6,6 +6,7 @@ import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
|
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
|
||||||
import xyz.alexcrea.cuanvil.group.ItemGroupManager;
|
import xyz.alexcrea.cuanvil.group.ItemGroupManager;
|
||||||
|
import xyz.alexcrea.cuanvil.recipe.CustomAnvilRecipeManager;
|
||||||
import xyz.alexcrea.cuanvil.util.MetricsUtil;
|
import xyz.alexcrea.cuanvil.util.MetricsUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
@ -18,12 +19,14 @@ public abstract class ConfigHolder {
|
||||||
public static ItemGroupConfigHolder ITEM_GROUP_HOLDER;
|
public static ItemGroupConfigHolder ITEM_GROUP_HOLDER;
|
||||||
public static ConflictConfigHolder CONFLICT_HOLDER;
|
public static ConflictConfigHolder CONFLICT_HOLDER;
|
||||||
public static UnitRepairHolder UNIT_REPAIR_HOLDER;
|
public static UnitRepairHolder UNIT_REPAIR_HOLDER;
|
||||||
|
public static CustomAnvilCraftHolder CUSTOM_RECIPE_HOLDER;
|
||||||
|
|
||||||
public static boolean loadConfig() {
|
public static boolean loadConfig() {
|
||||||
DEFAULT_CONFIG = new DefaultConfigHolder();
|
DEFAULT_CONFIG = new DefaultConfigHolder();
|
||||||
ITEM_GROUP_HOLDER = new ItemGroupConfigHolder();
|
ITEM_GROUP_HOLDER = new ItemGroupConfigHolder();
|
||||||
CONFLICT_HOLDER = new ConflictConfigHolder();
|
CONFLICT_HOLDER = new ConflictConfigHolder();
|
||||||
UNIT_REPAIR_HOLDER = new UnitRepairHolder();
|
UNIT_REPAIR_HOLDER = new UnitRepairHolder();
|
||||||
|
CUSTOM_RECIPE_HOLDER = new CustomAnvilCraftHolder();
|
||||||
|
|
||||||
boolean result = reloadAllFromDisk(true);
|
boolean result = reloadAllFromDisk(true);
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|
@ -41,6 +44,9 @@ public abstract class ConfigHolder {
|
||||||
sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail);
|
sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail);
|
||||||
if (!sucess) return false;
|
if (!sucess) return false;
|
||||||
sucess = UNIT_REPAIR_HOLDER.reloadFromDisk(hardfail);
|
sucess = UNIT_REPAIR_HOLDER.reloadFromDisk(hardfail);
|
||||||
|
if (!sucess) return false;
|
||||||
|
sucess = CUSTOM_RECIPE_HOLDER.reloadFromDisk(hardfail);
|
||||||
|
|
||||||
return sucess;
|
return sucess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,6 +248,7 @@ public abstract class ConfigHolder {
|
||||||
public static class UnitRepairHolder extends ResourceConfigHolder {
|
public static class UnitRepairHolder extends ResourceConfigHolder {
|
||||||
private final static String ITEM_GROUP_FILE_NAME = "unit_repair_item";
|
private final static String ITEM_GROUP_FILE_NAME = "unit_repair_item";
|
||||||
|
|
||||||
|
|
||||||
private UnitRepairHolder() {
|
private UnitRepairHolder() {
|
||||||
super(ITEM_GROUP_FILE_NAME);
|
super(ITEM_GROUP_FILE_NAME);
|
||||||
}
|
}
|
||||||
|
|
@ -253,4 +260,22 @@ public abstract class ConfigHolder {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Class for custom anvil craft
|
||||||
|
public static class CustomAnvilCraftHolder extends ResourceConfigHolder {
|
||||||
|
private final static String CUSTOM_CRAFT_FILE_NAME = "custom_recipes";
|
||||||
|
CustomAnvilRecipeManager recipeManager;
|
||||||
|
|
||||||
|
private CustomAnvilCraftHolder() {
|
||||||
|
super(CUSTOM_CRAFT_FILE_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reload() {
|
||||||
|
this.recipeManager = new CustomAnvilRecipeManager();
|
||||||
|
this.recipeManager.prepareRecipes(this.configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 enchantConflictConfigHash = 1406650190
|
||||||
private const val itemGroupsConfigHash = 1406650190
|
private const val itemGroupsConfigHash = 1406650190
|
||||||
private const val unitRepairItemConfigHash = 536871958
|
private const val unitRepairItemConfigHash = 536871958
|
||||||
|
private const val customAnvilCraftConfigHash = 0
|
||||||
private const val baseConfigPieName = "isDefaultBaseConfig"
|
private const val baseConfigPieName = "isDefaultBaseConfig"
|
||||||
private const val enchantLimitsConfigPieName = "isDefaultEnchantLimitsConfig"
|
private const val enchantLimitsConfigPieName = "isDefaultEnchantLimitsConfig"
|
||||||
private const val enchantValuesConfigPieName = "isDefaultEnchantValuesConfig"
|
private const val enchantValuesConfigPieName = "isDefaultEnchantValuesConfig"
|
||||||
private const val enchantConflictConfigPieName = "isDefaultEnchantConflictConfig"
|
private const val enchantConflictConfigPieName = "isDefaultEnchantConflictConfig"
|
||||||
private const val itemGroupsConfigPieName = "isDefaultItemGroupsConfig"
|
private const val itemGroupsConfigPieName = "isDefaultItemGroupsConfig"
|
||||||
private const val unitRepairItemConfigPieName = "isDefaultUnitRepairItemConfig"
|
private const val unitRepairItemConfigPieName = "isDefaultUnitRepairItemConfig"
|
||||||
|
private const val customAnvilCraftConfigPieName = "isDefaultCustomAnvilCraftConfig"
|
||||||
private var isDefaultBaseConfig = true
|
private var isDefaultBaseConfig = true
|
||||||
private var isDefaultEnchantLimitsConfig = true
|
private var isDefaultEnchantLimitsConfig = true
|
||||||
private var isDefaultEnchantValuesConfig = true
|
private var isDefaultEnchantValuesConfig = true
|
||||||
private var isDefaultEnchantConflictConfig = true
|
private var isDefaultEnchantConflictConfig = true
|
||||||
private var isDefaultItemGroupsConfig = true
|
private var isDefaultItemGroupsConfig = true
|
||||||
private var isDefaultUnitRepairItemConfig = true
|
private var isDefaultUnitRepairItemConfig = true
|
||||||
|
private var isDefaultCustomAnvilCraftConfig = true
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get hash of a key, value a pair of a configuration section
|
* 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 enchantConflictConfig = getConfigurationHash(ConfigHolder.CONFLICT_HOLDER.config)
|
||||||
val itemGroupConfig = getConfigurationHash(ConfigHolder.ITEM_GROUP_HOLDER.config)
|
val itemGroupConfig = getConfigurationHash(ConfigHolder.ITEM_GROUP_HOLDER.config)
|
||||||
val unitRepairConfig = getConfigurationHash(ConfigHolder.UNIT_REPAIR_HOLDER.config)
|
val unitRepairConfig = getConfigurationHash(ConfigHolder.UNIT_REPAIR_HOLDER.config)
|
||||||
|
val customRecipeConfig = getConfigurationHash(ConfigHolder.CUSTOM_RECIPE_HOLDER.config)
|
||||||
// Test if default
|
// Test if default
|
||||||
isDefaultBaseConfig = baseConfigHash == baseConfig
|
isDefaultBaseConfig = baseConfigHash == baseConfig
|
||||||
isDefaultEnchantLimitsConfig = enchantLimitsConfigHash == limitEnchantConfig
|
isDefaultEnchantLimitsConfig = enchantLimitsConfigHash == limitEnchantConfig
|
||||||
|
|
@ -80,6 +84,7 @@ object MetricsUtil {
|
||||||
isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig
|
isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig
|
||||||
isDefaultItemGroupsConfig = itemGroupsConfigHash == itemGroupConfig
|
isDefaultItemGroupsConfig = itemGroupsConfigHash == itemGroupConfig
|
||||||
isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig
|
isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig
|
||||||
|
isDefaultCustomAnvilCraftConfig = customAnvilCraftConfigHash == customRecipeConfig
|
||||||
// If not default and debug flag active, print the hash.
|
// If not default and debug flag active, print the hash.
|
||||||
if (ConfigOptions.debugLog) {
|
if (ConfigOptions.debugLog) {
|
||||||
if (!isDefaultBaseConfig) {
|
if (!isDefaultBaseConfig) {
|
||||||
|
|
@ -100,6 +105,9 @@ object MetricsUtil {
|
||||||
if (!isDefaultUnitRepairItemConfig) {
|
if (!isDefaultUnitRepairItemConfig) {
|
||||||
CustomAnvil.log("unitRepairConfig: $unitRepairConfig")
|
CustomAnvil.log("unitRepairConfig: $unitRepairConfig")
|
||||||
}
|
}
|
||||||
|
if (!isDefaultCustomAnvilCraftConfig) {
|
||||||
|
CustomAnvil.log("customRecipeConfig: $customRecipeConfig")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -141,6 +149,9 @@ object MetricsUtil {
|
||||||
metric.addCustomChart(Metrics.SimplePie(unitRepairItemConfigPieName) {
|
metric.addCustomChart(Metrics.SimplePie(unitRepairItemConfigPieName) {
|
||||||
isDefaultUnitRepairItemConfig.toString()
|
isDefaultUnitRepairItemConfig.toString()
|
||||||
})
|
})
|
||||||
|
metric.addCustomChart(Metrics.SimplePie(customAnvilCraftConfigPieName) {
|
||||||
|
isDefaultCustomAnvilCraftConfig.toString()
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
5
src/main/resources/custom_recipes.yml
Normal file
5
src/main/resources/custom_recipes.yml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# ----------------------------------------------------
|
||||||
|
# This config file is to store custom craft
|
||||||
|
# It is recommended to use the in game config editor for this configuration.
|
||||||
|
# /customanvilconfig With ca.config.edit permission
|
||||||
|
# ----------------------------------------------------
|
||||||
Loading…
Add table
Add a link
Reference in a new issue