-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Add WeatherCommand for flexible weather control and persistence #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package com.eternalcode.core.feature.weather; | ||
|
|
||
| import com.eternalcode.annotations.scan.command.DescriptionDocs; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.notice.NoticeService; | ||
| import com.eternalcode.core.viewer.Viewer; | ||
| import dev.rollczi.litecommands.annotations.argument.Arg; | ||
| import dev.rollczi.litecommands.annotations.command.Command; | ||
| import dev.rollczi.litecommands.annotations.context.Context; | ||
| import dev.rollczi.litecommands.annotations.context.Sender; | ||
| import dev.rollczi.litecommands.annotations.execute.Execute; | ||
| import dev.rollczi.litecommands.annotations.flag.Flag; | ||
| import dev.rollczi.litecommands.annotations.permission.Permission; | ||
| import java.util.Locale; | ||
| import org.bukkit.World; | ||
|
|
||
| @Command(name = "weather") | ||
| @Permission("eternalcore.weather") | ||
| class WeatherCommand { | ||
|
|
||
| private final NoticeService noticeService; | ||
| private final WeatherService weatherService; | ||
|
|
||
| @Inject | ||
| WeatherCommand(NoticeService noticeService, WeatherService weatherService) { | ||
| this.noticeService = noticeService; | ||
| this.weatherService = weatherService; | ||
| } | ||
|
|
||
| @Execute | ||
| @DescriptionDocs(description = "Sets weather in current world", arguments = "<sun|rain|thunder> [-p]") | ||
| void execute(@Sender Viewer viewer, @Context World world, @Arg WeatherType weatherType, @Flag("-p") boolean persistent) { | ||
| this.setWeather(viewer, world, weatherType, persistent); | ||
| } | ||
|
|
||
| @Execute | ||
| @DescriptionDocs(description = "Sets weather in specified world", arguments = "<sun|rain|thunder> <world> [-p]") | ||
| void execute( | ||
| @Sender Viewer viewer, | ||
| @Arg WeatherType weatherType, | ||
| @Arg World world, | ||
| @Flag("-p") boolean persistent | ||
| ) { | ||
| this.setWeather(viewer, world, weatherType, persistent); | ||
| } | ||
|
|
||
| @Execute(name = "clear") | ||
| @DescriptionDocs(description = "Enables natural weather cycle in current world") | ||
| void clearCurrentWorld(@Sender Viewer viewer, @Context World world) { | ||
| this.clearWeather(viewer, world); | ||
| } | ||
|
|
||
| @Execute(name = "clear") | ||
| @DescriptionDocs(description = "Enables natural weather cycle in specified world", arguments = "<world>") | ||
| void clearSelectedWorld(@Sender Viewer viewer, @Arg World world) { | ||
| this.clearWeather(viewer, world); | ||
| } | ||
|
|
||
| private void setWeather(Viewer viewer, World world, WeatherType weatherType, boolean persistent) { | ||
| this.weatherService.setWeather(world, weatherType, persistent); | ||
|
|
||
| this.noticeService.create() | ||
| .viewer(viewer) | ||
| .placeholder("{WORLD}", world.getName()) | ||
| .notice(translation -> switch (weatherType) { | ||
| case SUN -> translation.timeAndWeather().weatherSetSun(); | ||
| case RAIN -> translation.timeAndWeather().weatherSetRain(); | ||
| case THUNDER -> translation.timeAndWeather().weatherSetThunder(); | ||
| }) | ||
| .send(); | ||
|
|
||
| if (!persistent) { | ||
| return; | ||
| } | ||
|
|
||
| this.noticeService.create() | ||
| .viewer(viewer) | ||
| .placeholder("{WORLD}", world.getName()) | ||
| .placeholder("{WEATHER}", weatherType.name().toLowerCase(Locale.ROOT)) | ||
| .notice(translation -> translation.timeAndWeather().weatherPersistenceEnabled()) | ||
| .send(); | ||
| } | ||
|
|
||
| private void clearWeather(Viewer viewer, World world) { | ||
| this.weatherService.clearPersistentWeather(world); | ||
|
|
||
| this.noticeService.create() | ||
| .viewer(viewer) | ||
| .placeholder("{WORLD}", world.getName()) | ||
| .notice(translation -> translation.timeAndWeather().weatherPersistenceDisabled()) | ||
| .send(); | ||
| } | ||
|
Comment on lines
+84
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.eternalcode.core.feature.weather; | ||
|
|
||
| import com.eternalcode.commons.bukkit.scheduler.MinecraftScheduler; | ||
| import com.eternalcode.core.injector.annotations.Inject; | ||
| import com.eternalcode.core.injector.annotations.component.Service; | ||
| import org.bukkit.GameRule; | ||
| import org.bukkit.World; | ||
|
|
||
| @Service | ||
| class WeatherService { | ||
|
|
||
| private static final int DEFAULT_CLEAR_WEATHER_DURATION = 20 * 60 * 10; | ||
|
|
||
| private final MinecraftScheduler scheduler; | ||
|
|
||
| @Inject | ||
| WeatherService(MinecraftScheduler scheduler) { | ||
| this.scheduler = scheduler; | ||
| } | ||
|
|
||
| void setWeather(World world, WeatherType weatherType, boolean persistent) { | ||
| this.scheduler.run(() -> { | ||
| this.applyWeather(world, weatherType); | ||
| world.setGameRule(GameRule.DO_WEATHER_CYCLE, !persistent); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| }); | ||
| } | ||
|
Comment on lines
+21
to
+26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of |
||
|
|
||
| void clearPersistentWeather(World world) { | ||
| this.scheduler.run(() -> world.setGameRule(GameRule.DO_WEATHER_CYCLE, true)); | ||
| } | ||
|
|
||
| private void applyWeather(World world, WeatherType weatherType) { | ||
| switch (weatherType) { | ||
| case SUN -> { | ||
| world.setClearWeatherDuration(DEFAULT_CLEAR_WEATHER_DURATION); | ||
| world.setStorm(false); | ||
| world.setThundering(false); | ||
| } | ||
| case RAIN -> { | ||
| world.setStorm(true); | ||
| world.setThundering(false); | ||
| } | ||
| case THUNDER -> { | ||
| world.setStorm(true); | ||
| world.setThundering(true); | ||
| } | ||
| default -> throw new IllegalStateException("Unsupported weather type: " + weatherType); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.eternalcode.core.feature.weather; | ||
|
|
||
| public enum WeatherType { | ||
| SUN, | ||
| RAIN, | ||
| THUNDER | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
weatherType.name().toLowerCase(Locale.ROOT)for the{WEATHER}placeholder may lead to localization issues. In many languages (like Polish), the weather type needs to be a specific noun or adjective that agrees grammatically with the rest of the sentence (e.g., gender or case agreement). Hardcoding the enum name prevents proper translation of this part of the message.