From e6236113018740892db6b24c4dde0a8c52503666 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Sun, 23 Jun 2024 17:58:02 -0400 Subject: [PATCH] add options to disable beds during night/thunder --- .../realtimeweather/Configurator.java | 44 ++++++++++++++++++- .../realtimeweather/EventHandler.java | 21 +++++++++ src/main/resources/config.yml | 12 +++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/Configurator.java b/src/main/java/io/github/jack1424/realtimeweather/Configurator.java index eaac3b6..1d4029f 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/Configurator.java +++ b/src/main/java/io/github/jack1424/realtimeweather/Configurator.java @@ -14,9 +14,9 @@ public class Configurator { private final RealTimeWeather rtw; private final FileConfiguration configFile; private TimeZone timeZone; - private boolean debug, timeEnabled, weatherEnabled, blockTimeSetCommand, blockWeatherCommand; + private boolean debug, timeEnabled, weatherEnabled, blockTimeSetCommand, blockWeatherCommand, disableBedsAtNight, disableBedsDuringThunder; private long timeSyncInterval, weatherSyncInterval; - private String apiKey, lat, lon; + private String apiKey, lat, lon, disableBedsAtNightMessage, disableBedsDuringThunderMessage; public Configurator(RealTimeWeather rtw) { this.rtw = rtw; @@ -30,6 +30,8 @@ public class Configurator { if (isTimeEnabled()) try { setBlockTimeSetCommand(configFile.getBoolean("BlockTimeSetCommand")); + setDisableBedsAtNight(configFile.getBoolean("DisableBedsAtNight")); + setDisableBedsAtNightMessage(configFile.getString("DisableBedsAtNightMessage")); setTimeSyncInterval(configFile.getLong("TimeSyncInterval")); setTimeZone(configFile.getString("Timezone")); } catch (ConfigurationException e) { @@ -44,6 +46,8 @@ public class Configurator { if (isWeatherEnabled()) try { setBlockWeatherCommand(configFile.getBoolean("BlockWeatherCommand")); + setDisableBedsDuringThunder(configFile.getBoolean("DisableBedsDuringThunder")); + setDisableBedsDuringThunderMessage(configFile.getString("DisableBedsDuringThunderMessage")); setWeatherSyncInterval(configFile.getLong("WeatherSyncInterval")); setAPIKey(configFile.getString("APIKey")); setLat(configFile.getString("Latitude")); @@ -84,6 +88,24 @@ public class Configurator { rtw.debug("BlockTimeSetCommand set to " + value); } + public boolean getDisableBedsAtNight() { + return disableBedsAtNight; + } + + public void setDisableBedsAtNight(boolean value) { + disableBedsAtNight = value; + rtw.debug("DisableBedsAtNight set to " + value); + } + + public String getDisableBedsAtNightMessage() { + return disableBedsAtNightMessage; + } + + public void setDisableBedsAtNightMessage(String value) { + disableBedsAtNightMessage = value; + rtw.debug("NightDisabledBedMessage set to " + value); + } + public long getTimeSyncInterval() { return timeSyncInterval; } @@ -128,6 +150,24 @@ public class Configurator { rtw.debug("BlockWeatherCommand set to " + value); } + public boolean getDisableBedsDuringThunder() { + return disableBedsDuringThunder; + } + + public void setDisableBedsDuringThunder(boolean value) { + disableBedsDuringThunder = value; + rtw.debug("DisableBedsDuringThunder set to " + value); + } + + public String getDisableBedsDuringThunderMessage() { + return disableBedsDuringThunderMessage; + } + + public void setDisableBedsDuringThunderMessage(String value) { + disableBedsDuringThunderMessage = value; + rtw.debug("ThunderDisabledBedMessage set to " + value); + } + public long getWeatherSyncInterval() { return weatherSyncInterval; } diff --git a/src/main/java/io/github/jack1424/realtimeweather/EventHandler.java b/src/main/java/io/github/jack1424/realtimeweather/EventHandler.java index 2c5e3c7..7bfd490 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/EventHandler.java +++ b/src/main/java/io/github/jack1424/realtimeweather/EventHandler.java @@ -1,7 +1,10 @@ package io.github.jack1424.realtimeweather; +import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; @@ -29,4 +32,22 @@ public class EventHandler implements Listener { event.getSender().sendMessage("Command disabled by RealTimeWeather"); } } + + @org.bukkit.event.EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onPlayerBedEnter(PlayerBedEnterEvent event) { + Player player = event.getPlayer(); + World playerWorld = player.getWorld(); + long worldTime = playerWorld.getTime(); + + if (config.isTimeEnabled() && config.getDisableBedsAtNight() && ((!playerWorld.hasStorm() && worldTime >= 12542 && worldTime <= 23459) + || (playerWorld.hasStorm() && worldTime >= 12010 && worldTime <= 23991))) { + event.setCancelled(true); + player.sendMessage(config.getDisableBedsAtNightMessage()); + } + + if (config.isWeatherEnabled() && config.getDisableBedsDuringThunder() && playerWorld.isThundering()) { + event.setCancelled(true); + player.sendMessage(config.getDisableBedsDuringThunderMessage()); + } + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7edd4d7..abaeee3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,6 +8,12 @@ SyncTime: false # Set to false to enable the /time set command (not recommended) # BlockTimeSetCommand: true # # +# Prevent players from sleeping in beds at night (they will still be able to set their spawn point) # +# If you disable this, strange things could happen # +DisableBedsAtNight: true +# The message sent to players when they try to sleep at night when beds are disabled # +DisableBedsAtNightMessage: '' +# # # You can change the time between time syncs from the default (5 seconds) below # TimeSyncInterval: 100 # # @@ -25,6 +31,12 @@ SyncWeather: false # Set to false to enable the /weather command (not recommended) # BlockWeatherCommand: true # # +# Prevent players from sleeping in beds during a thunderstorm (they will still be able to set their spawn point) # +# If you disable this, it could break weather syncing # +DisableBedsDuringThunder: true +# The message sent to players when they try to sleep during a thunderstorm when beds are disabled # +DisableBedsDuringThunderMessage: '' +# # # You can change the time between weather syncs from the default (5 minutes) below # # Due to OpenWeather's restrictions, setting this value below 2400 (2 minutes) will cause problems # # You can find a handy tick calculator here: https://mapmaking.fr/tick/ #