public class NearCommand implements CommandExecutor {
private final JavaPlugin plugin;
private final List<String[]> pointersList = new ArrayList<>();
private final int radius = 100;
private final LuckPerms luckPerms = LuckPermsProvider.get();
public NearCommand(NormalCore normalCore) {
this.plugin = NormalCore.getPlugin(NormalCore.class);
initPointersList();
}
private void initPointersList() {
add("↑");
add("↗", "↖");
add("→", "←");
add("↘", "↙");
add("↓");
}
private void add(String... strings) {
pointersList.add(strings);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("normalcore.near")) {
player.sendMessage(Coloriser.coloriser(plugin.getConfig().getString("NearCommand.noNearPerm")));
} else {
boolean hasPlayersNearby = false;
List<Player> nearbyPlayers = new ArrayList<>();
for (Entity entity : player.getNearbyEntities(radius, radius, radius)) {
if (entity.getType() == EntityType.PLAYER && entity instanceof Player) {
Player targetPlayer = (Player) entity;
User user = luckPerms.getUserManager().getUser(targetPlayer.getUniqueId());
if (user != null && user.getPrimaryGroup() != null) {
nearbyPlayers.add(targetPlayer);
hasPlayersNearby = true;
}
}
}
if (hasPlayersNearby) {
player.sendMessage(Coloriser.coloriser("&fИгроки поблизости:"));
String nearPrefix = Coloriser.coloriser(plugin.getConfig().getString("NearCommand.nearPrefix"));
TextComponent finalMessage = new TextComponent(nearPrefix);
for (int i = 0; i < nearbyPlayers.size(); i++) {
Player target = nearbyPlayers.get(i);
String playerInfo = map(player, target);
TextComponent textComponent = new TextComponent(playerInfo);
TextComponent inventoryButton = new TextComponent(Coloriser.coloriser("&a[Инвентарь]"));
inventoryButton.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(Coloriser.coloriser("&7Нажмите, чтобы открыть инвентарь")).create()));
inventoryButton.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/invsee " + target.getName()));
TextComponent highlightButton = new TextComponent(Coloriser.coloriser("&e[Подсветить]"));
highlightButton.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(Coloriser.coloriser("&7Нажмите, чтобы подсветить игрока")).create()));
highlightButton.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/wallhack"));
finalMessage.addExtra(textComponent);
finalMessage.addExtra("");
finalMessage.addExtra(inventoryButton);
finalMessage.addExtra(" ");
finalMessage.addExtra(highlightButton);
if (i < nearbyPlayers.size() - 1) {
finalMessage.addExtra(Coloriser.coloriser("&f, "));
}
}
player.spigot().sendMessage(finalMessage);
} else {
player.sendMessage(Coloriser.coloriser(plugin.getConfig().getString("NearCommand.noPlayersNearby")));
}
}
return true;
}
private String map(Player player, Player target) {
int distance = (int) player.getLocation().distance(target.getLocation());
String direction = getDirection(player, target);
User user = luckPerms.getUserManager().getUser(target.getUniqueId());
String groupName = user.getPrimaryGroup();
CachedMetaData metaData = luckPerms.getGroupManager().getGroup(groupName).getCachedData().getMetaData();
String groupPrefix = (metaData != null && metaData.getPrefix() != null)
? metaData.getPrefix().replace("&", "§")
: "неизвестно";
return Coloriser.coloriser(plugin.getConfig().getString("NearCommand.playerInfo")
.replace("{groupPrefix}", groupPrefix)
.replace("{player}", target.getName())
.replace("{direction}", direction)
.replace("{distance}", String.valueOf(distance)));
}
private String getDirection(Player player, Player target) {
Vector look = player.getLocation().getDirection().setY(0).normalize();
Vector vector = target.getLocation().toVector().subtract(player.getLocation().toVector()).setY(0).normalize();
int angle = (int) Math.round(Math.toDegrees(look.angle(vector)) / 45.0D);
String[] pointers = pointersList.get(angle);
if (pointers.length == 1) {
return Coloriser.coloriser(pointers[0]);
} else {
boolean right = getSign(look.getX()) * vector.getZ() >= look.getZ() * getSign(vector.getX());
return Coloriser.coloriser(pointers[right ? 0 : 1]);
}
}
private double getSign(double d) {
return d > 0.0D ? 1.0D : (d == 0.0D ? 0.0D : -1.0D);
}
}