From a033a36f1bd6c7b80e2ac5c64520f96d9e0dc1d7 Mon Sep 17 00:00:00 2001 From: YufiriaMazenta <2199098065@qq.com> Date: Mon, 8 Jan 2024 12:38:30 +0800 Subject: [PATCH] =?UTF-8?q?[1.9.0-dev2]=E9=85=8D=E6=96=B9=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=EF=BC=8C=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E5=BE=85=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../menu/creator/RecipeCreator.java | 1 - .../craftorithm/recipe/RecipeFactory.java | 412 ------------------ .../craftorithm/recipe/RecipeGroup.java | 98 ++--- .../craftorithm/recipe/RecipeGroupLoader.java | 296 +++++++++++++ .../craftorithm/recipe/RecipeManager.java | 57 +-- .../recipe/loader/RecipeGroupLoader.java | 127 ------ .../recipe/registry/AnvilRecipeRegistry.java | 14 +- .../registry/CookingRecipeRegistry.java | 34 +- .../registry/PotionMixRecipeRegistry.java | 8 +- .../registry/RandomCookingRecipeRegistry.java | 63 ++- .../recipe/registry/RecipeRegistry.java | 6 +- .../recipe/registry/ShapedRecipeRegistry.java | 13 +- .../registry/ShapelessRecipeRegistry.java | 13 +- .../registry/SmithingRecipeRegistry.java | 24 +- .../registry/StoneCuttingRecipeRegistry.java | 45 +- .../registry/UnlockableRecipeRegistry.java | 26 ++ .../registry/XSmithingRecipeRegistry.java | 30 +- src/main/resources/recipe_groups/example.yml | 77 +++- 19 files changed, 618 insertions(+), 728 deletions(-) delete mode 100644 src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeFactory.java create mode 100644 src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroupLoader.java delete mode 100644 src/main/java/com/github/yufiriamazenta/craftorithm/recipe/loader/RecipeGroupLoader.java create mode 100644 src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/UnlockableRecipeRegistry.java diff --git a/build.gradle.kts b/build.gradle.kts index 71c93f97..65a47be6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ import java.text.SimpleDateFormat -version = "1.9.0-dev1" +version = "1.9.0-dev2" plugins { `java-library` diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/menu/creator/RecipeCreator.java b/src/main/java/com/github/yufiriamazenta/craftorithm/menu/creator/RecipeCreator.java index 5aba0f19..3af12f82 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/menu/creator/RecipeCreator.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/menu/creator/RecipeCreator.java @@ -1,7 +1,6 @@ package com.github.yufiriamazenta.craftorithm.menu.creator; import com.github.yufiriamazenta.craftorithm.config.Languages; -import com.github.yufiriamazenta.craftorithm.recipe.RecipeFactory; import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup; import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager; import com.github.yufiriamazenta.craftorithm.recipe.RecipeType; diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeFactory.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeFactory.java deleted file mode 100644 index 42758ddc..00000000 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeFactory.java +++ /dev/null @@ -1,412 +0,0 @@ -package com.github.yufiriamazenta.craftorithm.recipe; - -import com.github.yufiriamazenta.craftorithm.Craftorithm; -import com.github.yufiriamazenta.craftorithm.config.PluginConfigs; -import com.github.yufiriamazenta.craftorithm.exception.UnsupportedVersionException; -import com.github.yufiriamazenta.craftorithm.item.ItemManager; -import com.github.yufiriamazenta.craftorithm.recipe.registry.*; -import crypticlib.CrypticLib; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.Tag; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.RecipeChoice; - -import java.lang.reflect.Field; -import java.util.*; -import java.util.function.BiFunction; - -public class RecipeFactory { - - private static final Map>> recipeRegistryProviderMap; - private static final Map>> multipleRecipeRegistryProviderMap; - - static { - recipeRegistryProviderMap = new HashMap<>(); - multipleRecipeRegistryProviderMap = new HashMap<>(); - - recipeRegistryProviderMap.put(RecipeType.SHAPED, RecipeFactory::newShapedRecipe); - recipeRegistryProviderMap.put(RecipeType.SHAPELESS, RecipeFactory::newShapelessRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.SHAPED, RecipeFactory::newMultipleShapedRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.SHAPELESS, RecipeFactory::newMultipleShapelessRecipe); - if (CrypticLib.minecraftVersion() >= 11400) { - recipeRegistryProviderMap.put(RecipeType.COOKING, RecipeFactory::newCookingRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.COOKING, RecipeFactory::newMultipleCookingRecipe); - recipeRegistryProviderMap.put(RecipeType.STONE_CUTTING, RecipeFactory::newStoneCuttingRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.STONE_CUTTING, RecipeFactory::newMultipleStoneCuttingRecipe); - recipeRegistryProviderMap.put(RecipeType.SMITHING, RecipeFactory::newSmithingRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.SMITHING, RecipeFactory::newMultipleSmithingRecipe); - } - - if (CrypticLib.minecraftVersion() >= 11700) { - recipeRegistryProviderMap.put(RecipeType.RANDOM_COOKING, RecipeFactory::newRandomCookingRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.RANDOM_COOKING, RecipeFactory::newMultipleRandomCookingRecipe); - } - - if (RecipeManager.INSTANCE.supportPotionMix()) { - recipeRegistryProviderMap.put(RecipeType.POTION, RecipeFactory::newPotionMixRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.POTION, RecipeFactory::newMultiplePotionMixRecipe); - } - - if (PluginConfigs.ENABLE_ANVIL_RECIPE.value()) { - recipeRegistryProviderMap.put(RecipeType.ANVIL, RecipeFactory::newAnvilRecipe); - multipleRecipeRegistryProviderMap.put(RecipeType.ANVIL, RecipeFactory::newMultipleAnvilRecipe); - } - } - - public static List newRecipeRegistry(YamlConfiguration config, String key) { - key = key.toLowerCase(Locale.ROOT); - String recipeTypeStr = config.getString("type", "shaped"); - RecipeType recipeType = RecipeType.valueOf(recipeTypeStr.toUpperCase(Locale.ROOT)); - boolean multiple = config.getBoolean("multiple", false); - if (multiple) { - return multipleRecipeRegistryProviderMap.getOrDefault(recipeType, (c, k) -> { - throw new UnsupportedVersionException("Can not create " + recipeType.name().toLowerCase() + " recipe registry"); - }).apply(config, key); - } else { - return recipeRegistryProviderMap.getOrDefault(recipeType, (c, k) -> { - throw new UnsupportedVersionException("Can not create " + recipeType.name().toLowerCase() + " recipe registry"); - }).apply(config, key); - } - } - - public static List newShapedRecipe(YamlConfiguration config, String key) { - Map recipeChoiceMap = getShapedRecipeChoiceMap(config.getConfigurationSection("source")); - ItemStack result = getResultItem(config); - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - - List shapeStrList = config.getStringList("shape"); - if (shapeStrList.size() > 3) { - shapeStrList = shapeStrList.subList(0, 3); - } - String[] shape = new String[shapeStrList.size()]; - shape = shapeStrList.toArray(shape); - RecipeRegistry recipeRegistry = new ShapedRecipeRegistry(key, namespacedKey, result).setShape(shape).setRecipeChoiceMap(recipeChoiceMap); - return Collections.singletonList(recipeRegistry); - } - - public static List newMultipleShapedRecipe(YamlConfiguration config, String key) { - Map recipeChoiceMap = getShapedRecipeChoiceMap(config.getConfigurationSection("source")); - ItemStack result = getResultItem(config); - List shapeList = config.getList("shape", new ArrayList<>()); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < shapeList.size(); i++) { - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - List shapeStrList = (List) shapeList.get(i); - if (shapeStrList.size() > 3) { - shapeStrList = shapeStrList.subList(0, 3); - } - String[] shape = new String[shapeStrList.size()]; - shape = shapeStrList.toArray(shape); - recipeRegistries.add(new ShapedRecipeRegistry(key, namespacedKey, result).setShape(shape).setRecipeChoiceMap(recipeChoiceMap)); - } - return recipeRegistries; - } - - public static List newShapelessRecipe(YamlConfiguration config, String key) { - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - ItemStack result = getResultItem(config); - List itemStrList = config.getStringList("source"); - List recipeChoiceList = new ArrayList<>(); - for (String itemStr : itemStrList) { - recipeChoiceList.add(getRecipeChoice(itemStr)); - } - RecipeRegistry recipeRegistry = new ShapelessRecipeRegistry(key, namespacedKey, result).setChoiceList(recipeChoiceList); - return Collections.singletonList(recipeRegistry); - } - - public static List newMultipleShapelessRecipe(YamlConfiguration config, String key) { - ItemStack result = getResultItem(config); - List itemsList = config.getList("source", new ArrayList<>()); - List recipeRegistries = new ArrayList<>(); - - for (int i = 0; i < itemsList.size(); i++) { - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - List itemStrList = (List) itemsList.get(i); - List choiceList = new ArrayList<>(); - for (String itemStr : itemStrList) { - choiceList.add(getRecipeChoice(itemStr)); - } - recipeRegistries.add(new ShapelessRecipeRegistry(key, namespacedKey, result).setChoiceList(choiceList)); - } - return recipeRegistries; - } - - public static List newCookingRecipe(YamlConfiguration config, String key) { - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - ItemStack result = getResultItem(config); - String choiceStr = config.getString("source.item", ""); - String cookingBlock = config.getString("source.block", "furnace"); - RecipeChoice source = getRecipeChoice(choiceStr); - float exp = (float) config.getDouble("exp", 0); - int time = config.getInt("time", 200); - RecipeRegistry recipeRegistry = new CookingRecipeRegistry(key, namespacedKey, result).setCookingBlock(cookingBlock).setSource(source).setExp(exp).setTime(time); - return Collections.singletonList(recipeRegistry); - } - - public static List newMultipleCookingRecipe(YamlConfiguration config, String key) { - ItemStack result = getResultItem(config); - float globalExp = (float) config.getDouble("exp", 0); - int globalTime = config.getInt("time", 200); - List recipeRegistries = new ArrayList<>(); - - List> sourceList = config.getMapList("source"); - for (int i = 0; i < sourceList.size(); i++) { - Map map = sourceList.get(i); - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - RecipeChoice source = getRecipeChoice((String) map.get("item")); - String cookingBlock = (String) map.get("block"); - float exp = map.containsKey("exp") ? Float.parseFloat(String.valueOf(map.get("exp"))) : globalExp; - int time = map.containsKey("time") ? (Integer) map.get("time") : globalTime; - recipeRegistries.add(new CookingRecipeRegistry(key, namespacedKey, result).setCookingBlock(cookingBlock).setSource(source).setExp(exp).setTime(time)); - } - return recipeRegistries; - } - - - private static List newRandomCookingRecipe(YamlConfiguration config, String key) { - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - ItemStack result = getResultItem(config); - String choiceStr = config.getString("source.item", ""); - String cookingBlock = config.getString("source.block", "furnace"); - RecipeChoice source = getRecipeChoice(choiceStr); - float exp = (float) config.getDouble("exp", 0); - int time = config.getInt("time", 200); - RecipeRegistry recipeRegistry = new RandomCookingRecipeRegistry(key, namespacedKey, result).setCookingBlock(cookingBlock).setSource(source).setExp(exp).setTime(time); - return Collections.singletonList(recipeRegistry); - } - - private static List newMultipleRandomCookingRecipe(YamlConfiguration config, String key) { - ItemStack result = getResultItem(config); - float globalExp = (float) config.getDouble("exp", 0); - int globalTime = config.getInt("time", 200); - List recipeRegistries = new ArrayList<>(); - - List> sourceList = config.getMapList("source"); - for (int i = 0; i < sourceList.size(); i++) { - Map map = sourceList.get(i); - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - RecipeChoice source = getRecipeChoice((String) map.get("item")); - String cookingBlock = (String) map.get("block"); - float exp = map.containsKey("exp") ? Float.parseFloat(String.valueOf(map.get("exp"))) : globalExp; - int time = map.containsKey("time") ? (Integer) map.get("time") : globalTime; - recipeRegistries.add(new RandomCookingRecipeRegistry(key, namespacedKey, result).setCookingBlock(cookingBlock).setSource(source).setExp(exp).setTime(time)); - } - return recipeRegistries; - } - - public static List newSmithingRecipe(YamlConfiguration config, String key) { - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - ItemStack result = getResultItem(config); - RecipeChoice base = getRecipeChoice(config.getString("source.base", "")); - RecipeChoice addition = getRecipeChoice(config.getString("source.addition", "")); - RecipeRegistry recipeRegistry; - if (CrypticLib.minecraftVersion() >= 12000) { - RecipeChoice template = getRecipeChoice(config.getString("source.template", "")); - XSmithingRecipeRegistry.SmithingType type = XSmithingRecipeRegistry.SmithingType.valueOf(config.getString("source.type", "default").toUpperCase()); - recipeRegistry = new XSmithingRecipeRegistry(key, namespacedKey, result).setSmithingType(type).setTemplate(template).setBase(base).setAddition(addition); - } else { - recipeRegistry = new SmithingRecipeRegistry(key, namespacedKey, result).setBase(base).setAddition(addition); - } - - return Collections.singletonList(recipeRegistry); - } - - public static List newMultipleSmithingRecipe(YamlConfiguration config, String key) { - ItemStack result = getResultItem(config); - List> sourceList = config.getMapList("source"); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < sourceList.size(); i++) { - Map map = sourceList.get(i); - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - RecipeChoice base = getRecipeChoice((String) map.get("base")); - RecipeChoice addition = getRecipeChoice((String) map.get("addition")); - String typeStr = (String) map.get("type"); - if (typeStr == null) { - typeStr = "DEFAULT"; - } - if (CrypticLib.minecraftVersion() >= 12000) { - RecipeChoice template = getRecipeChoice((String) map.get("template")); - XSmithingRecipeRegistry.SmithingType type = XSmithingRecipeRegistry.SmithingType.valueOf(typeStr.toUpperCase()); - recipeRegistries.add(new XSmithingRecipeRegistry(key, namespacedKey, result).setSmithingType(type).setTemplate(template).setBase(base).setAddition(addition)); - } else { - recipeRegistries.add(new SmithingRecipeRegistry(key, namespacedKey, result).setBase(base).setAddition(addition)); - } - } - return recipeRegistries; - } - - public static List newStoneCuttingRecipe(YamlConfiguration config, String key) { - RecipeChoice choice = getRecipeChoice(config.getString("source", "")); - - if (config.isList("result")) { - List resultList = config.getStringList("result"); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < resultList.size(); i++) { - ItemStack result = ItemManager.INSTANCE.matchItem(resultList.get(i)); - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - recipeRegistries.add(new StoneCuttingRecipeRegistry(key, namespacedKey, result).setSource(choice));; - } - return recipeRegistries; - } else { - ItemStack result = getResultItem(config); - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - return Collections.singletonList(new StoneCuttingRecipeRegistry(key, namespacedKey, result).setSource(choice)); - } - } - - public static List newMultipleStoneCuttingRecipe(YamlConfiguration config, String key) { - if (config.isList("result")) { - List resultList = config.getStringList("result"); - List sourceList = config.getStringList("source"); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < resultList.size(); i++) { - ItemStack result = ItemManager.INSTANCE.matchItem(resultList.get(i)); - for (int j = 0; j < sourceList.size(); j++) { - String fullKey = String.format(key + ".%d.%d", i, j); - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - RecipeChoice source = getRecipeChoice(sourceList.get(j)); - recipeRegistries.add(new StoneCuttingRecipeRegistry(key, namespacedKey, result).setSource(source)); - } - } - return recipeRegistries; - } else { - ItemStack result = getResultItem(config); - List sourceList = config.getStringList("source"); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < sourceList.size(); i++) { - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - RecipeChoice source = getRecipeChoice(sourceList.get(i)); - recipeRegistries.add(new StoneCuttingRecipeRegistry(key, namespacedKey, result).setSource(source)); - } - return recipeRegistries; - } - } - - public static List newPotionMixRecipe(YamlConfiguration config, String key) { - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - ItemStack result = getResultItem(config); - RecipeChoice input = getRecipeChoice(config.getString("source.input", "")); - RecipeChoice ingredient = getRecipeChoice(config.getString("source.ingredient", "")); - return Collections.singletonList(new PotionMixRecipeRegistry(key, namespacedKey, result).setInput(input).setIngredient(ingredient)); - } - - public static List newMultiplePotionMixRecipe(YamlConfiguration config, String key) { - ItemStack result = getResultItem(config); - List> sourceList = config.getMapList("source"); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < sourceList.size(); i++) { - Map map = sourceList.get(i); - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - RecipeChoice input = getRecipeChoice((String) map.get("input")); - RecipeChoice ingredient = getRecipeChoice((String) map.get("ingredient")); - recipeRegistries.add(new PotionMixRecipeRegistry(key, namespacedKey, result).setInput(input).setIngredient(ingredient)); - } - return recipeRegistries; - } - - public static List newAnvilRecipe(YamlConfiguration config, String key) { - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), key); - ItemStack result = getResultItem(config); - ItemStack base = ItemManager.INSTANCE.matchItem(config.getString("source.base", "")); - ItemStack addition = ItemManager.INSTANCE.matchItem(config.getString("source.addition", "")); - int costLevel = config.getInt("source.cost_level", 0); - boolean copyNbt = config.getBoolean("source.copy_nbt", true); - RecipeRegistry recipeRegistry = new AnvilRecipeRegistry(key, namespacedKey, result).setBase(base).setAddition(addition).setCopyNbt(copyNbt).setCostLevel(costLevel); - return Collections.singletonList(recipeRegistry); - } - - public static List newMultipleAnvilRecipe(YamlConfiguration config, String key) { - ItemStack result = getResultItem(config); - List> sourceList = config.getMapList("source"); - List recipeRegistries = new ArrayList<>(); - for (int i = 0; i < sourceList.size(); i++) { - Map map = sourceList.get(i); - String fullKey = key + "." + i; - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), fullKey); - ItemStack base = ItemManager.INSTANCE.matchItem((String) map.get("base")); - ItemStack addition = ItemManager.INSTANCE.matchItem((String) map.get("addition")); - int costLevel = map.containsKey("cost_level") ? (Integer) map.get("cost_level") : 0; - boolean copyNbt = map.containsKey("copy_nbt") ? (Boolean) map.get("copy_nbt") : true; - recipeRegistries.add(new AnvilRecipeRegistry(key, namespacedKey, result).setBase(base).setAddition(addition).setCopyNbt(copyNbt).setCostLevel(costLevel)); - } - return recipeRegistries; - } - - private static Map getShapedRecipeChoiceMap(ConfigurationSection section) { - Map recipeChoiceMap = new HashMap<>(); - if (section == null) - return recipeChoiceMap; - for (String key : section.getKeys(false)) { - char keyWord = key.toCharArray()[0]; - String itemStr = section.getString(key, ""); - if (itemStr.isEmpty()) { - throw new IllegalArgumentException("Empty recipe ingredient: " + key); - } - recipeChoiceMap.put(keyWord, getRecipeChoice(itemStr)); - } - return recipeChoiceMap; - } - - private static ItemStack getResultItem(YamlConfiguration config) { - String resultStr; - if (config.getString("type", "shaped").equals("random_cooking")) { - String tmpStr = config.getStringList("result").get(0); - tmpStr = tmpStr.substring(0, tmpStr.lastIndexOf(" ")); - resultStr = tmpStr; - } - else { - resultStr = config.getString("result", ""); - } - - if (resultStr.isEmpty()) { - return null; - } - return ItemManager.INSTANCE.matchItem(resultStr); - } - - public static RecipeChoice getRecipeChoice(String itemStr) { - if (!itemStr.contains(":")) { - Material material = Material.matchMaterial(itemStr); - if (material == null) { - throw new IllegalArgumentException(itemStr + " is a not exist item type"); - } - return new RecipeChoice.MaterialChoice(material); - } - int index = itemStr.indexOf(":"); - String namespace = itemStr.substring(0, index); - namespace = namespace.toLowerCase(); - switch (namespace) { - case "minecraft": - Material material = Material.matchMaterial(itemStr); - if (material == null) { - throw new IllegalArgumentException(itemStr + " is a not exist item type"); - } - return new RecipeChoice.MaterialChoice(material); - case "tag": - String tagStr = itemStr.substring(4).toUpperCase(Locale.ROOT); - Tag materialTag; - try { - Field field = Tag.class.getField(tagStr); - materialTag = (Tag) field.get(null); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - return new RecipeChoice.MaterialChoice(materialTag); - default: - ItemStack item = ItemManager.INSTANCE.matchItem(itemStr); - return new RecipeChoice.ExactChoice(item); - } - } - -} diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java index 1a2c4770..634d964f 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java @@ -1,41 +1,26 @@ package com.github.yufiriamazenta.craftorithm.recipe; -import com.github.yufiriamazenta.craftorithm.config.PluginConfigs; import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry; import crypticlib.config.ConfigWrapper; import org.bukkit.NamespacedKey; -import org.bukkit.inventory.Recipe; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArrayList; -//TODO 重构RecipeGroup public class RecipeGroup { - private String groupName; - private List groupRecipeKeys = new CopyOnWriteArrayList<>(); - private Map groupRegistrys = new ConcurrentHashMap<>(); - private final RecipeType recipeType; - private ConfigWrapper recipeGroupConfig; - private int sortId; - private boolean unlock; + protected String groupName; + protected Map groupRecipeKeyMap = new ConcurrentHashMap<>(); + protected Map groupRecipeRegistryMap = new ConcurrentHashMap<>(); + protected ConfigWrapper recipeGroupConfig; + protected int sortId; - public RecipeGroup(@NotNull String groupName, @NotNull RecipeType recipeType, @NotNull ConfigWrapper recipeGroupConfig) { - this(groupName, new ArrayList<>(), recipeType, recipeGroupConfig); - } - - public RecipeGroup(@NotNull String groupName, @NotNull List groupRecipeKeys, @NotNull RecipeType recipeType, @NotNull ConfigWrapper recipeGroupConfig) { + public RecipeGroup(@NotNull String groupName, @NotNull ConfigWrapper recipeGroupConfig, int sortId) { this.groupName = groupName; - this.groupRecipeKeys.addAll(groupRecipeKeys); - this.recipeType = recipeType; this.recipeGroupConfig = recipeGroupConfig; - this.sortId = recipeGroupConfig.config().getInt("sort_id", 0); - this.unlock = recipeGroupConfig.config().getBoolean("unlock", PluginConfigs.DEFAULT_RECIPE_UNLOCK.value()); + this.sortId = sortId; } public String groupName() { @@ -47,59 +32,72 @@ public RecipeGroup setGroupName(String groupName) { return this; } - public List groupRecipeKeys() { - return groupRecipeKeys; + public int sortId() { + return sortId; } - public RecipeGroup setGroupRecipeKeys(List groupRecipeKeys) { - this.groupRecipeKeys = groupRecipeKeys; + public RecipeGroup setSortId(int sortId) { + this.sortId = sortId; return this; } - public boolean contains(NamespacedKey namespacedKey) { - return groupRecipeKeys.contains(namespacedKey); + public @NotNull ConfigWrapper recipeGroupConfig() { + return recipeGroupConfig; } - public RecipeGroup addRecipeKey(NamespacedKey namespacedKey) { - if (groupRecipeKeys.contains(namespacedKey)) - return this; - groupRecipeKeys.add(namespacedKey); + public RecipeGroup setRecipeGroupConfig(ConfigWrapper recipeGroupConfig) { + this.recipeGroupConfig = recipeGroupConfig; return this; } - public boolean isEmpty() { - return groupRecipeKeys.isEmpty(); + public Map groupRecipeKeyMap() { + return groupRecipeKeyMap; } - public RecipeType recipeType() { - return recipeType; + public Map groupRecipeRegistryMap() { + return groupRecipeRegistryMap; } - public int sortId() { - return sortId; + public RecipeGroup addRecipeRegistry(String recipeName, RecipeRegistry recipeRegistry) { + if (!recipeRegistry.group().equals(groupName)) + throw new IllegalArgumentException( + "Cannot add recipe " + recipeName + " to group " + groupName + " because its group is " + recipeRegistry.group() + ); + groupRecipeKeyMap.put(recipeName, recipeRegistry.namespacedKey()); + groupRecipeRegistryMap.put(recipeRegistry.namespacedKey(), recipeRegistry); + return this; } - public RecipeGroup setSortId(int sortId) { - this.sortId = sortId; + public RecipeGroup removeRecipeRegistry(String recipeName) { + NamespacedKey recipeKey = groupRecipeKeyMap.get(recipeName); + if (recipeKey == null) { + return this; + } + groupRecipeRegistryMap.remove(recipeKey); + groupRecipeKeyMap.remove(recipeName); return this; } - public boolean unlock() { - return unlock; + public @Nullable RecipeRegistry getRecipeRegistry(@NotNull String recipeName) { + NamespacedKey recipeKey = groupRecipeKeyMap.get(recipeName); + if (recipeKey == null) { + return null; + } + return getRecipeRegistry(recipeKey); } - public RecipeGroup setUnlock(boolean unlock) { - this.unlock = unlock; - return this; + public @Nullable RecipeRegistry getRecipeRegistry(@NotNull NamespacedKey recipeKey) { + return groupRecipeRegistryMap.get(recipeKey); } - public @NotNull ConfigWrapper recipeGroupConfig() { - return recipeGroupConfig; + public void register() { + for (RecipeRegistry registry : groupRecipeRegistryMap.values()) { + registry.register(); + } } - public RecipeGroup setRecipeGroupConfig(ConfigWrapper recipeGroupConfig) { - this.recipeGroupConfig = recipeGroupConfig; - return this; + public void unregister(boolean deleteFile) { + RecipeManager.INSTANCE.removeCraftorithmRecipe(groupName, deleteFile); } } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroupLoader.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroupLoader.java new file mode 100644 index 00000000..5e55625e --- /dev/null +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroupLoader.java @@ -0,0 +1,296 @@ +package com.github.yufiriamazenta.craftorithm.recipe; + +import com.github.yufiriamazenta.craftorithm.Craftorithm; +import com.github.yufiriamazenta.craftorithm.config.PluginConfigs; +import com.github.yufiriamazenta.craftorithm.item.ItemManager; +import com.github.yufiriamazenta.craftorithm.recipe.registry.*; +import crypticlib.CrypticLib; +import crypticlib.config.ConfigWrapper; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Tag; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.RecipeChoice; + +import java.lang.reflect.Field; +import java.util.*; +import java.util.function.BiFunction; + +public class RecipeGroupLoader { + + public static final String TYPE_KEY = "type", RESULT_KEY = "result", SORT_ID = "sort_id"; + public static final List GLOBAL_KEYS = Arrays.asList(TYPE_KEY, RESULT_KEY, SORT_ID); + public final Map> RECIPE_REGISTRY_LOAD_MAP = new HashMap<>(); + protected ConfigWrapper configWrapper; + protected RecipeType globalType; + protected ItemStack globalResult; + protected int groupSortId; + protected String groupName; + + public RecipeGroupLoader(String groupName, ConfigWrapper configWrapper) { + this.configWrapper = configWrapper; + this.groupName = groupName; + String globalTypeStr = configWrapper.config().getString(TYPE_KEY); + if (globalTypeStr != null) + this.globalType = RecipeType.valueOf(globalTypeStr.toUpperCase()); + String globalResultStr = configWrapper.config().getString(RESULT_KEY); + if (globalResultStr != null) + this.globalResult = ItemManager.INSTANCE.matchItem(globalResultStr); + groupSortId = configWrapper.config().getInt(SORT_ID, 0); + loadDefRecipeLoadMap(); + } + + public RecipeGroup load() { + YamlConfiguration config = configWrapper.config(); + List recipeKeys = new ArrayList<>(config.getKeys(false)); + recipeKeys.removeAll(GLOBAL_KEYS); + RecipeGroup recipeGroup = new RecipeGroup(groupName, configWrapper, groupSortId); + for (String subName : recipeKeys) { + if (!config.isConfigurationSection(subName)) + continue; + ConfigurationSection recipeCfgSection = config.getConfigurationSection(subName); + RecipeType recipeType; + String recipeTypeStr = Objects.requireNonNull(recipeCfgSection).getString(TYPE_KEY); + if (recipeTypeStr == null) + recipeType = globalType; + else + recipeType = RecipeType.valueOf(recipeTypeStr.toUpperCase()); + //TODO 提醒使用者,此类型不可用 + RecipeRegistry recipeRegistry = RECIPE_REGISTRY_LOAD_MAP.getOrDefault(recipeType, (a, b) -> null).apply(subName, config); + if (recipeRegistry != null) + recipeGroup.addRecipeRegistry(subName, recipeRegistry); + } + return recipeGroup; + } + + private void loadDefRecipeLoadMap() { + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.SHAPED, this::loadShapedRegistry); + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.SHAPELESS, this::loadShapelessRegistry); + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.ANVIL, this::loadAnvilRegistry); + + if (CrypticLib.minecraftVersion() >= 11400) { + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.COOKING, this::loadCookingRegistry); + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.SMITHING, this::loadSmithingRegistry); + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.STONE_CUTTING, this::loadStonecuttingRegistry); + } + + if (CrypticLib.minecraftVersion() >= 11700) { + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.RANDOM_COOKING, this::loadRandomCookingRegistry); + } + + if (RecipeManager.INSTANCE.supportPotionMix()) { + RECIPE_REGISTRY_LOAD_MAP.put(RecipeType.POTION, this::loadPotionRegistry); + } + } + + protected RecipeRegistry loadShapedRegistry(String recipeName, ConfigurationSection configSection) { + ItemStack result = matchResult(configSection); + List shapeList = configSection.getStringList("source.shape"); + Map ingredientMap = new HashMap<>(); + ConfigurationSection ingredientCfgSection = configSection.getConfigurationSection("source.ingredients"); + if (ingredientCfgSection == null) + throw new IllegalArgumentException("Ingredient map cannot be null"); + for (String key : ingredientCfgSection.getKeys(false)) { + String ingredientStr = ingredientCfgSection.getString(key); + if (ingredientStr == null) + ingredientStr = Material.AIR.getKey().toString(); + ingredientStr = ingredientStr.split(" ")[0]; + RecipeChoice recipeChoice = matchRecipeChoice(ingredientStr); + ingredientMap.put(key.charAt(0), recipeChoice); + } + String[] shape = new String[shapeList.size()]; + return new ShapedRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setShape(shapeList.toArray(shape)) + .setRecipeChoiceMap(ingredientMap) + .setUnlock(matchUnlock(configSection)); + } + + protected RecipeRegistry loadShapelessRegistry(String recipeName, ConfigurationSection configSection) { + ItemStack result = matchResult(configSection); + List ingredientList = configSection.getStringList("source.ingredients"); + List recipeChoiceList = new ArrayList<>(); + for (String ingredient : ingredientList) { + RecipeChoice recipeChoice = matchRecipeChoice(ingredient); + recipeChoiceList.add(recipeChoice); + } + return new ShapelessRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setChoiceList(recipeChoiceList) + .setUnlock(matchUnlock(configSection)); + } + + protected RecipeRegistry loadCookingRegistry(String recipeName, ConfigurationSection configSection) { + ItemStack result = matchResult(configSection); + String choiceStr = Objects.requireNonNull(configSection.getString("source.ingredient")); + RecipeChoice ingredient = matchRecipeChoice(choiceStr); + String blockStr = configSection.getString("source.block", "furnace"); + float exp = (float) configSection.getDouble("source.exp", 0); + int time = configSection.getInt("source.time", 100); + return new CookingRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setCookingBlock(blockStr) + .setExp(exp) + .setTime(time) + .setIngredient(ingredient) + .setUnlock(matchUnlock(configSection)); + } + + protected RecipeRegistry loadSmithingRegistry(String recipeName, ConfigurationSection configSection) { + ItemStack result = matchResult(configSection); + String baseStr = Objects.requireNonNull(configSection.getString("source.base")); + RecipeChoice base = matchRecipeChoice(baseStr); + String additionStr = Objects.requireNonNull(configSection.getString("source.addition")); + RecipeChoice addition = matchRecipeChoice(additionStr); + String typeStr = configSection.getString("source.type", "transform"); + XSmithingRecipeRegistry.SmithingType smithingType = XSmithingRecipeRegistry.SmithingType.valueOf(typeStr.toUpperCase()); + boolean copyNbt = configSection.getBoolean("source.copy_nbt", true); + if (CrypticLib.minecraftVersion() >= 12000) { + String templateStr = Objects.requireNonNull(configSection.getString("source.template")); + RecipeChoice template = matchRecipeChoice(templateStr); + return new XSmithingRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setBase(base) + .setAddition(addition) + .setTemplate(template) + .setSmithingType(smithingType) + .setCopyNbt(copyNbt) + .setUnlock(matchUnlock(configSection)); + } else { + return new SmithingRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setBase(base) + .setAddition(addition) + .setCopyNbt(copyNbt) + .setUnlock(matchUnlock(configSection)); + } + } + + protected RecipeRegistry loadStonecuttingRegistry(String recipeName, ConfigurationSection configSection) { + List results = new ArrayList<>(); + if (!configSection.contains("result")) { + results.add(globalResult); + } else if (configSection.isList("result")) { + for (String resultStr : configSection.getStringList("result")) { + results.add(ItemManager.INSTANCE.matchItem(resultStr)); + } + } else { + results.add(ItemManager.INSTANCE.matchItem(Objects.requireNonNull(configSection.getString("result")))); + } + List ingredients = new ArrayList<>(); + if (configSection.isList("source.ingredients")) { + for (String ingredientStr : configSection.getStringList("source.ingredients")) { + ingredients.add(matchRecipeChoice(ingredientStr)); + } + } + return new StoneCuttingRecipeRegistry(groupName, generateRecipeKey(recipeName), results) + .setIngredientList(ingredients) + .setUnlock(matchUnlock(configSection)); + } + + protected RecipeRegistry loadRandomCookingRegistry(String recipeName, ConfigurationSection configSection) { + List resultStrList = configSection.getStringList("result"); + List randomResults = getRandomResults(resultStrList); + ItemStack result = randomResults.get(0).result(); + String choiceStr = Objects.requireNonNull(configSection.getString("source.ingredient")); + RecipeChoice ingredient = matchRecipeChoice(choiceStr); + String blockStr = configSection.getString("source.block", "furnace"); + float exp = (float) configSection.getDouble("source.exp", 0); + int time = configSection.getInt("source.time", 100); + return new RandomCookingRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setResults(randomResults) + .setExp(exp) + .setTime(time) + .setCookingBlock(blockStr) + .setIngredient(ingredient) + .setUnlock(matchUnlock(configSection)); + } + + protected RecipeRegistry loadPotionRegistry(String recipeName, ConfigurationSection configSection) { + ItemStack result = matchResult(configSection); + RecipeChoice input = matchRecipeChoice(Objects.requireNonNull(configSection.getString("source.input"))); + RecipeChoice ingredient = matchRecipeChoice(Objects.requireNonNull(configSection.getString("source.ingredient"))); + return new PotionMixRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setInput(input) + .setIngredient(ingredient); + } + + protected RecipeRegistry loadAnvilRegistry(String recipeName, ConfigurationSection configSection) { + if (PluginConfigs.ENABLE_ANVIL_RECIPE.value()) + return null; + ItemStack result = matchResult(configSection); + ItemStack base = ItemManager.INSTANCE.matchItem(Objects.requireNonNull(configSection.getString("source.base"))); + ItemStack addition = ItemManager.INSTANCE.matchItem(Objects.requireNonNull(configSection.getString("source.addition"))); + int costLevel = configSection.getInt("source.cost_level", 0); + boolean copyNbt = configSection.getBoolean("source.copy_nbt", true); + return new AnvilRecipeRegistry(groupName, generateRecipeKey(recipeName), result) + .setBase(base) + .setAddition(addition) + .setCopyNbt(copyNbt) + .setCostLevel(costLevel); + } + + + private List getRandomResults(List resultStr) { + List resultWeightList = new ArrayList<>(); + int sum = 0; + for (String result : resultStr) { + String item = result.substring(0, result.lastIndexOf(" ")); + int weight = Integer.parseInt(result.substring(result.lastIndexOf(" ") + 1)); + ItemStack itemStack = ItemManager.INSTANCE.matchItem(item); + sum += weight; + resultWeightList.add(new RandomCookingRecipeRegistry.RandomCookingResult(itemStack, sum)); + } + return resultWeightList; + } + + public NamespacedKey generateRecipeKey(String recipeName) { + return new NamespacedKey(Craftorithm.instance(), groupName + "." + recipeName); + } + + public boolean matchUnlock(ConfigurationSection configSection) { + return configSection.contains("unlock") ? configSection.getBoolean("unlock") : PluginConfigs.DEFAULT_RECIPE_UNLOCK.value(); + } + + public ItemStack matchResult(ConfigurationSection configSection) { + ItemStack result; + String resultStr = configSection.getString(RESULT_KEY); + if (resultStr == null) + result = globalResult; + else + result = ItemManager.INSTANCE.matchItem(resultStr); + return result; + } + + public RecipeChoice matchRecipeChoice(String ingredientName) { + if (!ingredientName.contains(":")) { + Material material = Material.matchMaterial(ingredientName); + if (material == null) { + throw new IllegalArgumentException(ingredientName + " is a not exist item type"); + } + return new RecipeChoice.MaterialChoice(material); + } + int index = ingredientName.indexOf(":"); + String namespace = ingredientName.substring(0, index); + namespace = namespace.toLowerCase(); + switch (namespace) { + case "minecraft": + Material material = Material.matchMaterial(ingredientName); + if (material == null) { + throw new IllegalArgumentException(ingredientName + " is a not exist item type"); + } + return new RecipeChoice.MaterialChoice(material); + case "tag": + String tagStr = ingredientName.substring(4).toUpperCase(Locale.ROOT); + Tag materialTag; + try { + Field field = Tag.class.getField(tagStr); + materialTag = (Tag) field.get(null); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + return new RecipeChoice.MaterialChoice(materialTag); + default: + ItemStack item = ItemManager.INSTANCE.matchItem(ingredientName); + return new RecipeChoice.ExactChoice(item); + } + } + +} diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeManager.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeManager.java index 1c207080..90bea98c 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeManager.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeManager.java @@ -30,10 +30,11 @@ public enum RecipeManager { INSTANCE; - public final File RECIPE_FILE_FOLDER = new File(Craftorithm.instance().getDataFolder().getPath(), "recipes"); + public final File RECIPE_FILE_FOLDER = new File(Craftorithm.instance().getDataFolder().getPath(), "recipes_groups"); private final ConfigWrapper removedRecipesConfigWrapper = new ConfigWrapper(Craftorithm.instance(), "removed_recipes.yml"); public final String PLUGIN_RECIPE_NAMESPACE = "craftorithm"; - private final Map> pluginRecipeMap; + + private final Map recipeGroupMap; private final Map> recipeRegisterMap; private final Map>> recipeRemoverMap; private final Map recipeUnlockMap; @@ -53,7 +54,7 @@ public enum RecipeManager { private boolean supportPotionMix; RecipeManager() { - pluginRecipeMap = new ConcurrentHashMap<>(); + recipeGroupMap = new ConcurrentHashMap<>(); recipeRegisterMap = new ConcurrentHashMap<>(); recipeRemoverMap = new ConcurrentHashMap<>(); recipeUnlockMap = new ConcurrentHashMap<>(); @@ -119,6 +120,7 @@ public void reloadRecipeManager() { } private void loadRecipes() { + for (Map.Entry> pluginRecipeMapEntry : pluginRecipeMap.entrySet()) { Map recipeGroupMap = pluginRecipeMapEntry.getValue(); recipeGroupMap.forEach((recipeGroupName, recipeGroup) -> loadRecipeGroup(recipeGroup)); @@ -143,20 +145,12 @@ public void loadRecipeGroup(RecipeGroup recipeGroup) { } } - public void addRecipeGroup(RecipeGroup recipeGroup) { - RecipeType recipeType = recipeGroup.recipeType(); - if (pluginRecipeMap.containsKey(recipeType)) { - Map recipeGroupMap = pluginRecipeMap.get(recipeType); - recipeGroupMap.put(recipeGroup.groupName(), recipeGroup); - } else { - Map recipeGroupMap = new ConcurrentHashMap<>(); - recipeGroupMap.put(recipeGroup.groupName(), recipeGroup); - pluginRecipeMap.put(recipeType, recipeGroupMap); - } + public RecipeGroup addRecipeGroup(RecipeGroup recipeGroup) { + return recipeGroupMap.put(recipeGroup.groupName(), recipeGroup); } private void loadRecipeGroups() { - pluginRecipeMap.clear(); + recipeGroupMap.clear(); if (!RECIPE_FILE_FOLDER.exists()) { boolean mkdirResult = RECIPE_FILE_FOLDER.mkdir(); if (!mkdirResult) @@ -172,20 +166,21 @@ private void loadRecipeGroups() { int lastDotIndex = recipeGroupName.lastIndexOf("."); recipeGroupName = recipeGroupName.substring(0, lastDotIndex); ConfigWrapper recipeGroupConfigWrapper = new ConfigWrapper(file); - RecipeType recipeType = RecipeType.valueOf(recipeGroupConfigWrapper.config().getString("type").toUpperCase()); - RecipeGroup recipeGroup = new RecipeGroup(recipeGroupName, recipeType, recipeGroupConfigWrapper); + RecipeGroup recipeGroup = new RecipeGroupLoader(recipeGroupName, recipeGroupConfigWrapper).load(); addRecipeGroup(recipeGroup); } } + /** + * 内部使用的注册方法,一般情况下请勿使用 + */ + @Deprecated public void regRecipe(String recipeGroupName, Recipe recipe, RecipeType recipeType) { if (!pluginRecipeMap.containsKey(recipeType)) pluginRecipeMap.put(recipeType, new ConcurrentHashMap<>()); Map recipeGroupMap = pluginRecipeMap.get(recipeType); if (!recipeGroupMap.containsKey(recipeGroupName)) throw new IllegalArgumentException("Can not find recipe group " + recipeGroupName + ", use addRecipeGroup() method to add recipe group."); - RecipeGroup recipeGroup = recipeGroupMap.get(recipeGroupName); - recipeGroup.addRecipeKey(getRecipeKey(recipe)); recipeRegisterMap.getOrDefault(recipeType, recipe1 -> { throw new UnsupportedVersionException("Can not register " + recipeType.name().toLowerCase() + " recipe"); }).accept(recipe); @@ -482,30 +477,8 @@ public boolean supportPotionMix() { private void saveDefConfigFile(List allFiles) { if (!PluginConfigs.RELEASE_DEFAULT_RECIPES.value()) return; - Craftorithm.instance().saveResource("recipes/example_shaped.yml", false); - Craftorithm.instance().saveResource("recipes/example_shapeless.yml", false); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_shaped.yml")); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_shapeless.yml")); - if (CrypticLib.minecraftVersion() >= 11400) { - Craftorithm.instance().saveResource("recipes/example_smithing.yml", false); - Craftorithm.instance().saveResource("recipes/example_stone_cutting.yml", false); - Craftorithm.instance().saveResource("recipes/example_cooking.yml", false); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_cooking.yml")); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_smithing.yml")); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_stone_cutting.yml")); - } - if (CrypticLib.minecraftVersion() >= 11700) { - Craftorithm.instance().saveResource("recipes/example_random_cooking.yml", false); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_random_cooking.yml")); - } - if (supportPotionMix()) { - Craftorithm.instance().saveResource("recipes/example_potion.yml", false); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_potion.yml")); - } - if (PluginConfigs.ENABLE_ANVIL_RECIPE.value()) { - Craftorithm.instance().saveResource("recipes/example_anvil.yml", false); - allFiles.add(new File(RECIPE_FILE_FOLDER, "example_anvil.yml")); - } + Craftorithm.instance().saveResource("recipe_groups/example.yml", false); + allFiles.add(new File(RECIPE_FILE_FOLDER, "example.yml")); } } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/loader/RecipeGroupLoader.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/loader/RecipeGroupLoader.java deleted file mode 100644 index 6131ace9..00000000 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/loader/RecipeGroupLoader.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.github.yufiriamazenta.craftorithm.recipe.loader; - -import com.github.yufiriamazenta.craftorithm.Craftorithm; -import com.github.yufiriamazenta.craftorithm.item.ItemManager; -import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup; -import com.github.yufiriamazenta.craftorithm.recipe.RecipeType; -import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry; -import com.github.yufiriamazenta.craftorithm.recipe.registry.ShapedRecipeRegistry; -import crypticlib.config.ConfigWrapper; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.Tag; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.RecipeChoice; - -import java.lang.reflect.Field; -import java.util.*; -import java.util.function.BiFunction; -import java.util.function.Function; - -public class RecipeGroupLoader { - - public static final String TYPE_KEY = "type", RESULT_KEY = "result"; - public static final List GLOBAL_KEYS = Arrays.asList(TYPE_KEY, RESULT_KEY); - public final Map> RECIPE_LOAD_MAP = new HashMap<>(); - protected ConfigWrapper configWrapper; - protected RecipeType globalType; - protected ItemStack globalResult; - protected String groupName; - - public RecipeGroupLoader(String groupName, ConfigWrapper configWrapper) { - this.configWrapper = configWrapper; - this.groupName = groupName; - String globalTypeStr = configWrapper.config().getString(TYPE_KEY); - if (globalTypeStr != null) - this.globalType = RecipeType.valueOf(globalTypeStr.toUpperCase()); - String globalResultStr = configWrapper.config().getString(RESULT_KEY); - if (globalResultStr != null) - this.globalResult = ItemManager.INSTANCE.matchItem(globalResultStr); - loadDefRecipeLoadMap(); - } - - public RecipeGroup load() { - YamlConfiguration config = configWrapper.config(); - List recipeKeys = new ArrayList<>(config.getKeys(false)); - recipeKeys.removeAll(GLOBAL_KEYS); - for (String recipeKey : recipeKeys) { - if (!config.isConfigurationSection(recipeKey)) - continue; - ConfigurationSection recipeCfgSection = config.getConfigurationSection(recipeKey); - RecipeType recipeType; - String recipeTypeStr = recipeCfgSection.getString(TYPE_KEY); - if (recipeTypeStr == null) - recipeType = globalType; - else - recipeType = RecipeType.valueOf(recipeTypeStr.toUpperCase()); - //TODO 加载配方注册器 - } - return null; - } - - private void loadDefRecipeLoadMap() { - RECIPE_LOAD_MAP.put(RecipeType.SHAPED, (subName, configSection) -> { - ItemStack result; - String resultStr = configSection.getString(RESULT_KEY); - if (resultStr == null) - result = globalResult; - else - result = ItemManager.INSTANCE.matchItem(resultStr); - List shapeList = configSection.getStringList("source.shape"); - Map ingredientMap = new HashMap<>(); - ConfigurationSection ingredientCfgSection = configSection.getConfigurationSection("source.ingredient_map"); - if (ingredientCfgSection == null) - throw new IllegalArgumentException("Ingredient map cannot be null"); - for (String key : ingredientCfgSection.getKeys(false)) { - String ingredientStr = ingredientCfgSection.getString(key); - if (ingredientStr == null) - ingredientStr = Material.AIR.getKey().toString(); - ingredientStr = ingredientStr.split(" ")[0]; - RecipeChoice recipeChoice = matchRecipeChoice(ingredientStr); - ingredientMap.put(key.charAt(0), recipeChoice); - } - NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), groupName + "." + subName); - String[] shape = new String[shapeList.size()]; - return new ShapedRecipeRegistry(groupName, namespacedKey, result) - .setShape(shapeList.toArray(shape)) - .setRecipeChoiceMap(ingredientMap); - }); - } - - public RecipeChoice matchRecipeChoice(String ingredientName) { - if (!ingredientName.contains(":")) { - Material material = Material.matchMaterial(ingredientName); - if (material == null) { - throw new IllegalArgumentException(ingredientName + " is a not exist item type"); - } - return new RecipeChoice.MaterialChoice(material); - } - int index = ingredientName.indexOf(":"); - String namespace = ingredientName.substring(0, index); - namespace = namespace.toLowerCase(); - switch (namespace) { - case "minecraft": - Material material = Material.matchMaterial(ingredientName); - if (material == null) { - throw new IllegalArgumentException(ingredientName + " is a not exist item type"); - } - return new RecipeChoice.MaterialChoice(material); - case "tag": - String tagStr = ingredientName.substring(4).toUpperCase(Locale.ROOT); - Tag materialTag; - try { - Field field = Tag.class.getField(tagStr); - materialTag = (Tag) field.get(null); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - return new RecipeChoice.MaterialChoice(materialTag); - default: - ItemStack item = ItemManager.INSTANCE.matchItem(ingredientName); - return new RecipeChoice.ExactChoice(item); - } - } - -} diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/AnvilRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/AnvilRecipeRegistry.java index 4fa88fd6..f592050c 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/AnvilRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/AnvilRecipeRegistry.java @@ -14,9 +14,9 @@ public class AnvilRecipeRegistry extends RecipeRegistry { - private ItemStack base, addition; - private boolean copyNbt; - private int costLevel; + protected ItemStack base, addition; + protected boolean copyNbt; + protected int costLevel; public AnvilRecipeRegistry(@Nullable String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) { super(group, namespacedKey, result); @@ -28,12 +28,12 @@ public AnvilRecipeRegistry(@Nullable String group, @NotNull NamespacedKey namesp public void register() { Preconditions.checkArgument(!ItemUtil.isAir(base), "Recipe base cannot be null"); Preconditions.checkArgument(!ItemUtil.isAir(addition), "Recipe base cannot be null"); - Preconditions.checkArgument(!ItemUtil.isAir(result()), "Recipe base cannot be null"); - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - AnvilRecipe anvilRecipe = new AnvilRecipe(namespacedKey(), result(), base, addition); + Preconditions.checkArgument(!ItemUtil.isAir(result), "Recipe base cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + AnvilRecipe anvilRecipe = new AnvilRecipe(namespacedKey, result, base, addition); anvilRecipe.setCopyNbt(copyNbt); anvilRecipe.setCostLevel(costLevel); - RecipeManager.INSTANCE.regRecipe(group(), anvilRecipe, RecipeType.ANVIL); + RecipeManager.INSTANCE.regRecipe(group, anvilRecipe, RecipeType.ANVIL); } public ItemStack base() { diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/CookingRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/CookingRecipeRegistry.java index e3699376..73ec094c 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/CookingRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/CookingRecipeRegistry.java @@ -9,12 +9,12 @@ import java.util.Objects; -public class CookingRecipeRegistry extends RecipeRegistry { +public class CookingRecipeRegistry extends UnlockableRecipeRegistry { - private RecipeChoice source; - private int time; - private float exp; - private CookingBlock cookingBlock; + protected RecipeChoice source; + protected int time; + protected float exp; + protected CookingBlock cookingBlock; public CookingRecipeRegistry(@NotNull String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) { @@ -25,34 +25,40 @@ public CookingRecipeRegistry(@NotNull String group, @NotNull NamespacedKey names @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe key cannot be null"); + CookingRecipe cookingRecipe = generateCookingRecipe(); + RecipeManager.INSTANCE.regRecipe(group, cookingRecipe, RecipeType.COOKING); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); + } + + protected CookingRecipe generateCookingRecipe() { + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(result, "Recipe key cannot be null"); Objects.requireNonNull(source, "Recipe ingredient cannot be null"); CookingRecipe cookingRecipe; switch (cookingBlock) { case FURNACE: default: - cookingRecipe = new FurnaceRecipe(namespacedKey(), result(), source, exp, time); + cookingRecipe = new FurnaceRecipe(namespacedKey, result, source, exp, time); break; case SMOKER: - cookingRecipe = new SmokingRecipe(namespacedKey(), result(), source, exp, time); + cookingRecipe = new SmokingRecipe(namespacedKey, result, source, exp, time); break; case BLAST_FURNACE: - cookingRecipe = new BlastingRecipe(namespacedKey(), result(), source, exp, time); + cookingRecipe = new BlastingRecipe(namespacedKey, result, source, exp, time); break; case CAMPFIRE: - cookingRecipe = new CampfireRecipe(namespacedKey(), result(), source, exp, time); + cookingRecipe = new CampfireRecipe(namespacedKey, result, source, exp, time); break; } - cookingRecipe.setGroup(group()); - RecipeManager.INSTANCE.regRecipe(group(), cookingRecipe, RecipeType.COOKING); + cookingRecipe.setGroup(group); + return cookingRecipe; } public RecipeChoice source() { return source; } - public CookingRecipeRegistry setSource(RecipeChoice source) { + public CookingRecipeRegistry setIngredient(RecipeChoice source) { this.source = source; return this; } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/PotionMixRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/PotionMixRecipeRegistry.java index 55a2a2c5..b8e36303 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/PotionMixRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/PotionMixRecipeRegistry.java @@ -39,12 +39,12 @@ public PotionMixRecipeRegistry setIngredient(RecipeChoice ingredient) { @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe result cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(result, "Recipe result cannot be null"); Objects.requireNonNull(ingredient, "Recipe ingredient cannot be null"); Objects.requireNonNull(input, "Recipe input cannot be null"); - PotionMixRecipe potionMixRecipe = new PotionMixRecipe(new PotionMix(namespacedKey(), result(), input, ingredient)); - RecipeManager.INSTANCE.regRecipe(group(), potionMixRecipe, RecipeType.POTION); + PotionMixRecipe potionMixRecipe = new PotionMixRecipe(new PotionMix(namespacedKey, result, input, ingredient)); + RecipeManager.INSTANCE.regRecipe(group, potionMixRecipe, RecipeType.POTION); } } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RandomCookingRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RandomCookingRecipeRegistry.java index 8a819a16..3dfcf07f 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RandomCookingRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RandomCookingRecipeRegistry.java @@ -6,37 +6,60 @@ import org.bukkit.inventory.*; import org.jetbrains.annotations.NotNull; +import java.util.List; import java.util.Objects; public class RandomCookingRecipeRegistry extends CookingRecipeRegistry { + private List results; + //TODO 存抽奖结果 public RandomCookingRecipeRegistry(@NotNull String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) { super(group, namespacedKey, result); } @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe key cannot be null"); - Objects.requireNonNull(source(), "Recipe ingredient cannot be null"); - CookingRecipe cookingRecipe; - switch (cookingBlock()) { - case FURNACE: - default: - cookingRecipe = new FurnaceRecipe(namespacedKey(), result(), source(), exp(), time()); - break; - case SMOKER: - cookingRecipe = new SmokingRecipe(namespacedKey(), result(), source(), exp(), time()); - break; - case BLAST_FURNACE: - cookingRecipe = new BlastingRecipe(namespacedKey(), result(), source(), exp(), time()); - break; - case CAMPFIRE: - cookingRecipe = new CampfireRecipe(namespacedKey(), result(), source(), exp(), time()); - break; + CookingRecipe cookingRecipe = generateCookingRecipe(); + RecipeManager.INSTANCE.regRecipe(group, cookingRecipe, RecipeType.RANDOM_COOKING); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); + } + + public List results() { + return results; + } + + public RandomCookingRecipeRegistry setResults(List results) { + this.results = results; + return this; + } + + public static class RandomCookingResult { + private ItemStack result; + private int weight; + + public RandomCookingResult(ItemStack result, int weight) { + this.result = result; + this.weight = weight; + } + + public ItemStack result() { + return result; + } + + public RandomCookingResult setResult(ItemStack result) { + this.result = result; + return this; + } + + public int weight() { + return weight; + } + + public RandomCookingResult setWeight(int weight) { + this.weight = weight; + return this; } - cookingRecipe.setGroup(group()); - RecipeManager.INSTANCE.regRecipe(group(), cookingRecipe, RecipeType.RANDOM_COOKING); } + } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RecipeRegistry.java index c132fc6d..1df0c8a9 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/RecipeRegistry.java @@ -7,9 +7,9 @@ public abstract class RecipeRegistry { - private NamespacedKey namespacedKey; - private ItemStack result; - private String group; + protected NamespacedKey namespacedKey; + protected ItemStack result; + protected String group; public RecipeRegistry(@Nullable String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) { this.group = group; diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapedRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapedRecipeRegistry.java index da0be01f..9779533e 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapedRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapedRecipeRegistry.java @@ -13,7 +13,7 @@ import java.util.Objects; import java.util.Set; -public final class ShapedRecipeRegistry extends RecipeRegistry { +public final class ShapedRecipeRegistry extends UnlockableRecipeRegistry { private String[] shape; private Map recipeChoiceMap; @@ -42,11 +42,11 @@ public ShapedRecipeRegistry setRecipeChoiceMap(Map reci @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe key cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(result, "Recipe key cannot be null"); Objects.requireNonNull(shape, "Recipe shape cannot be null"); Objects.requireNonNull(recipeChoiceMap, "Recipe ingredients cannot be null"); - ShapedRecipe shapedRecipe = new ShapedRecipe(namespacedKey(), result()); + ShapedRecipe shapedRecipe = new ShapedRecipe(namespacedKey, result); shapedRecipe.shape(shape); Set shapeStrChars = new HashSet<>(); for (String s : shape) { @@ -60,9 +60,10 @@ public void register() { shapedRecipe.setIngredient(ingredientKey, recipeChoiceMap.get(ingredientKey)); } - shapedRecipe.setGroup(group()); + shapedRecipe.setGroup(group); - RecipeManager.INSTANCE.regRecipe(group(), shapedRecipe, RecipeType.SHAPED); + RecipeManager.INSTANCE.regRecipe(group, shapedRecipe, RecipeType.SHAPED); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapelessRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapelessRecipeRegistry.java index 9c262abe..d4d976a4 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapelessRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/ShapelessRecipeRegistry.java @@ -11,7 +11,7 @@ import java.util.List; import java.util.Objects; -public class ShapelessRecipeRegistry extends RecipeRegistry { +public class ShapelessRecipeRegistry extends UnlockableRecipeRegistry { private List choiceList; @@ -30,14 +30,15 @@ public ShapelessRecipeRegistry setChoiceList(List choiceList) { @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe key cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(result, "Recipe key cannot be null"); Objects.requireNonNull(choiceList, "Recipe ingredients cannot be null"); - ShapelessRecipe shapelessRecipe = new ShapelessRecipe(namespacedKey(), result()); + ShapelessRecipe shapelessRecipe = new ShapelessRecipe(namespacedKey, result); for (RecipeChoice choice : choiceList) { shapelessRecipe.addIngredient(choice); } - shapelessRecipe.setGroup(group()); - RecipeManager.INSTANCE.regRecipe(group(), shapelessRecipe, RecipeType.SHAPELESS); + shapelessRecipe.setGroup(group); + RecipeManager.INSTANCE.regRecipe(group, shapelessRecipe, RecipeType.SHAPELESS); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); } } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/SmithingRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/SmithingRecipeRegistry.java index 4d570be2..31e37d4d 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/SmithingRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/SmithingRecipeRegistry.java @@ -13,9 +13,11 @@ /** * 1.20以下的锻造台配方注册器 */ -public class SmithingRecipeRegistry extends RecipeRegistry { +public class SmithingRecipeRegistry extends UnlockableRecipeRegistry { - private RecipeChoice base, addition; + protected RecipeChoice base, addition; + + protected boolean copyNbt = true; public SmithingRecipeRegistry(@NotNull String recipeGroup, @NotNull NamespacedKey namespacedKey, ItemStack result) { super(recipeGroup, namespacedKey, result); @@ -39,14 +41,24 @@ public SmithingRecipeRegistry setAddition(RecipeChoice addition) { return this; } + public boolean copyNbt() { + return copyNbt; + } + + public SmithingRecipeRegistry setCopyNbt(boolean copyNbt) { + this.copyNbt = copyNbt; + return this; + } + @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe result cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(result, "Recipe result cannot be null"); Objects.requireNonNull(base, "Recipe base cannot be null"); Objects.requireNonNull(addition, "Recipe addition cannot be null"); - SmithingRecipe smithingRecipe = new SmithingRecipe(namespacedKey(), result(), base, addition); - RecipeManager.INSTANCE.regRecipe(group(), smithingRecipe, RecipeType.SMITHING); + SmithingRecipe smithingRecipe = new SmithingRecipe(namespacedKey, result, base, addition, copyNbt); + RecipeManager.INSTANCE.regRecipe(group, smithingRecipe, RecipeType.SMITHING); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); } } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/StoneCuttingRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/StoneCuttingRecipeRegistry.java index 33d5df0a..dd193320 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/StoneCuttingRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/StoneCuttingRecipeRegistry.java @@ -8,32 +8,53 @@ import org.bukkit.inventory.StonecuttingRecipe; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; +import java.util.concurrent.CopyOnWriteArrayList; -public class StoneCuttingRecipeRegistry extends RecipeRegistry { +public class StoneCuttingRecipeRegistry extends UnlockableRecipeRegistry { - private RecipeChoice source; + private List results; + private List ingredientList = new CopyOnWriteArrayList<>(); + private List generateRecipeKeys = new CopyOnWriteArrayList<>(); - public StoneCuttingRecipeRegistry(@NotNull String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) { - super(group, namespacedKey, result); + public StoneCuttingRecipeRegistry(@NotNull String group, @NotNull NamespacedKey namespacedKey, @NotNull List results) { + super(group, namespacedKey, null); + this.results = results; } @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(result(), "Recipe result cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(result, "Recipe result cannot be null"); Objects.requireNonNull(source, "Recipe ingredient cannot be null"); - StonecuttingRecipe stonecuttingRecipe = new StonecuttingRecipe(namespacedKey(), result(), source); - stonecuttingRecipe.setGroup(group()); + StonecuttingRecipe stonecuttingRecipe = new StonecuttingRecipe(namespacedKey, result, source); + stonecuttingRecipe.setGroup(group); RecipeManager.INSTANCE.regRecipe(group(), stonecuttingRecipe, RecipeType.STONE_CUTTING); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); } - public RecipeChoice source() { - return source; + public List ingredientList() { + return ingredientList; } - public StoneCuttingRecipeRegistry setSource(RecipeChoice source) { - this.source = source; + public StoneCuttingRecipeRegistry setIngredientList(List ingredientList) { + this.ingredientList = ingredientList; return this; } + + public List results() { + return results; + } + + public StoneCuttingRecipeRegistry setResults(List results) { + this.results = results; + return this; + } + + public List generateRecipeKeys() { + return generateRecipeKeys; + } + } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/UnlockableRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/UnlockableRecipeRegistry.java new file mode 100644 index 00000000..afcc4b4f --- /dev/null +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/UnlockableRecipeRegistry.java @@ -0,0 +1,26 @@ +package com.github.yufiriamazenta.craftorithm.recipe.registry; + +import com.github.yufiriamazenta.craftorithm.config.PluginConfigs; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public abstract class UnlockableRecipeRegistry extends RecipeRegistry { + + protected boolean unlock = PluginConfigs.DEFAULT_RECIPE_UNLOCK.value(); + + public UnlockableRecipeRegistry(@Nullable String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) { + super(group, namespacedKey, result); + } + + public boolean unlock() { + return unlock; + } + + public UnlockableRecipeRegistry setUnlock(boolean unlock) { + this.unlock = unlock; + return this; + } + +} diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/XSmithingRecipeRegistry.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/XSmithingRecipeRegistry.java index 2c347395..4273f700 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/XSmithingRecipeRegistry.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/registry/XSmithingRecipeRegistry.java @@ -19,6 +19,21 @@ public XSmithingRecipeRegistry(@NotNull String recipeGroup, @NotNull NamespacedK this.smithingType = SmithingType.TRANSFORM; } + @Override + public XSmithingRecipeRegistry setBase(RecipeChoice base) { + return (XSmithingRecipeRegistry) super.setBase(base); + } + + @Override + public XSmithingRecipeRegistry setAddition(RecipeChoice addition) { + return (XSmithingRecipeRegistry) super.setAddition(addition); + } + + @Override + public XSmithingRecipeRegistry setCopyNbt(boolean copyNbt) { + return (XSmithingRecipeRegistry) super.setCopyNbt(copyNbt); + } + public RecipeChoice template() { return template; } @@ -30,26 +45,27 @@ public XSmithingRecipeRegistry setTemplate(RecipeChoice template) { @Override public void register() { - Objects.requireNonNull(namespacedKey(), "Recipe key cannot be null"); - Objects.requireNonNull(base(), "Recipe base cannot be null"); - Objects.requireNonNull(addition(), "Recipe addition cannot be null"); + Objects.requireNonNull(namespacedKey, "Recipe key cannot be null"); + Objects.requireNonNull(base, "Recipe base cannot be null"); + Objects.requireNonNull(addition, "Recipe addition cannot be null"); Objects.requireNonNull(template, "Recipe template cannot be null"); SmithingRecipe smithingRecipe; switch (smithingType) { default: - Objects.requireNonNull(result(), "Recipe result cannot be null"); - smithingRecipe = new SmithingRecipe(namespacedKey(), result(), base(), addition()); + Objects.requireNonNull(result, "Recipe result cannot be null"); + smithingRecipe = new SmithingRecipe(namespacedKey, result, base, addition, copyNbt); break; case TRIM: - smithingRecipe = new SmithingTrimRecipe(namespacedKey(), template, base(), addition()); + smithingRecipe = new SmithingTrimRecipe(namespacedKey, template, base, addition, copyNbt); break; case TRANSFORM: Objects.requireNonNull(result(), "Recipe result cannot be null"); - smithingRecipe = new SmithingTransformRecipe(namespacedKey(), result(), template, base(), addition()); + smithingRecipe = new SmithingTransformRecipe(namespacedKey, result, template, base, addition, copyNbt); break; } RecipeManager.INSTANCE.regRecipe(group(), smithingRecipe, RecipeType.SMITHING); + RecipeManager.INSTANCE.recipeUnlockMap().put(namespacedKey, unlock); } public SmithingType smithingType() { diff --git a/src/main/resources/recipe_groups/example.yml b/src/main/resources/recipe_groups/example.yml index 494a1834..536e1b7d 100644 --- a/src/main/resources/recipe_groups/example.yml +++ b/src/main/resources/recipe_groups/example.yml @@ -1,20 +1,77 @@ type: shaped result: minecraft:bedrock sort_id: 1 -recipe1: +shaped: type: shaped result: minecraft:bedrock + unlock: true source: shape: - - 'aaa' - - 'bbb' - - 'ccc' - ingredient_map: + - aaa + - bbb + - ccc + ingredients: a: minecraft:command_block b: minecraft:barrier c: minecraft:chain_command_block - unlock: true - -recipe2: - type: - 其他键: \ No newline at end of file +shapeless: + type: shapeless + result: minecraft:bedrock + source: + ingredients: + - minecraft:command_block + - minecraft:command_block + - minecraft:command_block + - minecraft:command_block + - minecraft:command_block +cooking: + type: cooking + result: minecraft:bedrock + source: + block: furnace + ingredient: minecraft:command_block + exp: 1.0 + time: 100 +smithing: + type: smithing + result: minecraft:bedrock + source: + copy_nbt: true + template: minecraft:command_block + base: minecraft:command_block + addition: minecraft:command_block + type: transform +stone_cutting: + type: stone_cutting + result: + - minecraft:bedrock + - minecraft:barrier + source: + ingredients: + - minecraft:command_block + - minecraft:chain_command_block +random_cooking: + type: random_cooking + result: + - minecraft:bedrock 50 + - minecraft:barrier 25 + - minecraft:chain_command_block 25 + source: + block: furnace + ingredient: minecraft:command_block + exp: 1.0 + time: 100 +potion: + type: potion + result: minecraft:bedrock + source: + ingredient: minecraft:command_block + input: minecraft:chain_command_block +anvil: + type: anvil + result: minecraft:bedrock + source: + base: minecraft:command_block + addition: minecraft:chain_command_block + copy_nbt: true + cost_level: 1 \ No newline at end of file