thunder & partially weather
I'm done with this for today
This commit is contained in:
parent
11a83a1862
commit
af0972f69f
20 changed files with 460 additions and 74 deletions
|
@ -14,20 +14,21 @@
|
|||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</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>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
|
|
7
DOMAINS.md
Normal file
7
DOMAINS.md
Normal 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
|
60
dependency-reduced-pom.xml
Normal file
60
dependency-reduced-pom.xml
Normal 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>
|
3
pom.xml
3
pom.xml
|
@ -30,7 +30,7 @@
|
|||
<dependency>
|
||||
<groupId>eu.m724</groupId>
|
||||
<artifactId>wtapi</artifactId>
|
||||
<version>0.2</version>
|
||||
<version>0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -57,6 +57,7 @@
|
|||
<artifactSet>
|
||||
<includes>
|
||||
<include>eu.m724:wtapi</include>
|
||||
<include>org.java-websocket:Java-WebSocket</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
</configuration>
|
||||
|
|
33
src/main/java/eu/m724/realweather/GlobalConstants.java
Normal file
33
src/main/java/eu/m724/realweather/GlobalConstants.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -11,8 +11,10 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import eu.m724.realweather.commands.GeoCommand;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
import eu.m724.realweather.mapper.MapperConfig;
|
||||
import eu.m724.realweather.mapper.MapperEventHandler;
|
||||
import eu.m724.realweather.object.UserException;
|
||||
import eu.m724.realweather.thunder.ThunderConfig;
|
||||
import eu.m724.realweather.thunder.ThunderMaster;
|
||||
|
@ -38,7 +40,7 @@ public class RealWeatherPlugin extends JavaPlugin {
|
|||
logger = getLogger();
|
||||
|
||||
File dataFolder = getDataFolder();
|
||||
File modulesFolder = new File(dataFolder, "modules");
|
||||
File modulesFolder = new File("modules");
|
||||
modulesFolder.mkdir();
|
||||
|
||||
YamlConfiguration configuration,
|
||||
|
@ -47,12 +49,17 @@ public class RealWeatherPlugin extends JavaPlugin {
|
|||
|
||||
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 {
|
||||
configuration = getConfig(new File(dataFolder, "config.yml"));
|
||||
mapConfiguration = getConfig(new File(dataFolder, "map.yml"));
|
||||
weatherConfiguration = getConfig(new File(modulesFolder, "weather.yml"));
|
||||
thunderConfiguration = getConfig(new File(modulesFolder, "thunder.yml"));
|
||||
timeConfiguration = getConfig(new File(modulesFolder, "time.yml"));
|
||||
configuration = getConfig("config.yml");
|
||||
mapConfiguration = getConfig("map.yml");
|
||||
weatherConfiguration = getConfig("modules/weather.yml");
|
||||
thunderConfiguration = getConfig("modules/thunder.yml");
|
||||
timeConfiguration = getConfig("modules/time.yml");
|
||||
} catch (IOException e) {
|
||||
logger.severe("Failed to load config!");
|
||||
e.printStackTrace();
|
||||
|
@ -64,29 +71,33 @@ public class RealWeatherPlugin extends JavaPlugin {
|
|||
DebugLogger.baseLogger = logger;
|
||||
DebugLogger.debugLevel = configuration.getInt("debug");
|
||||
|
||||
if (configuration.getBoolean("enabled")) {
|
||||
if (!configuration.getBoolean("enabled")) {
|
||||
logger.info("plugin disabled by admin");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
|
||||
GlobalConstants.plugin = this;
|
||||
|
||||
DebugLogger.info("loading mapper", 1);
|
||||
mapper = new Mapper(
|
||||
GlobalConstants.mapper = new Mapper(
|
||||
MapperConfig.fromConfiguration(mapConfiguration));
|
||||
GlobalConstants.mapper.registerEvents(this);
|
||||
|
||||
try {
|
||||
DebugLogger.info("loading weather", 1);
|
||||
weatherMaster = new WeatherMaster(
|
||||
WeatherConfig.fromConfiguration(weatherConfiguration), mapper);
|
||||
GlobalConstants.weatherConfig = WeatherConfig.fromConfiguration(weatherConfiguration);
|
||||
weatherMaster = new WeatherMaster(GlobalConstants.weatherConfig);
|
||||
weatherMaster.init();
|
||||
|
||||
DebugLogger.info("loading thunder", 1);
|
||||
thunderMaster = new ThunderMaster(
|
||||
ThunderConfig.fromConfiguration(thunderConfiguration), mapper, this);
|
||||
GlobalConstants.thunderConfig = ThunderConfig.fromConfiguration(thunderConfiguration);
|
||||
thunderMaster = new ThunderMaster(GlobalConstants.thunderConfig);
|
||||
thunderMaster.init();
|
||||
|
||||
DebugLogger.info("loading time", 1);
|
||||
timeMaster = new TimeMaster(
|
||||
TimeConfig.fromConfiguration(timeConfiguration), mapper);
|
||||
GlobalConstants.timeConfig = TimeConfig.fromConfiguration(timeConfiguration);
|
||||
timeMaster = new TimeMaster(GlobalConstants.timeConfig);
|
||||
timeMaster.init();
|
||||
} catch (UserException e) {
|
||||
logger.severe("There are errors in your config:");
|
||||
|
@ -102,15 +113,18 @@ public class RealWeatherPlugin extends JavaPlugin {
|
|||
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
|
||||
getCommand("geo").setExecutor(new GeoCommand());
|
||||
|
||||
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);
|
||||
|
||||
if (!configFile.exists()) {
|
||||
final InputStream defConfigStream = getResource("config.yml");
|
||||
final InputStream defConfigStream = getResource(configFilePath);
|
||||
|
||||
if (defConfigStream == null)
|
||||
return null;
|
||||
|
|
56
src/main/java/eu/m724/realweather/commands/GeoCommand.java
Normal file
56
src/main/java/eu/m724/realweather/commands/GeoCommand.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,20 +1,26 @@
|
|||
package eu.m724.realweather.mapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import eu.m724.wtapi.object.Coordinates;
|
||||
|
||||
public class Mapper {
|
||||
private MapperConfig config;
|
||||
private List<World> worlds;
|
||||
private List<World> worlds = new ArrayList<>();
|
||||
|
||||
public Mapper(MapperConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void registerEvents(Plugin plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvents(new MapperEventHandler(this), plugin);
|
||||
}
|
||||
|
||||
public Coordinates locationToCoordinates(Location location) {
|
||||
double latitude = -location.getZ() / config.scaleLatitude;
|
||||
double longitude = location.getX() / config.scaleLongitude;
|
||||
|
|
|
@ -2,8 +2,6 @@ package eu.m724.realweather.thunder;
|
|||
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import eu.m724.realweather.time.TimeConfig;
|
||||
|
||||
public class ThunderConfig {
|
||||
public boolean enabled;
|
||||
|
||||
|
|
|
@ -2,32 +2,26 @@ package eu.m724.realweather.thunder;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
import eu.m724.realweather.object.UserException;
|
||||
import eu.m724.wtapi.provider.WeatherProvider;
|
||||
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.impl.lightningmaps.LightningMapsProvider;
|
||||
import eu.m724.wtapi.thunder.impl.lightningmaps.TimedStrike;
|
||||
import eu.m724.wtapi.thunder.impl.blitzortung.BlitzortungProvider;
|
||||
|
||||
public class ThunderMaster {
|
||||
private ThunderConfig config;
|
||||
private Mapper mapper;
|
||||
private ThunderProvider provider;
|
||||
private Plugin plugin;
|
||||
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.mapper = mapper;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,11 +40,8 @@ public class ThunderMaster {
|
|||
|
||||
provider.init();
|
||||
|
||||
provider.registerStrikeHandler(coords -> {
|
||||
strikes.add(new TimedStrike(System.currentTimeMillis() + provider.getDelay(), coords));
|
||||
});
|
||||
|
||||
thunderTask = new ThunderTask(this, mapper);
|
||||
thunderTask = new ThunderTask(provider);
|
||||
thunderTask.init();
|
||||
thunderTask.runTaskTimer(plugin, 0, config.refresh);
|
||||
|
||||
DebugLogger.info("thunder loaded", 1);
|
||||
|
@ -58,8 +49,8 @@ public class ThunderMaster {
|
|||
|
||||
private ThunderProvider createProvider() {
|
||||
switch (config.provider) {
|
||||
case "lightningmaps":
|
||||
return new LightningMapsProvider();
|
||||
case "blitzortung":
|
||||
return new BlitzortungProvider();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -1,38 +1,53 @@
|
|||
package eu.m724.realweather.thunder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
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 {
|
||||
private ThunderMaster thunderMaster;
|
||||
private Mapper mapper;
|
||||
private ThunderProvider thunderProvider;
|
||||
private Mapper mapper = GlobalConstants.getMapper();
|
||||
private ArrayList<TimedStrike> strikes = new ArrayList<>();
|
||||
|
||||
public ThunderTask(ThunderMaster thunderMaster, Mapper mapper) {
|
||||
this.thunderMaster = thunderMaster;
|
||||
this.mapper = mapper;
|
||||
public ThunderTask(ThunderProvider thunderProvider) {
|
||||
this.thunderProvider = thunderProvider;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
thunderProvider.registerStrikeHandler(coords -> {
|
||||
strikes.add(new TimedStrike(System.currentTimeMillis() + thunderProvider.getDelay(), coords));
|
||||
});
|
||||
thunderProvider.start();
|
||||
DebugLogger.info("thunderprovider started", 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
DebugLogger.info("thundertask running", 3);
|
||||
while (thunderMaster.strikes.size() > 0) {
|
||||
TimedStrike strike = thunderMaster.strikes.get(0);
|
||||
thunderMaster.strikes.remove(0);
|
||||
thunderProvider.tick();
|
||||
|
||||
while (strikes.size() > 0) {
|
||||
TimedStrike strike = strikes.get(0);
|
||||
strikes.remove(0);
|
||||
|
||||
DebugLogger.info("strike: %f %f", 2, strike.coordinates.latitude, strike.coordinates.longitude);
|
||||
|
||||
mapper.getWorlds().forEach(w -> {
|
||||
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()) {
|
||||
location.setY(w.getHighestBlockYAt(location));
|
||||
w.spawnEntity(location, EntityType.LIGHTNING_BOLT);
|
||||
// for some reason all isloaded etc methods utilizing the Chunk object actually load that chunk before
|
||||
if (w.isChunkLoaded(location.getBlockX() / 16, location.getBlockZ() / 16)) {
|
||||
location.setY(w.getHighestBlockYAt(location) + 1);
|
||||
w.strikeLightning(location);
|
||||
DebugLogger.info("spawnd lightning in %s on y level %d", 2, w.getName(), location.getBlockY());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,23 +1,16 @@
|
|||
package eu.m724.realweather.time;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
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 {
|
||||
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.mapper = mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package eu.m724.realweather.weather;
|
||||
|
||||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
import eu.m724.realweather.object.UserException;
|
||||
import eu.m724.wtapi.provider.WeatherProvider;
|
||||
|
@ -9,12 +10,11 @@ import eu.m724.wtapi.provider.impl.openweathermap.OpenWeatherMapProvider;
|
|||
|
||||
public class WeatherMaster {
|
||||
private WeatherConfig config;
|
||||
private Mapper mapper;
|
||||
private WeatherProvider provider;
|
||||
private Mapper mapper = GlobalConstants.getMapper();
|
||||
|
||||
public WeatherMaster(WeatherConfig config, Mapper mapper) {
|
||||
public WeatherMaster(WeatherConfig config) {
|
||||
this.config = config;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -14,4 +14,4 @@ updater:
|
|||
# 1 - debug loading modules
|
||||
# 2 - also debug processing conditions
|
||||
# 3 - also log tasks running, this will spam
|
||||
debug: 0
|
||||
debug: 3
|
|
@ -3,10 +3,9 @@
|
|||
############################
|
||||
|
||||
# true if the list below is a blacklist, false otherwise
|
||||
worldBlacklist: true
|
||||
worldBlacklist: false
|
||||
worlds:
|
||||
- disabled_world
|
||||
- something
|
||||
- world
|
||||
|
||||
dimensions:
|
||||
# blocks per 1 deg, can't be decimal
|
||||
|
|
|
@ -5,8 +5,10 @@
|
|||
enabled: false
|
||||
|
||||
# currently only lightningmaps
|
||||
provider: lightningmaps
|
||||
provider: blitzortung
|
||||
|
||||
# how often should we poll for updates and spawn lightning
|
||||
# note that this runs synchronously so increase if lag
|
||||
refresh: 50 # millis
|
||||
# this is a synchronous task
|
||||
# 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
|
||||
|
|
|
@ -39,5 +39,13 @@ permissions:
|
|||
realweather.geo.tp:
|
||||
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:
|
||||
description: Receive notifications for RealWeather updates
|
Loading…
Reference in a new issue