From d657a481f5ce6b9ebb2e92e3d00cdfa60a0a8579 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Sat, 15 Feb 2025 04:35:02 +0800 Subject: [PATCH] Optimize Recipes --- .../resources/default/configuration/ores.yml | 8 ++-- .../default/configuration/palm_tree.yml | 6 +++ .../item/recipe/BukkitRecipeManager.java | 31 +++++-------- .../item/recipe/RecipeEventListener.java | 18 +++----- .../plugin/injector/BukkitInjector.java | 43 ++++++++++++------- .../plugin/user/BukkitServerPlayer.java | 7 ++- .../core/item/recipe/AbstractRecipe.java | 10 ++++- .../core/item/recipe/CookingRecipe.java | 6 ++- .../core/item/recipe/CraftingTableRecipe.java | 6 ++- .../item/recipe/CustomBlastingRecipe.java | 7 +-- .../item/recipe/CustomCampfireRecipe.java | 7 +-- .../core/item/recipe/CustomShapedRecipe.java | 7 +-- .../item/recipe/CustomShapelessRecipe.java | 7 +-- .../item/recipe/CustomSmeltingRecipe.java | 7 +-- .../core/item/recipe/CustomSmokingRecipe.java | 7 +-- .../craftengine/core/item/recipe/Recipe.java | 2 + .../core/item/recipe/RecipeFactory.java | 2 +- .../core/item/recipe/RecipeTypes.java | 4 +- .../craftengine/core/util/HeptaFunction.java | 6 +++ 19 files changed, 108 insertions(+), 83 deletions(-) create mode 100644 core/src/main/java/net/momirealms/craftengine/core/util/HeptaFunction.java diff --git a/bukkit-loader/src/main/resources/resources/default/configuration/ores.yml b/bukkit-loader/src/main/resources/resources/default/configuration/ores.yml index 8879757df..486513731 100644 --- a/bukkit-loader/src/main/resources/resources/default/configuration/ores.yml +++ b/bukkit-loader/src/main/resources/resources/default/configuration/ores.yml @@ -88,7 +88,7 @@ recipes: default:topaz_from_smelting_topaz_ore: type: smelting experience: 1.0 - categroy: MISC + category: misc group: topaz time: 200 ingredient: "default:topaz_ore" @@ -98,7 +98,7 @@ recipes: default:topaz_from_smelting_deepslate_topaz_ore: type: smelting experience: 1.0 - categroy: MISC + category: misc group: topaz time: 200 ingredient: "default:deepslate_topaz_ore" @@ -108,7 +108,7 @@ recipes: default:topaz_from_blasting_topaz_ore: type: blasting experience: 1.0 - categroy: MISC + category: misc group: topaz time: 100 ingredient: "default:topaz_ore" @@ -118,7 +118,7 @@ recipes: default:topaz_from_blasting_deepslate_topaz_ore: type: blasting experience: 1.0 - categroy: MISC + category: misc group: topaz time: 100 ingredient: "default:deepslate_topaz_ore" diff --git a/bukkit-loader/src/main/resources/resources/default/configuration/palm_tree.yml b/bukkit-loader/src/main/resources/resources/default/configuration/palm_tree.yml index ae342596f..ef7d3154a 100644 --- a/bukkit-loader/src/main/resources/resources/default/configuration/palm_tree.yml +++ b/bukkit-loader/src/main/resources/resources/default/configuration/palm_tree.yml @@ -262,6 +262,8 @@ items: recipes: default:palm_planks: type: shapeless + category: building + group: planks ingredients: A: "#default:palm_logs" result: @@ -269,6 +271,8 @@ recipes: count: 4 default:palm_wood: type: shaped + category: building + group: bark pattern: - "AA" - "AA" @@ -279,6 +283,8 @@ recipes: count: 3 default:stripped_palm_wood: type: shaped + category: building + group: bark pattern: - "AA" - "AA" diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/BukkitRecipeManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/BukkitRecipeManager.java index 6b2a347bb..2f32d2fc7 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/BukkitRecipeManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/BukkitRecipeManager.java @@ -24,7 +24,7 @@ import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.config.ConfigManager; import net.momirealms.craftengine.core.registry.BuiltInRegistries; import net.momirealms.craftengine.core.registry.Holder; -import net.momirealms.craftengine.core.util.HexaFunction; +import net.momirealms.craftengine.core.util.HeptaFunction; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.PentaFunction; import net.momirealms.craftengine.core.util.VersionHelper; @@ -190,9 +190,6 @@ public class BukkitRecipeManager implements RecipeManager { private final VanillaRecipeReader recipeReader; private final List injectedDataPackRecipes; private final List registeredCustomRecipes; - // [internal:xxx] + [custom:custom] - // includes injected vanilla recipes and custom recipes - private final Set customRecipes; // data pack recipe resource locations [minecraft:xxx] private final Set dataPackRecipes; @@ -206,7 +203,6 @@ public class BukkitRecipeManager implements RecipeManager { this.injectedDataPackRecipes = new ArrayList<>(); this.registeredCustomRecipes = new ArrayList<>(); this.dataPackRecipes = new HashSet<>(); - this.customRecipes = new HashSet<>(); this.recipeEventListener = new RecipeEventListener(plugin, this, plugin.itemManager()); if (VersionHelper.isVersionNewerThan1_21()) { this.crafterEventListener = new CrafterEventListener(plugin, this, plugin.itemManager()); @@ -239,7 +235,7 @@ public class BukkitRecipeManager implements RecipeManager { @Override public boolean isCustomRecipe(Key key) { - return this.customRecipes.contains(key); + return this.byId.containsKey(key); } @Override @@ -279,7 +275,6 @@ public class BukkitRecipeManager implements RecipeManager { this.recipes.clear(); this.byId.clear(); this.dataPackRecipes.clear(); - this.customRecipes.clear(); try { // do not unregister them @@ -314,17 +309,17 @@ public class BukkitRecipeManager implements RecipeManager { @Override public void parseSection(Pack pack, Path path, Key id, Map section) { if (!ConfigManager.enableRecipeSystem()) return; - if (this.customRecipes.contains(id)) { + if (this.byId.containsKey(id)) { this.plugin.logger().warn(path, "Duplicated recipe " + id); return; } - Recipe recipe = RecipeTypes.fromMap(section); + Recipe recipe = RecipeTypes.fromMap(id, section); NamespacedKey key = NamespacedKey.fromString(id.toString()); BUKKIT_RECIPE_REGISTER.get(recipe.type()).accept(key, recipe); try { this.registeredCustomRecipes.add(key); - this.customRecipes.add(id); this.recipes.computeIfAbsent(recipe.type(), k -> new ArrayList<>()).add(recipe); + this.byId.put(id, recipe); } catch (Exception e) { plugin.logger().warn("Failed to add custom recipe " + id, e); } @@ -337,8 +332,8 @@ public class BukkitRecipeManager implements RecipeManager { // example: stone button public void addVanillaInternalRecipe(Key id, Recipe recipe) { - this.customRecipes.add(id); this.recipes.computeIfAbsent(recipe.type(), k -> new ArrayList<>()).add(recipe); + this.byId.put(id, recipe); } @Nullable @@ -357,20 +352,13 @@ public class BukkitRecipeManager implements RecipeManager { @Nullable @Override public Recipe getRecipe(Key type, RecipeInput input, Key lastRecipe) { - List> recipes = this.recipes.get(type); - if (recipes == null) return null; if (lastRecipe != null) { Recipe last = byId.get(lastRecipe); if (last != null && last.matches(input)) { return last; } - for (Recipe recipe : recipes) { - if (recipe.matches(input)) { - return recipe; - } - } } - return null; + return getRecipe(type, input); } @Override @@ -524,6 +512,7 @@ public class BukkitRecipeManager implements RecipeManager { } CustomShapelessRecipe ceRecipe = new CustomShapelessRecipe<>( + id, recipe.category(), recipe.group(), ingredientList, @@ -581,6 +570,7 @@ public class BukkitRecipeManager implements RecipeManager { } CustomShapedRecipe ceRecipe = new CustomShapedRecipe<>( + id, recipe.category(), recipe.group(), new CustomShapedRecipe.Pattern<>(recipe.pattern(), ingredients), @@ -604,7 +594,7 @@ public class BukkitRecipeManager implements RecipeManager { private void handleDataPackCookingRecipe(Key id, VanillaCookingRecipe recipe, PentaFunction> constructor1, - HexaFunction, Integer, Float, CustomRecipeResult, CookingRecipe> constructor2, + HeptaFunction, Integer, Float, CustomRecipeResult, CookingRecipe> constructor2, Method fromBukkitRecipeMethod, Consumer callback) { NamespacedKey key = new NamespacedKey(id.namespace(), id.value()); @@ -638,6 +628,7 @@ public class BukkitRecipeManager implements RecipeManager { } CookingRecipe ceRecipe = constructor2.apply( + id, recipe.category(), recipe.group(), Ingredient.of(holders), diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java index 8eb86655e..26272d301 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/recipe/RecipeEventListener.java @@ -158,27 +158,19 @@ public class RecipeEventListener implements Listener { } BukkitServerPlayer serverPlayer = this.plugin.adapt(player); - // though it might be inaccurate after reloading - // but it's worthy to cache - Recipe lastRecipe = serverPlayer.lastUsedRecipe(); - if (lastRecipe != null && (lastRecipe.type() == RecipeTypes.SHAPELESS || lastRecipe.type() == RecipeTypes.SHAPED )) { - if (lastRecipe.matches(input)) { - inventory.setResult(lastRecipe.getResult(serverPlayer)); - return; - } - } + Key lastRecipe = serverPlayer.lastUsedRecipe(); - Recipe ceRecipe = this.recipeManager.getRecipe(RecipeTypes.SHAPELESS, input); + Recipe ceRecipe = this.recipeManager.getRecipe(RecipeTypes.SHAPELESS, input, lastRecipe); if (ceRecipe != null) { inventory.setResult(ceRecipe.getResult(serverPlayer)); - serverPlayer.setLastUsedRecipe(ceRecipe); + serverPlayer.setLastUsedRecipe(ceRecipe.id()); correctCraftingRecipeUsed(inventory, ceRecipe); return; } - ceRecipe = this.recipeManager.getRecipe(RecipeTypes.SHAPED, input); + ceRecipe = this.recipeManager.getRecipe(RecipeTypes.SHAPED, input, lastRecipe); if (ceRecipe != null) { inventory.setResult(ceRecipe.getResult(serverPlayer)); - serverPlayer.setLastUsedRecipe(ceRecipe); + serverPlayer.setLastUsedRecipe(ceRecipe.id()); correctCraftingRecipeUsed(inventory, ceRecipe); return; } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BukkitInjector.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BukkitInjector.java index d1c5b6c3f..163b2a37c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BukkitInjector.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BukkitInjector.java @@ -79,6 +79,7 @@ public class BukkitInjector { private static Class clazz$InjectedCacheChecker; private static Field field$InjectedCacheChecker$recipeType; private static Field field$InjectedCacheChecker$lastRecipe; + private static Field field$InjectedCacheChecker$lastCustomRecipe; public static void init() { try { @@ -87,6 +88,7 @@ public class BukkitInjector { .implement(Reflections.clazz$RecipeManager$CachedCheck) .defineField("recipeType", Reflections.clazz$RecipeType, Visibility.PUBLIC) .defineField("lastRecipe", Object.class, Visibility.PUBLIC) + .defineField("lastCustomRecipe", Key.class, Visibility.PUBLIC) .method(ElementMatchers.named("getRecipeFor").or(ElementMatchers.named("a"))) .intercept(MethodDelegation.to( VersionHelper.isVersionNewerThan1_21_2() ? @@ -102,6 +104,7 @@ public class BukkitInjector { .getLoaded(); field$InjectedCacheChecker$recipeType = clazz$InjectedCacheChecker.getDeclaredField("recipeType"); field$InjectedCacheChecker$lastRecipe = clazz$InjectedCacheChecker.getDeclaredField("lastRecipe"); + field$InjectedCacheChecker$lastCustomRecipe = clazz$InjectedCacheChecker.getDeclaredField("lastCustomRecipe"); // Paletted Container clazz$InjectedPalettedContainer = byteBuddy @@ -336,20 +339,22 @@ public class BukkitInjector { CookingInput input = new CookingInput<>(new OptimizedIDItem<>(idHolder.get(), itemStack)); net.momirealms.craftengine.core.item.recipe.CookingRecipe ceRecipe; + Key lastCustomRecipe = (Key) field$InjectedCacheChecker$lastCustomRecipe.get(thisObj); if (type == Reflections.instance$RecipeType$SMELTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$BLASTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$SMOKING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input, lastCustomRecipe); } else { return Optional.empty(); } - if (ceRecipe == null) { return Optional.empty(); } + // Cache recipes, it might be incorrect on reloading + field$InjectedCacheChecker$lastCustomRecipe.set(thisObj, ceRecipe.id()); // It doesn't matter at all field$InjectedCacheChecker$lastRecipe.set(thisObj, resourceLocation); return Optional.of(Optional.ofNullable(recipeManager.getRecipeHolderByRecipe(ceRecipe)).orElse(pair.getSecond())); @@ -392,20 +397,22 @@ public class BukkitInjector { CookingInput input = new CookingInput<>(new OptimizedIDItem<>(idHolder.get(), itemStack)); net.momirealms.craftengine.core.item.recipe.CookingRecipe ceRecipe; + Key lastCustomRecipe = (Key) field$InjectedCacheChecker$lastCustomRecipe.get(thisObj); if (type == Reflections.instance$RecipeType$SMELTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$BLASTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$SMOKING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input, lastCustomRecipe); } else { return Optional.empty(); } - if (ceRecipe == null) { return Optional.empty(); } + // Cache recipes, it might be incorrect on reloading + field$InjectedCacheChecker$lastCustomRecipe.set(thisObj, ceRecipe.id()); // It doesn't matter at all field$InjectedCacheChecker$lastRecipe.set(thisObj, id); return Optional.of(Optional.ofNullable(recipeManager.getRecipeHolderByRecipe(ceRecipe)).orElse(holder)); @@ -447,20 +454,22 @@ public class BukkitInjector { CookingInput input = new CookingInput<>(new OptimizedIDItem<>(idHolder.get(), itemStack)); net.momirealms.craftengine.core.item.recipe.CookingRecipe ceRecipe; + Key lastCustomRecipe = (Key) field$InjectedCacheChecker$lastCustomRecipe.get(thisObj); if (type == Reflections.instance$RecipeType$SMELTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$BLASTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$SMOKING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input, lastCustomRecipe); } else { return Optional.empty(); } - if (ceRecipe == null) { return Optional.empty(); } + // Cache recipes, it might be incorrect on reloading + field$InjectedCacheChecker$lastCustomRecipe.set(thisObj, ceRecipe.id()); // It doesn't matter at all field$InjectedCacheChecker$lastRecipe.set(thisObj, id); return Optional.of(Optional.ofNullable(recipeManager.getRecipeHolderByRecipe(ceRecipe)).orElse(holder)); @@ -503,20 +512,22 @@ public class BukkitInjector { CookingInput input = new CookingInput<>(new OptimizedIDItem<>(idHolder.get(), itemStack)); net.momirealms.craftengine.core.item.recipe.CookingRecipe ceRecipe; + Key lastCustomRecipe = (Key) field$InjectedCacheChecker$lastCustomRecipe.get(thisObj); if (type == Reflections.instance$RecipeType$SMELTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMELTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$BLASTING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.BLASTING, input, lastCustomRecipe); } else if (type == Reflections.instance$RecipeType$SMOKING) { - ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input); + ceRecipe = (net.momirealms.craftengine.core.item.recipe.CookingRecipe) recipeManager.getRecipe(RecipeTypes.SMOKING, input, lastCustomRecipe); } else { return Optional.empty(); } - if (ceRecipe == null) { return Optional.empty(); } + // Cache recipes, it might be incorrect on reloading + field$InjectedCacheChecker$lastCustomRecipe.set(thisObj, ceRecipe.id()); // It doesn't matter at all field$InjectedCacheChecker$lastRecipe.set(thisObj, id); return Optional.of(Optional.ofNullable(recipeManager.getRecipeHolderByRecipe(ceRecipe)).orElse(holder)); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java index ea147c682..2a9897ca5 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java @@ -10,7 +10,6 @@ import net.momirealms.craftengine.bukkit.world.BukkitWorld; import net.momirealms.craftengine.core.entity.player.InteractionHand; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.Item; -import net.momirealms.craftengine.core.item.recipe.Recipe; import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.network.ConnectionState; import net.momirealms.craftengine.core.util.Direction; @@ -61,7 +60,7 @@ public class BukkitServerPlayer extends Player { private int resentSoundTick; private int resentSwingTick; - private Recipe lastUsedRecipe = null; + private Key lastUsedRecipe = null; public BukkitServerPlayer(BukkitCraftEngine plugin, Channel channel) { this.channel = channel; @@ -536,11 +535,11 @@ public class BukkitServerPlayer extends Player { return resentSwingTick == gameTicks(); } - public Recipe lastUsedRecipe() { + public Key lastUsedRecipe() { return lastUsedRecipe; } - public void setLastUsedRecipe(Recipe lastUsedRecipe) { + public void setLastUsedRecipe(Key lastUsedRecipe) { this.lastUsedRecipe = lastUsedRecipe; } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipe.java index 65c47fbe0..0bc36ece2 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipe.java @@ -1,12 +1,15 @@ package net.momirealms.craftengine.core.item.recipe; +import net.momirealms.craftengine.core.util.Key; import org.jetbrains.annotations.Nullable; public abstract class AbstractRecipe implements Recipe { protected String group; + protected Key id; - protected AbstractRecipe(String group) { + protected AbstractRecipe(Key id, String group) { this.group = group; + this.id = id; } @Override @@ -14,4 +17,9 @@ public abstract class AbstractRecipe implements Recipe { public String group() { return group; } + + @Override + public Key id() { + return id; + } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CookingRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CookingRecipe.java index 9243790a9..990a578a2 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CookingRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CookingRecipe.java @@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.item.recipe; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.recipe.input.RecipeInput; +import net.momirealms.craftengine.core.util.Key; public abstract class CookingRecipe extends AbstractRecipe { protected final CookingRecipeCategory category; @@ -10,13 +11,14 @@ public abstract class CookingRecipe extends AbstractRecipe { protected final float experience; protected final int cookingTime; - protected CookingRecipe(CookingRecipeCategory category, + protected CookingRecipe(Key id, + CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { - super(group); + super(id, group); this.category = category; this.ingredient = ingredient; this.result = result; diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CraftingTableRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CraftingTableRecipe.java index a90fda93e..bdf102645 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CraftingTableRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CraftingTableRecipe.java @@ -1,10 +1,12 @@ package net.momirealms.craftengine.core.item.recipe; +import net.momirealms.craftengine.core.util.Key; + public abstract class CraftingTableRecipe extends AbstractRecipe { protected final CraftingRecipeCategory category; - protected CraftingTableRecipe(CraftingRecipeCategory category, String group) { - super(group); + protected CraftingTableRecipe(Key id, CraftingRecipeCategory category, String group) { + super(id, group); this.category = category; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomBlastingRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomBlastingRecipe.java index 146769322..07558d7a0 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomBlastingRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomBlastingRecipe.java @@ -12,8 +12,8 @@ import java.util.*; public class CustomBlastingRecipe extends CookingRecipe { public static final Factory FACTORY = new Factory<>(); - public CustomBlastingRecipe(CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { - super(category, group, ingredient, cookingTime, experience, result); + public CustomBlastingRecipe(Key id, CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { + super(id, category, group, ingredient, cookingTime, experience, result); } @Override @@ -25,7 +25,7 @@ public class CustomBlastingRecipe extends CookingRecipe { @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @Override - public Recipe> create(Map arguments) { + public Recipe> create(Key id, Map arguments) { CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null; int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); @@ -40,6 +40,7 @@ public class CustomBlastingRecipe extends CookingRecipe { } } return new CustomBlastingRecipe( + id, recipeCategory, group, Ingredient.of(holders), diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomCampfireRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomCampfireRecipe.java index 77a84024a..ad49f9885 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomCampfireRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomCampfireRecipe.java @@ -12,8 +12,8 @@ import java.util.*; public class CustomCampfireRecipe extends CookingRecipe { public static final Factory FACTORY = new Factory<>(); - public CustomCampfireRecipe(CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { - super(category, group, ingredient, cookingTime, experience, result); + public CustomCampfireRecipe(Key id, CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { + super(id, category, group, ingredient, cookingTime, experience, result); } @Override @@ -25,7 +25,7 @@ public class CustomCampfireRecipe extends CookingRecipe { @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @Override - public Recipe> create(Map arguments) { + public Recipe> create(Key id, Map arguments) { CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null; int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); @@ -40,6 +40,7 @@ public class CustomCampfireRecipe extends CookingRecipe { } } return new CustomCampfireRecipe( + id, recipeCategory, group, Ingredient.of(holders), diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapedRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapedRecipe.java index b559dd8f3..5b638fa66 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapedRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapedRecipe.java @@ -18,8 +18,8 @@ public class CustomShapedRecipe extends CraftingTableRecipe { private final Pattern pattern; private final CustomRecipeResult result; - public CustomShapedRecipe(CraftingRecipeCategory category, String group, Pattern pattern, CustomRecipeResult result) { - super(category, group); + public CustomShapedRecipe(Key id, CraftingRecipeCategory category, String group, Pattern pattern, CustomRecipeResult result) { + super(id, category, group); this.pattern = pattern; this.parsedPattern = pattern.parse(); this.result = result; @@ -140,7 +140,7 @@ public class CustomShapedRecipe extends CraftingTableRecipe { @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @Override - public Recipe create(Map arguments) { + public Recipe create(Key id, Map arguments) { List pattern = MiscUtils.getAsStringList(arguments.get("pattern")); if (pattern.isEmpty()) { throw new IllegalArgumentException("pattern cannot be empty"); @@ -173,6 +173,7 @@ public class CustomShapedRecipe extends CraftingTableRecipe { ingredients.put(ch, Ingredient.of(holders)); } return new CustomShapedRecipe( + id, recipeCategory, group, new Pattern<>(pattern.toArray(new String[0]), ingredients), diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapelessRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapelessRecipe.java index 5af1fe30b..e9a89a112 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapelessRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomShapelessRecipe.java @@ -18,8 +18,8 @@ public class CustomShapelessRecipe extends CraftingTableRecipe { private final PlacementInfo placementInfo; private final CustomRecipeResult result; - public CustomShapelessRecipe(CraftingRecipeCategory category, String group, List> ingredients, CustomRecipeResult result) { - super(category, group); + public CustomShapelessRecipe(Key id, CraftingRecipeCategory category, String group, List> ingredients, CustomRecipeResult result) { + super(id, category, group); this.ingredients = ingredients; this.result = result; this.placementInfo = PlacementInfo.create(ingredients); @@ -63,7 +63,7 @@ public class CustomShapelessRecipe extends CraftingTableRecipe { @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @Override - public Recipe create(Map arguments) { + public Recipe create(Key id, Map arguments) { Map ingredientMap = MiscUtils.castToMap(arguments.get("ingredients"), true); if (ingredientMap == null) { throw new IllegalArgumentException("ingredients cannot be empty"); @@ -84,6 +84,7 @@ public class CustomShapelessRecipe extends CraftingTableRecipe { ingredients.add(Ingredient.of(holders)); } return new CustomShapelessRecipe( + id, recipeCategory, group, ingredients, diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmeltingRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmeltingRecipe.java index ee4fcb155..b2a8e353b 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmeltingRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmeltingRecipe.java @@ -12,8 +12,8 @@ import java.util.*; public class CustomSmeltingRecipe extends CookingRecipe { public static final Factory FACTORY = new Factory<>(); - public CustomSmeltingRecipe(CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { - super(category, group, ingredient, cookingTime, experience, result); + public CustomSmeltingRecipe(Key id, CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { + super(id, category, group, ingredient, cookingTime, experience, result); } @Override @@ -25,7 +25,7 @@ public class CustomSmeltingRecipe extends CookingRecipe { @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @Override - public Recipe> create(Map arguments) { + public Recipe> create(Key id, Map arguments) { CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null; int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); @@ -40,6 +40,7 @@ public class CustomSmeltingRecipe extends CookingRecipe { } } return new CustomSmeltingRecipe( + id, recipeCategory, group, Ingredient.of(holders), diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmokingRecipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmokingRecipe.java index 6a994edb7..e5a8d7656 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmokingRecipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/CustomSmokingRecipe.java @@ -12,8 +12,8 @@ import java.util.*; public class CustomSmokingRecipe extends CookingRecipe { public static final Factory FACTORY = new Factory<>(); - public CustomSmokingRecipe(CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { - super(category, group, ingredient, cookingTime, experience, result); + public CustomSmokingRecipe(Key id, CookingRecipeCategory category, String group, Ingredient ingredient, int cookingTime, float experience, CustomRecipeResult result) { + super(id, category, group, ingredient, cookingTime, experience, result); } @Override @@ -25,7 +25,7 @@ public class CustomSmokingRecipe extends CookingRecipe { @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @Override - public Recipe> create(Map arguments) { + public Recipe> create(Key id, Map arguments) { CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null; int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); @@ -40,6 +40,7 @@ public class CustomSmokingRecipe extends CookingRecipe { } } return new CustomSmokingRecipe( + id, recipeCategory, group, Ingredient.of(holders), diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/Recipe.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/Recipe.java index 3b3084c28..525e75b3e 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/Recipe.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/Recipe.java @@ -15,6 +15,8 @@ public interface Recipe { @NotNull Key type(); + Key id(); + @Nullable String group(); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeFactory.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeFactory.java index e1e0ac552..e6092042f 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeFactory.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeFactory.java @@ -8,7 +8,7 @@ import java.util.Map; public interface RecipeFactory { - Recipe create(Map arguments); + Recipe create(Key id, Map arguments); @SuppressWarnings({"unchecked", "rawtypes"}) default CustomRecipeResult parseResult(Map arguments) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeTypes.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeTypes.java index d76f6ea7f..a2bc1c164 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeTypes.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/RecipeTypes.java @@ -33,7 +33,7 @@ public class RecipeTypes { } @SuppressWarnings("unchecked") - public static Recipe fromMap(Map map) { + public static Recipe fromMap(Key id, Map map) { String type = (String) map.get("type"); if (type == null) { throw new NullPointerException("recipe type cannot be null"); @@ -43,6 +43,6 @@ public class RecipeTypes { if (factory == null) { throw new IllegalArgumentException("Unknown recipe type: " + type); } - return factory.create(map); + return factory.create(id, map); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/HeptaFunction.java b/core/src/main/java/net/momirealms/craftengine/core/util/HeptaFunction.java new file mode 100644 index 000000000..d71bbbe64 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/util/HeptaFunction.java @@ -0,0 +1,6 @@ +package net.momirealms.craftengine.core.util; + +@FunctionalInterface +public interface HeptaFunction { + R apply(T t, U u, V v, W w, X x, Y y, O o); +} \ No newline at end of file