You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.4 KiB
Java
109 lines
4.4 KiB
Java
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<CommandSourceStack> 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<CommandSourceStack> 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<String, CosmeticRef> 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<CommandSourceStack> 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<String, CosmeticRef> 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<CommandSourceStack> ctx) {
|
|
ServerPlayer player = ctx.getSource().getPlayer();
|
|
if (player == null) {
|
|
ctx.getSource().sendFailure(Component.literal("Must be run by a player"));
|
|
return 0;
|
|
}
|
|
Map<String, CosmeticRef> 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() {}
|
|
}
|