2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 28 Jun 2020 19:27:20 -0400
Subject: [PATCH] Paper dumpitem command
Let's you quickly view the item in your hands NBT data
2022-07-09 01:01:42 +02:00
diff --git a/src/main/java/io/papermc/paper/command/PaperCommand.java b/src/main/java/io/papermc/paper/command/PaperCommand.java
2024-01-25 10:54:46 +01:00
index 69d093d3450931038ac3d27d7874060d13dc2225..27775df10a490ff75ca377e8373931738f1b817c 100644
2022-07-09 01:01:42 +02:00
--- a/src/main/java/io/papermc/paper/command/PaperCommand.java
+++ b/src/main/java/io/papermc/paper/command/PaperCommand.java
2024-01-25 10:54:46 +01:00
@@ -39,6 +39,7 @@ public final class PaperCommand extends Command {
commands.put(Set.of("version"), new VersionCommand());
2024-01-24 13:07:40 +01:00
commands.put(Set.of("dumpplugins"), new DumpPluginsCommand());
2024-01-24 14:05:59 +01:00
commands.put(Set.of("syncloadinfo"), new SyncLoadInfoCommand());
2022-07-09 01:01:42 +02:00
+ commands.put(Set.of("dumpitem"), new DumpItemCommand());
2021-06-11 14:02:28 +02:00
2022-07-09 01:01:42 +02:00
return commands.entrySet().stream()
.flatMap(entry -> entry.getKey().stream().map(s -> Map.entry(s, entry.getValue())))
diff --git a/src/main/java/io/papermc/paper/command/subcommands/DumpItemCommand.java b/src/main/java/io/papermc/paper/command/subcommands/DumpItemCommand.java
new file mode 100644
2024-04-30 03:49:19 +02:00
index 0000000000000000000000000000000000000000..522609d685159c71aeadbb16ee11caa826a20ff7
2022-07-09 01:01:42 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/subcommands/DumpItemCommand.java
2024-04-30 03:49:19 +02:00
@@ -0,0 +1,133 @@
2022-07-09 01:01:42 +02:00
+package io.papermc.paper.command.subcommands;
+
+import io.papermc.paper.adventure.PaperAdventure;
2024-04-30 03:49:19 +02:00
+import io.papermc.paper.command.CommandUtil;
2022-07-09 01:01:42 +02:00
+import io.papermc.paper.command.PaperSubcommand;
2024-04-27 08:57:49 +02:00
+import java.util.ArrayList;
2024-04-30 03:49:19 +02:00
+import java.util.Collections;
+import java.util.IdentityHashMap;
2024-04-27 08:57:49 +02:00
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
2024-04-30 03:49:19 +02:00
+import java.util.Set;
+import java.util.function.Consumer;
2022-07-09 01:01:42 +02:00
+import net.kyori.adventure.text.Component;
2024-04-27 08:57:49 +02:00
+import net.kyori.adventure.text.ComponentLike;
+import net.kyori.adventure.text.JoinConfiguration;
+import net.kyori.adventure.text.TextComponent;
+import net.minecraft.core.Registry;
+import net.minecraft.core.RegistryAccess;
2024-04-30 03:49:19 +02:00
+import net.minecraft.core.component.DataComponentMap;
2024-04-27 08:57:49 +02:00
+import net.minecraft.core.component.DataComponentPatch;
+import net.minecraft.core.component.DataComponentType;
2024-04-30 03:49:19 +02:00
+import net.minecraft.core.component.TypedDataComponent;
2022-12-08 00:49:41 +01:00
+import net.minecraft.core.registries.Registries;
2024-04-27 08:57:49 +02:00
+import net.minecraft.nbt.NbtOps;
+import net.minecraft.nbt.NbtUtils;
2024-04-30 03:49:19 +02:00
+import net.minecraft.nbt.SnbtPrinterTagVisitor;
2024-04-27 08:57:49 +02:00
+import net.minecraft.nbt.Tag;
+import net.minecraft.resources.RegistryOps;
2022-07-09 01:01:42 +02:00
+import net.minecraft.world.item.ItemStack;
+import org.bukkit.command.CommandSender;
2024-04-27 08:57:49 +02:00
+import org.bukkit.craftbukkit.CraftServer;
2022-07-09 01:01:42 +02:00
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.entity.Player;
+import org.checkerframework.checker.nullness.qual.NonNull;
2024-04-30 03:49:19 +02:00
+import org.checkerframework.checker.nullness.qual.Nullable;
2022-07-09 01:01:42 +02:00
+import org.checkerframework.framework.qual.DefaultQualifier;
+
2024-04-27 08:57:49 +02:00
+import static net.kyori.adventure.text.Component.join;
2022-07-09 01:01:42 +02:00
+import static net.kyori.adventure.text.Component.text;
2024-04-27 08:57:49 +02:00
+import static net.kyori.adventure.text.Component.textOfChildren;
+import static net.kyori.adventure.text.event.ClickEvent.copyToClipboard;
+import static net.kyori.adventure.text.format.NamedTextColor.AQUA;
2022-07-09 01:01:42 +02:00
+import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
2024-04-27 08:57:49 +02:00
+import static net.kyori.adventure.text.format.NamedTextColor.RED;
+import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
2022-07-09 01:01:42 +02:00
+import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
2024-04-30 03:49:19 +02:00
+import static net.kyori.adventure.text.format.TextColor.color;
2022-07-09 01:01:42 +02:00
+import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
+
+@DefaultQualifier(NonNull.class)
+public final class DumpItemCommand implements PaperSubcommand {
+ @Override
+ public boolean execute(final CommandSender sender, final String subCommand, final String[] args) {
2024-04-30 03:49:19 +02:00
+ this.doDumpItem(sender, args.length > 0 && "all".equals(args[0]));
2022-07-09 01:01:42 +02:00
+ return true;
+ }
+
2024-04-30 03:49:19 +02:00
+ @SuppressWarnings({"unchecked", "OptionalAssignedToNull", "rawtypes"})
+ private void doDumpItem(final CommandSender sender, final boolean includeAllComponents) {
2024-04-27 08:57:49 +02:00
+ if (!(sender instanceof final Player player)) {
2021-06-17 20:13:55 +02:00
+ sender.sendMessage("Only players can use this command");
+ return;
+ }
2024-04-27 08:57:49 +02:00
+ final ItemStack itemStack = CraftItemStack.asNMSCopy(player.getInventory().getItemInMainHand());
+ final TextComponent.Builder visualOutput = Component.text();
+ final StringBuilder itemCommandBuilder = new StringBuilder();
+ final String itemName = itemStack.getItemHolder().unwrapKey().orElseThrow().location().toString();
+ itemCommandBuilder.append(itemName);
+ visualOutput.append(text(itemName, YELLOW)); // item type
2024-04-30 03:49:19 +02:00
+ final Set<DataComponentType<?>> referencedComponentTypes = Collections.newSetFromMap(new IdentityHashMap<>());
2024-04-27 08:57:49 +02:00
+ final DataComponentPatch patch = itemStack.getComponentsPatch();
2024-04-30 03:49:19 +02:00
+ referencedComponentTypes.addAll(patch.entrySet().stream().map(Map.Entry::getKey).toList());
+ final DataComponentMap prototype = itemStack.getItem().components();
+ if (includeAllComponents) {
+ referencedComponentTypes.addAll(prototype.keySet());
+ }
2024-04-27 08:57:49 +02:00
+
+ final RegistryAccess.Frozen access = ((CraftServer) sender.getServer()).getServer().registryAccess();
+ final RegistryOps<Tag> ops = access.createSerializationContext(NbtOps.INSTANCE);
+ final Registry<DataComponentType<?>> registry = access.registryOrThrow(Registries.DATA_COMPONENT_TYPE);
2024-04-30 03:49:19 +02:00
+ final List<ComponentLike> componentComponents = new ArrayList<>();
+ final List<String> commandComponents = new ArrayList<>();
+ for (final DataComponentType<?> type : referencedComponentTypes) {
+ final String path = registry.getResourceKey(type).orElseThrow().location().getPath();
+ final @Nullable Optional<?> patchedValue = patch.get(type);
+ final @Nullable TypedDataComponent<?> prototypeValue = prototype.getTyped(type);
+ if (patchedValue != null) {
+ if (patchedValue.isEmpty()) {
2024-04-27 08:57:49 +02:00
+ componentComponents.add(text().append(text('!', RED), text(path, AQUA)));
+ commandComponents.add("!" + path);
+ } else {
2024-04-30 03:49:19 +02:00
+ final Tag serialized = (Tag) ((DataComponentType) type).codecOrThrow().encodeStart(ops, patchedValue.get()).getOrThrow();
+ writeComponentValue(componentComponents::add, commandComponents::add, path, serialized);
2022-07-09 01:01:42 +02:00
+ }
2024-04-30 03:49:19 +02:00
+ } else if (includeAllComponents && prototypeValue != null) {
+ final Tag serialized = prototypeValue.encodeValue(ops).getOrThrow();
+ writeComponentValue(componentComponents::add, commandComponents::add, path, serialized);
2024-04-27 08:57:49 +02:00
+ }
+ }
2024-04-30 03:49:19 +02:00
+ if (!componentComponents.isEmpty()) {
+ visualOutput.append(
+ text("[", color(0x8910CE)),
+ join(JoinConfiguration.separator(text(",", GRAY)), componentComponents),
+ text("]", color(0x8910CE))
+ );
+ itemCommandBuilder
+ .append("[")
+ .append(String.join(",", commandComponents))
+ .append("]");
+ }
+ player.sendMessage(visualOutput.build().compact());
+ final Component copyMsg = text("Click to copy item definition to clipboard for use with /pgive", GRAY, ITALIC);
+ sender.sendMessage(copyMsg.clickEvent(copyToClipboard(itemCommandBuilder.toString())));
+ }
+
+ private static void writeComponentValue(final Consumer<Component> visualOutput, final Consumer<String> commandOutput, final String path, final Tag serialized) {
+ visualOutput.accept(textOfChildren(
+ text(path, color(0xFF7FD7)),
+ text("=", WHITE),
+ PaperAdventure.asAdventure(NbtUtils.toPrettyComponent(serialized))
+ ));
+ commandOutput.accept(path + "=" + new SnbtPrinterTagVisitor("", 0, new ArrayList<>()).visit(serialized));
+ }
+
+ @Override
+ public List<String> tabComplete(final CommandSender sender, final String subCommand, final String[] args) {
+ if (args.length == 1) {
+ return CommandUtil.getListMatchingLast(sender, args, "all");
+ }
+ return Collections.emptyList();
2021-06-11 14:02:28 +02:00
+ }
2022-07-09 01:01:42 +02:00
+}