From 243a8f000229f9692048849073a9e0ad7d84196c Mon Sep 17 00:00:00 2001 From: Yaruma3341 Date: Thu, 16 Jan 2020 22:34:27 +0100 Subject: [PATCH] code cleanup --- src/de/steamwar/realtime/Realtime.java | 122 ++++++++++++------------- 1 file changed, 59 insertions(+), 63 deletions(-) diff --git a/src/de/steamwar/realtime/Realtime.java b/src/de/steamwar/realtime/Realtime.java index 5fa529e..8861de8 100644 --- a/src/de/steamwar/realtime/Realtime.java +++ b/src/de/steamwar/realtime/Realtime.java @@ -55,7 +55,7 @@ public class Realtime extends JavaPlugin implements Listener { }, 0, 20 * 60); //Weather Updater - Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> updateCurrentWeather(), 0, 20 * 60 * 10); //Die Wetterdaten werden bei OpenWeatherMAP nur alle 10 Minuten erneuert + Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, () -> updateCurrentWeather(), 0, 20 * 60 * 10); //Die Wetterdaten werden bei OpenWeatherMAP nur alle 10 Minuten erneuert } @@ -81,81 +81,77 @@ public class Realtime extends JavaPlugin implements Listener { * Thunderstorm: ID 2xx ^ */ - Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable() { - InputStream inputStream = null; - DataInputStream dataInputStream; + InputStream inputStream = null; + DataInputStream dataInputStream; - @Override - public void run() { - try { - URL url = new URL("http://api.openweathermap.org/data/2.5/weather?id=2934245&APPID=16e8ffada1fbdbe3f3903802b0785751"); - inputStream = url.openStream(); + try { + 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)); + dataInputStream = new DataInputStream(new BufferedInputStream(inputStream)); - StringBuilder stringBuilder = new StringBuilder(); - String inputLine; + 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); - } + 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); + JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString); - String weather; + String weather; - JSONArray jsonArray = (JSONArray) jsonObject.get("weather"); - JSONObject arrayObject = (JSONObject) jsonArray.get(0); - weather = String.valueOf((int) (long) arrayObject.get("id")); + JSONArray jsonArray = (JSONArray) jsonObject.get("weather"); + 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": - weatherState = WeatherState.SNOW; + oldWeatherState = weatherState; + String firstChar = Character.toString(weather.charAt(0)); + switch (firstChar) { + case "6": + weatherState = WeatherState.SNOW; + break; + case "5": case "3": //3 = Drizzle + weatherState = WeatherState.RAIN; + break; + case "2": + weatherState = WeatherState.THUNDER_STORM; + break; + default: + 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 "5": case "3": //3 = Drizzle - weatherState = WeatherState.RAIN; + case RAIN: + case SNOW: + world.setThundering(false); + world.setStorm(true); break; - case "2": - weatherState = WeatherState.THUNDER_STORM; - break; - default: - weatherState = WeatherState.CLEAR_SKY; //case 8 ebenfalls CLEAR_SKY + case THUNDER_STORM: + world.setThundering(true); + world.setStorm(true); 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); } }, 0); }