mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-02 17:32:03 +01:00
#1397: Add ItemMeta#getAsComponentString()
By: 2008Choco <hawkeboyz2@hotmail.com>
This commit is contained in:
parent
f323d82f21
commit
f89c0d3ad7
1 changed files with 38 additions and 0 deletions
|
@ -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<NBTBase> ops = registryAccess.createSerializationContext(DynamicOpsNBT.INSTANCE);
|
||||
IRegistry<DataComponentType<?>> componentTypeRegistry = registryAccess.registryOrThrow(Registries.DATA_COMPONENT_TYPE);
|
||||
|
||||
StringJoiner componentString = new StringJoiner(",", "[", "]");
|
||||
for (Entry<DataComponentType<?>, 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());
|
||||
|
|
Loading…
Reference in a new issue