closes #1 realtime weather #2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.idea
|
||||
target
|
||||
*.iml
|
12
pom.xml
12
pom.xml
@ -43,11 +43,11 @@
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>steamwar</groupId>
|
||||
<artifactId>Spigot</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>steamwar</groupId>
|
||||
<artifactId>Spigot</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -3,7 +3,12 @@ package de.steamwar.realtime;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockFormEvent;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
@ -18,19 +23,19 @@ import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class Realtime extends JavaPlugin {
|
||||
public class Realtime extends JavaPlugin implements Listener {
|
||||
|
||||
@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() {
|
||||
PluginManager pluginManager = Bukkit.getPluginManager();
|
||||
pluginManager.registerEvents(this, this);
|
||||
|
||||
WeatherState oldWeatherState = null;
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat("HHmm");
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
|
||||
//Zeit
|
||||
DateFormat dateFormat = new SimpleDateFormat("HHmm");
|
||||
Date date = new Date();
|
||||
int time = Integer.parseInt((dateFormat.format(date) + "0"));
|
||||
|
||||
@ -42,6 +47,8 @@ public class Realtime extends JavaPlugin {
|
||||
|
||||
//Wetter
|
||||
WeatherState weatherState = getCurrentWeather();
|
||||
|
||||
if(oldWeatherState != null && oldWeatherState == weatherState)
|
||||
return;
|
||||
|
||||
|
||||
for(World world : Bukkit.getWorlds()) {
|
||||
@ -69,6 +76,8 @@ public class Realtime extends JavaPlugin {
|
||||
}, 0, 20 * 60);
|
||||
}
|
||||
|
||||
|
||||
private String jsonString = "";
|
||||
public WeatherState getCurrentWeather() {
|
||||
|
||||
/*
|
||||
@ -88,29 +97,36 @@ 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");
|
||||
inputStream = url.openStream();
|
||||
|
||||
dataInputStream = new DataInputStream(new BufferedInputStream(inputStream));
|
||||
new BukkitRunnable() {
|
||||
InputStream inputStream = null;
|
||||
DataInputStream dataInputStream;
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String inputLine;
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?id=2950159&APPID=16e8ffada1fbdbe3f3903802b0785751");
|
||||
inputStream = url.openStream();
|
||||
|
||||
while ((inputLine = dataInputStream.readLine()) != null) {
|
||||
stringBuilder.append(inputLine);
|
||||
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);
|
||||
}
|
||||
}
|
||||
jsonString = stringBuilder.toString();
|
||||
} catch (MalformedURLException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
}
|
||||
}.runTaskAsynchronously(this);
|
||||
|
||||
|
||||
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
|
||||
|
||||
@ -139,4 +155,9 @@ public class Realtime extends JavaPlugin {
|
||||
THUNDER_STORM,
|
||||
SNOW
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void handleBlockForm(BlockFormEvent event) {
|
||||
Bukkit.broadcastMessage(event.getBlock() + " : " + event.getNewState().toString());
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren
Du startest bereits einen AsyncRepeatingTask, der sofort loslegt (ohne verzögerung). Das hier verzögert das Hochfahren des Servers, und ich bin darauf bedacht, die Startzeit kurz zu halten. Bitte hier nicht initialisieren.
Das ist doch schon gar nicht mehr im Source Code