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:
@@ -55,7 +55,20 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
|
||||
static {
|
||||
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) -> {
|
||||
CustomShapedRecipe<ItemStack> ceRecipe = (CustomShapedRecipe<ItemStack>) recipe;
|
||||
@@ -68,7 +81,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
}
|
||||
shapedRecipe.shape(ceRecipe.pattern().pattern());
|
||||
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 {
|
||||
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()));
|
||||
}
|
||||
for (Ingredient<ItemStack> ingredient : ceRecipe.ingredientsInUse()) {
|
||||
shapelessRecipe.addIngredient(new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ingredient)));
|
||||
shapelessRecipe.addIngredient(ingredientToBukkitRecipeChoice(ingredient));
|
||||
}
|
||||
try {
|
||||
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;
|
||||
FurnaceRecipe furnaceRecipe = new FurnaceRecipe(
|
||||
key, ceRecipe.result(ItemBuildContext.EMPTY),
|
||||
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())),
|
||||
ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
|
||||
ceRecipe.experience(), ceRecipe.cookingTime()
|
||||
);
|
||||
if (ceRecipe.group() != null) {
|
||||
@@ -123,7 +136,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
CustomSmokingRecipe<ItemStack> ceRecipe = (CustomSmokingRecipe<ItemStack>) recipe;
|
||||
SmokingRecipe smokingRecipe = new SmokingRecipe(
|
||||
key, ceRecipe.result(ItemBuildContext.EMPTY),
|
||||
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())),
|
||||
ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
|
||||
ceRecipe.experience(), ceRecipe.cookingTime()
|
||||
);
|
||||
if (ceRecipe.group() != null) {
|
||||
@@ -144,7 +157,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
CustomBlastingRecipe<ItemStack> ceRecipe = (CustomBlastingRecipe<ItemStack>) recipe;
|
||||
BlastingRecipe blastingRecipe = new BlastingRecipe(
|
||||
key, ceRecipe.result(ItemBuildContext.EMPTY),
|
||||
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())),
|
||||
ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
|
||||
ceRecipe.experience(), ceRecipe.cookingTime()
|
||||
);
|
||||
if (ceRecipe.group() != null) {
|
||||
@@ -165,7 +178,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
CustomCampfireRecipe<ItemStack> ceRecipe = (CustomCampfireRecipe<ItemStack>) recipe;
|
||||
CampfireRecipe campfireRecipe = new CampfireRecipe(
|
||||
key, ceRecipe.result(ItemBuildContext.EMPTY),
|
||||
new RecipeChoice.MaterialChoice(ingredientToBukkitMaterials(ceRecipe.ingredient())),
|
||||
ingredientToBukkitRecipeChoice(ceRecipe.ingredient()),
|
||||
ceRecipe.experience(), ceRecipe.cookingTime()
|
||||
);
|
||||
if (ceRecipe.group() != null) {
|
||||
@@ -495,6 +508,10 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
VanillaStoneCuttingRecipe recipe = this.recipeReader.readStoneCutting(jsonObject);
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
NamespacedKey key = new NamespacedKey(id.namespace(), id.value());
|
||||
ItemStack result = createResultStack(recipe.result());
|
||||
@@ -669,22 +709,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
Set<Material> materials = new HashSet<>();
|
||||
Set<Holder<Key>> holders = new HashSet<>();
|
||||
|
||||
boolean hasCustomItemInTag = false;
|
||||
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());
|
||||
}
|
||||
}
|
||||
boolean hasCustomItemInTag = readVanillaIngredients(false, recipe.ingredient(), materials::add, holders::add);
|
||||
org.bukkit.inventory.CookingRecipe<?> cookingRecipe = constructor1.apply(key, result, new RecipeChoice.MaterialChoice(new ArrayList<>(materials)), recipe.experience(), recipe.cookingTime());
|
||||
if (recipe.group() != null) {
|
||||
cookingRecipe.setGroup(recipe.group());
|
||||
@@ -717,6 +742,52 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
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) {
|
||||
Set<Material> materials = new HashSet<>();
|
||||
List<Holder<Key>> holders = this.plugin.itemManager().tagToVanillaItems(tag);
|
||||
@@ -766,12 +837,16 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
|
||||
return Key.of(prefix, fileName);
|
||||
}
|
||||
|
||||
private static List<Material> ingredientToBukkitMaterials(Ingredient<ItemStack> ingredient) {
|
||||
Set<Material> materials = new HashSet<>();
|
||||
for (Holder<Key> holder : ingredient.items()) {
|
||||
materials.add(getMaterialById(holder.value()));
|
||||
private static RecipeChoice ingredientToBukkitRecipeChoice(Ingredient<ItemStack> ingredient) {
|
||||
if (ingredient == null) {
|
||||
return EmptyRecipeChoice.INSTANCE;
|
||||
} 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) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -838,5 +838,13 @@ public class RecipeEventListener implements Listener {
|
||||
SmithingInventory inventory = event.getInventory();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("world.flag.FeatureFlagSet")
|
||||
|
||||
@@ -21,11 +21,11 @@ public class CustomBlastingRecipe<T> extends CustomCookingRecipe<T> {
|
||||
return RecipeTypes.BLASTING;
|
||||
}
|
||||
|
||||
public static class Factory<A> implements RecipeFactory<CustomBlastingRecipe<A>> {
|
||||
public static class Factory<A> implements RecipeFactory<A> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@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;
|
||||
String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
|
||||
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));
|
||||
|
||||
@@ -21,11 +21,11 @@ public class CustomCampfireRecipe<T> extends CustomCookingRecipe<T> {
|
||||
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"})
|
||||
@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;
|
||||
String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
|
||||
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));
|
||||
|
||||
@@ -21,11 +21,11 @@ public class CustomSmeltingRecipe<T> extends CustomCookingRecipe<T> {
|
||||
return RecipeTypes.SMELTING;
|
||||
}
|
||||
|
||||
public static class Factory<A> implements RecipeFactory<CustomSmeltingRecipe<A>> {
|
||||
public static class Factory<A> implements RecipeFactory<A> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@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;
|
||||
String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
|
||||
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));
|
||||
|
||||
@@ -2,39 +2,69 @@ package net.momirealms.craftengine.core.item.recipe;
|
||||
|
||||
import net.momirealms.craftengine.core.item.ItemBuildContext;
|
||||
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.MiscUtils;
|
||||
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 static final Factory<?> FACTORY = new Factory<>();
|
||||
private final Key id;
|
||||
private final CustomRecipeResult<T> result;
|
||||
private final Ingredient<T> template;
|
||||
private final Ingredient<T> base;
|
||||
private final Ingredient<T> template;
|
||||
private final Ingredient<T> addition;
|
||||
|
||||
public CustomSmithingTransformRecipe(Key id,
|
||||
CustomRecipeResult<T> result,
|
||||
Ingredient<T> template,
|
||||
Ingredient<T> base,
|
||||
Ingredient<T> addition
|
||||
@Nullable Ingredient<T> addition,
|
||||
@Nullable Ingredient<T> base,
|
||||
@Nullable Ingredient<T> template,
|
||||
CustomRecipeResult<T> result
|
||||
) {
|
||||
this.id = id;
|
||||
this.result = result;
|
||||
this.template = template;
|
||||
this.base = base;
|
||||
this.template = template;
|
||||
this.addition = addition;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
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
|
||||
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
|
||||
@@ -56,4 +86,46 @@ public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
|
||||
public CustomRecipeResult<T> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ public class CustomSmokingRecipe<T> extends CustomCookingRecipe<T> {
|
||||
return RecipeTypes.SMOKING;
|
||||
}
|
||||
|
||||
public static class Factory<A> implements RecipeFactory<CustomSmokingRecipe<A>> {
|
||||
public static class Factory<A> implements RecipeFactory<A> {
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "DuplicatedCode"})
|
||||
@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;
|
||||
String group = arguments.containsKey("group") ? arguments.get("group").toString() : null;
|
||||
int cookingTime = MiscUtils.getAsInt(arguments.getOrDefault("time", 80));
|
||||
|
||||
@@ -18,6 +18,7 @@ public class RecipeTypes {
|
||||
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 SMITHING_TRANSFORM = Key.of("minecraft:smithing_transform");
|
||||
public static final Key SMITHING_TRIM = Key.of("minecraft:smithing_trim");
|
||||
|
||||
static {
|
||||
register(SHAPED, CustomShapedRecipe.FACTORY);
|
||||
@@ -27,6 +28,7 @@ public class RecipeTypes {
|
||||
register(BLASTING, CustomBlastingRecipe.FACTORY);
|
||||
register(CAMPFIRE_COOKING, CustomCampfireRecipe.FACTORY);
|
||||
register(STONE_CUTTING, CustomStoneCuttingRecipe.FACTORY);
|
||||
register(SMITHING_TRANSFORM, CustomSmithingTransformRecipe.FACTORY);
|
||||
}
|
||||
|
||||
public static <T> void register(Key key, RecipeFactory<T> factory) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
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;
|
||||
|
||||
@@ -9,4 +11,9 @@ public class VanillaBlastingRecipe extends VanillaCookingRecipe {
|
||||
public VanillaBlastingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
|
||||
super(category, group, result, ingredient, experience, cookingTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.BLASTING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
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;
|
||||
|
||||
@@ -9,4 +11,9 @@ public class VanillaCampfireRecipe extends VanillaCookingRecipe {
|
||||
public VanillaCampfireRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
|
||||
super(category, group, result, ingredient, experience, cookingTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.CAMPFIRE_COOKING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import net.momirealms.craftengine.core.item.recipe.CookingRecipeCategory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class VanillaCookingRecipe extends VanillaRecipe {
|
||||
public abstract class VanillaCookingRecipe extends VanillaGroupedRecipe {
|
||||
protected final List<String> ingredient;
|
||||
protected final CookingRecipeCategory category;
|
||||
protected final float experience;
|
||||
|
||||
@@ -2,7 +2,7 @@ package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
import net.momirealms.craftengine.core.item.recipe.CraftingRecipeCategory;
|
||||
|
||||
public class VanillaCraftingRecipe extends VanillaRecipe {
|
||||
public abstract class VanillaCraftingRecipe extends VanillaGroupedRecipe {
|
||||
protected final CraftingRecipeCategory category;
|
||||
|
||||
protected VanillaCraftingRecipe(CraftingRecipeCategory category, String group, RecipeResult result) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,10 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
public abstract class VanillaRecipe {
|
||||
protected final String group;
|
||||
protected final RecipeResult result;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
protected VanillaRecipe(String group, RecipeResult result) {
|
||||
this.group = group;
|
||||
this.result = result;
|
||||
}
|
||||
public interface VanillaRecipe {
|
||||
|
||||
public String group() {
|
||||
return group;
|
||||
}
|
||||
Key type();
|
||||
|
||||
public RecipeResult result() {
|
||||
return result;
|
||||
}
|
||||
RecipeResult result();
|
||||
}
|
||||
|
||||
@@ -17,4 +17,6 @@ public interface VanillaRecipeReader {
|
||||
VanillaCampfireRecipe readCampfire(JsonObject json);
|
||||
|
||||
VanillaStoneCuttingRecipe readStoneCutting(JsonObject json);
|
||||
|
||||
VanillaSmithingTransformRecipe readSmithingTransform(JsonObject json);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
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.Map;
|
||||
@@ -26,4 +28,9 @@ public class VanillaShapedRecipe extends VanillaCraftingRecipe {
|
||||
public String[] pattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.SHAPED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
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;
|
||||
|
||||
@@ -15,4 +17,9 @@ public class VanillaShapelessRecipe extends VanillaCraftingRecipe {
|
||||
public List<List<String>> ingredients() {
|
||||
return ingredients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.SHAPELESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
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;
|
||||
|
||||
@@ -9,4 +11,9 @@ public class VanillaSmeltingRecipe extends VanillaCookingRecipe {
|
||||
public VanillaSmeltingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
|
||||
super(category, group, result, ingredient, experience, cookingTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.SMELTING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.momirealms.craftengine.core.item.recipe.vanilla;
|
||||
|
||||
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;
|
||||
|
||||
@@ -9,4 +11,9 @@ public class VanillaSmokingRecipe extends VanillaCookingRecipe {
|
||||
public VanillaSmokingRecipe(CookingRecipeCategory category, String group, RecipeResult result, List<String> ingredient, float experience, int cookingTime) {
|
||||
super(category, group, result, ingredient, experience, cookingTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.SMOKING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
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 VanillaStoneCuttingRecipe extends VanillaRecipe {
|
||||
public class VanillaStoneCuttingRecipe extends VanillaGroupedRecipe {
|
||||
private final 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() {
|
||||
return ingredient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return RecipeTypes.STONE_CUTTING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
List<String> ingredients = new ArrayList<>();
|
||||
if (json.isJsonObject()) {
|
||||
@@ -126,6 +136,12 @@ public class VanillaRecipeReader1_20 extends AbstractRecipeReader {
|
||||
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) {
|
||||
List<List<String>> ingredients = new ArrayList<>();
|
||||
for (JsonElement element : json) {
|
||||
|
||||
@@ -26,4 +26,9 @@ public class VanillaRecipeReader1_20_5 extends VanillaRecipeReader1_20 {
|
||||
protected RecipeResult readStoneCuttingResult(JsonObject json) {
|
||||
return readCraftingResult(json.getAsJsonObject("result"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull RecipeResult readSmithingResult(JsonObject object) {
|
||||
return readCraftingResult(object.getAsJsonObject());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user