Timer functionality as suggested a month ago (#7)

* first implementation of timer

* first implementation of timer

* String message turned to a translatable text
This commit is contained in:
Thierry Schmid
2024-08-10 12:34:26 +02:00
committed by GitHub
parent 4ee3169caa
commit c5f5a278e3
4 changed files with 90 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import me.lucaslah.weatherchanger.keys.ToggleClearKey;
import me.lucaslah.weatherchanger.keys.ToggleOffKey;
import me.lucaslah.weatherchanger.keys.ToggleRainKey;
import me.lucaslah.weatherchanger.keys.ToggleThunderKey;
import me.lucaslah.weatherchanger.timerlogic.Timer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
@@ -27,6 +28,8 @@ public class WeatherChanger {
private static WeatherChangerPlatform platform;
private static KeybindingManager keybindingManager;
private static CommandManager commandManager;
private static Timer weatherTimer;
private static boolean timerEnabled = false; // Default to false
public static void init(WeatherChangerPlatform platform) {
WeatherChanger.platform = platform;
@@ -57,6 +60,8 @@ public class WeatherChanger {
.add(new ToggleThunderKey());
commandManager.add(new WeatherChangerCommand());
weatherTimer = new Timer();
}
private static void loadConfig() {
@@ -151,4 +156,12 @@ public class WeatherChanger {
public static void sendClientMessage(Text message) {
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(message);
}
public static void toggleTimer() {
timerEnabled = !timerEnabled;
}
public static boolean isTimerEnabled() {
return timerEnabled;
}
}

View File

@@ -48,6 +48,15 @@ public class WeatherChangerCommand extends Command {
})
);
command.then(LiteralArgumentBuilder.<T>literal("toggleTimer")
.executes(context -> {
WeatherChanger.toggleTimer();
Text message = WeatherChanger.isTimerEnabled() ? Text.translatable("commands.weatherchanger.timer.on") : Text.translatable("commands.weatherchanger.timer.off");
sendClientMessage(message);
return 1;
})
);
LiteralCommandNode<T> node = dispatcher.register(command);
dispatcher.register(LiteralArgumentBuilder.<T>literal("cweather").redirect(node));
}

View File

@@ -0,0 +1,66 @@
package me.lucaslah.weatherchanger.timerlogic;
import me.lucaslah.weatherchanger.WeatherChanger;
import me.lucaslah.weatherchanger.config.WcMode;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.Random;
public class Timer {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final Random random = new Random();
public Timer() {
scheduleWeatherChange();
scheduleWeatherIntensityAdjustment();
}
private void scheduleWeatherChange() {
int initialDelay = 0; // Start immediately
int delay = 20 + random.nextInt(11); // 20-30 minutes
scheduler.scheduleWithFixedDelay(this::decideWeather, initialDelay, delay, TimeUnit.MINUTES);
}
private void decideWeather() {
if (!WeatherChanger.isTimerEnabled()) return;
if (random.nextDouble() < 0.75) { // 75% chance to change the weather
if (random.nextDouble() < 0.66) { // 66% chance for rain
WeatherChanger.setMode(WcMode.RAIN);
} else { // 33% chance for thunderstorm
WeatherChanger.setMode(WcMode.THUNDER);
}
}
}
private void scheduleWeatherIntensityAdjustment() {
int initialDelay = 1; // Start after 1 minute
int delay = 1 + random.nextInt(2); // 1-2 minutes
scheduler.scheduleWithFixedDelay(this::adjustWeatherIntensity, initialDelay, delay, TimeUnit.MINUTES);
}
private void adjustWeatherIntensity() {
if (!WeatherChanger.isTimerEnabled()) return;
WcMode currentMode = WeatherChanger.getMode();
double chance = random.nextDouble();
if (currentMode == WcMode.RAIN) {
if (chance < 0.05) { // 5% chance to worsen from rain to thunderstorm
WeatherChanger.setMode(WcMode.THUNDER);
} else if (chance < 0.20) { // 15% chance to clear up from rain
WeatherChanger.setMode(WcMode.CLEAR);
}
} else if (currentMode == WcMode.THUNDER) {
if (chance < 0.05) { // 5% chance to worsen, but already at worst so no change
// No change
} else if (chance < 0.20) { // 15% chance to improve from thunderstorm to rain
WeatherChanger.setMode(WcMode.RAIN);
}
} else if (currentMode == WcMode.CLEAR) {
// No change, clear weather cannot improve or worsen in this context
}
}
}

View File

@@ -3,6 +3,8 @@
"commands.weatherchanger.set.clear": "Set client weather to: Clear",
"commands.weatherchanger.set.rain": "Set client weather to: Rain",
"commands.weatherchanger.set.thunder": "Set client weather to: Thunder",
"commands.weatherchanger.timer.on": "Timer enabled",
"commands.weatherchanger.timer.off": "Timer disabled",
"keys.weatherchanger.off.name": "Toggle Weather Off",
"keys.weatherchanger.clear.name": "Toggle Weather Clear",
"keys.weatherchanger.rain.name": "Toggle Weather Rain",