Add WeatherCommand.java

This commit is contained in:
Len 2024-08-15 17:15:31 +02:00
parent ff2824f287
commit 6a8e21852c

View File

@ -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<CommandSourceStack> command() {
final LiteralArgumentBuilder<CommandSourceStack> 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);
}
}