78 lines
4.0 KiB
Java
78 lines
4.0 KiB
Java
package com.thebrokenrail.sorcerycraft.command;
|
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
|
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
|
import com.mojang.brigadier.suggestion.Suggestions;
|
|
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
|
import com.thebrokenrail.sorcerycraft.spell.SpellPlayerEntity;
|
|
import com.thebrokenrail.sorcerycraft.spell.SpellTag;
|
|
import net.minecraft.command.arguments.EntityArgumentType;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.server.command.CommandManager;
|
|
import net.minecraft.server.command.ServerCommandSource;
|
|
import net.minecraft.text.TranslatableText;
|
|
import net.minecraft.util.Formatting;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
public class SpellCommand {
|
|
public static SuggestionProvider<ServerCommandSource> getOptions() {
|
|
return (ctx, builder) -> getSuggestionsBuilder(builder, new String[]{"clear", "list"});
|
|
}
|
|
|
|
private static CompletableFuture<Suggestions> getSuggestionsBuilder(SuggestionsBuilder builder, String[] list) {
|
|
String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
|
|
|
|
if (list.length < 1) {
|
|
return Suggestions.empty();
|
|
}
|
|
|
|
for (String str : list) {
|
|
if (str.toLowerCase(Locale.ROOT).startsWith(remaining)) {
|
|
builder.suggest(str);
|
|
}
|
|
}
|
|
return builder.buildFuture();
|
|
}
|
|
|
|
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
|
|
dispatcher.register(CommandManager.literal("spell")
|
|
.requires(source -> source.hasPermissionLevel(4))
|
|
.then(CommandManager.argument("action", StringArgumentType.word())
|
|
.suggests(getOptions())
|
|
.then(CommandManager.argument("player", EntityArgumentType.player())
|
|
.executes(ctx -> {
|
|
String action = StringArgumentType.getString(ctx, "action");
|
|
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
|
switch (action) {
|
|
case "clear": {
|
|
SpellPlayerEntity spellPlayer = (SpellPlayerEntity) player;
|
|
spellPlayer.setSpells(new HashMap<>());
|
|
ctx.getSource().sendFeedback(new TranslatableText("command.sorcerycraft.spell.cleared_spells", player.getDisplayName()), true);
|
|
return 1;
|
|
}
|
|
case "list": {
|
|
ctx.getSource().sendFeedback(new TranslatableText("command.sorcerycraft.spell.listing_spells", player.getDisplayName()), false);
|
|
SpellPlayerEntity spellPlayer = (SpellPlayerEntity) player;
|
|
Map<String, Integer> spells = spellPlayer.getSpells();
|
|
for (Map.Entry<String, Integer> entry : spells.entrySet()) {
|
|
ctx.getSource().sendFeedback(SpellTag.getTranslatedSpell(entry.getKey(), entry.getValue()).formatted(Formatting.YELLOW), false);
|
|
}
|
|
return 0;
|
|
}
|
|
default: {
|
|
ctx.getSource().sendFeedback(new TranslatableText("command.sorcerycraft.spell.invalid_action", action).formatted(Formatting.RED), true);
|
|
return -1;
|
|
}
|
|
}
|
|
})
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|