ServerEvents.commandRegistry((event) =>
{
    const { commands: Commands, arguments: Arguments } = event;

    event.register(Commands.literal("selectclass")
        .executes((context) => showClassOptions(context.source.player))
        .then(Commands.argument("class", Arguments.STRING.create(event))
            .executes((context) => selectClass(context.source.player, Arguments.STRING.getResult(context, "class"), false))));

    event.register(Commands.literal("setclass")
        .requires((source) => source.hasPermission(2))
        .then(Commands.argument("target", Arguments.PLAYER.create(event))
            .then(Commands.argument("class", Arguments.STRING.create(event))
                .executes((context) => selectClass(Arguments.PLAYER.getResult(context, "target"), Arguments.STRING.getResult(context, "class"), true)))));
});

function showClassOptions(player)
{
    const classOptions = Object.keys(global.tfc_classes).join(", ");
    player.displayClientMessage(Component.green("The available classes are: " + classOptions), false);
    return 1;
}

function selectClass(player, classId, force)
{
    const playerClass = global.tfc_classes_util.getClassOfPlayer(player);
    if(playerClass && !force)
    {
        player.displayClientMessage(Component.red("You already have the class " + playerClass.id + " selected."), false);
        return 1;
    }
    if(!global.tfc_classes[classId])
    {
        player.displayClientMessage(Component.red("No class " + classId + " exists."), false);
        return 1;
    }
    global.tfc_classes_util.setClassOfPlayer(player, classId);
    player.displayClientMessage(Component.green("You have chosen the class " + classId + "!"), false);
    return 1;
}