Command update /geo
All checks were successful
/ deploy (push) Successful in 1m17s

This commit is contained in:
Minecon724 2024-11-04 16:48:00 +01:00
parent 5d53237ae0
commit a03fd98de4
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8

View file

@ -1,6 +1,8 @@
package eu.m724.realweather.commands; 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.Location;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -13,13 +15,10 @@ import eu.m724.realweather.weather.PlayerWeatherCache;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Weather; import eu.m724.wtapi.object.Weather;
import net.md_5.bungee.api.ChatColor; 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 { public class GeoCommand implements CommandExecutor {
private PlayerWeatherCache playerWeatherCache = GlobalConstants.getPlayerWeatherCache(); private final PlayerWeatherCache playerWeatherCache = GlobalConstants.getPlayerWeatherCache();
private Mapper mapper = GlobalConstants.getMapper(); private final Mapper mapper = GlobalConstants.getMapper();
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -33,68 +32,72 @@ public class GeoCommand implements CommandExecutor {
Weather weather = playerWeatherCache.getWeather(player); Weather weather = playerWeatherCache.getWeather(player);
String address = formatAddress(weather); String address = formatAddress(weather);
BaseComponent[] component = new ComponentBuilder("\nGeolocation: ").color(ChatColor.GOLD) colorize(player, "");
.append("lat: %f, lon: %f\n".formatted(coordinates.latitude, coordinates.longitude)).color(ChatColor.AQUA) colorize(player, "&6Geolocation: &b%f&7, %b%f &7(lat, lon)", coordinates.latitude, coordinates.longitude);
.append("Position: ").color(ChatColor.GRAY) colorize(player, "&7Position: &3%f&8, %3%f &8(x, z)", location.getX(), location.getZ());
.append("x: %f, z: %f\n".formatted(location.getX(), location.getZ())).color(ChatColor.DARK_AQUA) colorize(player, "&7City: &3%s", address);
.append("Address: ").color(ChatColor.GRAY) colorize(player, "");
.append(address + "\n").color(ChatColor.DARK_AQUA)
.create();
player.spigot().sendMessage(component);
} else {
sender.sendMessage("Add arguments to use this command in console");
} }
} else if (args.length >= 2) { } 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 {
x = Double.parseDouble(args[0]);
z = Double.parseDouble(args[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;
}
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 {
double latitude, longitude; double latitude, longitude;
try { try {
latitude = Double.parseDouble(args[0]); String[] split = args[0].split(",");
longitude = Double.parseDouble(args[1]); latitude = Double.parseDouble(split[0]);
longitude = Double.parseDouble(split[1]);
} catch (NumberFormatException e) { } 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; return true;
} }
Coordinates coordinates = new Coordinates(latitude, longitude); Coordinates coordinates = new Coordinates(latitude, longitude);
Location location = mapper.coordinatesToLocation(player.getWorld(), coordinates); Location location = mapper.coordinatesToLocation(null, coordinates);
BaseComponent[] component = new ComponentBuilder("\nPosition: ").color(ChatColor.GOLD) colorize(sender, "");
.append( colorize(sender, "&6Position: &b%f&7, %b%f &7(x, z)", location.getX(), location.getZ());
new ComponentBuilder("x: %f, z: %f\n".formatted(location.getX(), location.getZ())) colorize(sender, "&7Geolocation: &3%f&8, %3%f &8(lat, lon)", coordinates.latitude, coordinates.longitude);
.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/tp %f ~ %f".formatted(location.getX(), location.getZ()))) colorize(sender, "&7Input interpreted as geolocation, because you separated with a comma");
.create() colorize(sender, "");
).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");
}
} }
} else {
sender.sendMessage("Not enough arguments");
}
return true; return true;
} }
private void colorize(CommandSender sender, String text, Object... format) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', text.formatted(format)));
}
private String formatAddress(Weather weather) { private String formatAddress(Weather weather) {
if (weather == null) return "Weather not retrieved yet"; if (weather == null) return "Not retrieved yet";
Coordinates coordinates = weather.coordinates; Coordinates coordinates = weather.coordinates;
if (coordinates.country == null && coordinates.city == null) if (coordinates.country == null && coordinates.city == null)
return "Unknown"; return "Unknown";
else if (coordinates.city == null) else if (coordinates.city == null)
return "Country: " + coordinates.country; return "Somewhere in " + coordinates.country;
else if (coordinates.country == null) else if (coordinates.country == null)
return coordinates.city; return coordinates.city;
return coordinates.city + ", " + coordinates.country; return coordinates.city + ", " + coordinates.country;