From a6bd920d17049445a818ef501b656bc6163b413b Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sat, 22 Jun 2024 13:22:48 +0200 Subject: [PATCH] fix /geo --- pom.xml | 2 +- .../m724/realweather/RealWeatherPlugin.java | 2 + .../m724/realweather/commands/GeoCommand.java | 40 +++++++++++++------ .../eu/m724/realweather/updater/Updater.java | 4 +- .../weather/DynamicWeatherApplier.java | 2 +- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 326faa3..119567f 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ eu.m724 wtapi - 0.6 + 0.7 diff --git a/src/main/java/eu/m724/realweather/RealWeatherPlugin.java b/src/main/java/eu/m724/realweather/RealWeatherPlugin.java index e4b50a4..7d628da 100644 --- a/src/main/java/eu/m724/realweather/RealWeatherPlugin.java +++ b/src/main/java/eu/m724/realweather/RealWeatherPlugin.java @@ -24,6 +24,7 @@ import eu.m724.realweather.time.TimeMaster; import eu.m724.realweather.updater.SignatureValidator; import eu.m724.realweather.updater.Updater; import eu.m724.realweather.updater.UpdaterConfig; +import eu.m724.realweather.weather.PlayerWeatherDirectory; import eu.m724.realweather.weather.WeatherConfig; import eu.m724.realweather.weather.WeatherMaster; import eu.m724.wtapi.provider.exception.NoSuchProviderException; @@ -94,6 +95,7 @@ public class RealWeatherPlugin extends JavaPlugin { } GlobalConstants.plugin = this; + GlobalConstants.playerWeatherDirectory = new PlayerWeatherDirectory(); DebugLogger.info("loading mapper", 1); GlobalConstants.mapperConfig = MapperConfig.fromConfiguration(mapConfiguration); diff --git a/src/main/java/eu/m724/realweather/commands/GeoCommand.java b/src/main/java/eu/m724/realweather/commands/GeoCommand.java index fbd674c..54df908 100644 --- a/src/main/java/eu/m724/realweather/commands/GeoCommand.java +++ b/src/main/java/eu/m724/realweather/commands/GeoCommand.java @@ -8,6 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import eu.m724.realweather.GlobalConstants; +import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.weather.PlayerWeatherDirectory; import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Weather; @@ -16,8 +17,8 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; public class GeoCommand implements CommandExecutor { - PlayerWeatherDirectory playerWeatherDirectory = - GlobalConstants.getPlayerWeatherDirectory(); + private PlayerWeatherDirectory playerWeatherDirectory = GlobalConstants.getPlayerWeatherDirectory(); + private Mapper mapper = GlobalConstants.getMapper(); @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -26,22 +27,21 @@ public class GeoCommand implements CommandExecutor { if (args.length == 0) { if (player != null) { Location location = player.getLocation(); - Coordinates coordinates = GlobalConstants.getMapper().locationToCoordinates(location); + Coordinates coordinates = mapper.locationToCoordinates(location); + + Weather weather = playerWeatherDirectory.getWeather(player); + String address = formatAddress(weather); BaseComponent[] component = new ComponentBuilder("\nGeolocation: ").color(ChatColor.GOLD) .append("lat: %f, lon: %f\n".formatted(coordinates.latitude, coordinates.longitude)).color(ChatColor.AQUA) .append("Position: ").color(ChatColor.GRAY) - .append("x: %f, z: %f".formatted(location.getX(), location.getZ())).color(ChatColor.DARK_AQUA) + .append("x: %f, z: %f\n".formatted(location.getX(), location.getZ())).color(ChatColor.DARK_AQUA) + .append("Address: ").color(ChatColor.GRAY) + .append(address + "\n").color(ChatColor.DARK_AQUA) .create(); // TODO improve readability - + player.spigot().sendMessage(component); - Weather weather = playerWeatherDirectory.getWeather(player); - if (weather != null) { - component = new ComponentBuilder("You're in ").color(ChatColor.GRAY) - .append(weather.city + "\n").color(ChatColor.DARK_AQUA) - .create(); - player.spigot().sendMessage(component); - } + } else { sender.sendMessage("Add arguments to use this command in console"); } @@ -57,7 +57,7 @@ public class GeoCommand implements CommandExecutor { } Coordinates coordinates = new Coordinates(latitude, longitude); - Location location = GlobalConstants.getMapper().coordinatesToLocation(player.getWorld(), coordinates); + Location location = mapper.coordinatesToLocation(player.getWorld(), coordinates); BaseComponent[] component = new ComponentBuilder("\nPosition: ").color(ChatColor.GOLD) .append("x: %f, z: %f\n".formatted(location.getX(), location.getZ())).color(ChatColor.AQUA) @@ -72,6 +72,7 @@ public class GeoCommand implements CommandExecutor { Location targetLoc = location.getWorld().getHighestBlockAt(location).getLocation().add(0, 1, 0); player.teleport(targetLoc); + player.sendMessage("Teleporting... The server will freeze for a second"); } } @@ -80,5 +81,18 @@ public class GeoCommand implements CommandExecutor { } return true; } + + private String formatAddress(Weather weather) { + if (weather == null) return "Weather not retrieved yet"; + Coordinates coordinates = weather.coordinates; + + if (coordinates.country == null && coordinates.city == null) + return "Unknown"; + else if (coordinates.city == null) + return "Country: " + coordinates.country; + else if (coordinates.country == null) + return coordinates.city; + return coordinates.city + ", " + coordinates.country; + } } diff --git a/src/main/java/eu/m724/realweather/updater/Updater.java b/src/main/java/eu/m724/realweather/updater/Updater.java index 463cecf..d980c8e 100644 --- a/src/main/java/eu/m724/realweather/updater/Updater.java +++ b/src/main/java/eu/m724/realweather/updater/Updater.java @@ -69,9 +69,9 @@ public class Updater extends BukkitRunnable implements Listener { DebugLogger.info("Unable to contact the update server! %d", 0, statusCode); DebugLogger.info("As it's a server error, it's probably temporary and not a configuration issue, so proceeding.", 0); } else { - DebugLogger.info("Update server returned unexpected status code! %d", 0, statusCode); + DebugLogger.info("Update server returned unexpected status code: %d", 0, statusCode); if (plugin.getDescription().getVersion().endsWith("SNAPSHOT")) { - DebugLogger.info("It looks like this is a development snapshot, possibly built from source. So actually this is expected.", 0); + DebugLogger.info("Ignore as you're running a development snapshot.", 0); } else { DebugLogger.info("This is probably critical. RealWeather will continue without updater.", 0); DebugLogger.info("Try restarting the server. If that doesn't help, contact support.", 0); diff --git a/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java b/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java index cffb570..3aabeb4 100644 --- a/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java +++ b/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java @@ -33,7 +33,7 @@ public class DynamicWeatherApplier extends BukkitRunnable { if (player.hasPermission("realweather.actionbar")) { String text = String.format("%f %f (%s) - %s", weather.coordinates.latitude, weather.coordinates.longitude, - weather.city, weather.description); + weather.coordinates.getAddress(), weather.description); BaseComponent component = TextComponent.fromLegacy(text); player.spigot().sendMessage(ChatMessageType.ACTION_BAR, component);