From 05acfdac91e7ca25dc0b7780d43ad7df95bbd238 Mon Sep 17 00:00:00 2001 From: Kryolisc Date: Tue, 1 Nov 2022 11:05:48 +0100 Subject: [PATCH] add multi-world support and update to 1.19 while i'm at it --- pom.xml | 4 +-- .../realtimesync/RealTimeSync.java | 19 +++++++++-- .../realtimesync/entity/WorldConfig.java | 26 ++++++++++++++ .../handler/UpdateTimeHandler.java | 34 +++++++++++-------- src/main/resources/config.yml | 17 +++++----- src/main/resources/plugin.yml | 3 +- 6 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/edouardcourty/realtimesync/entity/WorldConfig.java diff --git a/pom.xml b/pom.xml index 9211c93..0fcc04f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.edouardcourty RealTimeSync - 1.0.0 + 1.1.0 UTF-8 @@ -25,7 +25,7 @@ org.spigotmc spigot-api - 1.16.4-R0.1-SNAPSHOT + 1.19-R0.1-SNAPSHOT provided diff --git a/src/main/java/com/edouardcourty/realtimesync/RealTimeSync.java b/src/main/java/com/edouardcourty/realtimesync/RealTimeSync.java index 78a1444..ab78a47 100644 --- a/src/main/java/com/edouardcourty/realtimesync/RealTimeSync.java +++ b/src/main/java/com/edouardcourty/realtimesync/RealTimeSync.java @@ -1,8 +1,11 @@ package com.edouardcourty.realtimesync; +import com.edouardcourty.realtimesync.entity.WorldConfig; import com.edouardcourty.realtimesync.handler.ConfigFileHandler; import com.edouardcourty.realtimesync.handler.UpdateTimeHandler; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; public class RealTimeSync extends JavaPlugin { @Override @@ -13,8 +16,20 @@ public void onEnable() ConfigFileHandler.init(this); ConfigFileHandler.handle(); - UpdateTimeHandler.init(this); - UpdateTimeHandler.startLoop(); + // Start handlers later, so all worlds had a chance to load first + final RealTimeSync instance = this; + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + ConfigurationSection worldSection = instance.getConfig().getConfigurationSection("worlds"); + assert worldSection != null; + for(String worldName : worldSection.getKeys(false)) { + WorldConfig config = new WorldConfig(worldName, worldSection.getInt(worldName + ".update_rate"), worldSection.getBoolean(worldName + ".force_daylightcycle_false")); + (new UpdateTimeHandler(instance, config)).startLoop(); + } + } + }; + runnable.runTaskLater(this, 1L); } @Override diff --git a/src/main/java/com/edouardcourty/realtimesync/entity/WorldConfig.java b/src/main/java/com/edouardcourty/realtimesync/entity/WorldConfig.java new file mode 100644 index 0000000..bec211b --- /dev/null +++ b/src/main/java/com/edouardcourty/realtimesync/entity/WorldConfig.java @@ -0,0 +1,26 @@ +package com.edouardcourty.realtimesync.entity; + +public class WorldConfig { + + protected String worldName; + protected int updateRate; + protected boolean forceDaylightcycleFalse; + + public WorldConfig(String worldName, int updateRate, boolean forceDaylightcycleFalse) { + this.worldName = worldName; + this.updateRate = updateRate; + this.forceDaylightcycleFalse = forceDaylightcycleFalse; + } + + public String getWorldName() { + return worldName; + } + + public int getUpdateRate() { + return updateRate; + } + + public boolean isForceDaylightcycleFalse() { + return forceDaylightcycleFalse; + } +} diff --git a/src/main/java/com/edouardcourty/realtimesync/handler/UpdateTimeHandler.java b/src/main/java/com/edouardcourty/realtimesync/handler/UpdateTimeHandler.java index a8ae103..70e7535 100644 --- a/src/main/java/com/edouardcourty/realtimesync/handler/UpdateTimeHandler.java +++ b/src/main/java/com/edouardcourty/realtimesync/handler/UpdateTimeHandler.java @@ -1,5 +1,6 @@ package com.edouardcourty.realtimesync.handler; +import com.edouardcourty.realtimesync.entity.WorldConfig; import com.edouardcourty.realtimesync.repository.TimeRepository; import org.bukkit.Bukkit; import org.bukkit.GameRule; @@ -8,29 +9,32 @@ import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Objects; public class UpdateTimeHandler { - public static World world; - public static JavaPlugin plugin; - public static int updateRate; - public static GameRule doDayLightCycleGamerule = GameRule.DO_DAYLIGHT_CYCLE; + public World world; + public JavaPlugin plugin; - static public void init(JavaPlugin myPlugin) - { - plugin = myPlugin; - world = plugin.getServer().getWorld(Objects.requireNonNull(plugin.getConfig().getString("world"))); - updateRate = plugin.getConfig().getInt("update_rate"); + public WorldConfig config; + public GameRule doDayLightCycleGamerule = GameRule.DO_DAYLIGHT_CYCLE; + + public UpdateTimeHandler(JavaPlugin plugin, WorldConfig config) { + this.plugin = plugin; + this.world = this.plugin.getServer().getWorld(config.getWorldName()); + this.config = config; } - static public void startLoop() + public void startLoop() { - Bukkit.getLogger().info(String.format("Starting loop for world %s. Updating every %s ticks.", world.getName(), updateRate)); - Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, UpdateTimeHandler::handle, 0, updateRate); + if(this.world == null) { + this.plugin.getLogger().severe("World " + this.config.getWorldName() + " not found"); + return; + } + Bukkit.getLogger().info(String.format("Starting loop for world %s. Updating every %s ticks.", world.getName(), this.config.getUpdateRate())); + Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::handle, 0, this.config.getUpdateRate()); } - static public void handle() + public void handle() { SimpleDateFormat sdfHours = new SimpleDateFormat("HH"); SimpleDateFormat sdfMinutes = new SimpleDateFormat("mm"); @@ -46,7 +50,7 @@ static public void handle() boolean hasDayLightCycleActivated = world.getGameRuleValue(doDayLightCycleGamerule); - if (plugin.getConfig().getBoolean("force_daylightcycle_false") && hasDayLightCycleActivated) { + if (this.config.isForceDaylightcycleFalse() && hasDayLightCycleActivated) { world.setGameRule(doDayLightCycleGamerule, false); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 30ea1d3..60137ef 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -4,15 +4,14 @@ # + ------------------------ + # World you want to change the time of -world: world - -# Update rate (in game ticks, 20 ticks = 1 second) -update_rate: 80 - -# Forces the world to activate the doDayLightCycle gamerule that stops the time from changing by itself -# The default state of this gamerule in a world is true. Settings this to true will set it to false. -# Not enabling this can result in weird-looking sky at sunrise and sunset, since the sun will move before being updated. -force_daylightcycle_false: true +worlds: + world: + # Update rate (in game ticks, 20 ticks = 1 second) + update_rate: 80 + # Forces the world to activate the doDayLightCycle gamerule that stops the time from changing by itself + # The default state of this gamerule in a world is true. Settings this to true will set it to false. + # Not enabling this can result in weird-looking sky at sunrise and sunset, since the sun will move before being updated. + force_daylightcycle_false: true # Will spam the console. Makes the plugin log every time the in-game time is changed. debug: false diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 18f5d24..7429340 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,3 +1,4 @@ name: RealTimeSync main: com.edouardcourty.realtimesync.RealTimeSync -version: 1.0.0 \ No newline at end of file +version: 1.1.0 +api-version: 1.19 \ No newline at end of file