mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-27 02:49:15 +00:00
Optimize Recipes
This commit is contained in:
@@ -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<T> implements Recipe<T> {
|
||||
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<T> implements Recipe<T> {
|
||||
public String group() {
|
||||
return group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key id() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> extends AbstractRecipe<T> {
|
||||
protected final CookingRecipeCategory category;
|
||||
@@ -10,13 +11,14 @@ public abstract class CookingRecipe<T> extends AbstractRecipe<T> {
|
||||
protected final float experience;
|
||||
protected final int cookingTime;
|
||||
|
||||
protected CookingRecipe(CookingRecipeCategory category,
|
||||
protected CookingRecipe(Key id,
|
||||
CookingRecipeCategory category,
|
||||
String group,
|
||||
Ingredient<T> ingredient,
|
||||
int cookingTime,
|
||||
float experience,
|
||||
CustomRecipeResult<T> result) {
|
||||
super(group);
|
||||
super(id, group);
|
||||
this.category = category;
|
||||
this.ingredient = ingredient;
|
||||
this.result = result;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package net.momirealms.craftengine.core.item.recipe;
|
||||
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
public abstract class CraftingTableRecipe<T> extends AbstractRecipe<T> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ import java.util.*;
|
||||
public class CustomBlastingRecipe<T> extends CookingRecipe<T> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
|
||||
public CustomBlastingRecipe(CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(category, group, ingredient, cookingTime, experience, result);
|
||||
public CustomBlastingRecipe(Key id, CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(id, category, group, ingredient, cookingTime, experience, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,7 +25,7 @@ public class CustomBlastingRecipe<T> extends CookingRecipe<T> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@Override
|
||||
public Recipe<CustomBlastingRecipe<A>> create(Map<String, Object> arguments) {
|
||||
public Recipe<CustomBlastingRecipe<A>> create(Key id, Map<String, Object> 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<T> extends CookingRecipe<T> {
|
||||
}
|
||||
}
|
||||
return new CustomBlastingRecipe(
|
||||
id,
|
||||
recipeCategory,
|
||||
group,
|
||||
Ingredient.of(holders),
|
||||
|
||||
@@ -12,8 +12,8 @@ import java.util.*;
|
||||
public class CustomCampfireRecipe<T> extends CookingRecipe<T> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
|
||||
public CustomCampfireRecipe(CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(category, group, ingredient, cookingTime, experience, result);
|
||||
public CustomCampfireRecipe(Key id, CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(id, category, group, ingredient, cookingTime, experience, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,7 +25,7 @@ public class CustomCampfireRecipe<T> extends CookingRecipe<T> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@Override
|
||||
public Recipe<CustomCampfireRecipe<A>> create(Map<String, Object> arguments) {
|
||||
public Recipe<CustomCampfireRecipe<A>> create(Key id, Map<String, Object> 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<T> extends CookingRecipe<T> {
|
||||
}
|
||||
}
|
||||
return new CustomCampfireRecipe(
|
||||
id,
|
||||
recipeCategory,
|
||||
group,
|
||||
Ingredient.of(holders),
|
||||
|
||||
@@ -18,8 +18,8 @@ public class CustomShapedRecipe<T> extends CraftingTableRecipe<T> {
|
||||
private final Pattern<T> pattern;
|
||||
private final CustomRecipeResult<T> result;
|
||||
|
||||
public CustomShapedRecipe(CraftingRecipeCategory category, String group, Pattern<T> pattern, CustomRecipeResult<T> result) {
|
||||
super(category, group);
|
||||
public CustomShapedRecipe(Key id, CraftingRecipeCategory category, String group, Pattern<T> pattern, CustomRecipeResult<T> result) {
|
||||
super(id, category, group);
|
||||
this.pattern = pattern;
|
||||
this.parsedPattern = pattern.parse();
|
||||
this.result = result;
|
||||
@@ -140,7 +140,7 @@ public class CustomShapedRecipe<T> extends CraftingTableRecipe<T> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@Override
|
||||
public Recipe<A> create(Map<String, Object> arguments) {
|
||||
public Recipe<A> create(Key id, Map<String, Object> arguments) {
|
||||
List<String> pattern = MiscUtils.getAsStringList(arguments.get("pattern"));
|
||||
if (pattern.isEmpty()) {
|
||||
throw new IllegalArgumentException("pattern cannot be empty");
|
||||
@@ -173,6 +173,7 @@ public class CustomShapedRecipe<T> extends CraftingTableRecipe<T> {
|
||||
ingredients.put(ch, Ingredient.of(holders));
|
||||
}
|
||||
return new CustomShapedRecipe(
|
||||
id,
|
||||
recipeCategory,
|
||||
group,
|
||||
new Pattern<>(pattern.toArray(new String[0]), ingredients),
|
||||
|
||||
@@ -18,8 +18,8 @@ public class CustomShapelessRecipe<T> extends CraftingTableRecipe<T> {
|
||||
private final PlacementInfo<T> placementInfo;
|
||||
private final CustomRecipeResult<T> result;
|
||||
|
||||
public CustomShapelessRecipe(CraftingRecipeCategory category, String group, List<Ingredient<T>> ingredients, CustomRecipeResult<T> result) {
|
||||
super(category, group);
|
||||
public CustomShapelessRecipe(Key id, CraftingRecipeCategory category, String group, List<Ingredient<T>> ingredients, CustomRecipeResult<T> result) {
|
||||
super(id, category, group);
|
||||
this.ingredients = ingredients;
|
||||
this.result = result;
|
||||
this.placementInfo = PlacementInfo.create(ingredients);
|
||||
@@ -63,7 +63,7 @@ public class CustomShapelessRecipe<T> extends CraftingTableRecipe<T> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@Override
|
||||
public Recipe<A> create(Map<String, Object> arguments) {
|
||||
public Recipe<A> create(Key id, Map<String, Object> arguments) {
|
||||
Map<String, Object> ingredientMap = MiscUtils.castToMap(arguments.get("ingredients"), true);
|
||||
if (ingredientMap == null) {
|
||||
throw new IllegalArgumentException("ingredients cannot be empty");
|
||||
@@ -84,6 +84,7 @@ public class CustomShapelessRecipe<T> extends CraftingTableRecipe<T> {
|
||||
ingredients.add(Ingredient.of(holders));
|
||||
}
|
||||
return new CustomShapelessRecipe(
|
||||
id,
|
||||
recipeCategory,
|
||||
group,
|
||||
ingredients,
|
||||
|
||||
@@ -12,8 +12,8 @@ import java.util.*;
|
||||
public class CustomSmeltingRecipe<T> extends CookingRecipe<T> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
|
||||
public CustomSmeltingRecipe(CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(category, group, ingredient, cookingTime, experience, result);
|
||||
public CustomSmeltingRecipe(Key id, CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(id, category, group, ingredient, cookingTime, experience, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,7 +25,7 @@ public class CustomSmeltingRecipe<T> extends CookingRecipe<T> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@Override
|
||||
public Recipe<CustomSmeltingRecipe<A>> create(Map<String, Object> arguments) {
|
||||
public Recipe<CustomSmeltingRecipe<A>> create(Key id, Map<String, Object> 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<T> extends CookingRecipe<T> {
|
||||
}
|
||||
}
|
||||
return new CustomSmeltingRecipe(
|
||||
id,
|
||||
recipeCategory,
|
||||
group,
|
||||
Ingredient.of(holders),
|
||||
|
||||
@@ -12,8 +12,8 @@ import java.util.*;
|
||||
public class CustomSmokingRecipe<T> extends CookingRecipe<T> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
|
||||
public CustomSmokingRecipe(CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(category, group, ingredient, cookingTime, experience, result);
|
||||
public CustomSmokingRecipe(Key id, CookingRecipeCategory category, String group, Ingredient<T> ingredient, int cookingTime, float experience, CustomRecipeResult<T> result) {
|
||||
super(id, category, group, ingredient, cookingTime, experience, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,7 +25,7 @@ public class CustomSmokingRecipe<T> extends CookingRecipe<T> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@Override
|
||||
public Recipe<CustomSmokingRecipe<A>> create(Map<String, Object> arguments) {
|
||||
public Recipe<CustomSmokingRecipe<A>> create(Key id, Map<String, Object> 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<T> extends CookingRecipe<T> {
|
||||
}
|
||||
}
|
||||
return new CustomSmokingRecipe(
|
||||
id,
|
||||
recipeCategory,
|
||||
group,
|
||||
Ingredient.of(holders),
|
||||
|
||||
@@ -15,6 +15,8 @@ public interface Recipe<T> {
|
||||
@NotNull
|
||||
Key type();
|
||||
|
||||
Key id();
|
||||
|
||||
@Nullable
|
||||
String group();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.Map;
|
||||
|
||||
public interface RecipeFactory<T> {
|
||||
|
||||
Recipe<T> create(Map<String, Object> arguments);
|
||||
Recipe<T> create(Key id, Map<String, Object> arguments);
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
default CustomRecipeResult<T> parseResult(Map<String, Object> arguments) {
|
||||
|
||||
@@ -33,7 +33,7 @@ public class RecipeTypes {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Recipe<T> fromMap(Map<String, Object> map) {
|
||||
public static <T> Recipe<T> fromMap(Key id, Map<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.momirealms.craftengine.core.util;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface HeptaFunction<T, U, V, W, X, Y, O, R> {
|
||||
R apply(T t, U u, V v, W w, X x, Y y, O o);
|
||||
}
|
||||
Reference in New Issue
Block a user