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

优化物品构建,避免使用Bukkit ItemStack

This commit is contained in:
XiaoMoMi
2025-06-13 03:12:53 +08:00
parent 76787a92f6
commit 0f6f5a5bef
3 changed files with 48 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
package net.momirealms.craftengine.bukkit.item;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.core.item.*;
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
@@ -10,7 +10,6 @@ import net.momirealms.craftengine.core.plugin.context.event.EventTrigger;
import net.momirealms.craftengine.core.plugin.context.function.Function;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
@@ -19,22 +18,23 @@ import java.util.List;
import java.util.Map;
public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
private final Material material;
private final Object item;
private final Object clientItem;
public BukkitCustomItem(Holder<Key> id, Material material, Key materialKey, Key clientBoundMaterialKey,
public BukkitCustomItem(Holder<Key> id, Object item, Object clientItem, Key materialKey, Key clientBoundMaterialKey,
List<ItemBehavior> behaviors,
List<ItemDataModifier<ItemStack>> modifiers, List<ItemDataModifier<ItemStack>> clientBoundModifiers,
ItemSettings settings,
Map<EventTrigger, List<Function<PlayerOptionalContext>>> events) {
super(id, materialKey, clientBoundMaterialKey, behaviors, modifiers, clientBoundModifiers, settings, events);
this.material = material;
this.item = item;
this.clientItem = clientItem;
}
@Override
public ItemStack buildItemStack(ItemBuildContext context, int count) {
ItemStack item = new ItemStack(this.material);
ItemStack item = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.constructor$ItemStack(this.item, count));
Item<ItemStack> wrapped = BukkitCraftEngine.instance().itemManager().wrap(item);
wrapped.count(count);
for (ItemDataModifier<ItemStack> modifier : this.modifiers) {
modifier.apply(wrapped, context);
}
@@ -43,7 +43,7 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
@Override
public Item<ItemStack> buildItem(ItemBuildContext context) {
ItemStack item = new ItemStack(this.material);
ItemStack item = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(FastNMS.INSTANCE.constructor$ItemStack(this.item, 1));
Item<ItemStack> wrapped = BukkitCraftEngine.instance().itemManager().wrap(item);
for (ItemDataModifier<ItemStack> modifier : dataModifiers()) {
modifier.apply(wrapped, context);
@@ -51,24 +51,33 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
return BukkitCraftEngine.instance().itemManager().wrap(wrapped.load());
}
public static Builder<ItemStack> builder(Material material) {
return new BuilderImpl(material);
public Object clientItem() {
return clientItem;
}
public Object item() {
return item;
}
public static Builder<ItemStack> builder(Object item, Object clientBoundItem) {
return new BuilderImpl(item, clientBoundItem);
}
public static class BuilderImpl implements Builder<ItemStack> {
private Holder<Key> id;
private final Key materialKey;
private final Material material;
private Key clientBoundMaterialKey;
private Key itemKey;
private final Object item;
private Key clientBoundItemKey;
private final Object clientBoundItem;
private final Map<EventTrigger, List<Function<PlayerOptionalContext>>> events = new EnumMap<>(EventTrigger.class);
private final List<ItemBehavior> behaviors = new ArrayList<>(4);
private final List<ItemDataModifier<ItemStack>> modifiers = new ArrayList<>(4);
private final List<ItemDataModifier<ItemStack>> clientBoundModifiers = new ArrayList<>(4);
private ItemSettings settings;
public BuilderImpl(Material material) {
this.material = material;
this.materialKey = KeyUtils.namespacedKey2Key(material.getKey());
public BuilderImpl(Object item, Object clientBoundItem) {
this.item = item;
this.clientBoundItem = clientBoundItem;
}
@Override
@@ -78,8 +87,14 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
}
@Override
public Builder<ItemStack> clientBoundMaterial(Key clientBoundMaterialKey) {
this.clientBoundMaterialKey = clientBoundMaterialKey;
public Builder<ItemStack> clientBoundMaterial(Key clientBoundMaterial) {
this.clientBoundItemKey = clientBoundMaterial;
return this;
}
@Override
public Builder<ItemStack> material(Key material) {
this.itemKey = material;
return this;
}
@@ -134,7 +149,7 @@ public class BukkitCustomItem extends AbstractCustomItem<ItemStack> {
@Override
public CustomItem<ItemStack> build() {
this.modifiers.addAll(this.settings.modifiers());
return new BukkitCustomItem(this.id, this.material, this.materialKey, this.clientBoundMaterialKey, List.copyOf(this.behaviors),
return new BukkitCustomItem(this.id, this.item, this.clientBoundItem, this.itemKey, this.clientBoundItemKey, List.copyOf(this.behaviors),
List.copyOf(this.modifiers), List.copyOf(this.clientBoundModifiers), this.settings, this.events);
}
}

View File

@@ -13,6 +13,7 @@ import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBuiltInRegistries;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MRegistries;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MRegistryOps;
import net.momirealms.craftengine.bukkit.util.ItemUtils;
import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.core.entity.player.Player;
@@ -201,11 +202,18 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
@Override
protected CustomItem.Builder<ItemStack> createPlatformItemBuilder(Holder<Key> id, Key materialId, Key clientBoundMaterialId) {
Material material = ResourceConfigUtils.requireNonNullOrThrow(Registry.MATERIAL.get(KeyUtils.toNamespacedKey(materialId)), () -> new LocalizedResourceConfigException("warning.config.item.invalid_material", materialId.toString()));
if (!clientBoundMaterialId.equals(materialId)) {
ResourceConfigUtils.requireNonNullOrThrow(Registry.MATERIAL.get(KeyUtils.toNamespacedKey(clientBoundMaterialId)), () -> new LocalizedResourceConfigException("warning.config.item.invalid_material", clientBoundMaterialId.toString()));
Object item = FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(materialId));
Object clientBoundItem = materialId == clientBoundMaterialId ? item : FastNMS.INSTANCE.method$Registry$getValue(MBuiltInRegistries.ITEM, KeyUtils.toResourceLocation(clientBoundMaterialId));
if (item == null) {
throw new LocalizedResourceConfigException("warning.config.item.invalid_material", materialId.toString());
}
return BukkitCustomItem.builder(material).id(id).clientBoundMaterial(clientBoundMaterialId);
if (clientBoundItem == null) {
throw new LocalizedResourceConfigException("warning.config.item.invalid_material", clientBoundMaterialId.toString());
}
return BukkitCustomItem.builder(item, clientBoundItem)
.id(id)
.material(materialId)
.clientBoundMaterial(clientBoundMaterialId);
}
@SuppressWarnings("unchecked")