diff --git a/src/main/java/eu/m724/realweather/commands/GeoCommand.java b/src/main/java/eu/m724/realweather/commands/GeoCommand.java index 0133a9b..bdda541 100644 --- a/src/main/java/eu/m724/realweather/commands/GeoCommand.java +++ b/src/main/java/eu/m724/realweather/commands/GeoCommand.java @@ -1,6 +1,8 @@ package eu.m724.realweather.commands; -import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.*; +import net.md_5.bungee.api.chat.hover.content.Content; +import net.md_5.bungee.api.chat.hover.content.Text; import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -13,88 +15,89 @@ import eu.m724.realweather.weather.PlayerWeatherCache; import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Weather; import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.BaseComponent; -import net.md_5.bungee.api.chat.ComponentBuilder; -// TODO unmess components here public class GeoCommand implements CommandExecutor { - private PlayerWeatherCache playerWeatherCache = GlobalConstants.getPlayerWeatherCache(); - private Mapper mapper = GlobalConstants.getMapper(); + private final PlayerWeatherCache playerWeatherCache = GlobalConstants.getPlayerWeatherCache(); + private final Mapper mapper = GlobalConstants.getMapper(); @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) { + if (player != null) { Location location = player.getLocation(); Coordinates coordinates = mapper.locationToCoordinates(location); - + Weather weather = playerWeatherCache.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\n".formatted(location.getX(), location.getZ())).color(ChatColor.DARK_AQUA) - .append("Address: ").color(ChatColor.GRAY) - .append(address + "\n").color(ChatColor.DARK_AQUA) - .create(); - - player.spigot().sendMessage(component); - - } else { - sender.sendMessage("Add arguments to use this command in console"); + + colorize(player, ""); + colorize(player, "&6Geolocation: &b%f&7, %b%f &7(lat, lon)", coordinates.latitude, coordinates.longitude); + colorize(player, "&7Position: &3%f&8, %3%f &8(x, z)", location.getX(), location.getZ()); + colorize(player, "&7City: &3%s", address); + colorize(player, ""); } - } else if (args.length >= 2) { - double latitude, longitude; + } else if (args.length >= 3) { + colorize(sender, "&cInvalid arguments, &7make sure it's &a\"/geo lat,lon\" &7or &a\"/geo x z\" &7or just &a\"/geo\""); + } else if (args.length == 2) { + double x, z; try { - latitude = Double.parseDouble(args[0]); - longitude = Double.parseDouble(args[1]); + x = Double.parseDouble(args[0]); + z = Double.parseDouble(args[1]); } catch (NumberFormatException e) { - sender.sendMessage("Arguments should be latitude and longitude"); + colorize(sender, "&cInvalid arguments, &7make sure it's &a\"/geo lat,lon\" &7or &a\"/geo x z\" &7or just &a\"/geo\""); return true; } - - Coordinates coordinates = new Coordinates(latitude, longitude); - Location location = mapper.coordinatesToLocation(player.getWorld(), coordinates); - - BaseComponent[] component = new ComponentBuilder("\nPosition: ").color(ChatColor.GOLD) - .append( - new ComponentBuilder("x: %f, z: %f\n".formatted(location.getX(), location.getZ())) - .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/tp %f ~ %f".formatted(location.getX(), location.getZ()))) - .create() - ).color(ChatColor.AQUA) - .append("Geolocation: ").color(ChatColor.GRAY) - .append("lat: %f, lon: %f\n".formatted(coordinates.latitude, coordinates.longitude)).color(ChatColor.DARK_AQUA) - .create(); - - player.spigot().sendMessage(component); - - if (args.length == 3) { - if (args[2].equalsIgnoreCase("tp") && player != null && player.hasPermission("realweather.command.geo.tp")) { - Location targetLoc = - location.getWorld().getHighestBlockAt(location).getLocation().add(0, 1, 0); - player.teleport(targetLoc); - player.sendMessage("Teleporting... The server will freeze for a second"); - } - } - + + Location location = new Location(null, x, 0, z); + Coordinates coordinates = mapper.locationToCoordinates(location); + + colorize(sender, ""); + colorize(sender, "&6Position: &b%f&7, %b%f &7(x, z)", location.getX(), location.getZ()); + colorize(sender, "&7Geolocation: &3%f&8, %3%f &8(lat, lon)", coordinates.latitude, coordinates.longitude); + colorize(sender, "&7Input interpreted as position, because you separated with a space"); + colorize(sender, ""); + + return true; } else { - sender.sendMessage("Not enough arguments"); + double latitude, longitude; + + try { + String[] split = args[0].split(","); + latitude = Double.parseDouble(split[0]); + longitude = Double.parseDouble(split[1]); + } catch (NumberFormatException e) { + colorize(sender, "&cInvalid arguments, &7make sure it's &a\"/geo lat,lon\" &7or &a\"/geo x z\" &7or just &a\"/geo\""); + return true; + } + + Coordinates coordinates = new Coordinates(latitude, longitude); + Location location = mapper.coordinatesToLocation(null, coordinates); + + colorize(sender, ""); + colorize(sender, "&6Position: &b%f&7, %b%f &7(x, z)", location.getX(), location.getZ()); + colorize(sender, "&7Geolocation: &3%f&8, %3%f &8(lat, lon)", coordinates.latitude, coordinates.longitude); + colorize(sender, "&7Input interpreted as geolocation, because you separated with a comma"); + colorize(sender, ""); } + return true; } + + private void colorize(CommandSender sender, String text, Object... format) { + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', text.formatted(format))); + } private String formatAddress(Weather weather) { - if (weather == null) return "Weather not retrieved yet"; + if (weather == null) return "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; + return "Somewhere in " + coordinates.country; else if (coordinates.country == null) return coordinates.city; return coordinates.city + ", " + coordinates.country;