done?
This commit is contained in:
parent
67e7ff692b
commit
0af7e1dd7b
8 changed files with 263 additions and 38 deletions
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"java.configuration.updateBuildConfiguration": "automatic"
|
||||||
|
}
|
53
src/main/java/pl/minecon724/realweather/GetStateTask.java
Normal file
53
src/main/java/pl/minecon724/realweather/GetStateTask.java
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package pl.minecon724.realweather;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import pl.minecon724.realweather.WeatherState.State;
|
||||||
|
|
||||||
|
public class GetStateTask extends BukkitRunnable {
|
||||||
|
|
||||||
|
Provider provider;
|
||||||
|
String source;
|
||||||
|
double pointLatitude;
|
||||||
|
double pointLongitude;
|
||||||
|
List<String> worlds;
|
||||||
|
|
||||||
|
public GetStateTask(
|
||||||
|
Provider provider, String source,
|
||||||
|
double pointLatitude, double pointLongitude,
|
||||||
|
List<String> worlds
|
||||||
|
) {
|
||||||
|
this.provider = provider;
|
||||||
|
this.source = source;
|
||||||
|
this.pointLatitude = pointLatitude;
|
||||||
|
this.pointLongitude = pointLongitude;
|
||||||
|
this.worlds = worlds;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (source == "point") {
|
||||||
|
State state = provider.request_state(pointLatitude, pointLongitude);
|
||||||
|
for (String w : worlds) {
|
||||||
|
World world = Bukkit.getWorld(w);
|
||||||
|
if (world == null) continue;
|
||||||
|
switch (state.simple) {
|
||||||
|
case CLEAR:
|
||||||
|
world.setThundering(false);
|
||||||
|
world.setStorm(false);
|
||||||
|
case RAIN:
|
||||||
|
world.setThundering(false);
|
||||||
|
world.setStorm(true);
|
||||||
|
case THUNDER:
|
||||||
|
world.setThundering(false);
|
||||||
|
world.setStorm(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package pl.minecon724.realweather;
|
package pl.minecon724.realweather;
|
||||||
|
|
||||||
public interface Source {
|
public interface Provider {
|
||||||
public void init();
|
public void init();
|
||||||
public WeatherState.State request_state(double lat, double lon);
|
public WeatherState.State request_state(double lat, double lon);
|
||||||
}
|
}
|
|
@ -1,14 +1,57 @@
|
||||||
package pl.minecon724.realweather;
|
package pl.minecon724.realweather;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
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.provider.OpenWeatherMapProvider;
|
||||||
|
|
||||||
public class RW extends JavaPlugin {
|
public class RW extends JavaPlugin {
|
||||||
FileConfiguration config;
|
FileConfiguration config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
config = getConfig();
|
config = getConfig();
|
||||||
|
ConfigurationSection weatherSec = config.getConfigurationSection("weather");
|
||||||
|
ConfigurationSection providerSec = config.getConfigurationSection("provider");
|
||||||
|
ConfigurationSection settingsSec = config.getConfigurationSection("settings");
|
||||||
|
|
||||||
|
String source = weatherSec.getString("source");
|
||||||
|
ConfigurationSection point = weatherSec.getConfigurationSection("point");
|
||||||
|
|
||||||
|
double pointLatitude = point.getDouble("latitude");
|
||||||
|
double pointLongitude = point.getDouble("longitude");
|
||||||
|
List<String> worlds = weatherSec.getStringList("worlds");
|
||||||
|
|
||||||
|
String choice = providerSec.getString("provider").toLowerCase();
|
||||||
|
ConfigurationSection providerCfg = providerSec.getConfigurationSection(choice);
|
||||||
|
|
||||||
|
if (providerCfg == null) {
|
||||||
|
Bukkit.getLogger().severe("Unknown provider: " + choice);
|
||||||
|
Bukkit.getLogger().info("The plugin will disable now");
|
||||||
|
Bukkit.getPluginManager().disablePlugin(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Provider provider = null;
|
||||||
|
if (choice == "openweathermap") {
|
||||||
|
provider = new OpenWeatherMapProvider( providerCfg.getString("apiKey") );
|
||||||
|
}
|
||||||
|
provider.init();
|
||||||
|
|
||||||
|
new GetStateTask(
|
||||||
|
provider, source, pointLatitude, pointLongitude, worlds
|
||||||
|
).runTaskTimerAsynchronously(this,
|
||||||
|
settingsSec.getLong("timeBeforeInitialRun"),
|
||||||
|
settingsSec.getLong("timeBetweenRecheck")
|
||||||
|
);
|
||||||
|
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
Bukkit.getLogger().info( String.format( this.getName() + "enabled! (%s ms)", Double.toString( Math.ceil(end-start) ) ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ public class WeatherState {
|
||||||
|
|
||||||
// State class
|
// State class
|
||||||
|
|
||||||
public class State {
|
public static class State {
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
|
|
||||||
|
@ -27,5 +27,23 @@ public class WeatherState {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.simple = simple;
|
this.simple = simple;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public State(Condition condition,
|
||||||
|
ConditionLevel level) {
|
||||||
|
this.condition = condition;
|
||||||
|
this.level = level;
|
||||||
|
this.simple = null;
|
||||||
|
switch (condition) {
|
||||||
|
case THUNDER:
|
||||||
|
this.simple = ConditionSimple.THUNDER;
|
||||||
|
case DRIZZLE:
|
||||||
|
case RAIN:
|
||||||
|
case SNOW:
|
||||||
|
this.simple = ConditionSimple.RAIN;
|
||||||
|
case CLEAR:
|
||||||
|
case CLOUDY:
|
||||||
|
this.simple = ConditionSimple.CLEAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,24 @@
|
||||||
package pl.minecon724.realweather.provider;
|
package pl.minecon724.realweather.provider;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import pl.minecon724.realweather.*;
|
import pl.minecon724.realweather.Provider;
|
||||||
import pl.minecon724.realweather.WeatherState.State;
|
import pl.minecon724.realweather.WeatherState.*;
|
||||||
|
|
||||||
public class OpenWeatherMapProvider implements Source {
|
public class OpenWeatherMapProvider implements Provider {
|
||||||
|
|
||||||
URL endpoint;
|
URL endpoint;
|
||||||
|
|
||||||
RW main;
|
|
||||||
String apiKey;
|
String apiKey;
|
||||||
|
|
||||||
public OpenWeatherMapProvider(RW main, String apiKey) {
|
public OpenWeatherMapProvider(String apiKey) {
|
||||||
this.main = main;
|
|
||||||
this.apiKey = apiKey;
|
this.apiKey = apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,38 +26,119 @@ public class OpenWeatherMapProvider implements Source {
|
||||||
try {
|
try {
|
||||||
endpoint = new URL("https://api.openweathermap.org");
|
endpoint = new URL("https://api.openweathermap.org");
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public State request_state(double lat, double lon) {
|
public State request_state(double lat, double lon) {
|
||||||
JSONObject json;
|
JSONObject json = new JSONObject();
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
HttpURLConnection con = (HttpURLConnection) endpoint.openConnection();
|
|
||||||
con.setRequestMethod("GET");
|
|
||||||
int status = con.getResponseCode();
|
|
||||||
InputStream stream = status > 299 ? con.getErrorStream() : con.getInputStream();
|
|
||||||
BufferedReader rd = new BufferedReader(
|
|
||||||
new InputStreamReader(stream));
|
|
||||||
String line;
|
|
||||||
StringBuffer content = new StringBuffer();
|
|
||||||
while ((line = rd.readLine()) != null) {
|
|
||||||
content.append(line);
|
|
||||||
}
|
|
||||||
rd.close();
|
|
||||||
con.disconnect();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
content['a']
|
|
||||||
Condition condition;
|
|
||||||
switch () {
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
URL url = new URL(
|
||||||
|
endpoint + String.format("/data/2.5/weather?lat=%s&lon=%s&appid=%s",
|
||||||
|
Double.toString(lat), Double.toString(lon), apiKey
|
||||||
|
));
|
||||||
|
|
||||||
|
InputStream is = url.openStream();
|
||||||
|
BufferedReader rd = new BufferedReader( new InputStreamReader(is, Charset.forName("UTF-8")) );
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
int c;
|
||||||
|
while ((c = rd.read()) != -1) {
|
||||||
|
sb.append((char) c);
|
||||||
}
|
}
|
||||||
|
is.close();
|
||||||
|
json = new JSONObject(sb.toString());
|
||||||
|
} catch (Exception e) { e.printStackTrace(); }
|
||||||
|
|
||||||
|
int stateId = json.getJSONArray("weather")
|
||||||
|
.getJSONObject(0).getInt("id");
|
||||||
|
|
||||||
|
// Here comes the mess
|
||||||
|
Condition condition = Condition.CLEAR;
|
||||||
|
ConditionLevel level = ConditionLevel.LIGHT;
|
||||||
|
if (stateId < 300) {
|
||||||
|
condition = Condition.THUNDER;
|
||||||
|
switch (stateId) {
|
||||||
|
case 200:
|
||||||
|
case 210:
|
||||||
|
case 230:
|
||||||
|
level = ConditionLevel.LIGHT;
|
||||||
|
case 201:
|
||||||
|
case 211:
|
||||||
|
case 221:
|
||||||
|
case 231:
|
||||||
|
level = ConditionLevel.MODERATE;
|
||||||
|
case 202:
|
||||||
|
case 212:
|
||||||
|
case 232:
|
||||||
|
level = ConditionLevel.HEAVY;
|
||||||
|
}
|
||||||
|
} else if (stateId < 400) {
|
||||||
|
condition = Condition.DRIZZLE;
|
||||||
|
switch (stateId) {
|
||||||
|
case 300:
|
||||||
|
case 310:
|
||||||
|
level = ConditionLevel.LIGHT;
|
||||||
|
case 301:
|
||||||
|
case 311:
|
||||||
|
case 313:
|
||||||
|
case 321:
|
||||||
|
level = ConditionLevel.MODERATE;
|
||||||
|
case 302:
|
||||||
|
case 312:
|
||||||
|
case 314:
|
||||||
|
level = ConditionLevel.HEAVY;
|
||||||
|
}
|
||||||
|
} else if (stateId < 600) {
|
||||||
|
condition = Condition.RAIN;
|
||||||
|
switch (stateId) {
|
||||||
|
case 500:
|
||||||
|
case 520:
|
||||||
|
level = ConditionLevel.LIGHT;
|
||||||
|
case 501:
|
||||||
|
case 511:
|
||||||
|
case 521:
|
||||||
|
case 531:
|
||||||
|
level = ConditionLevel.MODERATE;
|
||||||
|
case 502:
|
||||||
|
case 522:
|
||||||
|
level = ConditionLevel.HEAVY;
|
||||||
|
case 503:
|
||||||
|
case 504:
|
||||||
|
level = ConditionLevel.EXTREME;
|
||||||
|
}
|
||||||
|
} else if (stateId < 700) {
|
||||||
|
condition = Condition.SNOW;
|
||||||
|
switch (stateId) {
|
||||||
|
case 600:
|
||||||
|
case 612:
|
||||||
|
case 615:
|
||||||
|
case 620:
|
||||||
|
level = ConditionLevel.LIGHT;
|
||||||
|
case 601:
|
||||||
|
case 611:
|
||||||
|
case 613:
|
||||||
|
case 616:
|
||||||
|
case 621:
|
||||||
|
level = ConditionLevel.MODERATE;
|
||||||
|
case 602:
|
||||||
|
case 622:
|
||||||
|
level = ConditionLevel.HEAVY;
|
||||||
|
}
|
||||||
|
} else if (stateId > 800) {
|
||||||
|
condition = Condition.CLOUDY;
|
||||||
|
switch (stateId) {
|
||||||
|
case 801:
|
||||||
|
level = ConditionLevel.LIGHT;
|
||||||
|
case 802:
|
||||||
|
level = ConditionLevel.MODERATE;
|
||||||
|
case 803:
|
||||||
|
level = ConditionLevel.HEAVY;
|
||||||
|
case 804:
|
||||||
|
level = ConditionLevel.EXTREME;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
State state = new State(condition, level);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
30
src/main/resources/config.yml
Normal file
30
src/main/resources/config.yml
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
weather:
|
||||||
|
# In which worlds weather control is allowed?
|
||||||
|
# If a world doesn't exist nothing will happen
|
||||||
|
worlds:
|
||||||
|
- world
|
||||||
|
- second_world
|
||||||
|
- third_world
|
||||||
|
# Point for a static location
|
||||||
|
# Fake weather and player locations not implemented yet
|
||||||
|
source: point
|
||||||
|
point:
|
||||||
|
latitude: 41.84201
|
||||||
|
longitude: -89.485937
|
||||||
|
|
||||||
|
provider:
|
||||||
|
# Your provider choice
|
||||||
|
# Case insensitive
|
||||||
|
choice: openweathermap
|
||||||
|
# Provider settings here
|
||||||
|
# Unlike the previous option, these are case sensitive
|
||||||
|
openweathermap:
|
||||||
|
apiKey: ''
|
||||||
|
# More providers soon!
|
||||||
|
|
||||||
|
settings:
|
||||||
|
# 20 is one second
|
||||||
|
timeBetweenRecheck: 1200
|
||||||
|
|
||||||
|
# Advanced options here
|
||||||
|
timeBeforeInitialRun: 0
|
Loading…
Reference in a new issue