Optimizing code
Dieser Commit ist enthalten in:
Ursprung
585a604a17
Commit
d7eaf140a9
|
@ -6,27 +6,21 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||
import java.io.File;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Config {
|
||||
|
||||
class Config {
|
||||
private Config() {}
|
||||
|
||||
private static Realtime instance = Realtime.getInstance();
|
||||
|
||||
//Cache
|
||||
public static final boolean RealTime;
|
||||
public static final boolean RealWeather;
|
||||
static final boolean RealTime;
|
||||
static final boolean RealWeather;
|
||||
|
||||
static {
|
||||
if(!new File(Bukkit.getWorlds().get(0).getWorldFolder(), "weather.yml").exists()) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Config fehlt!");
|
||||
Bukkit.getLogger().log(Level.WARNING, "Wetter-Config fehlt!");
|
||||
RealTime = true;
|
||||
RealWeather = false;
|
||||
} else {
|
||||
FileConfiguration config = instance.getConfig();
|
||||
FileConfiguration config = Realtime.getInstance().getConfig();
|
||||
RealTime = config.getBoolean("RealTime");
|
||||
RealWeather = config.getBoolean("RealWeather");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -10,24 +10,17 @@ import org.json.simple.JSONArray;
|
|||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.*;
|
||||
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 implements Listener {
|
||||
|
||||
private static Realtime instance;
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat("HHmm");
|
||||
|
||||
WeatherState oldWeatherState = null;
|
||||
WeatherState weatherState = null;
|
||||
private WeatherState oldWeatherState = null;
|
||||
private WeatherState weatherState = null;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
@ -39,28 +32,22 @@ public class Realtime extends JavaPlugin implements Listener {
|
|||
if(Config.RealTime) {
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
|
||||
Date date = new Date();
|
||||
int time = Integer.parseInt((dateFormat.format(date) + "0"));
|
||||
|
||||
time -= 6000;
|
||||
|
||||
if(time < 0) {
|
||||
time+= 24000;
|
||||
}
|
||||
long time = date.getHours() * 1000 + (long)(date.getMinutes() * 16.66666666666667) + (long)(date.getSeconds() * 0.1666666666666667) - 6000;
|
||||
|
||||
for(World world : Bukkit.getWorlds())
|
||||
world.setTime(time);
|
||||
}, 0, 20 * 60);
|
||||
}, 0, 600);
|
||||
}
|
||||
|
||||
//Weather Updater
|
||||
if(Config.RealWeather) {
|
||||
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, () -> updateCurrentWeather(), 0, 20 * 60 * 10); //Die Wetterdaten werden bei OpenWeatherMAP nur alle 10 Minuten erneuert
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::updateCurrentWeather, 0, 12000); //Die Wetterdaten werden bei OpenWeatherMAP nur alle 10 Minuten erneuert
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String jsonString = "";
|
||||
public void updateCurrentWeather() {
|
||||
private void updateCurrentWeather() {
|
||||
|
||||
/*
|
||||
* https://openweathermap.org/current
|
||||
|
@ -92,17 +79,18 @@ public class Realtime extends JavaPlugin implements Listener {
|
|||
|
||||
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();
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException("Could not read weather data", e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException | NullPointerException e) {
|
||||
//ignored
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -132,10 +120,8 @@ public class Realtime extends JavaPlugin implements Listener {
|
|||
}
|
||||
|
||||
//Weltänderungen
|
||||
if(oldWeatherState == null || oldWeatherState != weatherState){
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(getInstance(), () -> {
|
||||
if(oldWeatherState != null && oldWeatherState == weatherState)
|
||||
return;
|
||||
|
||||
for(World world : Bukkit.getWorlds()) {
|
||||
switch (weatherState) {
|
||||
case CLEAR_SKY:
|
||||
|
@ -155,13 +141,9 @@ public class Realtime extends JavaPlugin implements Listener {
|
|||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
public void shutdown(final String reason) {
|
||||
Bukkit.getOnlinePlayers().forEach(player -> player.kickPlayer(reason));
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
|
||||
public static Realtime getInstance() {
|
||||
static Realtime getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren