Add PlayerWeatherCommand.java and WeatherArgument.java

This commit is contained in:
Len 2024-08-15 17:23:12 +02:00
parent 6a8e21852c
commit be8b7e20df
3 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,67 @@
package com.alttd.essentia.commands.admin;
import com.alttd.essentia.commands.EssentiaCommand;
import com.alttd.essentia.commands.argumement.GameModeArgument;
import com.alttd.essentia.commands.argumement.WeatherArgument;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
import org.bukkit.GameMode;
import org.bukkit.WeatherType;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Random;
// TODO -- output messages
public class PlayerWeatherCommand implements EssentiaCommand {
static String commandName = "playerweather";
@Override
public @NotNull LiteralCommandNode<CommandSourceStack> command() {
final LiteralArgumentBuilder<CommandSourceStack> builder =
Commands.literal(commandName)
.requires(
commandSourceStack -> commandSourceStack.getSender().hasPermission("essentia.command.admin." + commandName) &&
commandSourceStack.getSender() instanceof Player
)
.then(
Commands.argument("weather", new WeatherArgument())
.executes(commandContext -> {
if (!(commandContext.getSource().getSender() instanceof Player player))
return 1;
WeatherType weatherType = commandContext.getArgument("weather", WeatherType.class);
execute(commandContext.getSource().getSender(), player, weatherType);
return 1;
})
.then(
Commands.argument("player", ArgumentTypes.player())
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission("essentia.command.admin." + commandName + ".other"))
.executes(commandContext -> {
CommandSourceStack sourceStack = commandContext.getSource();
Player target = commandContext.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(sourceStack).getFirst();
WeatherType weatherType = commandContext.getArgument("weather", WeatherType.class);
execute(commandContext.getSource().getSender(), target, weatherType);
return 1;
})
)
);
return builder.build();
}
public void execute(CommandSender sender, Player target, WeatherType weatherType) {
target.setPlayerWeather(weatherType);
}
public List<String> aliases() {
return List.of("pweather");
}
}

View File

@ -17,6 +17,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Random;
// TODO -- output messages
public class WeatherCommand implements EssentiaCommand {
static String commandName = "weather";

View File

@ -0,0 +1,48 @@
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.WeatherType;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public class WeatherArgument implements CustomArgumentType.Converted<WeatherType, String> {
@Override
public @NotNull WeatherType convert(String nativeType) throws CommandSyntaxException {
try {
return WeatherType.valueOf(nativeType.toUpperCase());
} catch (Exception e) {
Message message = MessageComponentSerializer.message().serialize(Component.text("Invalid WeatherType %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 (WeatherType weatherType : WeatherType.values()) {
builder.suggest(weatherType.name().toLowerCase());
}
return CompletableFuture.completedFuture(
builder.build()
);
}
}