Update GameModeCommand to use native GameModeArgument

This commit is contained in:
Len 2024-10-04 16:47:09 +02:00
parent 430e6fb898
commit 162ffd3599
2 changed files with 1 additions and 50 deletions

View File

@ -1,7 +1,6 @@
package com.alttd.essentia.commands.admin;
import com.alttd.essentia.commands.EssentiaCommand;
import com.alttd.essentia.commands.argumement.GameModeArgument;
import com.alttd.essentia.configuration.Config;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
@ -28,7 +27,7 @@ public class GameModeCommand implements EssentiaCommand {
commandSourceStack -> commandSourceStack.getSender().hasPermission("essentia.command.admin." + commandName)
)
.then(
Commands.argument("gamemode", new GameModeArgument())
Commands.argument("gamemode", ArgumentTypes.gameMode())
.executes((source) -> {
if (!(source.getSource().getSender() instanceof Player player))
return 1;

View File

@ -1,48 +0,0 @@
package com.alttd.essentia.commands.argumement;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.GameMode;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public class GameModeArgument implements CustomArgumentType.Converted<GameMode, String> {
@Override
public @NotNull GameMode convert(String nativeType) throws CommandSyntaxException {
try {
return GameMode.valueOf(nativeType.toUpperCase());
} catch (Exception e) {
Message message = MessageComponentSerializer.message().serialize(Component.text("Invalid gamemode %s!".formatted(nativeType), NamedTextColor.RED));
throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
}
}
@Override
public @NotNull ArgumentType<String> getNativeType() {
return StringArgumentType.word();
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
for (GameMode gameMode : GameMode.values()) {
builder.suggest(gameMode.name().toLowerCase());
}
return CompletableFuture.completedFuture(
builder.build()
);
}
}