package com.razz.dfashion.cosmetic; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import com.razz.dfashion.DecoFashion; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; import net.minecraft.resources.Identifier; import net.minecraft.server.level.ServerPlayer; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.RegisterCommandsEvent; import java.util.HashMap; import java.util.Map; @EventBusSubscriber(modid = DecoFashion.MODID) public final class CosmeticCommands { @SubscribeEvent static void onRegisterCommands(RegisterCommandsEvent event) { CommandDispatcher dispatcher = event.getDispatcher(); dispatcher.register( Commands.literal(DecoFashion.MODID) .then(Commands.literal("equip") .then(Commands.argument("category", StringArgumentType.word()) .then(Commands.argument("id", StringArgumentType.greedyString()) .executes(CosmeticCommands::equip)))) .then(Commands.literal("unequip") .then(Commands.argument("category", StringArgumentType.word()) .executes(CosmeticCommands::unequip))) .then(Commands.literal("list") .executes(CosmeticCommands::list)) ); } private static int equip(CommandContext ctx) { ServerPlayer player = ctx.getSource().getPlayer(); if (player == null) { ctx.getSource().sendFailure(Component.literal("Must be run by a player")); return 0; } String category = StringArgumentType.getString(ctx, "category"); String idStr = StringArgumentType.getString(ctx, "id"); Identifier id = Identifier.parse(idStr); Map equipped = new HashMap<>(player.getData(CosmeticAttachments.EQUIPPED.get())); equipped.put(category, new CosmeticRef.Local(id)); player.setData(CosmeticAttachments.EQUIPPED.get(), equipped); ctx.getSource().sendSuccess( () -> Component.literal("Equipped " + id + " in category '" + category + "'"), false ); return 1; } private static int unequip(CommandContext ctx) { ServerPlayer player = ctx.getSource().getPlayer(); if (player == null) { ctx.getSource().sendFailure(Component.literal("Must be run by a player")); return 0; } String category = StringArgumentType.getString(ctx, "category"); Map equipped = new HashMap<>(player.getData(CosmeticAttachments.EQUIPPED.get())); CosmeticRef removed = equipped.remove(category); player.setData(CosmeticAttachments.EQUIPPED.get(), equipped); ctx.getSource().sendSuccess( () -> Component.literal(removed != null ? "Unequipped " + formatRef(removed) + " from '" + category + "'" : "Nothing was equipped in '" + category + "'"), false ); return 1; } private static int list(CommandContext ctx) { ServerPlayer player = ctx.getSource().getPlayer(); if (player == null) { ctx.getSource().sendFailure(Component.literal("Must be run by a player")); return 0; } Map equipped = player.getData(CosmeticAttachments.EQUIPPED.get()); if (equipped.isEmpty()) { ctx.getSource().sendSuccess(() -> Component.literal("No cosmetics equipped"), false); } else { equipped.forEach((cat, ref) -> ctx.getSource().sendSuccess(() -> Component.literal(cat + " = " + formatRef(ref)), false) ); } return 1; } private static String formatRef(CosmeticRef ref) { return switch (ref) { case CosmeticRef.Local l -> l.id().toString(); case CosmeticRef.Shared s -> "shared:" + s.hash().substring(0, 8) + "…"; }; } private CosmeticCommands() {} }