Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.edouardcourty</groupId>
<artifactId>RealTimeSync</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -25,7 +25,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/edouardcourty/realtimesync/RealTimeSync.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Boolean> 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<Boolean> 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");
Expand All @@ -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);
}

Expand Down
17 changes: 8 additions & 9 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: RealTimeSync
main: com.edouardcourty.realtimesync.RealTimeSync
version: 1.0.0
version: 1.1.0
api-version: 1.19