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

add smithing transform

This commit is contained in:
XiaoMoMi
2025-03-19 20:25:21 +08:00
parent 917e1b5141
commit b298f76b86
6 changed files with 49 additions and 19 deletions

View File

@@ -126,18 +126,11 @@ public class UniversalItemFactory extends BukkitItemFactory {
@Override
protected Optional<Integer> maxDamage(ItemWrapper<ItemStack> item) {
// if (!item.hasTag("CustomFishing", "max_dur")) return Optional.empty();
// return Optional.of(item.get("CustomFishing", "max_dur"));
return Optional.of((int) item.getItem().getType().getMaxDurability());
}
@Override
protected void maxDamage(ItemWrapper<ItemStack> item, Integer damage) {
// if (damage == null) {
// item.remove("CustomFishing", "max_dur");
// } else {
// item.set(damage, "CustomFishing", "max_dur");
// }
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}

View File

@@ -780,7 +780,7 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
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);
CraftEngine.instance().logger().warn("Failed to convert transform recipe", e);
}
});
this.injectedDataPackRecipes.add(key);

View File

@@ -11,12 +11,11 @@ import net.momirealms.craftengine.bukkit.util.ItemUtils;
import net.momirealms.craftengine.bukkit.util.LegacyInventoryUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.item.*;
import net.momirealms.craftengine.core.item.recipe.CustomCampfireRecipe;
import net.momirealms.craftengine.core.item.recipe.OptimizedIDItem;
import net.momirealms.craftengine.core.item.recipe.Recipe;
import net.momirealms.craftengine.core.item.recipe.RecipeTypes;
import net.momirealms.craftengine.core.item.recipe.*;
import net.momirealms.craftengine.core.item.recipe.input.CraftingInput;
import net.momirealms.craftengine.core.item.recipe.input.SingleItemInput;
import net.momirealms.craftengine.core.item.recipe.input.SmithingInput;
import net.momirealms.craftengine.core.plugin.config.ConfigManager;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
@@ -38,6 +37,7 @@ import org.bukkit.event.inventory.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.*;
import org.bukkit.inventory.view.AnvilView;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@@ -845,6 +845,42 @@ public class RecipeEventListener implements Listener {
return;
}
ItemStack base = inventory.getInputEquipment();
ItemStack template = inventory.getInputTemplate();
ItemStack addition = inventory.getInputMineral();
SmithingInput<ItemStack> input = new SmithingInput<>(
getOptimizedIDItem(base),
getOptimizedIDItem(template),
getOptimizedIDItem(addition)
);
Recipe<ItemStack> ceRecipe = this.recipeManager.getRecipe(RecipeTypes.SMITHING_TRANSFORM, input);
if (ceRecipe == null) {
event.setResult(null);
return;
}
Player player;
try {
player = (Player) Reflections.method$InventoryView$getPlayer.invoke(event.getView());
} catch (ReflectiveOperationException e) {
this.plugin.logger().warn("Failed to get inventory viewer", e);
return;
}
CustomSmithingTransformRecipe<ItemStack> transformRecipe = (CustomSmithingTransformRecipe<ItemStack>) ceRecipe;
ItemStack processed = transformRecipe.assemble(new ItemBuildContext(this.plugin.adapt(player), ContextHolder.EMPTY), this.itemManager.wrap(base));
event.setResult(processed);
}
private OptimizedIDItem<ItemStack> getOptimizedIDItem(@Nullable ItemStack itemStack) {
if (ItemUtils.isEmpty(itemStack)) {
return EMPTY;
} else {
Item<ItemStack> wrappedItem = this.itemManager.wrap(itemStack);
Optional<Holder.Reference<Key>> idHolder = BuiltInRegistries.OPTIMIZED_ITEM_ID.get(wrappedItem.id());
return idHolder.map(keyReference -> new OptimizedIDItem<>(keyReference, itemStack)).orElse(EMPTY);
}
}
}

View File

@@ -143,6 +143,7 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
this.factory.customName(this.item, displayName);
return this;
}
@Override
public Item<I> itemName(String itemName) {
this.factory.itemName(this.item, itemName);

View File

@@ -25,9 +25,9 @@ public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
private final List<ItemDataProcessor> processors;
public CustomSmithingTransformRecipe(Key id,
@Nullable Ingredient<T> addition,
@Nullable Ingredient<T> base,
@Nullable Ingredient<T> template,
@Nullable Ingredient<T> addition,
CustomRecipeResult<T> result,
List<ItemDataProcessor> processors
) {
@@ -50,12 +50,12 @@ public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
private boolean checkIngredient(Ingredient<T> ingredient, OptimizedIDItem<T> item) {
if (ingredient != null) {
if (item == null) {
if (item == null || item.isEmpty()) {
return false;
}
return ingredient.test(item);
} else {
return item == null;
return item == null || item.isEmpty();
}
}
@@ -124,12 +124,12 @@ public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
@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"));
List<String> addition = MiscUtils.getAsStringList(arguments.get("addition-input"));
List<String> template = MiscUtils.getAsStringList(arguments.get("template-type"));
return new CustomSmithingTransformRecipe<>(
id,
toIngredient(addition), toIngredient(base), toIngredient(template), parseResult(arguments),
List.of(new ItemDataProcessor[]{})
toIngredient(base), toIngredient(template),toIngredient(addition), parseResult(arguments),
List.of(ItemDataProcessor.MERGE_ALL)
);
}

View File

@@ -11,7 +11,7 @@ public class VanillaSmithingTransformRecipe implements VanillaRecipe {
private final List<String> template;
private final List<String> addition;
public VanillaSmithingTransformRecipe(List<String> addition, List<String> base, List<String> template, RecipeResult result) {
public VanillaSmithingTransformRecipe(List<String> base, List<String> template, List<String> addition, RecipeResult result) {
this.result = result;
this.base = base;
this.template = template;