diff --git a/build.gradle.kts b/build.gradle.kts index 81f9835d..65f0f67b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ import java.text.SimpleDateFormat -version = "2.0.0-dev12" +version = "2.0.0-dev13" plugins { `java-library` 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 eb993bda..7df4a15f 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java @@ -83,7 +83,7 @@ public RecipeGroup removeRecipeRegistry(String recipeName) { return this; } RecipeRegistry registry = groupRecipeRegistryMap.get(recipeKey); - RecipeManager.INSTANCE.recipeRemoverMap().get(registry.recipeType()).accept(Collections.singletonList(recipeKey)); + registry.recipeType().remover().accept(Collections.singletonList(recipeKey)); groupRecipeRegistryMap.remove(recipeKey); groupRecipeKeyMap.remove(recipeName); return this; 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 e5c078a2..2b97ec86 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeManager.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeManager.java @@ -3,7 +3,6 @@ import com.github.yufiriamazenta.craftorithm.Craftorithm; import com.github.yufiriamazenta.craftorithm.config.Languages; import com.github.yufiriamazenta.craftorithm.config.PluginConfigs; -import com.github.yufiriamazenta.craftorithm.exception.UnsupportedVersionException; import com.github.yufiriamazenta.craftorithm.recipe.custom.AnvilRecipe; import com.github.yufiriamazenta.craftorithm.recipe.custom.CustomRecipe; import com.github.yufiriamazenta.craftorithm.recipe.custom.PotionMixRecipe; @@ -23,7 +22,6 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.function.Consumer; import static com.github.yufiriamazenta.craftorithm.recipe.RecipeType.*; @@ -34,8 +32,6 @@ public enum RecipeManager { public static final String PLUGIN_RECIPE_NAMESPACE = "craftorithm"; private final ConfigWrapper removedRecipesConfigWrapper = new ConfigWrapper(Craftorithm.instance(), "removed_recipes.yml"); private final Map recipeGroupMap = new ConcurrentHashMap<>(); - private final Map> recipeRegisterMap = new ConcurrentHashMap<>(); - private final Map>> recipeRemoverMap = new ConcurrentHashMap<>(); private final Map recipeUnlockMap = new ConcurrentHashMap<>(); private final List removeRecipeRecycleBin = new CopyOnWriteArrayList<>(); private final Map serverRecipesCache = new ConcurrentHashMap<>(); @@ -44,52 +40,12 @@ public enum RecipeManager { private boolean supportPotionMix; RecipeManager() { - //设置各类型配方的注册方法 - recipeRegisterMap.put(SHAPED, Bukkit::addRecipe); - recipeRemoverMap.put(SHAPED, this::removeRecipes); - recipeRegisterMap.put(SHAPELESS, Bukkit::addRecipe); - recipeRemoverMap.put(SHAPELESS, this::removeRecipes); - if (CrypticLib.minecraftVersion() >= 11400) { - recipeRegisterMap.put(COOKING, Bukkit::addRecipe); - recipeRemoverMap.put(COOKING, this::removeRecipes); - recipeRegisterMap.put(RecipeType.STONE_CUTTING, Bukkit::addRecipe); - recipeRemoverMap.put(RecipeType.STONE_CUTTING, this::removeRecipes); - recipeRegisterMap.put(RecipeType.SMITHING, Bukkit::addRecipe); - recipeRemoverMap.put(RecipeType.SMITHING, this::removeRecipes); - } - if (CrypticLib.minecraftVersion() >= 11700) { - recipeRegisterMap.put(RecipeType.RANDOM_COOKING, Bukkit::addRecipe); - recipeRemoverMap.put(RecipeType.RANDOM_COOKING, this::removeRecipes); - } - - if (PluginConfigs.ENABLE_ANVIL_RECIPE.value()) { - recipeRegisterMap.put(RecipeType.ANVIL, recipe -> { - anvilRecipeMap.put(getRecipeKey(recipe), (AnvilRecipe) recipe); - }); - recipeRemoverMap.put(RecipeType.ANVIL, keys -> { - for (NamespacedKey key : keys) { - anvilRecipeMap.remove(key); - } - }); - } - try { Class.forName("io.papermc.paper.potion.PotionMix"); supportPotionMix = true; } catch (Exception e) { supportPotionMix = false; } - if (supportPotionMix) { - recipeRegisterMap.put(RecipeType.POTION, recipe -> { - Bukkit.getPotionBrewer().addPotionMix(((PotionMixRecipe) recipe).potionMix()); - potionMixRecipeMap.put(((PotionMixRecipe) recipe).key(), (PotionMixRecipe) recipe); - }); - recipeRemoverMap.put(RecipeType.POTION, recipeList -> { - for (NamespacedKey recipeKey : recipeList) { - Bukkit.getPotionBrewer().removePotionMix(recipeKey); - } - }); - } } public void reloadRecipeManager() { @@ -148,9 +104,7 @@ private void loadRecipeGroups() { public void regRecipe(String recipeGroupName, Recipe recipe, RecipeType recipeType) { if (!recipeGroupMap.containsKey(recipeGroupName)) throw new IllegalArgumentException("Can not find recipe group " + recipeGroupName + ", use addRecipeGroup() method to add recipe group."); - recipeRegisterMap.getOrDefault(recipeType, recipe1 -> { - throw new UnsupportedVersionException("Can not register " + recipeType.typeId().toLowerCase() + " recipe"); - }).accept(recipe); + recipeType.register().accept(recipe); } public Map recipeGroupMap() { @@ -212,7 +166,7 @@ public boolean removeRecipeGroup(String recipeGroupName, boolean deleteFile) { return false; recipeGroup.groupRecipeRegistryMap.forEach( (recipeKey, recipeRegistry) -> { - recipeRemoverMap.get(recipeRegistry.recipeType()).accept(Collections.singletonList(recipeKey)); + recipeRegistry.recipeType().remover().accept(Collections.singletonList(recipeKey)); } ); ConfigWrapper recipeConfig = recipeGroupMap.get(recipeGroupName).recipeGroupConfig(); @@ -251,7 +205,7 @@ private void addRecipeToRemovedRecipeRecycleBin(List recipeKeys) * @param recipeKeys 要删除的配方 * @return 删除的配方数量 */ - private int removeRecipes(List recipeKeys) { + public int removeRecipes(List recipeKeys) { if (recipeKeys == null || recipeKeys.isEmpty()) return 0; //删除表里缓存的一些数据 @@ -386,18 +340,14 @@ public void loadServerRecipeCache() { } } - public Map> recipeRegisterMap() { - return recipeRegisterMap; - } - - public Map>> recipeRemoverMap() { - return recipeRemoverMap; - } - public Map potionMixRecipeMap() { return potionMixRecipeMap; } + public Map anvilRecipeMap() { + return anvilRecipeMap; + } + public List getRecipeGroups() { return new ArrayList<>(recipeGroupMap.keySet()); } diff --git a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeType.java b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeType.java index 0795ab32..532f7435 100644 --- a/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeType.java +++ b/src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeType.java @@ -1,31 +1,128 @@ package com.github.yufiriamazenta.craftorithm.recipe; import com.github.yufiriamazenta.craftorithm.config.Languages; +import com.github.yufiriamazenta.craftorithm.config.PluginConfigs; +import com.github.yufiriamazenta.craftorithm.recipe.custom.AnvilRecipe; +import com.github.yufiriamazenta.craftorithm.recipe.custom.PotionMixRecipe; +import crypticlib.CrypticLib; import crypticlib.chat.entry.StringLangConfigEntry; +import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.Recipe; +import org.jetbrains.annotations.NotNull; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; public final class RecipeType { private static final Map BY_NAME = new ConcurrentHashMap<>(); - public static RecipeType SHAPED = new RecipeType("shaped", Languages.RECIPE_TYPE_NAME_SHAPED); - public static RecipeType SHAPELESS = new RecipeType("shapeless", Languages.RECIPE_TYPE_NAME_SHAPELESS); - public static RecipeType COOKING = new RecipeType("cooking", Languages.RECIPE_TYPE_NAME_COOKING); - public static RecipeType SMITHING = new RecipeType("smithing", Languages.RECIPE_TYPE_NAME_SMITHING); - public static RecipeType STONE_CUTTING = new RecipeType("stone_cutting", Languages.RECIPE_TYPE_NAME_STONE_CUTTING); - public static RecipeType RANDOM_COOKING = new RecipeType("random_cooking", Languages.RECIPE_TYPE_NAME_COOKING); - public static RecipeType UNKNOWN = new RecipeType("unknown", null); - public static RecipeType POTION = new RecipeType("potion", Languages.RECIPE_TYPE_NAME_POTION); - public static RecipeType ANVIL = new RecipeType("anvil", Languages.RECIPE_TYPE_NAME_ANVIL); + public static RecipeType SHAPED = new RecipeType( + "shaped", + Languages.RECIPE_TYPE_NAME_SHAPED, + Bukkit::addRecipe, + RecipeManager.INSTANCE::removeRecipes + ); + public static RecipeType SHAPELESS = new RecipeType( + "shapeless", + Languages.RECIPE_TYPE_NAME_SHAPELESS, + Bukkit::addRecipe, + RecipeManager.INSTANCE::removeRecipes + ); + public static RecipeType COOKING = new RecipeType( + "cooking", + Languages.RECIPE_TYPE_NAME_COOKING, + recipe -> { + if (CrypticLib.minecraftVersion() >= 11400) { + Bukkit.addRecipe(recipe); + } + }, + RecipeManager.INSTANCE::removeRecipes + ); + public static RecipeType SMITHING = new RecipeType( + "smithing", + Languages.RECIPE_TYPE_NAME_SMITHING, + recipe -> { + if (CrypticLib.minecraftVersion() >= 11400) { + Bukkit.addRecipe(recipe); + } + }, + RecipeManager.INSTANCE::removeRecipes + ); + public static RecipeType STONE_CUTTING = new RecipeType( + "stone_cutting", + Languages.RECIPE_TYPE_NAME_STONE_CUTTING, + recipe -> { + if (CrypticLib.minecraftVersion() >= 11400) { + Bukkit.addRecipe(recipe); + } + }, + RecipeManager.INSTANCE::removeRecipes + ); + public static RecipeType RANDOM_COOKING = new RecipeType( + "random_cooking", + Languages.RECIPE_TYPE_NAME_COOKING, + recipe -> { + if (CrypticLib.minecraftVersion() >= 11700) { + Bukkit.addRecipe(recipe); + } + }, + RecipeManager.INSTANCE::removeRecipes + ); + public static RecipeType UNKNOWN = new RecipeType( + "unknown", + null, + recipe -> {}, + keys -> {} + ); + public static RecipeType POTION = new RecipeType( + "potion", + Languages.RECIPE_TYPE_NAME_POTION, + recipe -> { + if (!RecipeManager.INSTANCE.supportPotionMix()) + return; + Bukkit.getPotionBrewer().addPotionMix(((PotionMixRecipe) recipe).potionMix()); + RecipeManager.INSTANCE.potionMixRecipeMap().put(((PotionMixRecipe) recipe).key(), (PotionMixRecipe) recipe); + }, + keys -> { + if (!RecipeManager.INSTANCE.supportPotionMix()) + return; + for (NamespacedKey recipeKey : keys) { + Bukkit.getPotionBrewer().removePotionMix(recipeKey); + RecipeManager.INSTANCE.potionMixRecipeMap().remove(recipeKey); + } + } + ); + public static RecipeType ANVIL = new RecipeType( + "anvil", + Languages.RECIPE_TYPE_NAME_ANVIL, + recipe -> { + if (!PluginConfigs.ENABLE_ANVIL_RECIPE.value()) + return; + RecipeManager.INSTANCE.anvilRecipeMap().put(RecipeManager.INSTANCE.getRecipeKey(recipe), (AnvilRecipe) recipe); + }, + keys -> { + if (!PluginConfigs.ENABLE_ANVIL_RECIPE.value()) + return; + for (NamespacedKey key : keys) { + RecipeManager.INSTANCE.anvilRecipeMap().remove(key); + } + } + ); private final String typeId; private StringLangConfigEntry typeName; + private Consumer register; + private Consumer> remover; - private RecipeType(String typeId, StringLangConfigEntry typeName) { + private RecipeType(@NotNull String typeId, StringLangConfigEntry typeName, @NotNull Consumer register, @NotNull Consumer> remover) { this.typeId = typeId.toUpperCase(); this.typeName = typeName; + this.register = register; + this.remover = remover; BY_NAME.put(this.typeId, this); } @@ -42,6 +139,24 @@ public RecipeType setTypeName(StringLangConfigEntry typeName) { return this; } + public Consumer register() { + return register; + } + + public RecipeType setRegister(Consumer register) { + this.register = register; + return this; + } + + public Consumer> remover() { + return remover; + } + + public RecipeType setRemover(Consumer> remover) { + this.remover = remover; + return this; + } + @Override public boolean equals(Object o) { if (this == o) return true;