mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-01 08:20:51 +01:00
120 lines
6.3 KiB
Diff
120 lines
6.3 KiB
Diff
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
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/command/PaperCommand.java b/src/main/java/io/papermc/paper/command/PaperCommand.java
|
|
index 69d093d3450931038ac3d27d7874060d13dc2225..27775df10a490ff75ca377e8373931738f1b817c 100644
|
|
--- a/src/main/java/io/papermc/paper/command/PaperCommand.java
|
|
+++ b/src/main/java/io/papermc/paper/command/PaperCommand.java
|
|
@@ -39,6 +39,7 @@ public final class PaperCommand extends Command {
|
|
commands.put(Set.of("version"), new VersionCommand());
|
|
commands.put(Set.of("dumpplugins"), new DumpPluginsCommand());
|
|
commands.put(Set.of("syncloadinfo"), new SyncLoadInfoCommand());
|
|
+ commands.put(Set.of("dumpitem"), new DumpItemCommand());
|
|
|
|
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
|
|
index 0000000000000000000000000000000000000000..efc4ec58ab4b3ceefd66b714d286a6187babc724
|
|
--- /dev/null
|
|
+++ b/src/main/java/io/papermc/paper/command/subcommands/DumpItemCommand.java
|
|
@@ -0,0 +1,94 @@
|
|
+package io.papermc.paper.command.subcommands;
|
|
+
|
|
+import io.papermc.paper.adventure.PaperAdventure;
|
|
+import io.papermc.paper.command.PaperSubcommand;
|
|
+import java.util.ArrayList;
|
|
+import java.util.List;
|
|
+import java.util.Map;
|
|
+import java.util.Optional;
|
|
+import net.kyori.adventure.text.Component;
|
|
+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;
|
|
+import net.minecraft.core.component.DataComponentPatch;
|
|
+import net.minecraft.core.component.DataComponentType;
|
|
+import net.minecraft.core.registries.Registries;
|
|
+import net.minecraft.nbt.NbtOps;
|
|
+import net.minecraft.nbt.NbtUtils;
|
|
+import net.minecraft.nbt.Tag;
|
|
+import net.minecraft.resources.RegistryOps;
|
|
+import net.minecraft.world.item.ItemStack;
|
|
+import org.bukkit.command.CommandSender;
|
|
+import org.bukkit.craftbukkit.CraftServer;
|
|
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
|
+import org.bukkit.entity.Player;
|
|
+import org.checkerframework.checker.nullness.qual.NonNull;
|
|
+import org.checkerframework.framework.qual.DefaultQualifier;
|
|
+
|
|
+import static net.kyori.adventure.text.Component.join;
|
|
+import static net.kyori.adventure.text.Component.text;
|
|
+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;
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.RED;
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
|
+import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
|
|
+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) {
|
|
+ this.doDumpItem(sender);
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void doDumpItem(final CommandSender sender) {
|
|
+ if (!(sender instanceof final Player player)) {
|
|
+ sender.sendMessage("Only players can use this command");
|
|
+ return;
|
|
+ }
|
|
+ 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
|
|
+ final DataComponentPatch patch = itemStack.getComponentsPatch();
|
|
+
|
|
+ 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);
|
|
+ if (!patch.isEmpty()) {
|
|
+ visualOutput.append(text("[", WHITE));
|
|
+ itemCommandBuilder.append("[");
|
|
+ final List<ComponentLike> componentComponents = new ArrayList<>();
|
|
+ final List<String> commandComponents = new ArrayList<>();
|
|
+ for (final Map.Entry<DataComponentType<?>, Optional<?>> entry : patch.entrySet()) {
|
|
+ final String path = registry.getResourceKey(entry.getKey()).orElseThrow().location().getPath();
|
|
+ if (entry.getValue().isEmpty()) {
|
|
+ componentComponents.add(text().append(text('!', RED), text(path, AQUA)));
|
|
+ commandComponents.add("!" + path);
|
|
+ } else {
|
|
+ final Tag serialized = (Tag) ((DataComponentType) entry.getKey()).codecOrThrow().encodeStart(ops, entry.getValue().get()).getOrThrow();
|
|
+ componentComponents.add(textOfChildren(
|
|
+ text(path, AQUA),
|
|
+ text("=", WHITE),
|
|
+ PaperAdventure.asAdventure(NbtUtils.toPrettyComponent(serialized))
|
|
+ ));
|
|
+ commandComponents.add(path + "=" + serialized.getAsString());
|
|
+ }
|
|
+
|
|
+ }
|
|
+ visualOutput
|
|
+ .append(join(JoinConfiguration.separator(text(",", WHITE)), componentComponents))
|
|
+ .append(text("]", WHITE));
|
|
+ itemCommandBuilder.append(String.join(",", commandComponents)).append("]");
|
|
+ }
|
|
+ final Component hoverMsg = text("Click to copy item definition to clipboard for use with /pgive", GRAY, ITALIC);
|
|
+ player.sendMessage(visualOutput.build().compact().hoverEvent(hoverMsg).clickEvent(copyToClipboard(itemCommandBuilder.toString())));
|
|
+ }
|
|
+}
|