diff --git a/.gitignore b/.gitignore index ee44a96..18f0443 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea target +*.iml \ No newline at end of file diff --git a/Realtime.iml b/Realtime.iml deleted file mode 100644 index f3c4b8a..0000000 --- a/Realtime.iml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index e9f4ae3..38f1ac8 100644 --- a/pom.xml +++ b/pom.xml @@ -43,11 +43,11 @@ - - steamwar - Spigot - 1.0 - provided - + + steamwar + Spigot + 1.12 + provided + \ No newline at end of file diff --git a/src/de/steamwar/realtime/Config.java b/src/de/steamwar/realtime/Config.java new file mode 100644 index 0000000..540fd47 --- /dev/null +++ b/src/de/steamwar/realtime/Config.java @@ -0,0 +1,32 @@ +package de.steamwar.realtime; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; + +import java.io.File; +import java.util.logging.Level; + +public class Config { + + private Config() {} + + private static Realtime instance = Realtime.getInstance(); + + //Cache + public static final boolean RealTime; + public static final boolean RealWeather; + + static { + if(!new File(Bukkit.getWorlds().get(0).getWorldFolder(), "weather.yml").exists()) { + Bukkit.getLogger().log(Level.SEVERE, "Config fehlt!"); + RealTime = true; + RealWeather = false; + } else { + FileConfiguration config = instance.getConfig(); + RealTime = config.getBoolean("RealTime"); + RealWeather = config.getBoolean("RealWeather"); + } + } + + +} diff --git a/src/de/steamwar/realtime/Realtime.java b/src/de/steamwar/realtime/Realtime.java index 47a86fa..01be430 100644 --- a/src/de/steamwar/realtime/Realtime.java +++ b/src/de/steamwar/realtime/Realtime.java @@ -3,6 +3,8 @@ package de.steamwar.realtime; import org.apache.commons.io.IOUtils; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.event.Listener; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -18,58 +20,47 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; -public class Realtime extends JavaPlugin { +public class Realtime extends JavaPlugin implements Listener { + + private static Realtime instance; + + DateFormat dateFormat = new SimpleDateFormat("HHmm"); + + WeatherState oldWeatherState = null; + WeatherState weatherState = null; @Override - public void onEnable(){ - Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> { - /*Date time = Calendar.getInstance().getTime(); - long l = time.getHours() * 1000 + (long)(time.getMinutes() * 16.66666666666667) + (long)(time.getSeconds() * 0.1666666666666667) - 6000; - for(World w : Bukkit.getWorlds()){ - w.setTime(l); - }*/ + public void onEnable() { + instance = this; + PluginManager pluginManager = Bukkit.getPluginManager(); + pluginManager.registerEvents(this, this); - //Zeit - DateFormat dateFormat = new SimpleDateFormat("HHmm"); - Date date = new Date(); - int time = Integer.parseInt((dateFormat.format(date) + "0")); + //Time updater + if(Config.RealTime) { + Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> { + Date date = new Date(); + int time = Integer.parseInt((dateFormat.format(date) + "0")); - time -= 6000; + time -= 6000; - if(time < 0) { - time+= 24000; - } - - //Wetter - WeatherState weatherState = getCurrentWeather(); - - - for(World world : Bukkit.getWorlds()) { - world.setTime(time); - - switch (weatherState) { - case CLEAR_SKY: - world.setThundering(false); - world.setStorm(false); - break; - case RAIN: - world.setThundering(false); - world.setStorm(true); - break; - case THUNDER_STORM: - world.setThundering(true); - world.setStorm(true); - break; - case SNOW: - world.setThundering(false); - world.setStorm(true); - //Biom muss geändert werden + if(time < 0) { + time+= 24000; } - } - }, 0, 20 * 60); + + for(World world : Bukkit.getWorlds()) + world.setTime(time); + }, 0, 20 * 60); + } + + //Weather Updater + if(Config.RealWeather) { + Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, () -> updateCurrentWeather(), 0, 20 * 60 * 10); //Die Wetterdaten werden bei OpenWeatherMAP nur alle 10 Minuten erneuert + } } - public WeatherState getCurrentWeather() { + + private String jsonString = ""; + public void updateCurrentWeather() { /* * https://openweathermap.org/current @@ -79,6 +70,7 @@ public class Realtime extends JavaPlugin { * &APPID=16e8ffada1fbdbe3f3903802b0785751 * * Berlin id: 2950159 + * Düsseldorf id: 2934245 * * ID Liste: https://openweathermap.org/weather-conditions * @@ -88,11 +80,12 @@ public class Realtime extends JavaPlugin { * Drizzle (Nieselregen): ID 3xx -> ^ * Thunderstorm: ID 2xx ^ */ + InputStream inputStream = null; DataInputStream dataInputStream; - String jsonString = ""; + try { - URL url = new URL("http://api.openweathermap.org/data/2.5/weather?id=2950159&APPID=16e8ffada1fbdbe3f3903802b0785751"); + URL url = new URL("http://api.openweathermap.org/data/2.5/weather?id=2934245&APPID=16e8ffada1fbdbe3f3903802b0785751"); inputStream = url.openStream(); dataInputStream = new DataInputStream(new BufferedInputStream(inputStream)); @@ -112,6 +105,7 @@ public class Realtime extends JavaPlugin { IOUtils.closeQuietly(inputStream); } + JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString); String weather; @@ -120,17 +114,55 @@ public class Realtime extends JavaPlugin { JSONObject arrayObject = (JSONObject) jsonArray.get(0); weather = String.valueOf((int) (long) arrayObject.get("id")); + oldWeatherState = weatherState; String firstChar = Character.toString(weather.charAt(0)); switch (firstChar) { case "6": - return WeatherState.SNOW; + weatherState = WeatherState.SNOW; + break; case "5": case "3": //3 = Drizzle - return WeatherState.RAIN; + weatherState = WeatherState.RAIN; + break; case "2": - return WeatherState.THUNDER_STORM; + weatherState = WeatherState.THUNDER_STORM; + break; default: - return WeatherState.CLEAR_SKY; //case 8 ebenfalls CLEAR_SKY + weatherState = WeatherState.CLEAR_SKY; //case 8 ebenfalls CLEAR_SKY + break; } + + //Weltänderungen + Bukkit.getScheduler().scheduleSyncDelayedTask(getInstance(), () -> { + if(oldWeatherState != null && oldWeatherState == weatherState) + return; + + for(World world : Bukkit.getWorlds()) { + switch (weatherState) { + case CLEAR_SKY: + world.setThundering(false); + world.setStorm(false); + break; + case RAIN: + case SNOW: + world.setThundering(false); + world.setStorm(true); + break; + case THUNDER_STORM: + world.setThundering(true); + world.setStorm(true); + break; + } + } + }, 0); + } + + public void shutdown(final String reason) { + Bukkit.getOnlinePlayers().forEach(player -> player.kickPlayer(reason)); + Bukkit.shutdown(); + } + + public static Realtime getInstance() { + return instance; } enum WeatherState {