diff --git a/plugin/src/main/java/com/alttd/essentia/commands/admin/PlayerWeatherCommand.java b/plugin/src/main/java/com/alttd/essentia/commands/admin/PlayerWeatherCommand.java new file mode 100644 index 0000000..d367b90 --- /dev/null +++ b/plugin/src/main/java/com/alttd/essentia/commands/admin/PlayerWeatherCommand.java @@ -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 command() { + final LiteralArgumentBuilder 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 aliases() { + return List.of("pweather"); + } + +} diff --git a/plugin/src/main/java/com/alttd/essentia/commands/admin/WeatherCommand.java b/plugin/src/main/java/com/alttd/essentia/commands/admin/WeatherCommand.java index 37fdb3b..a69cd17 100644 --- a/plugin/src/main/java/com/alttd/essentia/commands/admin/WeatherCommand.java +++ b/plugin/src/main/java/com/alttd/essentia/commands/admin/WeatherCommand.java @@ -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"; diff --git a/plugin/src/main/java/com/alttd/essentia/commands/argumement/WeatherArgument.java b/plugin/src/main/java/com/alttd/essentia/commands/argumement/WeatherArgument.java new file mode 100644 index 0000000..cd63c3c --- /dev/null +++ b/plugin/src/main/java/com/alttd/essentia/commands/argumement/WeatherArgument.java @@ -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 { + + @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 getNativeType() { + return StringArgumentType.word(); + } + + @Override + public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { + for (WeatherType weatherType : WeatherType.values()) { + builder.suggest(weatherType.name().toLowerCase()); + } + + return CompletableFuture.completedFuture( + builder.build() + ); + } +} \ No newline at end of file