SteamWar/RealTime
Archiviert
13
0

Merge branch 'weather' of SteamWar/RealTime into master

Dieser Commit ist enthalten in:
Lixfel 2020-02-24 17:48:31 +01:00 committet von Gitea
Commit 227380bca3
5 geänderte Dateien mit 121 neuen und 70 gelöschten Zeilen

1
.gitignore vendored
Datei anzeigen

@ -1,2 +1,3 @@
.idea
target
*.iml

Datei anzeigen

@ -1,14 +0,0 @@
<?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>

12
pom.xml
Datei anzeigen

@ -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.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

Datei anzeigen

@ -0,0 +1,32 @@
package de.steamwar.realtime;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
import java.util.logging.Level;
public class Config {
private Config() {}
private static Realtime instance = Realtime.getInstance();
//Cache
public static final boolean RealTime;
public static final boolean RealWeather;
static {
if(!new File(Bukkit.getWorlds().get(0).getWorldFolder(), "weather.yml").exists()) {
Bukkit.getLogger().log(Level.SEVERE, "Config fehlt!");
RealTime = true;
RealWeather = false;
} else {
FileConfiguration config = instance.getConfig();
RealTime = config.getBoolean("RealTime");
RealWeather = config.getBoolean("RealWeather");
}
}
}

Datei anzeigen

@ -3,6 +3,8 @@ package de.steamwar.realtime;
import org.apache.commons.io.IOUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@ -18,58 +20,47 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Realtime extends JavaPlugin {
public class Realtime extends JavaPlugin implements Listener {
private static Realtime instance;
DateFormat dateFormat = new SimpleDateFormat("HHmm");
WeatherState oldWeatherState = null;
WeatherState weatherState = null;
@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() {
instance = this;
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(this, this);
//Zeit
DateFormat dateFormat = new SimpleDateFormat("HHmm");
Date date = new Date();
int time = Integer.parseInt((dateFormat.format(date) + "0"));
//Time updater
if(Config.RealTime) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
Date date = new Date();
int time = Integer.parseInt((dateFormat.format(date) + "0"));
time -= 6000;
time -= 6000;
if(time < 0) {
time+= 24000;
}
//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
if(time < 0) {
time+= 24000;
}
}
}, 0, 20 * 60);
for(World world : Bukkit.getWorlds())
world.setTime(time);
}, 0, 20 * 60);
}
//Weather Updater
if(Config.RealWeather) {
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, () -> updateCurrentWeather(), 0, 20 * 60 * 10); //Die Wetterdaten werden bei OpenWeatherMAP nur alle 10 Minuten erneuert
}
}
public WeatherState getCurrentWeather() {
private String jsonString = "";
public void updateCurrentWeather() {
/*
* https://openweathermap.org/current
@ -79,6 +70,7 @@ public class Realtime extends JavaPlugin {
* &APPID=16e8ffada1fbdbe3f3903802b0785751
*
* Berlin id: 2950159
* Düsseldorf id: 2934245
*
* ID Liste: https://openweathermap.org/weather-conditions
*
@ -88,11 +80,12 @@ 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");
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));
@ -112,6 +105,7 @@ public class Realtime extends JavaPlugin {
IOUtils.closeQuietly(inputStream);
}
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
String weather;
@ -120,17 +114,55 @@ public class Realtime extends JavaPlugin {
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":
return WeatherState.SNOW;
weatherState = WeatherState.SNOW;
break;
case "5": case "3": //3 = Drizzle
return WeatherState.RAIN;
weatherState = WeatherState.RAIN;
break;
case "2":
return WeatherState.THUNDER_STORM;
weatherState = WeatherState.THUNDER_STORM;
break;
default:
return WeatherState.CLEAR_SKY; //case 8 ebenfalls CLEAR_SKY
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 RAIN:
case SNOW:
world.setThundering(false);
world.setStorm(true);
break;
case THUNDER_STORM:
world.setThundering(true);
world.setStorm(true);
break;
}
}
}, 0);
}
public void shutdown(final String reason) {
Bukkit.getOnlinePlayers().forEach(player -> player.kickPlayer(reason));
Bukkit.shutdown();
}
public static Realtime getInstance() {
return instance;
}
enum WeatherState {