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

添加attribute-modifiers

This commit is contained in:
XiaoMoMi
2025-07-06 05:21:25 +08:00
parent ace2ff8259
commit d165d83d9b
34 changed files with 474 additions and 36 deletions

View File

@@ -72,8 +72,18 @@ public class ComponentItemWrapper implements ItemWrapper<ItemStack> {
return getComponentInternal(type, MRegistryOps.NBT);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public Optional<Tag> getSparrowNBTComponent(Object type) {
return getComponentInternal(type, MRegistryOps.SPARROW_NBT);
Object componentType = ensureDataComponentType(type);
Codec codec = FastNMS.INSTANCE.method$DataComponentType$codec(componentType);
try {
Object componentData = FastNMS.INSTANCE.method$ItemStack$getComponent(getLiteralObject(), componentType);
if (componentData == null) return Optional.empty();
DataResult<Tag> result = codec.encodeStart(MRegistryOps.SPARROW_NBT, componentData);
return result.result().map(Tag::copy);
} catch (Throwable t) {
throw new RuntimeException("Cannot read component " + type.toString(), t);
}
}
@SuppressWarnings({"rawtypes", "unchecked"})

View File

@@ -29,7 +29,7 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
@Override
public Optional<Item<ItemStack>> c2s(Item<ItemStack> wrapped) {
Tag customData = wrapped.getNBTComponent(ComponentTypes.CUSTOM_DATA);
Tag customData = wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA);
if (!(customData instanceof CompoundTag compoundTag)) return Optional.empty();
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
boolean hasDifferentMaterial = false;
@@ -75,7 +75,7 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
if (!Config.interceptItem() && !hasDifferentMaterial) return Optional.empty();
return new OtherItem(wrapped, hasDifferentMaterial).process();
} else {
CompoundTag customData = Optional.ofNullable(wrapped.getNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
CompoundTag customData = Optional.ofNullable(wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
CompoundTag arguments = customData.getCompound(ArgumentModifier.ARGUMENTS_TAG);
ItemBuildContext context;
if (arguments == null) {
@@ -176,7 +176,7 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
}
public static boolean processModernItemName(Item<ItemStack> item, Supplier<CompoundTag> tag) {
Tag nameTag = item.getNBTComponent(ComponentTypes.ITEM_NAME);
Tag nameTag = item.getSparrowNBTComponent(ComponentTypes.ITEM_NAME);
if (nameTag == null) return false;
String tagStr = nameTag.getAsString();
Map<String, Component> tokens = CraftEngine.instance().fontManager().matchTags(tagStr);
@@ -189,7 +189,7 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
}
public static boolean processModernCustomName(Item<ItemStack> item, Supplier<CompoundTag> tag) {
Tag nameTag = item.getNBTComponent(ComponentTypes.CUSTOM_NAME);
Tag nameTag = item.getSparrowNBTComponent(ComponentTypes.CUSTOM_NAME);
if (nameTag == null) return false;
String tagStr = nameTag.getAsString();
Map<String, Component> tokens = CraftEngine.instance().fontManager().matchTags(tagStr);
@@ -202,7 +202,7 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
}
public static boolean processModernLore(Item<ItemStack> item, Supplier<CompoundTag> tagSupplier) {
Tag loreTag = item.getNBTComponent(ComponentTypes.LORE);
Tag loreTag = item.getSparrowNBTComponent(ComponentTypes.LORE);
boolean changed = false;
if (!(loreTag instanceof ListTag listTag)) {
return false;
@@ -254,7 +254,7 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
this.globalChanged = true;
}
if (this.globalChanged) {
CompoundTag customData = Optional.ofNullable(this.item.getNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
CompoundTag customData = Optional.ofNullable(this.item.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
customData.put(NETWORK_ITEM_TAG, getOrCreateTag());
this.item.setNBTComponent(ComponentKeys.CUSTOM_DATA, customData);
return Optional.of(this.item);

View File

@@ -111,7 +111,12 @@ public abstract class BukkitItemFactory<W extends ItemWrapper<ItemStack>> extend
}
@Override
protected Tag getNBTComponent(W item, Object type) {
public Object getNBTComponent(W item, Object type) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
protected Tag getSparrowNBTComponent(W item, Object type) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}

View File

@@ -227,7 +227,12 @@ public class ComponentItemFactory1_20_5 extends BukkitItemFactory<ComponentItemW
}
@Override
protected Tag getNBTComponent(ComponentItemWrapper item, Object type) {
public Object getNBTComponent(ComponentItemWrapper item, Object type) {
return item.getNBTComponent(type).orElse(null);
}
@Override
protected Tag getSparrowNBTComponent(ComponentItemWrapper item, Object type) {
return item.getSparrowNBTComponent(type).orElse(null);
}

View File

@@ -12,10 +12,8 @@ import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class CommonItemPacketHandler implements EntityPacketHandler {
public static final CommonItemPacketHandler INSTANCE = new CommonItemPacketHandler();