diff --git a/TODO.md b/TODO.md index e8250f6..e8af59c 100644 --- a/TODO.md +++ b/TODO.md @@ -3,4 +3,5 @@ - readd metrics - fix realtime - cache cleaning -- prevent packets \ No newline at end of file +- prevent packets +- tests \ No newline at end of file diff --git a/src/main/java/pl/minecon724/realweather/RW.java b/src/main/java/pl/minecon724/realweather/RW.java index e69fd15..a020b31 100644 --- a/src/main/java/pl/minecon724/realweather/RW.java +++ b/src/main/java/pl/minecon724/realweather/RW.java @@ -22,11 +22,11 @@ public class RW extends JavaPlugin { saveDefaultConfig(); config = getConfig(); - WorldMap worldMap = WorldMap.fromConfig( + WorldMap.init( config.getConfigurationSection("map") ); - WeatherCommander weatherCommander = new WeatherCommander(worldMap, this); + WeatherCommander weatherCommander = new WeatherCommander(this); try { weatherCommander.init( config.getConfigurationSection("weather") diff --git a/src/main/java/pl/minecon724/realweather/map/WorldMap.java b/src/main/java/pl/minecon724/realweather/map/WorldMap.java index 0bc99cd..d22b90d 100644 --- a/src/main/java/pl/minecon724/realweather/map/WorldMap.java +++ b/src/main/java/pl/minecon724/realweather/map/WorldMap.java @@ -6,17 +6,25 @@ import org.bukkit.entity.Player; import pl.minecon724.realweather.map.exceptions.GeoIPException; public class WorldMap { + private static WorldMap INSTANCE; + private final Type type; private Coordinates point; + public static WorldMap getInstance() { + if (INSTANCE == null) + throw new NullPointerException("No WorldMap"); + return INSTANCE; + } + public WorldMap(Type type, Coordinates point) { this.type = type; this.point = point; } - public static WorldMap fromConfig(ConfigurationSection config) + public static void init(ConfigurationSection config) throws IllegalArgumentException { Type type; @@ -49,10 +57,7 @@ public class WorldMap { ); } - WorldMap worldMap = new WorldMap(type, point); - - return worldMap; - + INSTANCE = new WorldMap(type, point); } /** diff --git a/src/main/java/pl/minecon724/realweather/realtime/PlayerTimeSyncTask.java b/src/main/java/pl/minecon724/realweather/realtime/PlayerTimeSyncTask.java new file mode 100644 index 0000000..394b234 --- /dev/null +++ b/src/main/java/pl/minecon724/realweather/realtime/PlayerTimeSyncTask.java @@ -0,0 +1,56 @@ +package pl.minecon724.realweather.realtime; + +import java.util.List; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import pl.minecon724.realweather.map.Coordinates; +import pl.minecon724.realweather.map.WorldMap; +import pl.minecon724.realweather.map.exceptions.GeoIPException; + +public class PlayerTimeSyncTask extends BukkitRunnable { + private WorldMap worldMap = WorldMap.getInstance(); + private Logger logger = Logger.getLogger("timezone sync"); + + private List worlds; + + public PlayerTimeSyncTask(List worlds) { + this.worlds = worlds; + } + + @Override + public void run() { + for (Player player : Bukkit.getOnlinePlayers()) { + if (!worlds.contains(player.getWorld())) { + player.resetPlayerTime(); + continue; + } + + Coordinates coordinates; + + try { + coordinates = worldMap.getCoordinates(player); + } catch (GeoIPException e) { + logger.warning( + String.format("Unable to determine GeoIP for %s (%s)", + player.getAddress().getHostString())); + + continue; + } + + // longitude + // / 15 as 15 degrees is 1 hour + // * 60 to minutes + // * 60 again to seconds + // * 20 to ticks + long offset = (long) (coordinates.longitude / 15 * 72000); + + player.setPlayerTime(offset, true); + } + } + +} diff --git a/src/main/java/pl/minecon724/realweather/realtime/RealTimeCommander.java b/src/main/java/pl/minecon724/realweather/realtime/RealTimeCommander.java index 44326f5..608fb05 100644 --- a/src/main/java/pl/minecon724/realweather/realtime/RealTimeCommander.java +++ b/src/main/java/pl/minecon724/realweather/realtime/RealTimeCommander.java @@ -19,8 +19,12 @@ public class RealTimeCommander implements Listener { List worldNames; double scale; ZoneId timezone; + boolean perPlayer; + + volatile List worlds; RealTimeTask task; + PlayerTimeSyncTask playerTimeSyncTask; public RealTimeCommander(RW plugin) { this.plugin = plugin; @@ -39,16 +43,20 @@ public class RealTimeCommander implements Listener { } worldNames = config.getStringList("worlds"); - scale = config.getDouble("scale"); + perPlayer = config.getBoolean("per_player"); plugin.getServer().getPluginManager().registerEvents(this, plugin); } public void start() { - task = new RealTimeTask(scale, timezone); + task = new RealTimeTask(scale, timezone, worlds); task.runTaskTimer(plugin, 0, 1); + + if (perPlayer) { + playerTimeSyncTask = new PlayerTimeSyncTask(worlds); + } } @EventHandler @@ -56,13 +64,13 @@ public class RealTimeCommander implements Listener { World world = event.getWorld(); if (worldNames.contains(world.getName())) - task.worlds.add(world); + worlds.add(world); } @EventHandler public void onWorldUnload(WorldUnloadEvent event) { World world = event.getWorld(); - task.worlds.remove(world); + worlds.remove(world); } } diff --git a/src/main/java/pl/minecon724/realweather/realtime/RealTimeTask.java b/src/main/java/pl/minecon724/realweather/realtime/RealTimeTask.java index 68a4dea..363b46e 100644 --- a/src/main/java/pl/minecon724/realweather/realtime/RealTimeTask.java +++ b/src/main/java/pl/minecon724/realweather/realtime/RealTimeTask.java @@ -2,7 +2,6 @@ package pl.minecon724.realweather.realtime; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.ArrayList; import java.util.List; import org.bukkit.World; @@ -11,11 +10,12 @@ import org.bukkit.scheduler.BukkitRunnable; public class RealTimeTask extends BukkitRunnable { double timeScale; ZoneId timezone; - List worlds = new ArrayList<>(); + List worlds; - public RealTimeTask(double timeScale, ZoneId timezone) { + public RealTimeTask(double timeScale, ZoneId timezone, List worlds) { this.timeScale = timeScale; this.timezone = timezone; + this.worlds = worlds; } @Override diff --git a/src/main/java/pl/minecon724/realweather/weather/WeatherCommander.java b/src/main/java/pl/minecon724/realweather/weather/WeatherCommander.java index 5c36de0..6d58e90 100644 --- a/src/main/java/pl/minecon724/realweather/weather/WeatherCommander.java +++ b/src/main/java/pl/minecon724/realweather/weather/WeatherCommander.java @@ -10,7 +10,7 @@ import pl.minecon724.realweather.weather.exceptions.DisabledException; import pl.minecon724.realweather.weather.provider.Provider; public class WeatherCommander { - WorldMap worldMap; + private WorldMap worldMap = WorldMap.getInstance(); RW plugin; boolean enabled; @@ -21,8 +21,7 @@ public class WeatherCommander { GetStateTask getStateTask; - public WeatherCommander(WorldMap worldMap, RW plugin) { - this.worldMap = worldMap; + public WeatherCommander(RW plugin) { this.plugin = plugin; } @@ -34,6 +33,7 @@ public class WeatherCommander { */ public void init(ConfigurationSection config) throws DisabledException, IllegalArgumentException { + enabled = config.getBoolean("enabled"); if (!enabled) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0923e2b..091921f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -61,7 +61,8 @@ time: # x day cycles / 24 hrs scale: 1.0 - # TODO # time based on... time? + # each player has time offset like timezones + # uses timezone as base, unless auto # uses settings from map - real: false + per_player: false