From 15652828ce84bc394d4f27ce183397a0d65b813a Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 24 Apr 2022 09:49:22 +0000 Subject: [PATCH] s --- .classpath | 26 +++++++------- pom.xml | 7 +++- .../minecon724/realweather/GetStateTask.java | 36 +++++++++++++++++-- .../java/pl/minecon724/realweather/RW.java | 14 +++++++- src/main/resources/config.yml | 5 ++- src/main/resources/plugin.yml | 3 +- 6 files changed, 72 insertions(+), 19 deletions(-) diff --git a/.classpath b/.classpath index 28e5303..1df54b7 100644 --- a/.classpath +++ b/.classpath @@ -1,11 +1,5 @@ - - - - - - @@ -21,13 +15,6 @@ - - - - - - - @@ -36,6 +23,19 @@ + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 84e27f0..c74f2b8 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 pl.minecon724 realweather - 0.0.2 + 0.1.0 17 @@ -33,6 +33,11 @@ bstats-bukkit 3.0.0 + + com.maxmind.geoip2 + geoip2 + 3.0.1 + diff --git a/src/main/java/pl/minecon724/realweather/GetStateTask.java b/src/main/java/pl/minecon724/realweather/GetStateTask.java index 20c7dbd..0b50c21 100644 --- a/src/main/java/pl/minecon724/realweather/GetStateTask.java +++ b/src/main/java/pl/minecon724/realweather/GetStateTask.java @@ -1,10 +1,17 @@ package pl.minecon724.realweather; +import java.net.InetAddress; import java.util.List; import java.util.logging.Logger; +import com.maxmind.geoip2.WebServiceClient; +import com.maxmind.geoip2.model.CityResponse; +import com.maxmind.geoip2.record.Location; + import org.bukkit.Bukkit; +import org.bukkit.WeatherType; import org.bukkit.World; +import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import pl.minecon724.realweather.WeatherState.ConditionSimple; @@ -18,11 +25,13 @@ public class GetStateTask extends BukkitRunnable { double pointLongitude; List worlds; Logger logger; + WebServiceClient client; public GetStateTask( Provider provider, String source, double pointLatitude, double pointLongitude, - List worlds, Logger logger + List worlds, Logger logger, + WebServiceClient client ) { this.provider = provider; this.source = source; @@ -30,6 +39,7 @@ public class GetStateTask extends BukkitRunnable { this.pointLongitude = pointLongitude; this.worlds = worlds; this.logger = logger; + this.client = client; } @Override @@ -44,7 +54,29 @@ public class GetStateTask extends BukkitRunnable { world.setThundering(state.simple == ConditionSimple.THUNDER ? true : false); world.setStorm(state.simple == ConditionSimple.CLEAR ? false : true); } + } else if (source.equals("player")) { + try { + InetAddress playerIp; + Location location; + State state; + double lat, lon; + for (Player p : Bukkit.getOnlinePlayers()) { + playerIp = p.getAddress().getAddress(); + location = client.city(playerIp).getLocation(); + lat = location.getLatitude(); + lon = location.getLongitude(); + logger.fine( String.format( + "%s's location is %f, %f", p.getName(), lat, lon + )); + state = provider.request_state(lat, lon); + logger.fine( String.format( + "Provider returned state %s %s for %s", state.condition.name(), state.level.name(), p.getName() + )); + p.setPlayerWeather(state.simple == ConditionSimple.CLEAR ? WeatherType.CLEAR : WeatherType.DOWNFALL); + } + } catch (Exception e) { + e.printStackTrace(); + } } } - } diff --git a/src/main/java/pl/minecon724/realweather/RW.java b/src/main/java/pl/minecon724/realweather/RW.java index d33d849..d492584 100644 --- a/src/main/java/pl/minecon724/realweather/RW.java +++ b/src/main/java/pl/minecon724/realweather/RW.java @@ -1,8 +1,11 @@ package pl.minecon724.realweather; +import java.io.File; import java.util.List; import java.util.logging.Level; +import com.maxmind.geoip2.WebServiceClient; + import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; @@ -13,6 +16,7 @@ import pl.minecon724.realweather.provider.OpenWeatherMapProvider; public class RW extends JavaPlugin { FileConfiguration config; + WebServiceClient client = null; @Override public void onEnable() { @@ -31,6 +35,7 @@ public class RW extends JavaPlugin { String source = weatherSec.getString("source"); ConfigurationSection point = weatherSec.getConfigurationSection("point"); + ConfigurationSection player = weatherSec.getConfigurationSection("player"); double pointLatitude = point.getDouble("latitude"); double pointLongitude = point.getDouble("longitude"); @@ -52,8 +57,15 @@ public class RW extends JavaPlugin { } provider.init(); + if (source.equals("player")) { + this.getLogger().info("This product includes GeoLite2 data created by MaxMind, available from https://maxmind.com"); + int accId = player.getInt("geolite2_accountId"); + String license = player.getString("geolite2_apiKey"); + client = new WebServiceClient.Builder(accId, license).build(); + } + new GetStateTask( - provider, source, pointLatitude, pointLongitude, worlds, this.getLogger() + provider, source, pointLatitude, pointLongitude, worlds, this.getLogger(), client ).runTaskTimerAsynchronously(this, settingsSec.getLong("timeBeforeInitialRun"), settingsSec.getLong("timeBetweenRecheck") diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6257f3e..53aaaa9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -11,6 +11,9 @@ weather: point: latitude: 41.84201 longitude: -89.485937 + player: + geolite2_accountId: 710438 + geolite2_apiKey: 'qLeseHp4QNQcqRGn' provider: # Your provider choice @@ -19,7 +22,7 @@ provider: # Provider settings here # Unlike the previous option, these are case sensitive openweathermap: - apiKey: '' + apiKey: 'd3d37fd3511ef1d4b44c7d574e9b56b8' # More providers soon! settings: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index eb4b7dd..a2b1a06 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,4 +5,5 @@ author: Minecon724 main: pl.minecon724.realweather.RW libraries: - org.json:json:20220320 - - org.bstats:bstats-bukkit:3.0.0 \ No newline at end of file + - org.bstats:bstats-bukkit:3.0.0 + - com.maxmind.geoip2:geoip2:3.0.1 \ No newline at end of file