Skip to content

Commit

Permalink
feat: components command
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeEchoCodes committed Oct 2, 2024
1 parent b60eced commit b1ffe9b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.spiritstudios.specter.impl.debug;

import dev.spiritstudios.specter.impl.debug.command.ComponentsCommand;
import dev.spiritstudios.specter.impl.debug.command.HealCommand;
import dev.spiritstudios.specter.impl.debug.command.MetatagCommand;
import net.fabricmc.api.ModInitializer;
Expand All @@ -11,6 +12,7 @@ public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
MetatagCommand.register(dispatcher);
HealCommand.register(dispatcher);
ComponentsCommand.register(dispatcher);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.spiritstudios.specter.impl.debug.command;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.component.ComponentChanges;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ComponentsCommand {
private static final SimpleCommandExceptionType NO_COMPONENTS_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.components.no_components"));

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("components")
.requires(source -> source.hasPermissionLevel(2))
.executes(context -> components(Objects.requireNonNull(context.getSource().getPlayer()).getMainHandStack(), context.getSource()))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> components(EntityArgumentType.getPlayer(context, "target").getMainHandStack(), context.getSource()))
)
);
}

private static int components(ItemStack stack, ServerCommandSource source) throws CommandSyntaxException {
ComponentChanges components = stack.getComponentChanges();
if (components.isEmpty() || !(stack.encode(source.getRegistryManager()) instanceof NbtCompound nbt) || !nbt.contains("components"))
throw NO_COMPONENTS_EXCEPTION.create();

NbtCompound componentsNbt = nbt.getCompound("components");
List<MutableText> componentsText = new ArrayList<>();
for (int i = 0; i < componentsNbt.getKeys().size(); i++) {
String key = componentsNbt.getKeys().toArray(new String[0])[i];

MutableText text = Text.empty();
NbtElement tag = componentsNbt.get(key);

if (tag == null) tag = new NbtCompound();

text.append(Text.literal(key).formatted(Formatting.DARK_AQUA));
text.append(Text.literal("="));
text.append(NbtHelper.toPrettyPrintedText(tag));

if (i != componentsNbt.getKeys().size() - 1)
text.append(Text.literal(", "));

componentsText.add(text);
}

MutableText output = Text.literal("[");
componentsText.forEach(output::append);
output.append(Text.literal("]"));

source.sendFeedback(() -> output, false);

return Command.SINGLE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class MetatagCommand {

public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("metatag")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.literal("dump")
.then(CommandManager.argument("registry", IdentifierArgumentType.identifier())
.suggests(REGISTRY_SUGGESTIONS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"item_group.specter.debug": "Specter Debug",
"item.specter-debug.loot_loader": "Loot Loader",
"commands.heal.not_living": "Cannot heal non-living entity",
"commands.heal.success": "Healed %s for %s health"
"commands.heal.success": "Healed %s for %s health",
"commands.components.no_components": "No components found"
}

0 comments on commit b1ffe9b

Please sign in to comment.