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

优化配方书匹配

This commit is contained in:
XiaoMoMi
2025-08-22 19:36:52 +08:00
parent 8157ad3099
commit 6f6bb4c9da
6 changed files with 63 additions and 23 deletions

View File

@@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.item.recipe;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.papermc.paper.potion.PotionMix;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
@@ -85,7 +84,6 @@ public class BukkitRecipeManager extends AbstractRecipeManager<ItemStack> {
}
}
private static final List<Object> MODIFIED_INGREDIENTS = new ArrayList<>();
private static final Map<Key, Function<Recipe<ItemStack>, Object>> ADD_RECIPE_FOR_MINECRAFT_RECIPE_HOLDER = Map.of(
RecipeSerializers.SHAPED, recipe -> {
CustomShapedRecipe<ItemStack> shapedRecipe = (CustomShapedRecipe<ItemStack>) recipe;
@@ -245,9 +243,9 @@ public class BukkitRecipeManager extends AbstractRecipeManager<ItemStack> {
Ingredient<ItemStack> actualIngredient = actualIngredients.get(i);
List<Object> items = getIngredientLooks(actualIngredient.items());
if (VersionHelper.isOrAbove1_21_4()) {
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (Set<Object>) new ObjectOpenHashSet<>(items));
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (Set<Object>) new CustomIngredientSet(items, actualIngredient));
} else if (VersionHelper.isOrAbove1_21_2()) {
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (List<Object>) items);
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (List<Object>) new CustomIngredientList(items, actualIngredient));
} else {
Object itemStackArray = Array.newInstance(CoreReflections.clazz$ItemStack, items.size());
for (int j = 0; j < items.size(); j++) {
@@ -255,7 +253,6 @@ public class BukkitRecipeManager extends AbstractRecipeManager<ItemStack> {
}
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (Object) itemStackArray);
}
MODIFIED_INGREDIENTS.add(ingredient);
}
}
@@ -498,20 +495,6 @@ public class BukkitRecipeManager extends AbstractRecipeManager<ItemStack> {
// send to players
CoreReflections.methodHandle$DedicatedPlayerList$reloadRecipes.invokeExact(CraftBukkitReflections.methodHandle$CraftServer$playerListGetter.invokeExact(Bukkit.getServer()));
// now we need to remove the fake `exact` choices
if (VersionHelper.isOrAbove1_21_4()) {
for (Object ingredient : MODIFIED_INGREDIENTS) {
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (Set<Object>) null);
}
} else if (VersionHelper.isOrAbove1_21_2()) {
for (Object ingredient : MODIFIED_INGREDIENTS) {
CoreReflections.methodHandle$Ingredient$itemStacksSetter.invokeExact(ingredient, (List<Object>) null);
}
}
// clear cache
MODIFIED_INGREDIENTS.clear();
} catch (Throwable e) {
this.plugin.logger().warn("Failed to run delayed recipe tasks", e);
}

View File

@@ -0,0 +1,29 @@
package net.momirealms.craftengine.bukkit.item.recipe;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.core.item.recipe.Ingredient;
import net.momirealms.craftengine.core.item.recipe.UniqueIdItem;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class CustomIngredientList extends ArrayList<Object> {
private final Ingredient<ItemStack> ingredient;
public CustomIngredientList(@NotNull Collection<?> c, Ingredient<ItemStack> ingredient) {
super(c);
this.ingredient = ingredient;
}
@Override
public boolean contains(Object o) {
if (o == null || FastNMS.INSTANCE.method$ItemStack$isEmpty(o)) {
return false;
}
return this.ingredient.test(UniqueIdItem.of(BukkitItemManager.instance().wrap(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(o))));
}
}

View File

@@ -0,0 +1,28 @@
package net.momirealms.craftengine.bukkit.item.recipe;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.core.item.recipe.Ingredient;
import net.momirealms.craftengine.core.item.recipe.UniqueIdItem;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashSet;
public class CustomIngredientSet extends HashSet<Object> {
private final Ingredient<ItemStack> ingredient;
public CustomIngredientSet(@NotNull Collection<?> c, Ingredient<ItemStack> ingredient) {
super(c);
this.ingredient = ingredient;
}
@Override
public boolean contains(Object o) {
if (o == null || FastNMS.INSTANCE.method$ItemStack$isEmpty(o)) {
return false;
}
return this.ingredient.test(UniqueIdItem.of(BukkitItemManager.instance().wrap(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(o))));
}
}