realweather/src/main/java/eu/m724/realweather/commands/GeoCommand.java
2024-06-22 13:22:48 +02:00

98 lines
3.6 KiB
Java

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.realweather.mapper.Mapper;
import eu.m724.realweather.weather.PlayerWeatherDirectory;
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;
public class GeoCommand implements CommandExecutor {
private PlayerWeatherDirectory playerWeatherDirectory = GlobalConstants.getPlayerWeatherDirectory();
private 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) {
Location location = player.getLocation();
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\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);
} else {
sender.sendMessage("Add arguments to use this command in console");
}
} else if (args.length >= 2) {
double latitude, longitude;
try {
latitude = Double.parseDouble(args[0]);
longitude = Double.parseDouble(args[1]);
} catch (NumberFormatException e) {
sender.sendMessage("Arguments should be latitude and longitude");
return true;
}
Coordinates coordinates = new Coordinates(latitude, longitude);
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)
.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;
}
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;
}
}