Skip to content

port .nbt to 1.20.5+ #4574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import meteordevelopment.meteorclient.utils.misc.ComponentMapReader;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.component.ComponentMap;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ComponentMapArgumentType implements ArgumentType<ComponentMap> {
private static final Collection<String> EXAMPLES = List.of("{foo=bar}");
private final ComponentMapReader reader;

public ComponentMapArgumentType(CommandRegistryAccess commandRegistryAccess) {
this.reader = new ComponentMapReader(commandRegistryAccess);
}

public static ComponentMapArgumentType componentMap(CommandRegistryAccess commandRegistryAccess) {
return new ComponentMapArgumentType(commandRegistryAccess);
}

public static <S extends CommandSource> ComponentMap getComponentMap(CommandContext<S> context, String name) {
return context.getArgument(name, ComponentMap.class);
}

@Override
public ComponentMap parse(StringReader reader) throws CommandSyntaxException {
return this.reader.consume(reader);
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
return this.reader.getSuggestions(builder);
}

@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,150 +5,176 @@

package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.serialization.DataResult;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.commands.arguments.CompoundNbtTagArgumentType;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.commands.arguments.ComponentMapArgumentType;
import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent;
import net.minecraft.command.CommandSource;
import net.minecraft.command.DataCommandObject;
import net.minecraft.command.EntityDataObject;
import net.minecraft.command.argument.NbtPathArgumentType;
import net.minecraft.component.ComponentMap;
import net.minecraft.command.argument.RegistryKeyArgumentType;
import net.minecraft.component.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import net.minecraft.util.Unit;

import java.util.List;
import java.util.Set;

import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
import static meteordevelopment.meteorclient.MeteorClient.mc;

public class NbtCommand extends Command {
private static final DynamicCommandExceptionType MALFORMED_ITEM_EXCEPTION = new DynamicCommandExceptionType(
error -> Text.stringifiedTranslatable("arguments.item.malformed", error)
);
private final Text copyButton = Text.literal("NBT").setStyle(Style.EMPTY
.withFormatting(Formatting.UNDERLINE)
.withClickEvent(new MeteorClickEvent(
ClickEvent.Action.RUN_COMMAND,
this.toString("copy")
))
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Text.literal("Copy the NBT data to your clipboard.")
)));

public NbtCommand() {
super("nbt", "Modifies NBT data for an item, example: .nbt add {display:{Name:'{\"text\":\"$cRed Name\"}'}}");
}

@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.executes(context -> {
error("This command is not yet updated for 1.20.5 and above!");
return SINGLE_SUCCESS;
});

// TODO: Update using Components over NBT
/*builder.then(literal("add").then(argument("nbt", CompoundNbtTagArgumentType.create()).executes(s -> {
builder.then(literal("add").then(argument("component", ComponentMapArgumentType.componentMap(REGISTRY_ACCESS)).executes(ctx -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();

if (validBasic(stack)) {
NbtCompound tag = CompoundNbtTagArgumentType.get(s);
ComponentMap itemComponents = stack.getComponents();
ComponentMap newComponents = ComponentMapArgumentType.getComponentMap(ctx, "component");

if (tag != null) {
ItemStack newStack = ItemStack.fromNbtOrEmpty(mc.world.getRegistryManager(), tag);
newStack.applyComponentsFrom(stack.getComponents());
ComponentMap testComponents = ComponentMap.of(itemComponents, newComponents);
DataResult<Unit> dataResult = ItemStack.validateComponents(testComponents);
dataResult.getOrThrow(MALFORMED_ITEM_EXCEPTION::create);

setStack(newStack);
} else {
error("Some of the NBT data could not be found, try using: " + Config.get().prefix.get() + "nbt set {nbt}");
}
stack.applyComponentsFrom(testComponents);

setStack(stack);
}

return SINGLE_SUCCESS;
})));

builder.then(literal("set").then(argument("nbt", CompoundNbtTagArgumentType.create()).executes(context -> {
builder.then(literal("set").then(argument("component", ComponentMapArgumentType.componentMap(REGISTRY_ACCESS)).executes(ctx -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();

if (validBasic(stack)) {
stack = ItemStack.fromNbtOrEmpty(mc.world.getRegistryManager(), CompoundNbtTagArgumentType.get(context));
ComponentMap components = ComponentMapArgumentType.getComponentMap(ctx, "component");
ComponentMapImpl stackComponents = (ComponentMapImpl) stack.getComponents();

DataResult<Unit> dataResult = ItemStack.validateComponents(components);
dataResult.getOrThrow(MALFORMED_ITEM_EXCEPTION::create);

ComponentChanges.Builder changesBuilder = ComponentChanges.builder();
Set<DataComponentType<?>> types = stackComponents.getTypes();

//set changes
for (Component<?> entry : components) {
changesBuilder.add(entry);
types.remove(entry.type());
}

//remove the rest
for (DataComponentType<?> type : types) {
changesBuilder.remove(type);
}

stackComponents.applyChanges(changesBuilder.build());

setStack(stack);
}

return SINGLE_SUCCESS;
})));

builder.then(literal("remove").then(argument("nbt_path", NbtPathArgumentType.nbtPath()).executes(context -> {
builder.then(literal("remove").then(argument("component", RegistryKeyArgumentType.registryKey(RegistryKeys.DATA_COMPONENT_TYPE)).executes(ctx -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();

if (validBasic(stack)) {
NbtPathArgumentType.NbtPath path = context.getArgument("nbt_path", NbtPathArgumentType.NbtPath.class);
path.remove(stack.encode(mc.world.getRegistryManager()));
@SuppressWarnings("unchecked")
RegistryKey<DataComponentType<?>> componentTypeKey = (RegistryKey<DataComponentType<?>>) ctx.getArgument("component", RegistryKey.class);

DataComponentType<?> componentType = Registries.DATA_COMPONENT_TYPE.get(componentTypeKey);

ComponentMapImpl components = (ComponentMapImpl) stack.getComponents();
components.applyChanges(ComponentChanges.builder().remove(componentType).build());

setStack(stack);
}

return SINGLE_SUCCESS;
}).suggests((ctx, suggestionsBuilder) -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();
if (stack != ItemStack.EMPTY) {
ComponentMap components = stack.getComponents();
return CommandSource.suggestMatching(components.getTypes().stream().map(Registries.DATA_COMPONENT_TYPE::getEntry).map(RegistryEntry::getIdAsString), suggestionsBuilder);
}
return suggestionsBuilder.buildFuture();
})));

builder.then(literal("get").executes(context -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();
DataCommandObject dataCommandObject = new EntityDataObject(mc.player);
NbtPathArgumentType.NbtPath handPath = NbtPathArgumentType.NbtPath.parse("SelectedItem");

if (stack == null) {
error("You must hold an item in your main hand.");
} else {
ComponentMap components = stack.getComponents();
MutableText text = Text.empty().append(copyButton);

MutableText copyButton = Text.literal("NBT");
copyButton.setStyle(copyButton.getStyle()
.withFormatting(Formatting.UNDERLINE)
.withClickEvent(new MeteorClickEvent(
ClickEvent.Action.RUN_COMMAND,
this.toString("copy")
))
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Text.literal("Copy the NBT data to your clipboard.")
)));

MutableText text = Text.literal("");
text.append(copyButton);

if (components == null) text.append("{}");
else text.append(" ").append(Text.of(components.toString()));

info(text);
try {
List<NbtElement> nbtElement = handPath.get(dataCommandObject.getNbt());
if (!nbtElement.isEmpty()) {
text.append(" ").append(NbtHelper.toPrettyPrintedText(nbtElement.getFirst()));
}
} catch (CommandSyntaxException e) {
text.append("{}");
}

info(text);

return SINGLE_SUCCESS;
}));

builder.then(literal("copy").executes(context -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();

if (stack == null) {
error("You must hold an item in your main hand.");
} else {
ComponentMap components = stack.getComponents();
mc.keyboard.setClipboard(components.toString());
MutableText nbt = Text.literal("NBT");
nbt.setStyle(nbt.getStyle()
.withFormatting(Formatting.UNDERLINE)
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Text.of(components.toString())
)));

MutableText text = Text.literal("");
text.append(nbt);
text.append(Text.literal(" data copied!"));

info(text);
}
DataCommandObject dataCommandObject = new EntityDataObject(mc.player);
NbtPathArgumentType.NbtPath handPath = NbtPathArgumentType.NbtPath.parse("SelectedItem");

return SINGLE_SUCCESS;
}));
MutableText text = Text.empty().append(copyButton);
String nbt = "{}";

builder.then(literal("paste").executes(context -> {
ItemStack stack = mc.player.getInventory().getMainHandStack();

if (validBasic(stack)) {
NbtCompound nbt = CompoundNbtTagArgumentType.create().parse(new StringReader(mc.keyboard.getClipboard()));
try {
List<NbtElement> nbtElement = handPath.get(dataCommandObject.getNbt());
if (!nbtElement.isEmpty()) {
text.append(" ").append(NbtHelper.toPrettyPrintedText(nbtElement.getFirst()));
nbt = nbtElement.getFirst().toString();
}
} catch (CommandSyntaxException e) {
text.append("{}");
}

stack = ItemStack.fromNbtOrEmpty(mc.world.getRegistryManager(), nbt);
mc.keyboard.setClipboard(nbt);

setStack(stack);
}
text.append(" data copied!");
info(text);

return SINGLE_SUCCESS;
}));
Expand All @@ -164,7 +190,7 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
}

return SINGLE_SUCCESS;
})));*/
})));
}

private void setStack(ItemStack stack) {
Expand All @@ -177,7 +203,7 @@ private boolean validBasic(ItemStack stack) {
return false;
}

if (stack == null) {
if (stack == ItemStack.EMPTY) {
error("You must hold an item in your main hand.");
return false;
}
Expand Down
Loading
Loading