diff --git a/SpongeAPI b/SpongeAPI index b41a9471cd6..570af09e42e 160000 --- a/SpongeAPI +++ b/SpongeAPI @@ -1 +1 @@ -Subproject commit b41a9471cd6b81253eed89702a7453e826833acf +Subproject commit 570af09e42e407ddfa31ec65e09e2491cbb0c46f diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 96a2a2955fe..833697f23f0 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -430,6 +430,11 @@ + + + + + @@ -438,6 +443,14 @@ + + + + + + + + @@ -1895,6 +1908,14 @@ + + + + + + + + @@ -1903,6 +1924,11 @@ + + + + + @@ -1917,6 +1943,14 @@ + + + + + + + + @@ -1928,6 +1962,14 @@ + + + + + + + + @@ -1942,6 +1984,14 @@ + + + + + + + + @@ -1953,6 +2003,14 @@ + + + + + + + + @@ -1967,6 +2025,14 @@ + + + + + + + + @@ -1981,6 +2047,14 @@ + + + + + + + + @@ -1995,6 +2069,14 @@ + + + + + + + + @@ -2009,6 +2091,14 @@ + + + + + + + + diff --git a/src/main/java/org/spongepowered/common/adventure/SpongeAdventure.java b/src/main/java/org/spongepowered/common/adventure/SpongeAdventure.java index 8694bc5ea71..b5d4e1e89c3 100644 --- a/src/main/java/org/spongepowered/common/adventure/SpongeAdventure.java +++ b/src/main/java/org/spongepowered/common/adventure/SpongeAdventure.java @@ -32,10 +32,8 @@ import io.netty.util.AttributeKey; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.ForwardingAudience; -import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.key.Key; -import net.kyori.adventure.nbt.api.BinaryTagHolder; import net.kyori.adventure.serializer.configurate4.ConfigurateComponentSerializer; import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.BlockNBTComponent; @@ -51,6 +49,7 @@ import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.event.DataComponentValue; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.format.NamedTextColor; @@ -65,8 +64,9 @@ import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; -import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; @@ -116,8 +116,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -493,13 +495,6 @@ public static ChatFormatting asVanilla(final NamedTextColor color) { return null; } - public static ChatType.Bound asVanilla(final MessageType type, final RegistryAccess registryAccess, final net.minecraft.network.chat.Component name) { - if (type == MessageType.CHAT) { - return ChatType.bind(ChatType.CHAT, registryAccess, name); - } - throw new IllegalArgumentException(type.name()); - } - @SuppressWarnings("ConstantConditions") public static HoverEvent asAdventure(final net.minecraft.network.chat.HoverEvent event) { final Action action = event.getAction(); @@ -521,7 +516,7 @@ public static HoverEvent asAdventure(final net.minecraft.network.chat.HoverEv return HoverEvent.showItem( SpongeAdventure.asAdventure(itemRegistry.getKey(itemStack.getItem())), itemStack.getCount(), - SpongeAdventure.asBinaryTagHolder(itemStack.getComponents()) + SpongeAdventure.asAdventure(itemStack.getComponentsPatch()) ); } throw new IllegalArgumentException(event.toString()); @@ -563,7 +558,7 @@ public static HoverEvent asAdventure(final net.minecraft.network.chat.HoverEv HoverEvent_ItemStackInfoAccessor.invoker$new( Holder.direct(itemRegistry.get(SpongeAdventure.asVanilla(value.item()))), value.count(), - SpongeAdventure.asVanillaCompound(value.nbt()) + SpongeAdventure.asVanilla(value.dataComponents()) ) ); } @@ -727,30 +722,36 @@ public static Set asAdventureFlags(final boolean darkenScreen, fin return flags; } - // NBT - - public static @Nullable DataComponentPatch asVanillaCompound(final @Nullable BinaryTagHolder tag) { - if (tag == null) { - return null; - } -// try { - // TODO requires Adventure Change tag.get(SpongeAdventure.NBT_CODEC); + @SuppressWarnings({"rawtypes", "unchecked"}) + public static DataComponentPatch asVanilla(final Map componentMap) { + if (componentMap == null) { return DataComponentPatch.EMPTY; -// } catch (final IOException e) { -// return null; -// } + } + final DataComponentPatch.Builder builder = DataComponentPatch.builder(); + componentMap.forEach((key, value) -> { + final DataComponentType type = BuiltInRegistries.DATA_COMPONENT_TYPE.get(SpongeAdventure.asVanilla(key)); + if (type != null && value instanceof SpongeDataComponentValue dcv) { + builder.set(type, dcv.value.orElse(null)); + } + }); + return builder.build(); } - public static @Nullable BinaryTagHolder asBinaryTagHolder(final DataComponentMap components) { + public static Map asAdventure(final DataComponentPatch components) { if (components == null) { - return null; - } - try { - // TODO requires Adventure Change - return BinaryTagHolder.encode(null, SpongeAdventure.NBT_CODEC); - } catch (final IOException e) { - return null; + return Map.of(); } + + Map map = new HashMap<>(); + components.entrySet().forEach(entry -> { + final ResourceLocation key = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(entry.getKey()); + map.put(SpongeAdventure.asAdventure(key), new SpongeDataComponentValue<>(entry.getValue())); + }); + return map; + } + + private record SpongeDataComponentValue(Optional value) implements DataComponentValue { + } // Key diff --git a/src/main/java/org/spongepowered/common/item/SpongeItemStack.java b/src/main/java/org/spongepowered/common/item/SpongeItemStack.java index b306eef4c40..cf862aa6a0b 100644 --- a/src/main/java/org/spongepowered/common/item/SpongeItemStack.java +++ b/src/main/java/org/spongepowered/common/item/SpongeItemStack.java @@ -84,7 +84,7 @@ public static final class BuilderImpl extends AbstractDataBuilder imp private ItemType type; private int quantity; private @Nullable LinkedHashMap, Object> keyValues; - private DataComponentMap components = DataComponentMap.EMPTY; + private DataComponentPatch components = DataComponentPatch.EMPTY; public BuilderImpl() { super(ItemStack.class, 1); @@ -126,10 +126,7 @@ public ItemStack.Builder fromItemStack(final ItemStack itemStack) { this.type = itemStack.type(); this.quantity = itemStack.quantity(); if ((Object) itemStack instanceof net.minecraft.world.item.ItemStack mcStack) { - final DataComponentPatch componentsPatch = mcStack.getComponentsPatch(); - // check if component is present on patch too - if not it is a default value - final DataComponentMap componentMap = mcStack.getComponents().filter(p -> componentsPatch.get(p) != null); - components = DataComponentMap.builder().addAll(componentMap).build(); + this.components = mcStack.getComponentsPatch(); // this.itemDataSet.addAll(((CustomDataHolderBridge) itemStack).bridge$getCustomManipulators()); } else { @@ -148,7 +145,8 @@ public ItemStack.Builder attributeModifier(final AttributeType attributeType, fi Objects.requireNonNull(modifier, "AttributeModifier cannot be null"); Objects.requireNonNull(equipmentType, "EquipmentType cannot be null"); - if (!this.components.has(DataComponents.ATTRIBUTE_MODIFIERS)) { + + if (this.components.get(DataComponents.ATTRIBUTE_MODIFIERS).isPresent()) { } @@ -206,7 +204,7 @@ public ItemStack.Builder fromSnapshot(final ItemStackSnapshot snapshot) { if (snapshot instanceof SpongeItemStackSnapshot) { - this.components = ((SpongeItemStackSnapshot) snapshot).getComponents(); + this.components = ((SpongeItemStackSnapshot) snapshot).getComponentsPatch(); } return this; @@ -222,8 +220,10 @@ public ItemStack.Builder fromBlockSnapshot(final BlockSnapshot blockSnapshot) { this.itemType(itemType.orElseThrow(() -> new IllegalArgumentException("ItemType not found for block type: " + blockTypeKey))); this.quantity(1); if (blockSnapshot instanceof SpongeBlockSnapshot) { - ((SpongeBlockSnapshot) blockSnapshot).getCompound().ifPresent(compoundTag -> - this.components = DataComponentMap.builder().set(DataComponents.BLOCK_ENTITY_DATA, CustomData.of(compoundTag)).build()); + ((SpongeBlockSnapshot) blockSnapshot).getCompound().ifPresent(compoundTag -> { + this.components = DataComponentPatch.builder().set(DataComponents.BLOCK_ENTITY_DATA, CustomData.of(compoundTag)).build(); + }); + // todo probably needs more testing, but this'll do donkey... } else { // TODO handle through the API specifically handling the rest of the data stuff // blockSnapshot.getContainers().forEach(this::itemData); @@ -262,7 +262,7 @@ protected Optional buildContent(final DataView container) throws Inva public ItemStack.Builder reset() { this.type = null; this.quantity = 1; - this.components = DataComponentMap.EMPTY; + this.components = DataComponentPatch.EMPTY; return this; } diff --git a/src/main/java/org/spongepowered/common/item/SpongeItemStackSnapshot.java b/src/main/java/org/spongepowered/common/item/SpongeItemStackSnapshot.java index 808012842b7..abd9ecd8a4b 100644 --- a/src/main/java/org/spongepowered/common/item/SpongeItemStackSnapshot.java +++ b/src/main/java/org/spongepowered/common/item/SpongeItemStackSnapshot.java @@ -28,7 +28,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import net.kyori.adventure.text.event.HoverEvent; -import net.minecraft.core.component.DataComponentMap; +import net.minecraft.core.component.DataComponentPatch; import org.checkerframework.checker.nullness.qual.Nullable; import org.spongepowered.api.ResourceKey; import org.spongepowered.api.Sponge; @@ -70,7 +70,7 @@ public class SpongeItemStackSnapshot implements ItemStackSnapshot { private final transient ItemStack privateStack; // only for internal use since the processors have a huge say private final ImmutableSet> keys; private final ImmutableSet> values; - private final DataComponentMap components; + private final DataComponentPatch components; private @Nullable UUID creatorUniqueId; @SuppressWarnings({"EqualsBetweenInconvertibleTypes", "ConstantConditions"}) @@ -84,7 +84,7 @@ public SpongeItemStackSnapshot(final ItemStack itemStack) { this.privateStack = itemStack; this.keys = ImmutableSet.of(); this.values = ImmutableSet.of(); - this.components = DataComponentMap.EMPTY; + this.components = DataComponentPatch.EMPTY; return; } this.itemType = itemStack.type(); @@ -101,8 +101,7 @@ public SpongeItemStackSnapshot(final ItemStack itemStack) { this.privateStack = itemStack.copy(); this.keys = keyBuilder.build(); this.values = valueBuilder.build(); - - this.components = DataComponentMap.builder().addAll(ItemStackUtil.toNative(this.privateStack).getComponents()).build(); // copy + this.components = ItemStackUtil.toNative(this.privateStack).getComponentsPatch(); } @Override @@ -216,7 +215,7 @@ public int getDamageValue() { return this.damageValue; } - public DataComponentMap getComponents() { + public DataComponentPatch getComponentsPatch() { return this.components; } @@ -284,6 +283,6 @@ public int hashCode() { @Override public HoverEvent asHoverEvent(final UnaryOperator op) { final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.ITEM_TYPE).valueKey(this.itemType); - return HoverEvent.showItem(op.apply(HoverEvent.ShowItem.of(resourceKey, this.quantity(), SpongeAdventure.asBinaryTagHolder(this.getComponents())))); + return HoverEvent.showItem(op.apply(HoverEvent.ShowItem.showItem(resourceKey, this.quantity(), SpongeAdventure.asAdventure(this.getComponentsPatch())))); } } diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/item/ItemStackMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/item/ItemStackMixin_API.java index 711073c8de9..b8cd837ac5f 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/item/ItemStackMixin_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/item/ItemStackMixin_API.java @@ -63,6 +63,7 @@ import org.spongepowered.common.item.SpongeItemStackSnapshot; import java.util.Collection; +import java.util.Map; import java.util.Objects; import java.util.function.UnaryOperator; @@ -81,7 +82,7 @@ public abstract class ItemStackMixin_API implements SerializableDataHolder.Mutab @Shadow public abstract Item shadow$getItem(); @Shadow public abstract net.minecraft.network.chat.Component shadow$getDisplayName(); @Shadow public abstract void shadow$applyComponents(final DataComponentPatch $$0); - @Shadow public abstract DataComponentMap shadow$getComponents(); + @Shadow public abstract DataComponentPatch shadow$getComponentsPatch(); @Shadow @Nullable public abstract T shadow$update(final DataComponentType $$0, final T $$1, final UnaryOperator $$2); // @formatter:on @@ -203,10 +204,11 @@ public Component asComponent() { @Override public @NonNull HoverEvent asHoverEvent(@NonNull final UnaryOperator op) { - final HoverEvent.ShowItem event = HoverEvent.ShowItem.of( + final HoverEvent.ShowItem event = HoverEvent.ShowItem.showItem( SpongeAdventure.asAdventure(SpongeCommon.vanillaRegistry(Registries.ITEM).getKey(this.shadow$getItem())), this.shadow$getCount(), - SpongeAdventure.asBinaryTagHolder(this.shadow$getComponents()) + Map.of() + //SpongeAdventure.asBinaryTagHolder(this.shadow$getComponentsPatch()) ); return HoverEvent.showItem(Objects.requireNonNull(op, "op").apply(event)); }