This commit is contained in:
Minecon724 2022-04-25 17:34:22 +00:00
parent 94464aec3d
commit 02fbc22ce4
6 changed files with 81 additions and 7 deletions

1
README.md Normal file
View file

@ -0,0 +1 @@
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Minecon724/RealWeather)

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.1</version> <version>0.2.0</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>

View file

@ -29,13 +29,19 @@ public class GetStateTask extends BukkitRunnable {
WebServiceClient client; WebServiceClient client;
boolean broadcast; boolean broadcast;
boolean debug, debugDox; boolean debug, debugDox;
double scaleLat, scaleLon;
int onExceed;
MapUtils mapUtils = new MapUtils();
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, boolean broadcast, WebServiceClient client, boolean broadcast,
boolean debug, boolean debugDox boolean debug, boolean debugDox,
double scaleLat, double scaleLon,
int onExceed
) { ) {
this.provider = provider; this.provider = provider;
this.source = source; this.source = source;
@ -47,6 +53,9 @@ public class GetStateTask extends BukkitRunnable {
this.broadcast = broadcast; this.broadcast = broadcast;
this.debug = debug; this.debug = debug;
this.debugDox = debugDox; this.debugDox = debugDox;
this.scaleLat = scaleLat;
this.scaleLon = scaleLon;
this.onExceed = onExceed;
} }
@Override @Override
@ -76,7 +85,7 @@ public class GetStateTask extends BukkitRunnable {
location = city.getLocation(); location = city.getLocation();
lat = location.getLatitude(); lat = location.getLatitude();
lon = location.getLongitude(); 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); state = provider.request_state(lat, lon);
if (debug) 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()
@ -90,6 +99,23 @@ public class GetStateTask extends BukkitRunnable {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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
) );
}
} }
} }
} }

View file

@ -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;
}
}

View file

@ -13,8 +13,9 @@ import pl.minecon724.realweather.provider.OpenWeatherMapProvider;
public class RW extends JavaPlugin { public class RW extends JavaPlugin {
FileConfiguration config; FileConfiguration config;
WebServiceClient client = null; WebServiceClient client = null;
@Override @Override
public void onEnable() { public void onEnable() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@ -29,6 +30,7 @@ public class RW extends JavaPlugin {
String source = weatherSec.getString("source"); String source = weatherSec.getString("source");
ConfigurationSection point = weatherSec.getConfigurationSection("point"); ConfigurationSection point = weatherSec.getConfigurationSection("point");
ConfigurationSection player = weatherSec.getConfigurationSection("player"); ConfigurationSection player = weatherSec.getConfigurationSection("player");
ConfigurationSection map = weatherSec.getConfigurationSection("map");
double pointLatitude = point.getDouble("latitude"); double pointLatitude = point.getDouble("latitude");
double pointLongitude = point.getDouble("longitude"); double pointLongitude = point.getDouble("longitude");
@ -57,12 +59,17 @@ 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();
} }
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 broadcast = settingsSec.getBoolean("broadcast");
boolean debug = settingsSec.getBoolean("debug"); boolean debug = settingsSec.getBoolean("debug");
boolean debugDox = settingsSec.getBoolean("debugDox"); boolean debugDox = settingsSec.getBoolean("debugDox");
new GetStateTask( 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, ).runTaskTimerAsynchronously(this,
settingsSec.getLong("timeBeforeInitialRun"), settingsSec.getLong("timeBeforeInitialRun"),
settingsSec.getLong("timeBetweenRecheck") settingsSec.getLong("timeBetweenRecheck")

View file

@ -5,8 +5,9 @@ weather:
- world - world
- second_world - second_world
- third_world - third_world
# "point" for a static location # "point" - static location
# "player" for using the player's location # "player" - player's location (fake weather)
# "map" - world resembles a real-world globe
source: point source: point
point: point:
latitude: 41.84201 latitude: 41.84201
@ -14,6 +15,22 @@ weather:
player: player:
geolite2_accountId: 710438 geolite2_accountId: 710438
geolite2_apiKey: 'qLeseHp4QNQcqRGn' 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 = <scale> degrees
scale_lat: 0.009
# Longitude scale, 1 block = <scale> 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: provider:
# Your provider choice # Your provider choice