This commit is contained in:
Minecon724 2022-04-24 17:05:51 +00:00
parent e184902841
commit 1ab0306c57
4 changed files with 27 additions and 13 deletions

View file

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pl.minecon724</groupId> <groupId>pl.minecon724</groupId>
<artifactId>realweather</artifactId> <artifactId>realweather</artifactId>
<version>0.1.0</version> <version>0.1.1</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>

View file

@ -6,6 +6,7 @@ import java.util.logging.Logger;
import com.maxmind.geoip2.WebServiceClient; import com.maxmind.geoip2.WebServiceClient;
import com.maxmind.geoip2.exception.AddressNotFoundException; import com.maxmind.geoip2.exception.AddressNotFoundException;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.Location; import com.maxmind.geoip2.record.Location;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -26,12 +27,15 @@ public class GetStateTask extends BukkitRunnable {
List<String> worlds; List<String> worlds;
Logger logger; Logger logger;
WebServiceClient client; WebServiceClient client;
boolean broadcast;
boolean debug, debugDox;
public GetStateTask( public GetStateTask(
Provider provider, String source, Provider provider, String source,
double pointLatitude, double pointLongitude, double pointLatitude, double pointLongitude,
List<String> worlds, Logger logger, List<String> worlds, Logger logger,
WebServiceClient client WebServiceClient client, boolean broadcast,
boolean debug, boolean debugDox
) { ) {
this.provider = provider; this.provider = provider;
this.source = source; this.source = source;
@ -40,6 +44,9 @@ public class GetStateTask extends BukkitRunnable {
this.worlds = worlds; this.worlds = worlds;
this.logger = logger; this.logger = logger;
this.client = client; this.client = client;
this.broadcast = broadcast;
this.debug = debug;
this.debugDox = debugDox;
} }
@Override @Override
@ -47,7 +54,7 @@ public class GetStateTask extends BukkitRunnable {
logger.info("Refreshing weather by " + source); logger.info("Refreshing weather by " + source);
if (source.equals("point")) { if (source.equals("point")) {
State state = provider.request_state(pointLatitude, pointLongitude); State state = provider.request_state(pointLatitude, pointLongitude);
logger.info(String.format("Provider returned state %s %s (%s)", state.condition.name(), state.level.name(), state.simple.name())); if (debug) logger.info(String.format("Provider returned state %s %s (%s)", state.condition.name(), state.level.name(), state.simple.name()));
for (String w : worlds) { for (String w : worlds) {
World world = Bukkit.getWorld(w); World world = Bukkit.getWorld(w);
if (world == null) continue; if (world == null) continue;
@ -61,20 +68,21 @@ public class GetStateTask extends BukkitRunnable {
Location location; Location location;
State state; State state;
double lat, lon; double lat, lon;
CityResponse city;
for (Player p : Bukkit.getOnlinePlayers()) { for (Player p : Bukkit.getOnlinePlayers()) {
curr = p; curr = p;
playerIp = p.getAddress().getAddress(); playerIp = p.getAddress().getAddress();
location = client.city(playerIp).getLocation(); city = client.city(playerIp);
location = city.getLocation();
lat = location.getLatitude(); lat = location.getLatitude();
lon = location.getLongitude(); lon = location.getLongitude();
logger.info( String.format( if (debugDox) logger.info( String.format( "%s's location is %f, %f", p.getName(), lat, lon ));
"%s's location is %f, %f", p.getName(), lat, lon
));
state = provider.request_state(lat, lon); state = provider.request_state(lat, lon);
logger.info( String.format( if (debug) logger.info( String.format(
"Provider returned state %s %s for %s", state.condition.name(), state.level.name(), p.getName() "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); p.setPlayerWeather(state.simple == ConditionSimple.CLEAR ? WeatherType.CLEAR : WeatherType.DOWNFALL);
if (broadcast) p.sendMessage( String.format("%s %s in %s", state.level.name(), state.condition.name(), city.getCity().getName()) );
} }
} catch (AddressNotFoundException e) { } catch (AddressNotFoundException e) {
logger.warning(String.format("%s's IP address (%s) is not a public IP address, therefore we can't retrieve their location.", curr.getName(), playerIp.toString())); logger.warning(String.format("%s's IP address (%s) is not a public IP address, therefore we can't retrieve their location.", curr.getName(), playerIp.toString()));

View file

@ -1,8 +1,6 @@
package pl.minecon724.realweather; package pl.minecon724.realweather;
import java.util.List; import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import com.maxmind.geoip2.WebServiceClient; import com.maxmind.geoip2.WebServiceClient;
@ -60,15 +58,19 @@ public class RW extends JavaPlugin {
client = new WebServiceClient.Builder(accId, license).host("geolite.info").build(); client = new WebServiceClient.Builder(accId, license).host("geolite.info").build();
} }
boolean broadcast = settingsSec.getBoolean("broadcast");
boolean debug = settingsSec.getBoolean("debug");
boolean debugDox = settingsSec.getBoolean("debugDox");
new GetStateTask( new GetStateTask(
provider, source, pointLatitude, pointLongitude, worlds, this.getLogger(), client provider, source, pointLatitude, pointLongitude, worlds, this.getLogger(), client, broadcast, debug, debugDox
).runTaskTimerAsynchronously(this, ).runTaskTimerAsynchronously(this,
settingsSec.getLong("timeBeforeInitialRun"), settingsSec.getLong("timeBeforeInitialRun"),
settingsSec.getLong("timeBetweenRecheck") settingsSec.getLong("timeBetweenRecheck")
); );
// new Metrics(this, 15020); // new Metrics(this, 15020);
// bstat disabled cuz error :( // ^^ https://www.spigotmc.org/threads/554949/
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
this.getLogger().info( String.format( this.getName() + " enabled! (%s ms)", Long.toString( end-start ) ) ); this.getLogger().info( String.format( this.getName() + " enabled! (%s ms)", Long.toString( end-start ) ) );

View file

@ -30,6 +30,10 @@ settings:
# 20 is one second # 20 is one second
# Shouldn't affect performance # Shouldn't affect performance
timeBetweenRecheck: 600 timeBetweenRecheck: 600
# Whether to broadcast messages about weather updates
broadcast: false
# Advanced options # Advanced options
timeBeforeInitialRun: 0 timeBeforeInitialRun: 0
debug: false
debugAllowDox: false