SteamWar/RealTime
Archiviert
13
0

added real time and realtime weather

Dieser Commit ist enthalten in:
BuildTools 2019-12-18 21:36:36 +01:00
Ursprung ede8f512ee
Commit e84b2625c3
2 geänderte Dateien mit 137 neuen und 3 gelöschten Zeilen

14
Realtime.iml Normale Datei
Datei anzeigen

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: steamwar:Spigot:1.0" level="project" />
</component>
</module>

Datei anzeigen

@ -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
}
}