minecraft-src/net/minecraft/server/commands/WeatherCommand.java
2025-07-04 01:41:11 +03:00

67 lines
2.8 KiB
Java

package net.minecraft.server.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.TimeArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.valueproviders.IntProvider;
public class WeatherCommand {
private static final int DEFAULT_TIME = -1;
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("weather")
.requires(commandSourceStack -> commandSourceStack.hasPermission(2))
.then(
Commands.literal("clear")
.executes(commandContext -> setClear(commandContext.getSource(), -1))
.then(
Commands.argument("duration", TimeArgument.time(1))
.executes(commandContext -> setClear(commandContext.getSource(), IntegerArgumentType.getInteger(commandContext, "duration")))
)
)
.then(
Commands.literal("rain")
.executes(commandContext -> setRain(commandContext.getSource(), -1))
.then(
Commands.argument("duration", TimeArgument.time(1))
.executes(commandContext -> setRain(commandContext.getSource(), IntegerArgumentType.getInteger(commandContext, "duration")))
)
)
.then(
Commands.literal("thunder")
.executes(commandContext -> setThunder(commandContext.getSource(), -1))
.then(
Commands.argument("duration", TimeArgument.time(1))
.executes(commandContext -> setThunder(commandContext.getSource(), IntegerArgumentType.getInteger(commandContext, "duration")))
)
)
);
}
private static int getDuration(CommandSourceStack source, int time, IntProvider timeProvider) {
return time == -1 ? timeProvider.sample(source.getServer().overworld().getRandom()) : time;
}
private static int setClear(CommandSourceStack source, int time) {
source.getServer().overworld().setWeatherParameters(getDuration(source, time, ServerLevel.RAIN_DELAY), 0, false, false);
source.sendSuccess(() -> Component.translatable("commands.weather.set.clear"), true);
return time;
}
private static int setRain(CommandSourceStack source, int time) {
source.getServer().overworld().setWeatherParameters(0, getDuration(source, time, ServerLevel.RAIN_DURATION), true, false);
source.sendSuccess(() -> Component.translatable("commands.weather.set.rain"), true);
return time;
}
private static int setThunder(CommandSourceStack source, int time) {
source.getServer().overworld().setWeatherParameters(0, getDuration(source, time, ServerLevel.THUNDER_DURATION), true, true);
source.sendSuccess(() -> Component.translatable("commands.weather.set.thunder"), true);
return time;
}
}