From e84b2625c33e539070a8b21b5665d9f799fc012c Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 18 Dec 2019 21:36:36 +0100 Subject: [PATCH] added real time and realtime weather --- Realtime.iml | 14 +++ src/de/steamwar/realtime/Realtime.java | 126 ++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 Realtime.iml diff --git a/Realtime.iml b/Realtime.iml new file mode 100644 index 0000000..f3c4b8a --- /dev/null +++ b/Realtime.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/de/steamwar/realtime/Realtime.java b/src/de/steamwar/realtime/Realtime.java index 2973c22..47a86fa 100644 --- a/src/de/steamwar/realtime/Realtime.java +++ b/src/de/steamwar/realtime/Realtime.java @@ -1,10 +1,21 @@ package de.steamwar.realtime; +import org.apache.commons.io.IOUtils; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.plugin.java.JavaPlugin; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; -import java.util.Calendar; +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Date; public class Realtime extends JavaPlugin { @@ -12,11 +23,120 @@ public class Realtime extends JavaPlugin { @Override public void onEnable(){ Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> { - Date time = Calendar.getInstance().getTime(); + /*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); + }*/ + + //Zeit + DateFormat dateFormat = new SimpleDateFormat("HHmm"); + Date date = new Date(); + int time = Integer.parseInt((dateFormat.format(date) + "0")); + + time -= 6000; + + if(time < 0) { + time+= 24000; } - }, 0, 600); + + //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 + } + } + }, 0, 20 * 60); + } + + public WeatherState getCurrentWeather() { + + /* + * https://openweathermap.org/current + * + * http://api.openweathermap.org/data/2.5/weather?id= + * ID + * &APPID=16e8ffada1fbdbe3f3903802b0785751 + * + * Berlin id: 2950159 + * + * ID Liste: https://openweathermap.org/weather-conditions + * + * Clear Sky: ID 800 + * Snow: ID 6xx -> xx sind alle Zahlen hinter der 6 + * Rain: ID 5xx -> ^ + * 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"); + inputStream = url.openStream(); + + dataInputStream = new DataInputStream(new BufferedInputStream(inputStream)); + + StringBuilder stringBuilder = new StringBuilder(); + String inputLine; + + while ((inputLine = dataInputStream.readLine()) != null) { + stringBuilder.append(inputLine); + } + jsonString = stringBuilder.toString(); + } catch (MalformedURLException ex) { + ex.printStackTrace(); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + IOUtils.closeQuietly(inputStream); + } + + JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString); + + String weather; + + JSONArray jsonArray = (JSONArray) jsonObject.get("weather"); + JSONObject arrayObject = (JSONObject) jsonArray.get(0); + weather = String.valueOf((int) (long) arrayObject.get("id")); + + String firstChar = Character.toString(weather.charAt(0)); + switch (firstChar) { + case "6": + return WeatherState.SNOW; + case "5": case "3": //3 = Drizzle + return WeatherState.RAIN; + case "2": + return WeatherState.THUNDER_STORM; + default: + return WeatherState.CLEAR_SKY; //case 8 ebenfalls CLEAR_SKY + } + } + + enum WeatherState { + CLEAR_SKY, + RAIN, + THUNDER_STORM, + SNOW } }