This commit is contained in:
parent
5d53237ae0
commit
a03fd98de4
1 changed files with 57 additions and 54 deletions
|
@ -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,13 +15,10 @@ 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) {
|
||||
|
@ -33,68 +32,72 @@ public class GeoCommand implements CommandExecutor {
|
|||
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) {
|
||||
} 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;
|
||||
|
||||
try {
|
||||
latitude = Double.parseDouble(args[0]);
|
||||
longitude = Double.parseDouble(args[1]);
|
||||
String[] split = args[0].split(",");
|
||||
latitude = Double.parseDouble(split[0]);
|
||||
longitude = Double.parseDouble(split[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);
|
||||
Location location = mapper.coordinatesToLocation(null, 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");
|
||||
}
|
||||
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, "");
|
||||
}
|
||||
|
||||
} else {
|
||||
sender.sendMessage("Not enough arguments");
|
||||
}
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue