Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
where changelog??
Browse files Browse the repository at this point in the history
I don't keep track of the changes because I do some work and commit at the end of the day
you can see what changed looking at the code and files changed
  • Loading branch information
Minecon724 committed Jan 16, 2024
1 parent a8fd1c4 commit ab6df79
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 20 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
- readd metrics
- fix realtime
- cache cleaning
- prevent packets
- prevent packets
- tests
4 changes: 2 additions & 2 deletions src/main/java/pl/minecon724/realweather/RW.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public void onEnable() {
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")
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/pl/minecon724/realweather/map/WorldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
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;
Expand Down Expand Up @@ -49,10 +57,7 @@ public static WorldMap fromConfig(ConfigurationSection config)
);
}

WorldMap worldMap = new WorldMap(type, point);

return worldMap;

INSTANCE = new WorldMap(type, point);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<World> worlds;

public PlayerTimeSyncTask(List<World> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public class RealTimeCommander implements Listener {
List<String> worldNames;
double scale;
ZoneId timezone;
boolean perPlayer;

volatile List<World> worlds;

RealTimeTask task;
PlayerTimeSyncTask playerTimeSyncTask;

public RealTimeCommander(RW plugin) {
this.plugin = plugin;
Expand All @@ -39,30 +43,34 @@ public void init(ConfigurationSection config)
}

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
public void onWorldLoad(WorldLoadEvent event) {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.World;
Expand All @@ -11,11 +10,12 @@
public class RealTimeTask extends BukkitRunnable {
double timeScale;
ZoneId timezone;
List<World> worlds = new ArrayList<>();
List<World> worlds;

public RealTimeTask(double timeScale, ZoneId timezone) {
public RealTimeTask(double timeScale, ZoneId timezone, List<World> worlds) {
this.timeScale = timeScale;
this.timezone = timezone;
this.worlds = worlds;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pl.minecon724.realweather.weather.provider.Provider;

public class WeatherCommander {
WorldMap worldMap;
private WorldMap worldMap = WorldMap.getInstance();
RW plugin;

boolean enabled;
Expand All @@ -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;
}

Expand All @@ -34,6 +33,7 @@ public WeatherCommander(WorldMap worldMap, RW plugin) {
*/
public void init(ConfigurationSection config)
throws DisabledException, IllegalArgumentException {

enabled = config.getBoolean("enabled");

if (!enabled)
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ab6df79

Please sign in to comment.