thunder & partially weather

I'm done with this for today
This commit is contained in:
Minecon724 2024-06-02 19:00:22 +02:00
parent 11a83a1862
commit af0972f69f
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
20 changed files with 460 additions and 74 deletions

View file

@ -14,20 +14,21 @@
</classpathentry> </classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

7
DOMAINS.md Normal file
View file

@ -0,0 +1,7 @@
If you're using a firewall, you must whitelist the following domains:
- weather:
* api.openweathermap.org
- thunder:
* ws1.blitzortung.org
* ws7.blitzortung.org
* ws8.blitzortung.org

View file

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.m724</groupId>
<artifactId>realweather</artifactId>
<version>0.9-SNAPSHOT</version>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>eu.m724:wtapi</include>
<include>org.java-websocket:Java-WebSocket</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>724rocks</id>
<url>https://git.724.rocks/api/packages/Minecon724/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.6-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
</project>

View file

@ -30,7 +30,7 @@
<dependency> <dependency>
<groupId>eu.m724</groupId> <groupId>eu.m724</groupId>
<artifactId>wtapi</artifactId> <artifactId>wtapi</artifactId>
<version>0.2</version> <version>0.3</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -57,6 +57,7 @@
<artifactSet> <artifactSet>
<includes> <includes>
<include>eu.m724:wtapi</include> <include>eu.m724:wtapi</include>
<include>org.java-websocket:Java-WebSocket</include>
</includes> </includes>
</artifactSet> </artifactSet>
</configuration> </configuration>

View file

@ -0,0 +1,33 @@
package eu.m724.realweather;
import org.bukkit.plugin.Plugin;
import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.thunder.ThunderConfig;
import eu.m724.realweather.time.TimeConfig;
import eu.m724.realweather.weather.WeatherConfig;
public class GlobalConstants {
static WeatherConfig weatherConfig;
static TimeConfig timeConfig;
static ThunderConfig thunderConfig;
static Mapper mapper;
static Plugin plugin;
public static WeatherConfig getWeatherConfig() {
return weatherConfig;
}
public static TimeConfig getTimeConfig() {
return timeConfig;
}
public static ThunderConfig getThunderConfig() {
return thunderConfig;
}
public static Mapper getMapper() {
return mapper;
}
public static Plugin getPlugin() {
return plugin;
}
}

View file

@ -11,8 +11,10 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import eu.m724.realweather.commands.GeoCommand;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.mapper.MapperConfig; import eu.m724.realweather.mapper.MapperConfig;
import eu.m724.realweather.mapper.MapperEventHandler;
import eu.m724.realweather.object.UserException; import eu.m724.realweather.object.UserException;
import eu.m724.realweather.thunder.ThunderConfig; import eu.m724.realweather.thunder.ThunderConfig;
import eu.m724.realweather.thunder.ThunderMaster; import eu.m724.realweather.thunder.ThunderMaster;
@ -38,7 +40,7 @@ public class RealWeatherPlugin extends JavaPlugin {
logger = getLogger(); logger = getLogger();
File dataFolder = getDataFolder(); File dataFolder = getDataFolder();
File modulesFolder = new File(dataFolder, "modules"); File modulesFolder = new File("modules");
modulesFolder.mkdir(); modulesFolder.mkdir();
YamlConfiguration configuration, YamlConfiguration configuration,
@ -47,12 +49,17 @@ public class RealWeatherPlugin extends JavaPlugin {
DebugLogger.info("loading configurations", 1); DebugLogger.info("loading configurations", 1);
if (!new File(dataFolder, "config.yml").exists()) {
logger.info("This is the first run of this plugin.");
logger.info("Please shutdown the server and input your API keys and settings");
}
try { try {
configuration = getConfig(new File(dataFolder, "config.yml")); configuration = getConfig("config.yml");
mapConfiguration = getConfig(new File(dataFolder, "map.yml")); mapConfiguration = getConfig("map.yml");
weatherConfiguration = getConfig(new File(modulesFolder, "weather.yml")); weatherConfiguration = getConfig("modules/weather.yml");
thunderConfiguration = getConfig(new File(modulesFolder, "thunder.yml")); thunderConfiguration = getConfig("modules/thunder.yml");
timeConfiguration = getConfig(new File(modulesFolder, "time.yml")); timeConfiguration = getConfig("modules/time.yml");
} catch (IOException e) { } catch (IOException e) {
logger.severe("Failed to load config!"); logger.severe("Failed to load config!");
e.printStackTrace(); e.printStackTrace();
@ -64,29 +71,33 @@ public class RealWeatherPlugin extends JavaPlugin {
DebugLogger.baseLogger = logger; DebugLogger.baseLogger = logger;
DebugLogger.debugLevel = configuration.getInt("debug"); DebugLogger.debugLevel = configuration.getInt("debug");
if (configuration.getBoolean("enabled")) { if (!configuration.getBoolean("enabled")) {
logger.info("plugin disabled by admin"); logger.info("plugin disabled by admin");
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return;
} }
GlobalConstants.plugin = this;
DebugLogger.info("loading mapper", 1); DebugLogger.info("loading mapper", 1);
mapper = new Mapper( GlobalConstants.mapper = new Mapper(
MapperConfig.fromConfiguration(mapConfiguration)); MapperConfig.fromConfiguration(mapConfiguration));
GlobalConstants.mapper.registerEvents(this);
try { try {
DebugLogger.info("loading weather", 1); DebugLogger.info("loading weather", 1);
weatherMaster = new WeatherMaster( GlobalConstants.weatherConfig = WeatherConfig.fromConfiguration(weatherConfiguration);
WeatherConfig.fromConfiguration(weatherConfiguration), mapper); weatherMaster = new WeatherMaster(GlobalConstants.weatherConfig);
weatherMaster.init(); weatherMaster.init();
DebugLogger.info("loading thunder", 1); DebugLogger.info("loading thunder", 1);
thunderMaster = new ThunderMaster( GlobalConstants.thunderConfig = ThunderConfig.fromConfiguration(thunderConfiguration);
ThunderConfig.fromConfiguration(thunderConfiguration), mapper, this); thunderMaster = new ThunderMaster(GlobalConstants.thunderConfig);
thunderMaster.init(); thunderMaster.init();
DebugLogger.info("loading time", 1); DebugLogger.info("loading time", 1);
timeMaster = new TimeMaster( GlobalConstants.timeConfig = TimeConfig.fromConfiguration(timeConfiguration);
TimeConfig.fromConfiguration(timeConfiguration), mapper); timeMaster = new TimeMaster(GlobalConstants.timeConfig);
timeMaster.init(); timeMaster.init();
} catch (UserException e) { } catch (UserException e) {
logger.severe("There are errors in your config:"); logger.severe("There are errors in your config:");
@ -102,15 +113,18 @@ public class RealWeatherPlugin extends JavaPlugin {
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
} }
getCommand("geo").setExecutor(new GeoCommand());
DebugLogger.info("ended loading", 1); DebugLogger.info("ended loading", 1);
} }
public YamlConfiguration getConfig(File configFile) throws IOException { public YamlConfiguration getConfig(String configFilePath) throws IOException {
File configFile = new File(this.getDataFolder(), configFilePath);
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
if (!configFile.exists()) { if (!configFile.exists()) {
final InputStream defConfigStream = getResource("config.yml"); final InputStream defConfigStream = getResource(configFilePath);
if (defConfigStream == null) if (defConfigStream == null)
return null; return null;

View file

@ -0,0 +1,56 @@
package eu.m724.realweather.commands;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import eu.m724.realweather.GlobalConstants;
import eu.m724.wtapi.object.Coordinates;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
public class GeoCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = sender instanceof Player ? (Player) sender : null;
if (args.length == 0) {
if (player != null) {
Location location = player.getLocation();
Coordinates coordinates = GlobalConstants.getMapper().locationToCoordinates(location);
String text = String.format("Position: %f %f %f\nGeolocation: %f %f", location.getX(), location.getY(), location.getZ(), coordinates.latitude, coordinates.longitude);
BaseComponent component = TextComponent.fromLegacy(text); // TODO add color
player.spigot().sendMessage(component);
} else {
sender.sendMessage("Add arguments to use this command in console");
}
} else if (args.length >= 2) {
double latitude = Double.parseDouble(args[0]);
double longitude = Double.parseDouble(args[0]);
Coordinates coordinates = new Coordinates(latitude, longitude);
Location location = GlobalConstants.getMapper().coordinatesToLocation(player.getWorld(), coordinates);
String text = String.format("Position: %f %f %f\nGeolocation: %f %f", location.getX(), location.getY(), location.getZ(), coordinates.latitude, coordinates.longitude);
BaseComponent component = TextComponent.fromLegacy(text);
player.spigot().sendMessage(component);
if (args.length == 3) {
if (args[2].equalsIgnoreCase("tp") && player != null && player.hasPermission("realweather.geo.tp")) {
Location targetLoc =
location.getWorld().getHighestBlockAt(location).getLocation().add(0, 1, 0);
player.teleport(targetLoc);
}
}
} else {
sender.sendMessage("Not enough arguments");
}
return true;
}
}

View file

@ -1,20 +1,26 @@
package eu.m724.realweather.mapper; package eu.m724.realweather.mapper;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
public class Mapper { public class Mapper {
private MapperConfig config; private MapperConfig config;
private List<World> worlds; private List<World> worlds = new ArrayList<>();
public Mapper(MapperConfig config) { public Mapper(MapperConfig config) {
this.config = config; this.config = config;
} }
public void registerEvents(Plugin plugin) {
plugin.getServer().getPluginManager().registerEvents(new MapperEventHandler(this), plugin);
}
public Coordinates locationToCoordinates(Location location) { public Coordinates locationToCoordinates(Location location) {
double latitude = -location.getZ() / config.scaleLatitude; double latitude = -location.getZ() / config.scaleLatitude;
double longitude = location.getX() / config.scaleLongitude; double longitude = location.getX() / config.scaleLongitude;

View file

@ -2,8 +2,6 @@ package eu.m724.realweather.thunder;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import eu.m724.realweather.time.TimeConfig;
public class ThunderConfig { public class ThunderConfig {
public boolean enabled; public boolean enabled;

View file

@ -2,32 +2,26 @@ package eu.m724.realweather.thunder;
import java.util.ArrayList; import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.object.UserException; import eu.m724.realweather.object.UserException;
import eu.m724.wtapi.provider.WeatherProvider;
import eu.m724.wtapi.provider.exception.ProviderException; import eu.m724.wtapi.provider.exception.ProviderException;
import eu.m724.wtapi.provider.impl.openweathermap.OpenWeatherMapProvider;
import eu.m724.wtapi.thunder.ThunderProvider; import eu.m724.wtapi.thunder.ThunderProvider;
import eu.m724.wtapi.thunder.impl.lightningmaps.LightningMapsProvider; import eu.m724.wtapi.thunder.impl.blitzortung.BlitzortungProvider;
import eu.m724.wtapi.thunder.impl.lightningmaps.TimedStrike;
public class ThunderMaster { public class ThunderMaster {
private ThunderConfig config; private ThunderConfig config;
private Mapper mapper;
private ThunderProvider provider; private ThunderProvider provider;
private Plugin plugin;
private ThunderTask thunderTask; private ThunderTask thunderTask;
ArrayList<TimedStrike> strikes = new ArrayList<>(); private Mapper mapper = GlobalConstants.getMapper();
private Plugin plugin = GlobalConstants.getPlugin();
public ThunderMaster(ThunderConfig config, Mapper mapper, Plugin plugin) { public ThunderMaster(ThunderConfig config) {
this.config = config; this.config = config;
this.mapper = mapper;
this.plugin = plugin;
} }
/** /**
@ -46,11 +40,8 @@ public class ThunderMaster {
provider.init(); provider.init();
provider.registerStrikeHandler(coords -> { thunderTask = new ThunderTask(provider);
strikes.add(new TimedStrike(System.currentTimeMillis() + provider.getDelay(), coords)); thunderTask.init();
});
thunderTask = new ThunderTask(this, mapper);
thunderTask.runTaskTimer(plugin, 0, config.refresh); thunderTask.runTaskTimer(plugin, 0, config.refresh);
DebugLogger.info("thunder loaded", 1); DebugLogger.info("thunder loaded", 1);
@ -58,8 +49,8 @@ public class ThunderMaster {
private ThunderProvider createProvider() { private ThunderProvider createProvider() {
switch (config.provider) { switch (config.provider) {
case "lightningmaps": case "blitzortung":
return new LightningMapsProvider(); return new BlitzortungProvider();
} }
return null; return null;

View file

@ -1,38 +1,53 @@
package eu.m724.realweather.thunder; package eu.m724.realweather.thunder;
import java.util.ArrayList;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.wtapi.thunder.impl.lightningmaps.TimedStrike; import eu.m724.wtapi.thunder.ThunderProvider;
import eu.m724.wtapi.thunder.impl.blitzortung.TimedStrike;
class ThunderTask extends BukkitRunnable { class ThunderTask extends BukkitRunnable {
private ThunderMaster thunderMaster; private ThunderProvider thunderProvider;
private Mapper mapper; private Mapper mapper = GlobalConstants.getMapper();
private ArrayList<TimedStrike> strikes = new ArrayList<>();
public ThunderTask(ThunderMaster thunderMaster, Mapper mapper) { public ThunderTask(ThunderProvider thunderProvider) {
this.thunderMaster = thunderMaster; this.thunderProvider = thunderProvider;
this.mapper = mapper; }
public void init() {
thunderProvider.registerStrikeHandler(coords -> {
strikes.add(new TimedStrike(System.currentTimeMillis() + thunderProvider.getDelay(), coords));
});
thunderProvider.start();
DebugLogger.info("thunderprovider started", 3);
} }
@Override @Override
public void run() { public void run() {
DebugLogger.info("thundertask running", 3); DebugLogger.info("thundertask running", 3);
while (thunderMaster.strikes.size() > 0) { thunderProvider.tick();
TimedStrike strike = thunderMaster.strikes.get(0);
thunderMaster.strikes.remove(0); while (strikes.size() > 0) {
TimedStrike strike = strikes.get(0);
strikes.remove(0);
DebugLogger.info("strike: %f %f", 2, strike.coordinates.latitude, strike.coordinates.longitude); DebugLogger.info("strike: %f %f", 2, strike.coordinates.latitude, strike.coordinates.longitude);
mapper.getWorlds().forEach(w -> { mapper.getWorlds().forEach(w -> {
Location location = mapper.coordinatesToLocation(w, strike.coordinates); Location location = mapper.coordinatesToLocation(w, strike.coordinates);
DebugLogger.info("in %s that converts to: %d %d %d", 2, w.getName(), location.getBlockX(), location.getBlockY(), location.getBlockZ()); DebugLogger.info("in %s that converts to: %d %d", 2, w.getName(), location.getBlockX(), location.getBlockZ());
if (w.getChunkAt(location).isLoaded()) { // for some reason all isloaded etc methods utilizing the Chunk object actually load that chunk before
location.setY(w.getHighestBlockYAt(location)); if (w.isChunkLoaded(location.getBlockX() / 16, location.getBlockZ() / 16)) {
w.spawnEntity(location, EntityType.LIGHTNING_BOLT); location.setY(w.getHighestBlockYAt(location) + 1);
w.strikeLightning(location);
DebugLogger.info("spawnd lightning in %s on y level %d", 2, w.getName(), location.getBlockY()); DebugLogger.info("spawnd lightning in %s on y level %d", 2, w.getName(), location.getBlockY());
} }

View file

@ -1,23 +1,16 @@
package eu.m724.realweather.time; package eu.m724.realweather.time;
import java.util.ArrayList;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.object.UserException; import eu.m724.realweather.object.UserException;
import eu.m724.realweather.thunder.ThunderConfig;
import eu.m724.wtapi.provider.exception.ProviderException;
import eu.m724.wtapi.thunder.ThunderProvider;
import eu.m724.wtapi.thunder.impl.lightningmaps.LightningMapsProvider;
import eu.m724.wtapi.thunder.impl.lightningmaps.TimedStrike;
public class TimeMaster { public class TimeMaster {
private TimeConfig config; private TimeConfig config;
private Mapper mapper; private Mapper mapper = GlobalConstants.getMapper();
public TimeMaster(TimeConfig config, Mapper mapper) { public TimeMaster(TimeConfig config) {
this.config = config; this.config = config;
this.mapper = mapper;
} }
/** /**

View file

@ -0,0 +1,106 @@
package eu.m724.realweather.weather;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.weather.event.AsyncWeatherUpdateEvent;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Weather;
import eu.m724.wtapi.provider.WeatherProvider;
public class AsyncWeatherRetrieveTask extends BukkitRunnable {
private WeatherProvider weatherProvider;
private boolean dynamic; // TODO handle config some other way
private Mapper mapper = GlobalConstants.getMapper();
private Plugin plugin = GlobalConstants.getPlugin();
private Server server = plugin.getServer();
public AsyncWeatherRetrieveTask(WeatherProvider weatherProvider, boolean dynamic) {
this.weatherProvider = weatherProvider;
this.dynamic = dynamic;
}
@Override
public void run() {
long delay = 6000;
if (dynamic) {
if (server.getOnlinePlayers().size() == 0) return;
List<Player> players = server.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("realweather.dynamic"))
.filter(player -> mapper.getWorlds().contains(player.getWorld()))
.collect(Collectors.toUnmodifiableList());
Coordinates[] coordinates = players.stream()
.map(player -> mapper.locationToCoordinates(player.getLocation()))
.toArray(Coordinates[]::new);
CompletableFuture<Weather[]> weathersFuture =
weatherProvider.getWeatherBulk(coordinates);
float hourly = weatherProvider.getQuotaHourly() / (float)(weatherProvider.getBulkLimit() * players.size());
int minTickDelay = (int) (72000 / hourly);
delay = Math.max(6000, minTickDelay);
try {
Weather[] weathers = weathersFuture.join();
for (int i=0; i<weathers.length; i++) {
Player player = players.get(i);
Weather weather = weathers[i];
AsyncWeatherUpdateEvent event =
new AsyncWeatherUpdateEvent(player, weather);
server.getPluginManager().callEvent(event);
// the event applies weather
}
/*DynamicWeatherApplier applier = new DynamicWeatherApplier(players, );
applier.runTask(plugin);*/
} catch (CompletionException e) { // TODO handle finer exceptions
DebugLogger.info("failed to retrieve weather data", 1);
if (DebugLogger.getDebugLevel() > 0)
e.printStackTrace();
delay = 200;
}
} else {
Coordinates point = mapper.getPoint();
CompletableFuture<Weather> weatherFuture = weatherProvider.getWeather(point);
try {
Weather weather = weatherFuture.join();
AsyncWeatherUpdateEvent event =
new AsyncWeatherUpdateEvent(null, weather);
server.getPluginManager().callEvent(event);
} catch (CompletionException e) { // TODO handle finer exceptions
DebugLogger.info("failed to retrieve weather data", 1);
if (DebugLogger.getDebugLevel() > 0)
e.printStackTrace();
delay = 200;
}
}
runTaskLaterAsynchronously(plugin, delay);
}
}

View file

@ -0,0 +1,50 @@
package eu.m724.realweather.weather;
import java.util.List;
import org.bukkit.WeatherType;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import eu.m724.realweather.DebugLogger;
import eu.m724.wtapi.object.Weather;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
public class DynamicWeatherApplier extends BukkitRunnable {
private List<Player> players;
private Weather[] weathers;
public DynamicWeatherApplier(List<Player> players, Weather[] weathers) {
this.players = players;
this.weathers = weathers;
}
@Override
public void run() {
DebugLogger.info("applying weather for %d players", 2, players.size());
for (int i=0; i<players.size(); i++) {
Player player = players.get(i);
Weather weather = weathers[i];
if (player.hasPermission("realweather.actionbar")) {
String text = String.format("%f %f (%s) - %s",
weather.coordinates.latitude, weather.coordinates.longitude,
weather.city, weather.description);
BaseComponent component = TextComponent.fromLegacy(text);
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, component);
DebugLogger.info("%s has actionbar permission so they received status", 2, player.getName());
}
if (weather.isRaining()) {
player.setPlayerWeather(WeatherType.DOWNFALL);
} else {
player.setPlayerWeather(WeatherType.CLEAR);
} // TODO
}
}
}

View file

@ -1,6 +1,7 @@
package eu.m724.realweather.weather; package eu.m724.realweather.weather;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.object.UserException; import eu.m724.realweather.object.UserException;
import eu.m724.wtapi.provider.WeatherProvider; import eu.m724.wtapi.provider.WeatherProvider;
@ -9,12 +10,11 @@ import eu.m724.wtapi.provider.impl.openweathermap.OpenWeatherMapProvider;
public class WeatherMaster { public class WeatherMaster {
private WeatherConfig config; private WeatherConfig config;
private Mapper mapper;
private WeatherProvider provider; private WeatherProvider provider;
private Mapper mapper = GlobalConstants.getMapper();
public WeatherMaster(WeatherConfig config, Mapper mapper) { public WeatherMaster(WeatherConfig config) {
this.config = config; this.config = config;
this.mapper = mapper;
} }
/** /**

View file

@ -0,0 +1,46 @@
package eu.m724.realweather.weather.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import eu.m724.wtapi.object.Weather;
/**
* Fired when a weather state is retrieved
* it doesn't mean a change
*/
public class AsyncWeatherUpdateEvent extends Event {
private static final HandlerList HANDLERS = new HandlerList();
private final Player player;
private final Weather weather;
public AsyncWeatherUpdateEvent(Player player, Weather weather) {
super(true);
this.player = player;
this.weather = weather;
}
/**
*
* @return a player that the weather is for, null if not dynamic
*/
public Player getPlayer() {
return player;
}
public Weather getWeather() {
return weather;
}
public static HandlerList getHandlerList() {
return HANDLERS;
}
@Override
public HandlerList getHandlers() {
return HANDLERS;
}
}

View file

@ -14,4 +14,4 @@ updater:
# 1 - debug loading modules # 1 - debug loading modules
# 2 - also debug processing conditions # 2 - also debug processing conditions
# 3 - also log tasks running, this will spam # 3 - also log tasks running, this will spam
debug: 0 debug: 3

View file

@ -3,10 +3,9 @@
############################ ############################
# true if the list below is a blacklist, false otherwise # true if the list below is a blacklist, false otherwise
worldBlacklist: true worldBlacklist: false
worlds: worlds:
- disabled_world - world
- something
dimensions: dimensions:
# blocks per 1 deg, can't be decimal # blocks per 1 deg, can't be decimal

View file

@ -5,8 +5,10 @@
enabled: false enabled: false
# currently only lightningmaps # currently only lightningmaps
provider: lightningmaps provider: blitzortung
# how often should we poll for updates and spawn lightning # how often should we poll for updates and spawn lightning
# note that this runs synchronously so increase if lag # this is a synchronous task
refresh: 50 # millis # if you put it too low you'll have constant lag
# but if you put it too high it will process a lot of data at once so you'll have lag spikes
refresh: 100 # ticks

View file

@ -39,5 +39,13 @@ permissions:
realweather.geo.tp: realweather.geo.tp:
description: Allows teleportation using /geo description: Allows teleportation using /geo
realweather.dynamic:
description: Includes player in dynamic conditions
default: true
realweather.actionbar:
description: Displays status on player's action bar
default: op
realweather.update.notify: realweather.update.notify:
description: Receive notifications for RealWeather updates description: Receive notifications for RealWeather updates