0.5 progress
(re)added some features, some refactoring errors have been eliminated but still needs work
This commit is contained in:
parent
e6f4fd527c
commit
c34d635dd1
18 changed files with 359 additions and 154 deletions
2
TODO.md
2
TODO.md
|
@ -3,3 +3,5 @@
|
||||||
- gh actions
|
- gh actions
|
||||||
- readd metrics
|
- readd metrics
|
||||||
- fix realtime
|
- fix realtime
|
||||||
|
- cache cleaning
|
||||||
|
- prevent packets
|
|
@ -1,23 +1,14 @@
|
||||||
package pl.minecon724.realweather;
|
package pl.minecon724.realweather;
|
||||||
|
|
||||||
import java.time.ZoneId;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.maxmind.geoip2.WebServiceClient;
|
import com.maxmind.geoip2.WebServiceClient;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import pl.minecon724.realweather.map.WorldMap;
|
import pl.minecon724.realweather.map.WorldMap;
|
||||||
import pl.minecon724.realweather.realtime.RTTask;
|
import pl.minecon724.realweather.realtime.RealTimeCommander;
|
||||||
import pl.minecon724.realweather.weather.GetStateTask;
|
|
||||||
import pl.minecon724.realweather.weather.WeatherCommander;
|
import pl.minecon724.realweather.weather.WeatherCommander;
|
||||||
import pl.minecon724.realweather.weather.exceptions.DisabledException;
|
import pl.minecon724.realweather.weather.exceptions.DisabledException;
|
||||||
import pl.minecon724.realweather.weather.exceptions.ProviderException;
|
|
||||||
import pl.minecon724.realweather.weather.provider.OpenWeatherMapProvider;
|
|
||||||
import pl.minecon724.realweather.weather.provider.Provider;
|
|
||||||
|
|
||||||
public class RW extends JavaPlugin {
|
public class RW extends JavaPlugin {
|
||||||
FileConfiguration config;
|
FileConfiguration config;
|
||||||
|
@ -40,22 +31,22 @@ public class RW extends JavaPlugin {
|
||||||
weatherCommander.init(
|
weatherCommander.init(
|
||||||
config.getConfigurationSection("weather")
|
config.getConfigurationSection("weather")
|
||||||
);
|
);
|
||||||
|
weatherCommander.start();
|
||||||
} catch (DisabledException e) {
|
} catch (DisabledException e) {
|
||||||
getLogger().info("Weather module disabled");
|
getLogger().info("Weather module disabled");
|
||||||
} // we leave ProviderException
|
} catch (IllegalArgumentException e) {
|
||||||
|
e.printStackTrace();
|
||||||
if (realtimeSec.getBoolean("enabled")) {
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
ZoneId zone;
|
|
||||||
try {
|
|
||||||
zone = ZoneId.of(realtimeSec.getString("timezone"));
|
|
||||||
} catch (Exception e) {
|
|
||||||
zone = ZoneId.systemDefault();
|
|
||||||
}
|
}
|
||||||
new RTTask(
|
|
||||||
realtimeSec.getDouble("timeScale"),
|
RealTimeCommander realTimeCommander = new RealTimeCommander(this);
|
||||||
zone,
|
try {
|
||||||
realtimeSec.getStringList("worlds")
|
realTimeCommander.init(
|
||||||
).runTaskTimer(this, 0, realtimeSec.getLong("interval"));
|
config.getConfigurationSection("time")
|
||||||
|
);
|
||||||
|
realTimeCommander.start();
|
||||||
|
} catch (DisabledException e) {
|
||||||
|
getLogger().info("Time module disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
long end = System.currentTimeMillis();
|
long end = System.currentTimeMillis();
|
||||||
|
|
48
src/main/java/pl/minecon724/realweather/map/Coordinates.java
Normal file
48
src/main/java/pl/minecon724/realweather/map/Coordinates.java
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package pl.minecon724.realweather.map;
|
||||||
|
|
||||||
|
import com.maxmind.geoip2.record.Location;
|
||||||
|
|
||||||
|
public class Coordinates {
|
||||||
|
public double latitude, longitude;
|
||||||
|
|
||||||
|
public Coordinates(double latitude, double longitude) {
|
||||||
|
this.latitude = latitude;
|
||||||
|
this.longitude = longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* converts Coordinates to double array
|
||||||
|
* @return array in order of latitude, longitude
|
||||||
|
*/
|
||||||
|
public double[] toDoubleArray() {
|
||||||
|
return new double[] {
|
||||||
|
this.latitude,
|
||||||
|
this.longitude
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Coordinates clamp() {
|
||||||
|
this.latitude = Math.max(-90.0, Math.min(90.0, this.latitude));
|
||||||
|
this.longitude = Math.max(-180.0, Math.min(180.0, this.longitude));
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Coordinates wrap() {
|
||||||
|
this.latitude = wrapDouble(-90.0, 90.0, this.latitude);
|
||||||
|
this.longitude = wrapDouble(-180.0, 180.0, this.longitude);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double wrapDouble(double min, double max, double val) {
|
||||||
|
return min + (val - min) % (max - min);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Coordinates fromGeoIpLocation(Location location) {
|
||||||
|
return new Coordinates(
|
||||||
|
location.getLatitude(),
|
||||||
|
location.getLongitude()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,26 +2,29 @@ package pl.minecon724.realweather.map;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.maxmind.geoip2.WebServiceClient;
|
import com.maxmind.geoip2.WebServiceClient;
|
||||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||||
import com.maxmind.geoip2.record.Location;
|
|
||||||
|
|
||||||
import pl.minecon724.realweather.map.exceptions.GeoIPException;
|
import pl.minecon724.realweather.map.exceptions.GeoIPException;
|
||||||
|
|
||||||
public class GeoLocator {
|
public class GeoLocator {
|
||||||
|
private static GeoLocator INSTANCE = null;
|
||||||
|
|
||||||
private WebServiceClient client;
|
private WebServiceClient client;
|
||||||
|
private Map<InetAddress, Coordinates> cache;
|
||||||
|
|
||||||
|
public static void init(int accountId, String apiKey) {
|
||||||
|
INSTANCE = new GeoLocator(
|
||||||
|
new WebServiceClient.Builder(accountId, apiKey)
|
||||||
|
.host("geolite.info").build());
|
||||||
|
}
|
||||||
|
|
||||||
public GeoLocator(WebServiceClient client) {
|
public GeoLocator(WebServiceClient client) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
this.cache = new HashMap<>();
|
||||||
|
|
||||||
public static GeoLocator fromConfig(int accountId, String apiKey) {
|
|
||||||
return new GeoLocator(
|
|
||||||
new WebServiceClient.Builder(accountId, apiKey)
|
|
||||||
.host("geolite.info").build()
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,19 +34,35 @@ public class GeoLocator {
|
||||||
* @throws GeoIp2Exception
|
* @throws GeoIp2Exception
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public double[] getLocation(InetAddress address)
|
public static Coordinates getCoordinates(InetAddress address)
|
||||||
throws GeoIPException {
|
throws GeoIPException {
|
||||||
|
|
||||||
Location location;
|
GeoLocator instance = INSTANCE;
|
||||||
|
|
||||||
|
Coordinates coordinates = null;
|
||||||
|
|
||||||
|
coordinates = instance.lookup(address);
|
||||||
|
if (coordinates != null)
|
||||||
|
return coordinates;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
location = client.city(address).getLocation();
|
coordinates = Coordinates.fromGeoIpLocation(
|
||||||
|
instance.client.city(address).getLocation()
|
||||||
|
);
|
||||||
} catch (IOException | GeoIp2Exception e) {
|
} catch (IOException | GeoIp2Exception e) {
|
||||||
throw new GeoIPException(e.getMessage());
|
throw new GeoIPException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new double[] {
|
instance.store(address, coordinates);
|
||||||
location.getLatitude(),
|
|
||||||
location.getLongitude()
|
return coordinates;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
private Coordinates lookup(InetAddress address) {
|
||||||
|
return this.cache.get(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void store(InetAddress address, Coordinates coordinates) {
|
||||||
|
this.cache.put(address, coordinates);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,35 +3,24 @@ package pl.minecon724.realweather.map;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
public class Globe {
|
public class Globe {
|
||||||
double scaleLatitide, scaleLongitude;
|
private static double[] SCALE;
|
||||||
boolean wrap;
|
private static boolean WRAP;
|
||||||
|
|
||||||
public Globe(double scaleLatitude,
|
public static void init(double scaleLatitude, double scaleLongitude, boolean wrap) {
|
||||||
double scaleLongitude,
|
SCALE = new double[] { scaleLatitude, scaleLongitude };
|
||||||
boolean wrap) {
|
WRAP = wrap;
|
||||||
this.scaleLatitide = scaleLatitude;
|
|
||||||
this.scaleLongitude = scaleLongitude;
|
|
||||||
this.wrap = wrap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double wrapDouble(double min, double max, double val) {
|
public static Coordinates toCoordiates(Location loc) {
|
||||||
return min + (val - min) % (max - min);
|
Coordinates coordinates = new Coordinates(
|
||||||
}
|
-loc.getZ() * SCALE[0],
|
||||||
|
loc.getX() * SCALE[1]);
|
||||||
|
|
||||||
public double[] playerPosAsCoords(Location loc) {
|
if (WRAP)
|
||||||
double[] out = new double[] {
|
coordinates.wrap();
|
||||||
loc.getX() * scaleLongitude,
|
else
|
||||||
-loc.getZ() * scaleLatitide
|
coordinates.clamp();
|
||||||
};
|
|
||||||
|
|
||||||
if (wrap) {
|
return coordinates;
|
||||||
out[0] = Math.max(-180.0, Math.min(180.0, out[0]));
|
|
||||||
out[1] = Math.max(-90.0, Math.min(90.0, out[1]));
|
|
||||||
} else {
|
|
||||||
out[0] = wrapDouble(-180.0, 180.0, out[0]);
|
|
||||||
out[1] = wrapDouble(-90.0, 90.0, out[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,18 +8,12 @@ import pl.minecon724.realweather.map.exceptions.GeoIPException;
|
||||||
public class WorldMap {
|
public class WorldMap {
|
||||||
private final Type type;
|
private final Type type;
|
||||||
|
|
||||||
private double[] point;
|
private Coordinates point;
|
||||||
private GeoLocator geoLocator;
|
|
||||||
private Globe globe;
|
|
||||||
|
|
||||||
public WorldMap(Type type,
|
public WorldMap(Type type,
|
||||||
double[] point,
|
Coordinates point) {
|
||||||
GeoLocator geoLocator,
|
|
||||||
Globe globe) {
|
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.point = point;
|
this.point = point;
|
||||||
this.geoLocator = geoLocator;
|
|
||||||
this.globe = globe;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WorldMap fromConfig(ConfigurationSection config)
|
public static WorldMap fromConfig(ConfigurationSection config)
|
||||||
|
@ -33,43 +27,41 @@ public class WorldMap {
|
||||||
throw new IllegalArgumentException("Invalid type");
|
throw new IllegalArgumentException("Invalid type");
|
||||||
}
|
}
|
||||||
|
|
||||||
double[] point = null;
|
Coordinates point = null;
|
||||||
GeoLocator geoLocator = null;
|
|
||||||
Globe globe = null;
|
|
||||||
|
|
||||||
if (type == Type.POINT) {
|
if (type == Type.POINT) {
|
||||||
point = new double[] {
|
point = new Coordinates(
|
||||||
config.getDouble("point.latitude"),
|
config.getDouble("point.latitude"),
|
||||||
config.getDouble("point.longitude")
|
config.getDouble("point.longitude")
|
||||||
};
|
);
|
||||||
|
|
||||||
} else if (type == Type.PLAYER) {
|
} else if (type == Type.PLAYER) {
|
||||||
geoLocator = GeoLocator.fromConfig(
|
GeoLocator.init(
|
||||||
config.getInt("player.geolite2_accountId"),
|
config.getInt("player.geolite2_accountId"),
|
||||||
config.getString("player.geolite2_api_key")
|
config.getString("player.geolite2_api_key")
|
||||||
);
|
);
|
||||||
|
|
||||||
} else if (type == Type.GLOBE) {
|
} else if (type == Type.GLOBE) {
|
||||||
globe = new Globe(
|
Globe.init(
|
||||||
config.getDouble("globe.scale_latitude"),
|
config.getDouble("globe.scale_latitude"),
|
||||||
config.getDouble("globe.scale_longitude"),
|
config.getDouble("globe.scale_longitude"),
|
||||||
config.getBoolean("globe.wrap")
|
config.getBoolean("globe.wrap")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldMap worldMap = new WorldMap(type, point, geoLocator, globe);
|
WorldMap worldMap = new WorldMap(type, point);
|
||||||
|
|
||||||
return worldMap;
|
return worldMap;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get player position as coords
|
* get coordinates of player
|
||||||
* @param player the player
|
* @param player the player
|
||||||
* @return latitude, longitude
|
* @return Coordinates
|
||||||
* @throws GeoIPException
|
* @throws GeoIPException
|
||||||
*/
|
*/
|
||||||
public double[] getPosition(Player player) throws GeoIPException {
|
public Coordinates getCoordinates(Player player) throws GeoIPException {
|
||||||
|
|
||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
case POINT:
|
case POINT:
|
||||||
|
@ -78,10 +70,11 @@ public class WorldMap {
|
||||||
if (player.getAddress().getAddress().isAnyLocalAddress())
|
if (player.getAddress().getAddress().isAnyLocalAddress())
|
||||||
throw new GeoIPException(player.getName() + "'s IP is local, check your proxy settings");
|
throw new GeoIPException(player.getName() + "'s IP is local, check your proxy settings");
|
||||||
|
|
||||||
return geoLocator.getLocation(
|
return GeoLocator.getCoordinates(
|
||||||
player.getAddress().getAddress());
|
player.getAddress().getAddress()
|
||||||
|
);
|
||||||
case GLOBE:
|
case GLOBE:
|
||||||
return globe.playerPosAsCoords(player.getLocation());
|
return Globe.toCoordiates(player.getLocation());
|
||||||
}
|
}
|
||||||
|
|
||||||
// this wont happen because we cover each type
|
// this wont happen because we cover each type
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package pl.minecon724.realweather.realtime;
|
||||||
|
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.world.WorldLoadEvent;
|
||||||
|
import org.bukkit.event.world.WorldUnloadEvent;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.RW;
|
||||||
|
import pl.minecon724.realweather.weather.exceptions.DisabledException;
|
||||||
|
|
||||||
|
public class RealTimeCommander implements Listener {
|
||||||
|
RW plugin;
|
||||||
|
|
||||||
|
List<String> worldNames;
|
||||||
|
double scale;
|
||||||
|
ZoneId timezone;
|
||||||
|
|
||||||
|
RealTimeTask task;
|
||||||
|
|
||||||
|
public RealTimeCommander(RW plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init(ConfigurationSection config)
|
||||||
|
throws DisabledException {
|
||||||
|
|
||||||
|
if (!config.getBoolean("enabled"))
|
||||||
|
throw new DisabledException();
|
||||||
|
|
||||||
|
try {
|
||||||
|
timezone = ZoneId.of(config.getString("timezone"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
timezone = ZoneId.systemDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
worldNames = config.getStringList("worlds");
|
||||||
|
|
||||||
|
scale = config.getDouble("scale");
|
||||||
|
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
task = new RealTimeTask(scale, timezone);
|
||||||
|
|
||||||
|
task.runTaskTimer(plugin, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onWorldLoad(WorldLoadEvent event) {
|
||||||
|
World world = event.getWorld();
|
||||||
|
|
||||||
|
if (worldNames.contains(world.getName()))
|
||||||
|
task.worlds.add(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onWorldUnload(WorldUnloadEvent event) {
|
||||||
|
World world = event.getWorld();
|
||||||
|
|
||||||
|
task.worlds.remove(world);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,24 +5,17 @@ import java.time.ZonedDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
public class RTTask extends BukkitRunnable {
|
public class RealTimeTask extends BukkitRunnable {
|
||||||
double timeScale;
|
double timeScale;
|
||||||
ZoneId timezone;
|
ZoneId timezone;
|
||||||
List<World> worlds;
|
List<World> worlds = new ArrayList<>();
|
||||||
|
|
||||||
public RTTask(double timeScale, ZoneId timezone, List<String> worlds) {
|
public RealTimeTask(double timeScale, ZoneId timezone) {
|
||||||
this.timeScale = timeScale;
|
this.timeScale = timeScale;
|
||||||
this.timezone = timezone;
|
this.timezone = timezone;
|
||||||
this.worlds = new ArrayList<World>();
|
|
||||||
for (String s : worlds) {
|
|
||||||
World world = Bukkit.getWorld(s);
|
|
||||||
if (world == null) continue;
|
|
||||||
this.worlds.add(world);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
|
@ -1,19 +1,20 @@
|
||||||
package pl.minecon724.realweather.weather;
|
package pl.minecon724.realweather.weather;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.WeatherType;
|
|
||||||
import org.bukkit.World;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.map.Coordinates;
|
||||||
import pl.minecon724.realweather.map.WorldMap;
|
import pl.minecon724.realweather.map.WorldMap;
|
||||||
import pl.minecon724.realweather.map.WorldMap.Type;
|
import pl.minecon724.realweather.map.WorldMap.Type;
|
||||||
import pl.minecon724.realweather.map.exceptions.GeoIPException;
|
import pl.minecon724.realweather.map.exceptions.GeoIPException;
|
||||||
import pl.minecon724.realweather.weather.WeatherState.ConditionSimple;
|
|
||||||
import pl.minecon724.realweather.weather.WeatherState.State;
|
import pl.minecon724.realweather.weather.WeatherState.State;
|
||||||
|
import pl.minecon724.realweather.weather.events.WeatherSyncEvent;
|
||||||
import pl.minecon724.realweather.weather.provider.Provider;
|
import pl.minecon724.realweather.weather.provider.Provider;
|
||||||
|
|
||||||
public class GetStateTask extends BukkitRunnable {
|
public class GetStateTask extends BukkitRunnable {
|
||||||
|
@ -22,16 +23,17 @@ public class GetStateTask extends BukkitRunnable {
|
||||||
|
|
||||||
Provider provider;
|
Provider provider;
|
||||||
WorldMap worldMap;
|
WorldMap worldMap;
|
||||||
List<World> worlds;
|
|
||||||
|
State storedState;
|
||||||
|
Map<Player, State> playerStoredState = new HashMap<>();
|
||||||
|
PluginManager pluginManager = Bukkit.getPluginManager();
|
||||||
|
|
||||||
public GetStateTask(
|
public GetStateTask(
|
||||||
Provider provider,
|
Provider provider,
|
||||||
WorldMap worldMap,
|
WorldMap worldMap
|
||||||
List<World> worlds
|
|
||||||
) {
|
) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.worldMap = worldMap;
|
this.worldMap = worldMap;
|
||||||
this.worlds = worlds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// That's a lot of variables
|
// That's a lot of variables
|
||||||
|
@ -40,32 +42,44 @@ public class GetStateTask extends BukkitRunnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
if (worldMap.getType() == Type.POINT) {
|
if (worldMap.getType() == Type.POINT) {
|
||||||
double[] position;
|
Coordinates coordinates;
|
||||||
try {
|
try {
|
||||||
position = worldMap.getPosition(null);
|
coordinates = worldMap.getCoordinates(null);
|
||||||
} catch (GeoIPException e) { return; }
|
} catch (GeoIPException e) { return; }
|
||||||
State state = provider.request_state(position);
|
State state = provider.request_state(coordinates);
|
||||||
|
|
||||||
for (World world : worlds) {
|
if (!state.equals(storedState)) {
|
||||||
if (world == null) return;
|
pluginManager.callEvent(
|
||||||
world.setThundering(state.getSimple() == ConditionSimple.THUNDER ? true : false);
|
new WeatherSyncEvent(null, storedState, state)
|
||||||
world.setStorm(state.getSimple() == ConditionSimple.CLEAR ? false : true);
|
);
|
||||||
|
|
||||||
|
storedState = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
double[] position;
|
Coordinates coordinates;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
position = worldMap.getPosition(player);
|
coordinates = worldMap.getCoordinates(player);
|
||||||
} catch (GeoIPException e) {
|
} catch (GeoIPException e) {
|
||||||
logger.info("GeoIP error for " + player.getName());
|
logger.info("GeoIP error for " + player.getName());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
State state = provider.request_state(position);
|
State state = provider.request_state(coordinates);
|
||||||
player.setPlayerWeather(state.getSimple() == ConditionSimple.CLEAR ? WeatherType.CLEAR : WeatherType.DOWNFALL);
|
|
||||||
|
if (!state.equals(playerStoredState.get(player))) {
|
||||||
|
pluginManager.callEvent(
|
||||||
|
new WeatherSyncEvent(
|
||||||
|
player,
|
||||||
|
playerStoredState.get(player),
|
||||||
|
state)
|
||||||
|
);
|
||||||
|
|
||||||
|
playerStoredState.put(player, state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package pl.minecon724.realweather.weather;
|
||||||
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
|
||||||
import pl.minecon724.realweather.weather.exceptions.ProviderException;
|
|
||||||
import pl.minecon724.realweather.weather.provider.OpenWeatherMapProvider;
|
import pl.minecon724.realweather.weather.provider.OpenWeatherMapProvider;
|
||||||
import pl.minecon724.realweather.weather.provider.Provider;
|
import pl.minecon724.realweather.weather.provider.Provider;
|
||||||
|
|
||||||
|
@ -15,8 +14,7 @@ public class Providers {
|
||||||
* @throws ProviderException
|
* @throws ProviderException
|
||||||
* @see Provider
|
* @see Provider
|
||||||
*/
|
*/
|
||||||
public static Provider getByName(String name, ConfigurationSection config)
|
public static Provider getByName(String name, ConfigurationSection config) {
|
||||||
throws ProviderException {
|
|
||||||
|
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "openweathermap":
|
case "openweathermap":
|
||||||
|
@ -26,8 +24,7 @@ public class Providers {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OpenWeatherMapProvider openWeatherMap(ConfigurationSection config)
|
public static OpenWeatherMapProvider openWeatherMap(ConfigurationSection config) {
|
||||||
throws ProviderException {
|
|
||||||
|
|
||||||
return new OpenWeatherMapProvider(
|
return new OpenWeatherMapProvider(
|
||||||
config.getString("apiKey"));
|
config.getString("apiKey"));
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package pl.minecon724.realweather.weather;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.world.WorldLoadEvent;
|
||||||
|
import org.bukkit.event.world.WorldUnloadEvent;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.weather.WeatherState.State;
|
||||||
|
import pl.minecon724.realweather.weather.events.WeatherSyncEvent;
|
||||||
|
|
||||||
|
public class WeatherChanger implements Listener {
|
||||||
|
private List<String> worldNames;
|
||||||
|
private List<World> worlds = new ArrayList<>();
|
||||||
|
|
||||||
|
public WeatherChanger(List<String> worldNames) {
|
||||||
|
this.worldNames = worldNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onWorldLoad(WorldLoadEvent event) {
|
||||||
|
World world = event.getWorld();
|
||||||
|
|
||||||
|
if (worldNames.contains(world.getName()))
|
||||||
|
worlds.add(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onWorldUnload(WorldUnloadEvent event) {
|
||||||
|
World world = event.getWorld();
|
||||||
|
|
||||||
|
worlds.remove(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onWeatherSync(WeatherSyncEvent event) {
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
State state = event.getState();
|
||||||
|
|
||||||
|
if (player != null) {
|
||||||
|
player.sendMessage("local: " + state.getCondition().name() + " "
|
||||||
|
+ state.getLevel().name() + " " + state.getSimple().name());
|
||||||
|
} else {
|
||||||
|
Bukkit.getServer().broadcastMessage("global: " + state.getCondition().name() + " "
|
||||||
|
+ state.getLevel().name() + " " + state.getSimple().name());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,15 +1,12 @@
|
||||||
package pl.minecon724.realweather.weather;
|
package pl.minecon724.realweather.weather;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.World;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
|
||||||
import pl.minecon724.realweather.RW;
|
import pl.minecon724.realweather.RW;
|
||||||
import pl.minecon724.realweather.map.WorldMap;
|
import pl.minecon724.realweather.map.WorldMap;
|
||||||
import pl.minecon724.realweather.weather.exceptions.DisabledException;
|
import pl.minecon724.realweather.weather.exceptions.DisabledException;
|
||||||
import pl.minecon724.realweather.weather.exceptions.ProviderException;
|
|
||||||
import pl.minecon724.realweather.weather.provider.Provider;
|
import pl.minecon724.realweather.weather.provider.Provider;
|
||||||
|
|
||||||
public class WeatherCommander {
|
public class WeatherCommander {
|
||||||
|
@ -17,7 +14,7 @@ public class WeatherCommander {
|
||||||
RW plugin;
|
RW plugin;
|
||||||
|
|
||||||
boolean enabled;
|
boolean enabled;
|
||||||
List<String> worlds;
|
List<String> worldNames;
|
||||||
String providerName;
|
String providerName;
|
||||||
Provider provider;
|
Provider provider;
|
||||||
ConfigurationSection providerConfig;
|
ConfigurationSection providerConfig;
|
||||||
|
@ -36,13 +33,13 @@ public class WeatherCommander {
|
||||||
* @throws ProviderException if invalid provider config
|
* @throws ProviderException if invalid provider config
|
||||||
*/
|
*/
|
||||||
public void init(ConfigurationSection config)
|
public void init(ConfigurationSection config)
|
||||||
throws DisabledException, ProviderException {
|
throws DisabledException, IllegalArgumentException {
|
||||||
enabled = config.getBoolean("enabled");
|
enabled = config.getBoolean("enabled");
|
||||||
|
|
||||||
if (!enabled)
|
if (!enabled)
|
||||||
throw new DisabledException();
|
throw new DisabledException();
|
||||||
|
|
||||||
worlds = config.getStringList("worlds");
|
worldNames = config.getStringList("worlds");
|
||||||
|
|
||||||
providerName = config.getString("provider.choice");
|
providerName = config.getString("provider.choice");
|
||||||
|
|
||||||
|
@ -51,13 +48,16 @@ public class WeatherCommander {
|
||||||
.getConfigurationSection(providerName);
|
.getConfigurationSection(providerName);
|
||||||
|
|
||||||
provider = Providers.getByName(providerName, providerConfig);
|
provider = Providers.getByName(providerName, providerConfig);
|
||||||
|
if (provider == null)
|
||||||
|
throw new IllegalArgumentException("Invalid provider: " + providerName);
|
||||||
provider.init();
|
provider.init();
|
||||||
|
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(
|
||||||
|
new WeatherChanger(worldNames), plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
getStateTask = new GetStateTask(provider,
|
getStateTask = new GetStateTask(provider, worldMap);
|
||||||
worldMap,
|
|
||||||
new ArrayList<World>());
|
|
||||||
|
|
||||||
getStateTask.runTaskTimerAsynchronously(plugin, 0, 1200);
|
getStateTask.runTaskTimerAsynchronously(plugin, 0, 1200);
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,5 +59,11 @@ public class WeatherState {
|
||||||
public ConditionSimple getSimple() {
|
public ConditionSimple getSimple() {
|
||||||
return simple;
|
return simple;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean equals(State state) {
|
||||||
|
return (state != null
|
||||||
|
&& this.getCondition() == state.getCondition()
|
||||||
|
&& this.getLevel() == state.getLevel());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package pl.minecon724.realweather.weather.events;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.event.player.PlayerEvent;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.weather.WeatherState.State;
|
||||||
|
|
||||||
|
public class WeatherSyncEvent extends PlayerEvent {
|
||||||
|
private static final HandlerList HANDLERS = new HandlerList();
|
||||||
|
|
||||||
|
private State oldState;
|
||||||
|
private State state;
|
||||||
|
|
||||||
|
public WeatherSyncEvent(Player player, State oldState, State state) {
|
||||||
|
super(player);
|
||||||
|
|
||||||
|
this.oldState = oldState;
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HandlerList getHandlers() {
|
||||||
|
return HANDLERS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HandlerList getHandlerList() {
|
||||||
|
return HANDLERS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getOldState() {
|
||||||
|
return oldState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +0,0 @@
|
||||||
package pl.minecon724.realweather.weather.exceptions;
|
|
||||||
|
|
||||||
public class ProviderException extends Exception {
|
|
||||||
|
|
||||||
}
|
|
|
@ -9,6 +9,7 @@ import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.map.Coordinates;
|
||||||
import pl.minecon724.realweather.weather.WeatherState.*;
|
import pl.minecon724.realweather.weather.WeatherState.*;
|
||||||
|
|
||||||
public class OpenWeatherMapProvider implements Provider {
|
public class OpenWeatherMapProvider implements Provider {
|
||||||
|
@ -29,13 +30,13 @@ public class OpenWeatherMapProvider implements Provider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public State request_state(double[] coords) {
|
public State request_state(Coordinates coordinates) {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
URL url = new URL(
|
URL url = new URL(
|
||||||
endpoint + String.format("/data/2.5/weather?lat=%s&lon=%s&appid=%s",
|
endpoint + String.format("/data/2.5/weather?lat=%s&lon=%s&appid=%s",
|
||||||
Double.toString(coords[0]), Double.toString(coords[1]), apiKey
|
Double.toString(coordinates.latitude), Double.toString(coordinates.longitude), apiKey
|
||||||
));
|
));
|
||||||
|
|
||||||
InputStream is = url.openStream();
|
InputStream is = url.openStream();
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package pl.minecon724.realweather.weather.provider;
|
package pl.minecon724.realweather.weather.provider;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.map.Coordinates;
|
||||||
import pl.minecon724.realweather.weather.WeatherState;
|
import pl.minecon724.realweather.weather.WeatherState;
|
||||||
|
|
||||||
public interface Provider {
|
public interface Provider {
|
||||||
public void init();
|
public void init();
|
||||||
public WeatherState.State request_state(double[] coords);
|
public WeatherState.State request_state(Coordinates coordinates);
|
||||||
public String getName();
|
public String getName();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ map:
|
||||||
# for example; if a player's position on map converts to 94 degrees (out of bounds), it becomes -86 degrees
|
# for example; if a player's position on map converts to 94 degrees (out of bounds), it becomes -86 degrees
|
||||||
wrap: true
|
wrap: true
|
||||||
|
|
||||||
realtime:
|
time:
|
||||||
# warning: this removes sleep
|
# warning: this removes sleep
|
||||||
enabled: false
|
enabled: false
|
||||||
|
|
||||||
|
@ -59,15 +59,9 @@ realtime:
|
||||||
timezone: 'auto'
|
timezone: 'auto'
|
||||||
|
|
||||||
# x day cycles / 24 hrs
|
# x day cycles / 24 hrs
|
||||||
timeScale: 1.0
|
scale: 1.0
|
||||||
|
|
||||||
# How often should we recalculate the time (in ticks)
|
# TODO
|
||||||
# Very minimal impact on performance
|
# time based on... time?
|
||||||
interval: 1
|
# uses settings from map
|
||||||
|
real: false
|
||||||
# makes time visible to players not in sync with server
|
|
||||||
virtual: false
|
|
||||||
|
|
||||||
# if above true, locks the server's time (in ticks)
|
|
||||||
# -1 to disable
|
|
||||||
lock: 0
|
|
||||||
|
|
Loading…
Reference in a new issue