From 6a8e21852c8f1f968cd5c85b5bc04d05ec5b9582 Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Thu, 15 Aug 2024 17:15:31 +0200 Subject: [PATCH] Add WeatherCommand.java --- .../commands/admin/WeatherCommand.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 plugin/src/main/java/com/alttd/essentia/commands/admin/WeatherCommand.java 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 new file mode 100644 index 0000000..37fdb3b --- /dev/null +++ b/plugin/src/main/java/com/alttd/essentia/commands/admin/WeatherCommand.java @@ -0,0 +1,99 @@ +package com.alttd.essentia.commands.admin; + +import com.alttd.essentia.commands.EssentiaCommand; +import com.mojang.brigadier.arguments.IntegerArgumentType; +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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Random; + +public class WeatherCommand implements EssentiaCommand { + + static String commandName = "weather"; + + @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.literal("clear") + .executes(commandContext -> { + if (commandContext.getSource().getSender() instanceof Player player) + clearWeather(player, -1); + + return 1; + }) + .then( + Commands.argument("duration", ArgumentTypes.time()) + .executes(commandContext -> { + int duration = commandContext.getArgument("duration", Integer.class); + if (commandContext.getSource().getSender() instanceof Player player) + clearWeather(player, duration); + + return 1; + }) + ) + ) + .then( + Commands.literal("rain") + .executes(commandContext -> { + if (commandContext.getSource().getSender() instanceof Player player) + setRain(player, -1, false); + + return 1; + }) + .then( + Commands.argument("duration", ArgumentTypes.time()) + .executes(commandContext -> { + int duration = commandContext.getArgument("duration", Integer.class); + if (commandContext.getSource().getSender() instanceof Player player) + setRain(player, duration, false); + return 1; + }) + ) + ) + .then( + Commands.literal("thunder") + .executes(commandContext -> { + if (commandContext.getSource().getSender() instanceof Player player) + setRain(player, -1, true); + + return 1; + }) + .then( + Commands.argument("duration", ArgumentTypes.time()) + .executes(commandContext -> { + int duration = commandContext.getArgument("duration", Integer.class); + if (commandContext.getSource().getSender() instanceof Player player) + setRain(player, duration, true); + return 1; + }) + ) + ); + return builder.build(); + } + + public void clearWeather(Player player, int duration) { + player.getWorld().setClearWeatherDuration(duration == -1 ? new Random().nextInt() : duration); + } + + public void setRain(Player player, int duration, boolean thunder) { + player.getWorld().setStorm(thunder); + player.getWorld().setWeatherDuration(duration == -1 ? new Random().nextInt() : duration); + } + +}