diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 2b58fa13bd..b12a1c6306 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -10,6 +10,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.collect.SetMultimap; import com.google.common.collect.Sets; +import com.mojang.serialization.DynamicOps; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -29,23 +30,29 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.StringJoiner; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nonnull; import javax.annotation.Nullable; import net.minecraft.core.Holder; +import net.minecraft.core.IRegistry; +import net.minecraft.core.IRegistryCustom; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.DataComponents; +import net.minecraft.core.registries.Registries; import net.minecraft.nbt.DynamicOpsNBT; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTCompressedStreamTools; import net.minecraft.nbt.NBTReadLimiter; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.nbt.SnbtPrinterTagVisitor; import net.minecraft.network.chat.IChatBaseComponent; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Unit; @@ -62,12 +69,15 @@ import net.minecraft.world.item.component.ItemLore; import net.minecraft.world.item.component.Unbreakable; import net.minecraft.world.item.enchantment.ItemEnchantments; import net.minecraft.world.level.block.state.IBlockData; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeModifier; import org.bukkit.block.data.BlockData; import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.craftbukkit.CraftEquipmentSlot; +import org.bukkit.craftbukkit.CraftRegistry; +import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.Overridden; import org.bukkit.craftbukkit.attribute.CraftAttribute; import org.bukkit.craftbukkit.attribute.CraftAttributeInstance; @@ -1283,6 +1293,34 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { return nbt.toString(); } + @Override + public String getAsComponentString() { + CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator(); + applyToItem(tag); + DataComponentPatch patch = tag.build(); + + IRegistryCustom registryAccess = CraftRegistry.getMinecraftRegistry(); + DynamicOps ops = registryAccess.createSerializationContext(DynamicOpsNBT.INSTANCE); + IRegistry> componentTypeRegistry = registryAccess.registryOrThrow(Registries.DATA_COMPONENT_TYPE); + + StringJoiner componentString = new StringJoiner(",", "[", "]"); + for (Entry, Optional> entry : patch.entrySet()) { + DataComponentType componentType = entry.getKey(); + Optional componentValue = entry.getValue(); + String componentKey = componentTypeRegistry.getResourceKey(componentType).orElseThrow().location().toString(); + + if (componentValue.isPresent()) { + NBTBase componentValueAsNBT = (NBTBase) ((DataComponentType) componentType).codecOrThrow().encodeStart(ops, componentValue.get()).getOrThrow(); + String componentValueAsNBTString = new SnbtPrinterTagVisitor("", 0, new ArrayList<>()).visit(componentValueAsNBT); + componentString.add(componentKey + "=" + componentValueAsNBTString); + } else { + // TODO: 1.21, remove component (!) format + } + } + + return componentString.toString(); + } + @Override public CustomItemTagContainer getCustomTagContainer() { return new DeprecatedCustomTagContainer(this.getPersistentDataContainer());