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

added some recipe methods

This commit is contained in:
XiaoMoMi
2025-03-19 05:26:17 +08:00
parent 4090501191
commit 917e1b5141
11 changed files with 110 additions and 6 deletions

View File

@@ -4,6 +4,8 @@ import com.saicone.rtag.RtagItem;
import net.momirealms.craftengine.core.item.ItemWrapper;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
@SuppressWarnings("UnstableApiUsage")
public class RTagItemWrapper implements ItemWrapper<ItemStack> {
private final RtagItem rtagItem;
@@ -110,4 +112,9 @@ public class RTagItemWrapper implements ItemWrapper<ItemStack> {
public ItemWrapper<ItemStack> copyWithCount(int count) {
return new RTagItemWrapper(new RtagItem(this.rtagItem.loadCopy()), count);
}
@Override
public Map<String, Object> getData() {
return this.rtagItem.get();
}
}

View File

@@ -1,7 +1,11 @@
package net.momirealms.craftengine.bukkit.item.factory;
import com.saicone.rtag.RtagItem;
import com.saicone.rtag.data.ComponentType;
import com.saicone.rtag.item.ItemObject;
import net.momirealms.craftengine.bukkit.item.RTagItemWrapper;
import net.momirealms.craftengine.bukkit.util.EnchantmentUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Enchantment;
import net.momirealms.craftengine.core.item.ItemWrapper;
@@ -287,4 +291,18 @@ public class ComponentItemFactory extends BukkitItemFactory {
if (!item.hasComponent(ComponentKeys.REPAIR_COST)) return Optional.empty();
return Optional.ofNullable((Integer) ComponentType.encodeJava(ComponentKeys.REPAIR_COST, item.getComponent(ComponentKeys.REPAIR_COST)).orElse(null));
}
@Override
protected ItemWrapper<ItemStack> merge(ItemWrapper<ItemStack> item1, ItemWrapper<ItemStack> item2) {
Object itemStack1 = item1.getLiteralObject();
Object itemStack2 = item2.getLiteralObject();
try {
Object itemStack3 = Reflections.method$ItemStack$transmuteCopy.invoke(itemStack1, Reflections.method$ItemStack$getItem.invoke(itemStack2), 1);
Reflections.method$ItemStack$applyComponents.invoke(itemStack3, Reflections.method$ItemStack$getComponentsPatch.invoke(itemStack2));
return new RTagItemWrapper(new RtagItem(ItemObject.asCraftMirror(itemStack3)), item2.count());
} catch (Exception e) {
this.plugin.logger().warn("Failed to merge item", e);
}
return null;
}
}

View File

@@ -1,8 +1,11 @@
package net.momirealms.craftengine.bukkit.item.factory;
import com.saicone.rtag.RtagItem;
import com.saicone.rtag.item.ItemObject;
import com.saicone.rtag.tag.TagBase;
import com.saicone.rtag.tag.TagCompound;
import com.saicone.rtag.tag.TagList;
import net.momirealms.craftengine.bukkit.item.RTagItemWrapper;
import net.momirealms.craftengine.core.item.Enchantment;
import net.momirealms.craftengine.core.item.ItemWrapper;
import net.momirealms.craftengine.core.plugin.CraftEngine;
@@ -229,4 +232,11 @@ public class UniversalItemFactory extends BukkitItemFactory {
if (!item.hasTag("RepairCost")) return Optional.empty();
return Optional.of(item.get("RepairCost"));
}
@Override
protected ItemWrapper<ItemStack> merge(ItemWrapper<ItemStack> item1, ItemWrapper<ItemStack> item2) {
Object itemStack = ItemObject.copy(item2.getLiteralObject());
ItemObject.setCustomDataTag(itemStack, ItemObject.getCustomDataTag(item1.getLiteralObject()));
return new RTagItemWrapper(new RtagItem(ItemObject.asCraftMirror(itemStack)), item2.count());
}
}

View File

@@ -764,14 +764,14 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
baseHolders.isEmpty() ? null : Ingredient.of(baseHolders),
templateHolders.isEmpty() ? null : Ingredient.of(templateHolders),
additionHolders.isEmpty() ? null : Ingredient.of(additionHolders),
new CustomRecipeResult<>(new CloneableConstantItem(recipe.result().isCustom() ? Key.of("!internal:custom") : Key.of(recipe.result().id()), result), recipe.result().count())
new CustomRecipeResult<>(new CloneableConstantItem(recipe.result().isCustom() ? Key.of("!internal:custom") : Key.of(recipe.result().id()), result), recipe.result().count()),
List.of(CustomSmithingTransformRecipe.ItemDataProcessor.MERGE_ALL)
);
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
new RecipeChoice.MaterialChoice(new ArrayList<>(additionMaterials))
);
if (hasCustomItemInTag) {

View File

@@ -4977,4 +4977,30 @@ public class Reflections {
clazz$CraftRecipe, clazz$Ingredient, RecipeChoice.class, boolean.class
)
);
// 1.20.5+
public static final Method method$ItemStack$transmuteCopy = ReflectionUtils.getMethod(
clazz$ItemStack, clazz$ItemStack, clazz$ItemLike, int.class
);
// 1.20.5+
public static final Class<?> clazz$DataComponentPatch = ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("core.component.DataComponentPatch")
);
// 1.20.5+
public static final Method method$ItemStack$getComponentsPatch = Optional.ofNullable(clazz$DataComponentPatch)
.map(it -> ReflectionUtils.getMethod(clazz$ItemStack, it))
.orElse(null);
// 1.20.5+
public static final Method method$ItemStack$applyComponents = Optional.ofNullable(clazz$DataComponentPatch)
.map(it -> ReflectionUtils.getMethod(clazz$ItemStack, void.class, it))
.orElse(null);
public static final Method method$ItemStack$getItem = requireNonNull(
ReflectionUtils.getMethod(
clazz$ItemStack, clazz$Item
)
);
}