From 02fbc22ce460509d79b2e5afbbe58c5164e4872f Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Mon, 25 Apr 2022 17:34:22 +0000 Subject: [PATCH] map mode --- README.md | 1 + pom.xml | 2 +- .../minecon724/realweather/GetStateTask.java | 30 +++++++++++++++++-- .../pl/minecon724/realweather/MapUtils.java | 23 ++++++++++++++ .../java/pl/minecon724/realweather/RW.java | 11 +++++-- src/main/resources/config.yml | 21 +++++++++++-- 6 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 README.md create mode 100644 src/main/java/pl/minecon724/realweather/MapUtils.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..bdc6d69 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Minecon724/RealWeather) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 43d6bd2..1bf810d 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 pl.minecon724 realweather - 0.1.1 + 0.2.0 17 diff --git a/src/main/java/pl/minecon724/realweather/GetStateTask.java b/src/main/java/pl/minecon724/realweather/GetStateTask.java index 76f81c2..7767759 100644 --- a/src/main/java/pl/minecon724/realweather/GetStateTask.java +++ b/src/main/java/pl/minecon724/realweather/GetStateTask.java @@ -29,13 +29,19 @@ public class GetStateTask extends BukkitRunnable { WebServiceClient client; boolean broadcast; boolean debug, debugDox; + double scaleLat, scaleLon; + int onExceed; + + MapUtils mapUtils = new MapUtils(); public GetStateTask( Provider provider, String source, double pointLatitude, double pointLongitude, List worlds, Logger logger, WebServiceClient client, boolean broadcast, - boolean debug, boolean debugDox + boolean debug, boolean debugDox, + double scaleLat, double scaleLon, + int onExceed ) { this.provider = provider; this.source = source; @@ -47,6 +53,9 @@ public class GetStateTask extends BukkitRunnable { this.broadcast = broadcast; this.debug = debug; this.debugDox = debugDox; + this.scaleLat = scaleLat; + this.scaleLon = scaleLon; + this.onExceed = onExceed; } @Override @@ -76,7 +85,7 @@ public class GetStateTask extends BukkitRunnable { location = city.getLocation(); lat = location.getLatitude(); lon = location.getLongitude(); - if (debugDox) logger.info( String.format( "%s's location is %f, %f", p.getName(), lat, lon )); + if (debugDox) logger.info( String.format( "%s's real location is %f, %f", p.getName(), lat, lon )); state = provider.request_state(lat, lon); if (debug) logger.info( String.format( "Provider returned state %s %s for %s", state.condition.name(), state.level.name(), p.getName() @@ -90,6 +99,23 @@ public class GetStateTask extends BukkitRunnable { } catch (Exception e) { e.printStackTrace(); } + } else if (source.equals("map")) { + double[] coords; + double lat, lon; + State state; + for (Player p : Bukkit.getOnlinePlayers()) { + coords = mapUtils.playerPosAsCoords(p.getLocation(), scaleLat, scaleLon, onExceed); + lon = coords[0]; + lat = coords[1]; + logger.info( String.format( "%s's location is %f, %f", p.getName(), lat, lon )); + state = provider.request_state(lat, lon); + if (debug) logger.info( String.format( + "Provider returned state %s %s for %f, %f", state.condition.name(), state.level.name(), lat, lon + )); + if (broadcast) p.sendMessage( String.format("%s %s in %f, %f", + state.level.name(), state.condition.name(), lat, lon + ) ); + } } } } diff --git a/src/main/java/pl/minecon724/realweather/MapUtils.java b/src/main/java/pl/minecon724/realweather/MapUtils.java new file mode 100644 index 0000000..97cf740 --- /dev/null +++ b/src/main/java/pl/minecon724/realweather/MapUtils.java @@ -0,0 +1,23 @@ +package pl.minecon724.realweather; + +import org.bukkit.Location; + +public class MapUtils { + double wrapDouble(double min, double max, double val) { + return min + (val - min) % (max - min); + } + + public double[] playerPosAsCoords(Location loc, double scaleLat, double scaleLon, int onExceed) { + double[] out = new double[2]; + out[0] = loc.getX() * scaleLon; + out[1] = loc.getZ() * scaleLat; + if (onExceed == 1) { + 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 if (onExceed == 2) { + out[0] = wrapDouble(-180.0, 180.0, out[0]); + out[1] = wrapDouble(-90.0, 90.0, out[1]); + } + return out; + } +} diff --git a/src/main/java/pl/minecon724/realweather/RW.java b/src/main/java/pl/minecon724/realweather/RW.java index d2b52a8..422f860 100644 --- a/src/main/java/pl/minecon724/realweather/RW.java +++ b/src/main/java/pl/minecon724/realweather/RW.java @@ -13,8 +13,9 @@ import pl.minecon724.realweather.provider.OpenWeatherMapProvider; public class RW extends JavaPlugin { FileConfiguration config; + WebServiceClient client = null; - + @Override public void onEnable() { long start = System.currentTimeMillis(); @@ -29,6 +30,7 @@ public class RW extends JavaPlugin { String source = weatherSec.getString("source"); ConfigurationSection point = weatherSec.getConfigurationSection("point"); ConfigurationSection player = weatherSec.getConfigurationSection("player"); + ConfigurationSection map = weatherSec.getConfigurationSection("map"); double pointLatitude = point.getDouble("latitude"); double pointLongitude = point.getDouble("longitude"); @@ -57,12 +59,17 @@ public class RW extends JavaPlugin { client = new WebServiceClient.Builder(accId, license).host("geolite.info").build(); } + double scale_lat = map.getDouble("scale_lat"); + double scale_lon = map.getDouble("scale_lon"); + int on_exceed = map.getInt("on_exceed"); + boolean broadcast = settingsSec.getBoolean("broadcast"); boolean debug = settingsSec.getBoolean("debug"); boolean debugDox = settingsSec.getBoolean("debugDox"); new GetStateTask( - provider, source, pointLatitude, pointLongitude, worlds, this.getLogger(), client, broadcast, debug, debugDox + provider, source, pointLatitude, pointLongitude, worlds, this.getLogger(), + client, broadcast, debug, debugDox, scale_lat, scale_lon, on_exceed ).runTaskTimerAsynchronously(this, settingsSec.getLong("timeBeforeInitialRun"), settingsSec.getLong("timeBetweenRecheck") diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 95ddba5..4530867 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,8 +5,9 @@ weather: - world - second_world - third_world - # "point" for a static location - # "player" for using the player's location + # "point" - static location + # "player" - player's location (fake weather) + # "map" - world resembles a real-world globe source: point point: latitude: 41.84201 @@ -14,6 +15,22 @@ weather: player: geolite2_accountId: 710438 geolite2_apiKey: 'qLeseHp4QNQcqRGn' + map: + # Man I've really suffered while working on this one (i hate maths) + # Info: + # Valid latitude range: -90 to 90 + # Valid longitude range: -180 to 180 + # 1 degree of latitude and longitude is about 111 km + # The defaults here assume 1 block = ~1 km + # Latitude scale, 1 block = degrees + scale_lat: 0.009 + # Longitude scale, 1 block = degrees + scale_lon: 0.009 + # What to do if player exceeds the range specified above + # 1 - do nothing (clamp to nearest allowed value) + # 2 - wrap the number + # for example; if the calculated player's latitude is 94 degrees (bad), it'll be converted to -86 degrees (good) + on_exceed: 2 provider: # Your provider choice