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

添加读取物品id

This commit is contained in:
XiaoMoMi
2025-07-11 04:51:04 +08:00
parent 0e76a1a939
commit 7b8e2c641a
14 changed files with 61 additions and 45 deletions

View File

@@ -13,6 +13,7 @@ import net.momirealms.craftengine.core.item.data.JukeboxPlayable;
import net.momirealms.craftengine.core.item.setting.EquipmentData;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.UniqueKey;
import net.momirealms.sparrow.nbt.Tag;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
@@ -79,6 +80,11 @@ public abstract class BukkitItemFactory<W extends ItemWrapper<ItemStack>> extend
return item.getItem();
}
@Override
protected UniqueKey recipeIngredientID(W item) {
return UniqueKey.create(id(item));
}
@Override
protected boolean is(W item, Key itemTag) {
Object literalObject = item.getLiteralObject();

View File

@@ -24,7 +24,6 @@ import java.util.ArrayList;
import java.util.List;
public class CrafterEventListener implements Listener {
private static final OptimizedIDItem<ItemStack> EMPTY = new OptimizedIDItem<>(null, null);
private final ItemManager<ItemStack> itemManager;
private final BukkitRecipeManager recipeManager;
private final BukkitCraftEngine plugin;
@@ -57,11 +56,10 @@ public class CrafterEventListener implements Listener {
List<OptimizedIDItem<ItemStack>> optimizedIDItems = new ArrayList<>();
for (ItemStack itemStack : ingredients) {
if (ItemStackUtils.isEmpty(itemStack)) {
optimizedIDItems.add(EMPTY);
optimizedIDItems.add(RecipeEventListener.EMPTY);
} else {
Item<ItemStack> wrappedItem = this.itemManager.wrap(itemStack);
UniqueKey uniqueId = UniqueKey.create(wrappedItem.id());
optimizedIDItems.add(new OptimizedIDItem<>(uniqueId, itemStack));
optimizedIDItems.add(new OptimizedIDItem<>(wrappedItem.recipeIngredientId(), itemStack));
}
}

View File

@@ -49,7 +49,7 @@ import java.util.Optional;
@SuppressWarnings("DuplicatedCode")
public class RecipeEventListener implements Listener {
private static final OptimizedIDItem<ItemStack> EMPTY = new OptimizedIDItem<>(null, null);
public static final OptimizedIDItem<ItemStack> EMPTY = new OptimizedIDItem<>(null, null);
private final ItemManager<ItemStack> itemManager;
private final BukkitRecipeManager recipeManager;
private final BukkitCraftEngine plugin;
@@ -74,10 +74,7 @@ public class RecipeEventListener implements Listener {
ItemStack item = event.getCurrentItem();
if (ItemStackUtils.isEmpty(item)) return;
if (ItemStackUtils.isEmpty(fuelStack)) {
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(item);
UniqueKey uniqueKey = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(uniqueKey, item));
SingleItemInput<ItemStack> input = new SingleItemInput<>(getOptimizedIDItem(item));
Key recipeType;
if (furnaceInventory.getType() == InventoryType.FURNACE) {
recipeType = RecipeTypes.SMELTING;
@@ -354,9 +351,7 @@ public class RecipeEventListener implements Listener {
if (optionalMCRecipe.isEmpty()) {
return;
}
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey uniqueKey = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(uniqueKey, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(getOptimizedIDItem(itemStack));
CustomCampfireRecipe<ItemStack> ceRecipe = (CustomCampfireRecipe<ItemStack>) this.recipeManager.recipeByInput(RecipeTypes.CAMPFIRE_COOKING, input);
if (ceRecipe == null) {
event.setCancelled(true);
@@ -381,9 +376,7 @@ public class RecipeEventListener implements Listener {
}
ItemStack itemStack = event.getSource();
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey itemId = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(itemId, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(getOptimizedIDItem(itemStack));
CustomCampfireRecipe<ItemStack> ceRecipe = (CustomCampfireRecipe<ItemStack>) this.recipeManager.recipeByInput(RecipeTypes.CAMPFIRE_COOKING, input);
if (ceRecipe == null) {
event.setTotalCookTime(Integer.MAX_VALUE);
@@ -411,9 +404,7 @@ public class RecipeEventListener implements Listener {
}
ItemStack itemStack = event.getSource();
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey itemId = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(itemId, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(getOptimizedIDItem(itemStack));
CustomCampfireRecipe<ItemStack> ceRecipe = (CustomCampfireRecipe<ItemStack>) this.recipeManager.recipeByInput(RecipeTypes.CAMPFIRE_COOKING, input);
if (ceRecipe == null) {
event.setCancelled(true);
@@ -820,7 +811,7 @@ public class RecipeEventListener implements Listener {
if (ItemStackUtils.isEmpty(usedItem)) continue;
if (usedItem.getAmount() != 1) continue;
Item<ItemStack> wrapped = BukkitItemManager.instance().wrap(usedItem);
if (wrapped == null) continue;
if (ItemUtils.isEmpty(wrapped)) continue;
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
if (optionalCustomItem.isPresent()) {
CustomItem<ItemStack> customItem = optionalCustomItem.get();
@@ -871,13 +862,7 @@ public class RecipeEventListener implements Listener {
List<OptimizedIDItem<ItemStack>> optimizedIDItems = new ArrayList<>();
for (ItemStack itemStack : ingredients) {
if (ItemStackUtils.isEmpty(itemStack)) {
optimizedIDItems.add(EMPTY);
} else {
Item<ItemStack> wrappedItem = this.itemManager.wrap(itemStack);
UniqueKey itemUniqueId = UniqueKey.create(wrappedItem.id());
optimizedIDItems.add(new OptimizedIDItem<>(itemUniqueId, itemStack));
}
optimizedIDItems.add(getOptimizedIDItem(itemStack));
}
CraftingInput<ItemStack> input;
@@ -1016,7 +1001,7 @@ public class RecipeEventListener implements Listener {
return EMPTY;
} else {
Item<ItemStack> wrappedItem = this.itemManager.wrap(itemStack);
return new OptimizedIDItem<>(UniqueKey.create(wrappedItem.id()), itemStack);
return new OptimizedIDItem<>(wrappedItem.recipeIngredientId(), itemStack);
}
}
}

View File

@@ -134,9 +134,7 @@ public class RecipeInjector {
);
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey uniqueKey = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(uniqueKey, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(wrappedItem.recipeIngredientId(), itemStack));
CustomCookingRecipe<ItemStack> ceRecipe = (CustomCookingRecipe<ItemStack>) recipeManager.recipeByInput(injectedCacheCheck.customRecipeType(), input, injectedCacheCheck.lastCustomRecipe());
if (ceRecipe == null) {
return Optional.empty();
@@ -182,9 +180,7 @@ public class RecipeInjector {
}
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey uniqueKey = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(uniqueKey, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(wrappedItem.recipeIngredientId(), itemStack));
CustomCookingRecipe<ItemStack> ceRecipe = (CustomCookingRecipe<ItemStack>) recipeManager.recipeByInput(injectedCacheCheck.customRecipeType(), input, injectedCacheCheck.lastCustomRecipe());
if (ceRecipe == null) {
return Optional.empty();
@@ -225,9 +221,7 @@ public class RecipeInjector {
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.field$SingleRecipeInput$item(args[0]));
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey uniqueKey = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(uniqueKey, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(wrappedItem.recipeIngredientId(), itemStack));
CustomCookingRecipe<ItemStack> ceRecipe = (CustomCookingRecipe<ItemStack>) recipeManager.recipeByInput(injectedCacheCheck.customRecipeType(), input, injectedCacheCheck.lastCustomRecipe());
if (ceRecipe == null) {
return Optional.empty();
@@ -272,9 +266,7 @@ public class RecipeInjector {
// 获取唯一内存地址id
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.field$SingleRecipeInput$item(args[0]));
Item<ItemStack> wrappedItem = BukkitItemManager.instance().wrap(itemStack);
UniqueKey uniqueKey = UniqueKey.create(wrappedItem.id());
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(uniqueKey, itemStack));
SingleItemInput<ItemStack> input = new SingleItemInput<>(new OptimizedIDItem<>(wrappedItem.recipeIngredientId(), itemStack));
CustomCookingRecipe<ItemStack> ceRecipe = (CustomCookingRecipe<ItemStack>) recipeManager.recipeByInput(injectedCacheCheck.customRecipeType(), input, injectedCacheCheck.lastCustomRecipe());
// 这个ce配方并不存在那么应该返回空
if (ceRecipe == null) {

View File

@@ -79,16 +79,14 @@ public class InteractUtils {
});
registerInteraction(BlockKeys.SOUL_CAMPFIRE, (player, item, blockState, result) -> {
if (!Config.enableRecipeSystem()) return false;
UniqueKey uniqueKey = UniqueKey.create(item.id());
return BukkitRecipeManager.instance().recipeByInput(RecipeTypes.CAMPFIRE_COOKING, new SingleItemInput<>(new OptimizedIDItem<>(
uniqueKey, item.getItem()
item.recipeIngredientId(), item.getItem()
))) != null;
});
registerInteraction(BlockKeys.CAMPFIRE, (player, item, blockState, result) -> {
if (!Config.enableRecipeSystem()) return false;
UniqueKey uniqueKey = UniqueKey.create(item.id());
return BukkitRecipeManager.instance().recipeByInput(RecipeTypes.CAMPFIRE_COOKING, new SingleItemInput<>(new OptimizedIDItem<>(
uniqueKey, item.getItem()
item.recipeIngredientId(), item.getItem()
))) != null;
});
registerInteraction(BlockKeys.DECORATED_POT, (player, item, blockState, result) -> true);