9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-21 07:59:19 +00:00

refactor recipes

This commit is contained in:
XiaoMoMi
2025-03-19 03:29:08 +08:00
parent 5fb238f090
commit 4653adc43c
26 changed files with 424 additions and 61 deletions

View File

@@ -55,7 +55,20 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
static { static {
BUKKIT_RECIPE_FACTORIES.put(RecipeTypes.SMITHING_TRANSFORM, (key, recipe) -> { BUKKIT_RECIPE_FACTORIES.put(RecipeTypes.SMITHING_TRANSFORM, (key, recipe) -> {
CustomSmithingTransformRecipe<ItemStack> ceRecipe = (CustomSmithingTransformRecipe<ItemStack>) recipe;
SmithingTransformRecipe transformRecipe = new SmithingTransformRecipe(
key, ceRecipe.result(ItemBuildContext.EMPTY),
ingredientToBukkitRecipeChoice(ceRecipe.template()),
ingredientToBukkitRecipeChoice(ceRecipe.base()),
ingredientToBukkitRecipeChoice(ceRecipe.addition()),
false
);
try {
Object craftRecipe = Reflections.method$CraftSmithingTransformRecipe$fromBukkitRecipe.invoke(null, transformRecipe);
Reflections.method$CraftRecipe$addToCraftingManager.invoke(craftRecipe);
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to convert smithing transform recipe", e);
}
}); });
BUKKIT_RECIPE_FACTORIES.put(RecipeTypes.SHAPED, (key, recipe) -> { BUKKIT_RECIPE_FACTORIES.put(RecipeTypes.SHAPED, (key, recipe) -> {
CustomShapedRecipe<ItemStack> ceRecipe = (CustomShapedRecipe<ItemStack>) recipe; CustomShapedRecipe<ItemStack> ceRecipe = (CustomShapedRecipe<ItemStack>) recipe;
@@ -68,7 +81,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
} }
shapedRecipe.shape(ceRecipe.pattern().pattern()); shapedRecipe.shape(ceRecipe.pattern().pattern());
for (Map.Entry<Character, Ingredient<ItemStack>> entry : ceRecipe.pattern().ingredients().entrySet()) { for (Map.Entry<Character, Ingredient<ItemStack>> entry : ceRecipe.pattern().ingredients().entrySet()) {
shapedRecipe.setIngredient(entry.getKey(), new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(entry.getValue()))); shapedRecipe.setIngredient(entry.getKey(), ingredientToBukkitRecipeChoice(entry.getValue()));
} }
try { try {
Object craftRecipe = Reflections.method$CraftShapedRecipe$fromBukkitRecipe.invoke(null, shapedRecipe); Object craftRecipe = Reflections.method$CraftShapedRecipe$fromBukkitRecipe.invoke(null, shapedRecipe);
@@ -88,7 +101,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
shapelessRecipe.setCategory(CraftingBookCategory.valueOf(Objects.requireNonNull(ceRecipe.category()).name())); shapelessRecipe.setCategory(CraftingBookCategory.valueOf(Objects.requireNonNull(ceRecipe.category()).name()));
} }
for (Ingredient<ItemStack> ingredient : ceRecipe.ingredientsInUse()) { for (Ingredient<ItemStack> ingredient : ceRecipe.ingredientsInUse()) {
shapelessRecipe.addIngredient(new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ingredient))); shapelessRecipe.addIngredient(ingredientToBukkitRecipeChoice(ingredient));
} }
try { try {
Object craftRecipe = Reflections.method$CraftShapelessRecipe$fromBukkitRecipe.invoke(null, shapelessRecipe); Object craftRecipe = Reflections.method$CraftShapelessRecipe$fromBukkitRecipe.invoke(null, shapelessRecipe);
@@ -102,7 +115,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
CustomSmeltingRecipe<ItemStack> ceRecipe = (CustomSmeltingRecipe<ItemStack>) recipe; CustomSmeltingRecipe<ItemStack> ceRecipe = (CustomSmeltingRecipe<ItemStack>) recipe;
FurnaceRecipe furnaceRecipe = new FurnaceRecipe( FurnaceRecipe furnaceRecipe = new FurnaceRecipe(
key, ceRecipe.result(ItemBuildContext.EMPTY), key, ceRecipe.result(ItemBuildContext.EMPTY),
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())), ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
ceRecipe.experience(), ceRecipe.cookingTime() ceRecipe.experience(), ceRecipe.cookingTime()
); );
if (ceRecipe.group() != null) { if (ceRecipe.group() != null) {
@@ -123,7 +136,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
CustomSmokingRecipe<ItemStack> ceRecipe = (CustomSmokingRecipe<ItemStack>) recipe; CustomSmokingRecipe<ItemStack> ceRecipe = (CustomSmokingRecipe<ItemStack>) recipe;
SmokingRecipe smokingRecipe = new SmokingRecipe( SmokingRecipe smokingRecipe = new SmokingRecipe(
key, ceRecipe.result(ItemBuildContext.EMPTY), key, ceRecipe.result(ItemBuildContext.EMPTY),
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())), ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
ceRecipe.experience(), ceRecipe.cookingTime() ceRecipe.experience(), ceRecipe.cookingTime()
); );
if (ceRecipe.group() != null) { if (ceRecipe.group() != null) {
@@ -144,7 +157,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
CustomBlastingRecipe<ItemStack> ceRecipe = (CustomBlastingRecipe<ItemStack>) recipe; CustomBlastingRecipe<ItemStack> ceRecipe = (CustomBlastingRecipe<ItemStack>) recipe;
BlastingRecipe blastingRecipe = new BlastingRecipe( BlastingRecipe blastingRecipe = new BlastingRecipe(
key, ceRecipe.result(ItemBuildContext.EMPTY), key, ceRecipe.result(ItemBuildContext.EMPTY),
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())), ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
ceRecipe.experience(), ceRecipe.cookingTime() ceRecipe.experience(), ceRecipe.cookingTime()
); );
if (ceRecipe.group() != null) { if (ceRecipe.group() != null) {
@@ -165,7 +178,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
CustomCampfireRecipe<ItemStack> ceRecipe = (CustomCampfireRecipe<ItemStack>) recipe; CustomCampfireRecipe<ItemStack> ceRecipe = (CustomCampfireRecipe<ItemStack>) recipe;
CampfireRecipe campfireRecipe = new CampfireRecipe( CampfireRecipe campfireRecipe = new CampfireRecipe(
key, ceRecipe.result(ItemBuildContext.EMPTY), key, ceRecipe.result(ItemBuildContext.EMPTY),
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())), ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
ceRecipe.experience(), ceRecipe.cookingTime() ceRecipe.experience(), ceRecipe.cookingTime()
); );
if (ceRecipe.group() != null) { if (ceRecipe.group() != null) {
@@ -495,6 +508,10 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
VanillaStoneCuttingRecipe recipe = this.recipeReader.readStoneCutting(jsonObject); VanillaStoneCuttingRecipe recipe = this.recipeReader.readStoneCutting(jsonObject);
handleDataPackStoneCuttingRecipe(id, recipe); handleDataPackStoneCuttingRecipe(id, recipe);
} }
case "minecraft:smithing_transform" -> {
VanillaSmithingTransformRecipe recipe = this.recipeReader.readSmithingTransform(jsonObject);
handleDataPackSmithingTransform(id, recipe, (injectLogics::add));
}
} }
} }
} }
@@ -520,6 +537,29 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
return future; return future;
} }
private boolean readVanillaIngredients(boolean hasCustomItemInTag, List<String> ingredients, Consumer<Material> materialConsumer, Consumer<Holder<Key>> holderConsumer) {
for (String item : ingredients) {
if (item.charAt(0) == '#') {
Key tag = Key.from(item.substring(1));
for (Material material : tagToMaterials(tag)) {
materialConsumer.accept(material);
}
if (!hasCustomItemInTag) {
if (!plugin.itemManager().tagToCustomItems(tag).isEmpty()) {
hasCustomItemInTag = true;
}
}
for (Holder<Key> holder : plugin.itemManager().tagToItems(tag)) {
holderConsumer.accept(holder);
}
} else {
materialConsumer.accept(MaterialUtils.getMaterial(item));
holderConsumer.accept(BuiltInRegistries.OPTIMIZED_ITEM_ID.get(Key.from(item)).orElseThrow());
}
}
return hasCustomItemInTag;
}
private void handleDataPackShapelessRecipe(Key id, VanillaShapelessRecipe recipe, Consumer<Runnable> callback) { private void handleDataPackShapelessRecipe(Key id, VanillaShapelessRecipe recipe, Consumer<Runnable> callback) {
NamespacedKey key = new NamespacedKey(id.namespace(), id.value()); NamespacedKey key = new NamespacedKey(id.namespace(), id.value());
ItemStack result = createResultStack(recipe.result()); ItemStack result = createResultStack(recipe.result());
@@ -669,22 +709,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
Set<Material> materials = new HashSet<>(); Set<Material> materials = new HashSet<>();
Set<Holder<Key>> holders = new HashSet<>(); Set<Holder<Key>> holders = new HashSet<>();
boolean hasCustomItemInTag = false; boolean hasCustomItemInTag = readVanillaIngredients(false, recipe.ingredient(), materials::add, holders::add);
for (String item : recipe.ingredient()) {
if (item.charAt(0) == '#') {
Key tag = Key.from(item.substring(1));
materials.addAll(tagToMaterials(tag));
if (!hasCustomItemInTag) {
if (!plugin.itemManager().tagToCustomItems(tag).isEmpty()) {
hasCustomItemInTag = true;
}
}
holders.addAll(plugin.itemManager().tagToItems(tag));
} else {
materials.add(MaterialUtils.getMaterial(item));
holders.add(BuiltInRegistries.OPTIMIZED_ITEM_ID.get(Key.from(item)).orElseThrow());
}
}
org.bukkit.inventory.CookingRecipe<?> cookingRecipe = constructor1.apply(key, result, new RecipeChoice.MaterialChoice(new ArrayList<>(materials)), recipe.experience(), recipe.cookingTime()); org.bukkit.inventory.CookingRecipe<?> cookingRecipe = constructor1.apply(key, result, new RecipeChoice.MaterialChoice(new ArrayList<>(materials)), recipe.experience(), recipe.cookingTime());
if (recipe.group() != null) { if (recipe.group() != null) {
cookingRecipe.setGroup(recipe.group()); cookingRecipe.setGroup(recipe.group());
@@ -717,6 +742,52 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
this.addInternalRecipe(id, ceRecipe); this.addInternalRecipe(id, ceRecipe);
} }
private void handleDataPackSmithingTransform(Key id, VanillaSmithingTransformRecipe recipe, Consumer<Runnable> callback) {
NamespacedKey key = new NamespacedKey(id.namespace(), id.value());
ItemStack result = createResultStack(recipe.result());
boolean hasCustomItemInTag;
Set<Material> additionMaterials = new HashSet<>();
Set<Holder<Key>> additionHolders = new HashSet<>();
hasCustomItemInTag = readVanillaIngredients(false, recipe.addition(), additionMaterials::add, additionHolders::add);
Set<Material> templateMaterials = new HashSet<>();
Set<Holder<Key>> templateHolders = new HashSet<>();
hasCustomItemInTag = readVanillaIngredients(hasCustomItemInTag, recipe.template(), templateMaterials::add, templateHolders::add);
Set<Material> baseMaterials = new HashSet<>();
Set<Holder<Key>> baseHolders = new HashSet<>();
hasCustomItemInTag = readVanillaIngredients(hasCustomItemInTag, recipe.base(), baseMaterials::add, baseHolders::add);
CustomSmithingTransformRecipe<ItemStack> ceRecipe = new CustomSmithingTransformRecipe<>(
id,
Ingredient.of(baseHolders),
Ingredient.of(templateHolders),
Ingredient.of(additionHolders),
new CustomRecipeResult<>(new CloneableConstantItem(recipe.result().isCustom() ? Key.of("!internal:custom") : Key.of(recipe.result().id()), result), recipe.result().count())
);
SmithingTransformRecipe transformRecipe = new SmithingTransformRecipe(key, result,
new RecipeChoice.MaterialChoice(new ArrayList<>(templateMaterials)),
new RecipeChoice.MaterialChoice(new ArrayList<>(baseMaterials)),
new RecipeChoice.MaterialChoice(new ArrayList<>(additionMaterials)),
true
);
if (hasCustomItemInTag) {
callback.accept(() -> {
try {
unregisterRecipe(key);
Reflections.method$CraftRecipe$addToCraftingManager.invoke(Reflections.method$CraftSmithingTransformRecipe$fromBukkitRecipe.invoke(null, transformRecipe));
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to convert smelting recipe", e);
}
});
this.injectedDataPackRecipes.add(key);
}
this.addInternalRecipe(id, ceRecipe);
}
private List<Material> tagToMaterials(Key tag) { private List<Material> tagToMaterials(Key tag) {
Set<Material> materials = new HashSet<>(); Set<Material> materials = new HashSet<>();
List<Holder<Key>> holders = this.plugin.itemManager().tagToVanillaItems(tag); List<Holder<Key>> holders = this.plugin.itemManager().tagToVanillaItems(tag);
@@ -766,12 +837,16 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
return Key.of(prefix, fileName); return Key.of(prefix, fileName);
} }
private static List<Material> ingredientToBukkitMaterials(Ingredient<ItemStack> ingredient) { private static RecipeChoice ingredientToBukkitRecipeChoice(Ingredient<ItemStack> ingredient) {
Set<Material> materials = new HashSet<>(); if (ingredient == null) {
for (Holder<Key> holder : ingredient.items()) { return EmptyRecipeChoice.INSTANCE;
materials.add(getMaterialById(holder.value())); } else {
Set<Material> materials = new HashSet<>();
for (Holder<Key> holder : ingredient.items()) {
materials.add(getMaterialById(holder.value()));
}
return new RecipeChoice.MaterialChoice(new ArrayList<>(materials));
} }
return new ArrayList<>(materials);
} }
private static Material getMaterialById(Key key) { private static Material getMaterialById(Key key) {

View File

@@ -0,0 +1,35 @@
package net.momirealms.craftengine.bukkit.item.recipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.jetbrains.annotations.NotNull;
record EmptyRecipeChoice() implements RecipeChoice {
static final RecipeChoice INSTANCE = new EmptyRecipeChoice();
@Override
@NotNull
public ItemStack getItemStack() {
throw new UnsupportedOperationException("This is an empty RecipeChoice");
}
@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
@NotNull
public RecipeChoice clone() {
return this;
}
@Override
public boolean test(final @NotNull ItemStack itemStack) {
return false;
}
@Override
@NotNull
public RecipeChoice validate(final boolean allowEmptyRecipes) {
if (allowEmptyRecipes) return this;
throw new IllegalArgumentException("empty RecipeChoice isn't allowed here");
}
}

View File

@@ -838,5 +838,13 @@ public class RecipeEventListener implements Listener {
SmithingInventory inventory = event.getInventory(); SmithingInventory inventory = event.getInventory();
if (!(inventory.getRecipe() instanceof SmithingTransformRecipe recipe)) return; if (!(inventory.getRecipe() instanceof SmithingTransformRecipe recipe)) return;
Key recipeId = Key.of(recipe.getKey().namespace(), recipe.getKey().value());
boolean isCustom = this.recipeManager.isCustomRecipe(recipeId);
// Maybe it's recipe from other plugins, then we ignore it
if (!isCustom) {
return;
}
} }
} }

View File

@@ -4125,6 +4125,18 @@ public class Reflections {
) )
); );
public static final Class<?> clazz$CraftSmithingTransformRecipe = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleCBClass("inventory.CraftSmithingTransformRecipe")
)
);
public static final Method method$CraftSmithingTransformRecipe$fromBukkitRecipe = requireNonNull(
ReflectionUtils.getStaticMethod(
clazz$CraftSmithingTransformRecipe, clazz$CraftSmithingTransformRecipe, SmithingTransformRecipe.class
)
);
public static final Class<?> clazz$FeatureFlagSet = requireNonNull( public static final Class<?> clazz$FeatureFlagSet = requireNonNull(
ReflectionUtils.getClazz( ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("world.flag.FeatureFlagSet") BukkitReflectionUtils.assembleMCClass("world.flag.FeatureFlagSet")

View File

@@ -21,11 +21,11 @@ public class CustomBlastingRecipe<T> extends CustomCookingRecipe<T> {
return RecipeTypes.BLASTING; return RecipeTypes.BLASTING;
} }
public static class Factory<A> implements RecipeFactory<CustomBlastingRecipe<A>> { public static class Factory<A> implements RecipeFactory<A> {
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
@Override @Override
public Recipe<CustomBlastingRecipe<A>> create(Key id, Map<String, Object> arguments) { public Recipe<A> create(Key id, Map<String, Object> arguments) {
CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; 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; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));

View File

@@ -21,11 +21,11 @@ public class CustomCampfireRecipe<T> extends CustomCookingRecipe<T> {
return RecipeTypes.CAMPFIRE_COOKING; return RecipeTypes.CAMPFIRE_COOKING;
} }
public static class Factory<A> implements RecipeFactory<CustomCampfireRecipe<A>> { public static class Factory<A> implements RecipeFactory<A> {
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
@Override @Override
public Recipe<CustomCampfireRecipe<A>> create(Key id, Map<String, Object> arguments) { public Recipe<A> create(Key id, Map<String, Object> arguments) {
CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; 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; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));

View File

@@ -21,11 +21,11 @@ public class CustomSmeltingRecipe<T> extends CustomCookingRecipe<T> {
return RecipeTypes.SMELTING; return RecipeTypes.SMELTING;
} }
public static class Factory<A> implements RecipeFactory<CustomSmeltingRecipe<A>> { public static class Factory<A> implements RecipeFactory<A> {
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
@Override @Override
public Recipe<CustomSmeltingRecipe<A>> create(Key id, Map<String, Object> arguments) { public Recipe<A> create(Key id, Map<String, Object> arguments) {
CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; 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; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));

View File

@@ -2,39 +2,69 @@ package net.momirealms.craftengine.core.item.recipe;
import net.momirealms.craftengine.core.item.ItemBuildContext; import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.recipe.input.RecipeInput; import net.momirealms.craftengine.core.item.recipe.input.RecipeInput;
import net.momirealms.craftengine.core.item.recipe.input.SmithingInput;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.*;
public class CustomSmithingTransformRecipe<T> implements Recipe<T> { public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
public static final Factory<?> FACTORY = new Factory<>();
private final Key id; private final Key id;
private final CustomRecipeResult<T> result; private final CustomRecipeResult<T> result;
private final Ingredient<T> template;
private final Ingredient<T> base; private final Ingredient<T> base;
private final Ingredient<T> template;
private final Ingredient<T> addition; private final Ingredient<T> addition;
public CustomSmithingTransformRecipe(Key id, public CustomSmithingTransformRecipe(Key id,
CustomRecipeResult<T> result, @Nullable Ingredient<T> addition,
Ingredient<T> template, @Nullable Ingredient<T> base,
Ingredient<T> base, @Nullable Ingredient<T> template,
Ingredient<T> addition CustomRecipeResult<T> result
) { ) {
this.id = id; this.id = id;
this.result = result; this.result = result;
this.template = template;
this.base = base; this.base = base;
this.template = template;
this.addition = addition; this.addition = addition;
} }
@SuppressWarnings("unchecked")
@Override @Override
public boolean matches(RecipeInput input) { public boolean matches(RecipeInput input) {
return false; SmithingInput<T> smithingInput = (SmithingInput<T>) input;
return checkIngredient(this.base, smithingInput.base())
&& checkIngredient(this.template, smithingInput.template())
&& checkIngredient(this.addition, smithingInput.addition());
}
private boolean checkIngredient(Ingredient<T> ingredient, OptimizedIDItem<T> item) {
if (ingredient != null) {
if (item == null) {
return false;
}
return ingredient.test(item);
} else {
return item == null;
}
} }
@Override @Override
public List<Ingredient<T>> ingredientsInUse() { public List<Ingredient<T>> ingredientsInUse() {
return List.of(); List<Ingredient<T>> ingredients = new ArrayList<>();
ingredients.add(this.base);
if (this.template != null) {
ingredients.add(this.template);
}
if (this.addition != null) {
ingredients.add(this.addition);
}
return ingredients;
} }
@Override @Override
@@ -56,4 +86,46 @@ public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
public CustomRecipeResult<T> result() { public CustomRecipeResult<T> result() {
return this.result; return this.result;
} }
@Nullable
public Ingredient<T> base() {
return base;
}
@Nullable
public Ingredient<T> template() {
return template;
}
@Nullable
public Ingredient<T> addition() {
return addition;
}
@SuppressWarnings({"DuplicatedCode"})
public static class Factory<A> implements RecipeFactory<A> {
@Override
public Recipe<A> create(Key id, Map<String, Object> arguments) {
List<String> base = MiscUtils.getAsStringList(arguments.get("base"));
List<String> addition = MiscUtils.getAsStringList(arguments.get("addition"));
List<String> template = MiscUtils.getAsStringList(arguments.get("template"));
return new CustomSmithingTransformRecipe<>(
id,
toIngredient(addition), toIngredient(base), toIngredient(template), parseResult(arguments)
);
}
private Ingredient<A> toIngredient(List<String> items) {
Set<Holder<Key>> holders = new HashSet<>();
for (String item : items) {
if (item.charAt(0) == '#') {
holders.addAll(CraftEngine.instance().itemManager().tagToItems(Key.of(item.substring(1))));
} else {
holders.add(BuiltInRegistries.OPTIMIZED_ITEM_ID.get(Key.of(item)).orElseThrow(() -> new IllegalArgumentException("Invalid vanilla/custom item: " + item)));
}
}
return Ingredient.of(holders);
}
}
} }

View File

@@ -21,11 +21,11 @@ public class CustomSmokingRecipe<T> extends CustomCookingRecipe<T> {
return RecipeTypes.SMOKING; return RecipeTypes.SMOKING;
} }
public static class Factory<A> implements RecipeFactory<CustomSmokingRecipe<A>> { public static class Factory<A> implements RecipeFactory<A> {
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"}) @SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
@Override @Override
public Recipe<CustomSmokingRecipe<A>> create(Key id, Map<String, Object> arguments) { public Recipe<A> create(Key id, Map<String, Object> arguments) {
CookingRecipeCategory recipeCategory = arguments.containsKey("category") ? CookingRecipeCategory.valueOf(arguments.get("category").toString().toUpperCase(Locale.ENGLISH)) : null; 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; String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80)); int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));

View File

@@ -18,6 +18,7 @@ public class RecipeTypes {
public static final Key CAMPFIRE_COOKING = Key.of("minecraft:campfire_cooking"); public static final Key CAMPFIRE_COOKING = Key.of("minecraft:campfire_cooking");
public static final Key STONE_CUTTING = Key.of("minecraft:stone_cutting"); public static final Key STONE_CUTTING = Key.of("minecraft:stone_cutting");
public static final Key SMITHING_TRANSFORM = Key.of("minecraft:smithing_transform"); public static final Key SMITHING_TRANSFORM = Key.of("minecraft:smithing_transform");
public static final Key SMITHING_TRIM = Key.of("minecraft:smithing_trim");
static { static {
register(SHAPED, CustomShapedRecipe.FACTORY); register(SHAPED, CustomShapedRecipe.FACTORY);
@@ -27,6 +28,7 @@ public class RecipeTypes {
register(BLASTING, CustomBlastingRecipe.FACTORY); register(BLASTING, CustomBlastingRecipe.FACTORY);
register(CAMPFIRE_COOKING, CustomCampfireRecipe.FACTORY); register(CAMPFIRE_COOKING, CustomCampfireRecipe.FACTORY);
register(STONE_CUTTING, CustomStoneCuttingRecipe.FACTORY); register(STONE_CUTTING, CustomStoneCuttingRecipe.FACTORY);
register(SMITHING_TRANSFORM, CustomSmithingTransformRecipe.FACTORY);
} }
public static <T> void register(Key key, RecipeFactory<T> factory) { public static <T> void register(Key key, RecipeFactory<T> factory) {

View File

@@ -0,0 +1,33 @@
package net.momirealms.craftengine.core.item.recipe.input;
import net.momirealms.craftengine.core.item.recipe.OptimizedIDItem;
import org.jetbrains.annotations.Nullable;
public class SmithingInput<T> implements RecipeInput {
private final OptimizedIDItem<T> base;
private final OptimizedIDItem<T> template;
private final OptimizedIDItem<T> addition;
public SmithingInput(@Nullable OptimizedIDItem<T> base,
@Nullable OptimizedIDItem<T> template,
@Nullable OptimizedIDItem<T> addition) {
this.base = base;
this.template = template;
this.addition = addition;
}
@Nullable
public OptimizedIDItem<T> base() {
return base;
}
@Nullable
public OptimizedIDItem<T> template() {
return template;
}
@Nullable
public OptimizedIDItem<T> addition() {
return addition;
}
}

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
@@ -9,4 +11,9 @@ public class VanillaBlastingRecipe extends VanillaCookingRecipe {
public VanillaBlastingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) { public VanillaBlastingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
super(category, group, result, ingredient, experience, cookingTime); super(category, group, result, ingredient, experience, cookingTime);
} }
@Override
public Key type() {
return RecipeTypes.BLASTING;
}
} }

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
@@ -9,4 +11,9 @@ public class VanillaCampfireRecipe extends VanillaCookingRecipe {
public VanillaCampfireRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) { public VanillaCampfireRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
super(category, group, result, ingredient, experience, cookingTime); super(category, group, result, ingredient, experience, cookingTime);
} }
@Override
public Key type() {
return RecipeTypes.CAMPFIRE_COOKING;
}
} }

View File

@@ -4,7 +4,7 @@ import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory;
import java.util.List; import java.util.List;
public abstract class VanillaCookingRecipe extends VanillaRecipe { public abstract class VanillaCookingRecipe extends VanillaGroupedRecipe {
protected final List<String> ingredient; protected final List<String> ingredient;
protected final CookingRecipeCategory category; protected final CookingRecipeCategory category;
protected final float experience; protected final float experience;

View File

@@ -2,7 +2,7 @@ package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory;
public class VanillaCraftingRecipe extends VanillaRecipe { public abstract class VanillaCraftingRecipe extends VanillaGroupedRecipe {
protected final CraftingRecipeCategory category; protected final CraftingRecipeCategory category;
protected VanillaCraftingRecipe(CraftingRecipeCategory category, String group, RecipeResult result) { protected VanillaCraftingRecipe(CraftingRecipeCategory category, String group, RecipeResult result) {

View File

@@ -0,0 +1,20 @@
package net.momirealms.craftengine.core.item.recipe.vanilla;
public abstract class VanillaGroupedRecipe implements VanillaRecipe {
protected final String group;
protected final RecipeResult result;
protected VanillaGroupedRecipe(String group, RecipeResult result) {
this.group = group;
this.result = result;
}
public String group() {
return group;
}
@Override
public RecipeResult result() {
return result;
}
}

View File

@@ -1,19 +1,10 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
public abstract class VanillaRecipe { import net.momirealms.craftengine.core.util.Key;
protected final String group;
protected final RecipeResult result;
protected VanillaRecipe(String group, RecipeResult result) { public interface VanillaRecipe {
this.group = group;
this.result = result;
}
public String group() { Key type();
return group;
}
public RecipeResult result() { RecipeResult result();
return result;
}
} }

View File

@@ -17,4 +17,6 @@ public interface VanillaRecipeReader {
VanillaCampfireRecipe readCampfire(JsonObject json); VanillaCampfireRecipe readCampfire(JsonObject json);
VanillaStoneCuttingRecipe readStoneCutting(JsonObject json); VanillaStoneCuttingRecipe readStoneCutting(JsonObject json);
VanillaSmithingTransformRecipe readSmithingTransform(JsonObject json);
} }

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -26,4 +28,9 @@ public class VanillaShapedRecipe extends VanillaCraftingRecipe {
public String[] pattern() { public String[] pattern() {
return pattern; return pattern;
} }
@Override
public Key type() {
return RecipeTypes.SHAPED;
}
} }

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
@@ -15,4 +17,9 @@ public class VanillaShapelessRecipe extends VanillaCraftingRecipe {
public List<List<String>> ingredients() { public List<List<String>> ingredients() {
return ingredients; return ingredients;
} }
@Override
public Key type() {
return RecipeTypes.SHAPELESS;
}
} }

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
@@ -9,4 +11,9 @@ public class VanillaSmeltingRecipe extends VanillaCookingRecipe {
public VanillaSmeltingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) { public VanillaSmeltingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
super(category, group, result, ingredient, experience, cookingTime); super(category, group, result, ingredient, experience, cookingTime);
} }
@Override
public Key type() {
return RecipeTypes.SMELTING;
}
} }

View File

@@ -0,0 +1,42 @@
package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List;
public class VanillaSmithingTransformRecipe implements VanillaRecipe {
private final RecipeResult result;
private final List<String> base;
private final List<String> template;
private final List<String> addition;
public VanillaSmithingTransformRecipe(List<String> addition, List<String> base, List<String> template, RecipeResult result) {
this.result = result;
this.base = base;
this.template = template;
this.addition = addition;
}
@Override
public Key type() {
return RecipeTypes.SMITHING_TRANSFORM;
}
@Override
public RecipeResult result() {
return result;
}
public List<String> base() {
return base;
}
public List<String> template() {
return template;
}
public List<String> addition() {
return addition;
}
}

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory; import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
@@ -9,4 +11,9 @@ public class VanillaSmokingRecipe extends VanillaCookingRecipe {
public VanillaSmokingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) { public VanillaSmokingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
super(category, group, result, ingredient, experience, cookingTime); super(category, group, result, ingredient, experience, cookingTime);
} }
@Override
public Key type() {
return RecipeTypes.SMOKING;
}
} }

View File

@@ -1,8 +1,11 @@
package net.momirealms.craftengine.core.item.recipe.vanilla; package net.momirealms.craftengine.core.item.recipe.vanilla;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.util.Key;
import java.util.List; import java.util.List;
public class VanillaStoneCuttingRecipe extends VanillaRecipe { public class VanillaStoneCuttingRecipe extends VanillaGroupedRecipe {
private final List<String> ingredient; private final List<String> ingredient;
public VanillaStoneCuttingRecipe(String group, RecipeResult result, List<String> ingredient) { public VanillaStoneCuttingRecipe(String group, RecipeResult result, List<String> ingredient) {
@@ -13,4 +16,9 @@ public class VanillaStoneCuttingRecipe extends VanillaRecipe {
public List<String> ingredient() { public List<String> ingredient() {
return ingredient; return ingredient;
} }
@Override
public Key type() {
return RecipeTypes.STONE_CUTTING;
}
} }

View File

@@ -91,6 +91,16 @@ public class VanillaRecipeReader1_20 extends AbstractRecipeReader {
); );
} }
@Override
public VanillaSmithingTransformRecipe readSmithingTransform(JsonObject json) {
return new VanillaSmithingTransformRecipe(
readSingleIngredient(json.get("base")),
readSingleIngredient(json.get("template")),
readSingleIngredient(json.get("addition")),
readSmithingResult(json.getAsJsonObject("result"))
);
}
protected List<String> readSingleIngredient(JsonElement json) { protected List<String> readSingleIngredient(JsonElement json) {
List<String> ingredients = new ArrayList<>(); List<String> ingredients = new ArrayList<>();
if (json.isJsonObject()) { if (json.isJsonObject()) {
@@ -126,6 +136,12 @@ public class VanillaRecipeReader1_20 extends AbstractRecipeReader {
return new RecipeResult(item, count, null); return new RecipeResult(item, count, null);
} }
@NotNull
protected RecipeResult readSmithingResult(JsonObject object) {
String item = object.get("item").getAsString();
return new RecipeResult(item, 1, null);
}
protected List<List<String>> readShapelessIngredients(JsonArray json) { protected List<List<String>> readShapelessIngredients(JsonArray json) {
List<List<String>> ingredients = new ArrayList<>(); List<List<String>> ingredients = new ArrayList<>();
for (JsonElement element : json) { for (JsonElement element : json) {

View File

@@ -26,4 +26,9 @@ public class VanillaRecipeReader1_20_5 extends VanillaRecipeReader1_20 {
protected RecipeResult readStoneCuttingResult(JsonObject json) { protected RecipeResult readStoneCuttingResult(JsonObject json) {
return readCraftingResult(json.getAsJsonObject("result")); return readCraftingResult(json.getAsJsonObject("result"));
} }
@Override
protected @NotNull RecipeResult readSmithingResult(JsonObject object) {
return readCraftingResult(object.getAsJsonObject());
}
} }