| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 5 | import net.minecraft.commands.CommandSourceStack;
|
| 6 | import net.minecraft.commands.Commands;
|
| 7 | import net.minecraft.commands.arguments.TimeArgument;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.util.valueproviders.IntProvider;
|
| 11 |
|
| 12 | public class WeatherCommand {
|
| 13 | private static final int DEFAULT_TIME = -1;
|
| 14 |
|
| 15 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 16 | dispatcher.register(
|
| 17 | Commands.literal("weather")
|
| 18 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 19 | .then(
|
| 20 | Commands.literal("clear")
|
| 21 | .executes(c -> setClear(c.getSource(), -1))
|
| 22 | .then(Commands.argument("duration", TimeArgument.time(1)).executes(c -> setClear(c.getSource(), IntegerArgumentType.getInteger(c, "duration"))))
|
| 23 | )
|
| 24 | .then(
|
| 25 | Commands.literal("rain")
|
| 26 | .executes(c -> setRain(c.getSource(), -1))
|
| 27 | .then(Commands.argument("duration", TimeArgument.time(1)).executes(c -> setRain(c.getSource(), IntegerArgumentType.getInteger(c, "duration"))))
|
| 28 | )
|
| 29 | .then(
|
| 30 | Commands.literal("thunder")
|
| 31 | .executes(c -> setThunder(c.getSource(), -1))
|
| 32 | .then(Commands.argument("duration", TimeArgument.time(1)).executes(c -> setThunder(c.getSource(), IntegerArgumentType.getInteger(c, "duration"))))
|
| 33 | )
|
| 34 | );
|
| 35 | }
|
| 36 |
|
| 37 | private static int getDuration(final CommandSourceStack source, final int input, final IntProvider defaultDistribution) {
|
| 38 | return input == -1 ? defaultDistribution.sample(source.getLevel().getRandom()) : input;
|
| 39 | }
|
| 40 |
|
| 41 | private static int setClear(final CommandSourceStack source, final int duration) {
|
| 42 | source.getServer().setWeatherParameters(getDuration(source, duration, ServerLevel.RAIN_DELAY), 0, false, false);
|
| 43 | source.sendSuccess(() -> Component.translatable("commands.weather.set.clear"), true);
|
| 44 | return duration;
|
| 45 | }
|
| 46 |
|
| 47 | private static int setRain(final CommandSourceStack source, final int duration) {
|
| 48 | source.getServer().setWeatherParameters(0, getDuration(source, duration, ServerLevel.RAIN_DURATION), true, false);
|
| 49 | source.sendSuccess(() -> Component.translatable("commands.weather.set.rain"), true);
|
| 50 | return duration;
|
| 51 | }
|
| 52 |
|
| 53 | private static int setThunder(final CommandSourceStack source, final int duration) {
|
| 54 | source.getServer().setWeatherParameters(0, getDuration(source, duration, ServerLevel.THUNDER_DURATION), true, true);
|
| 55 | source.sendSuccess(() -> Component.translatable("commands.weather.set.thunder"), true);
|
| 56 | return duration;
|
| 57 | }
|
| 58 | }
|
| 59 |
|