public class Gift implements Listener {
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player p = (Player) sender;
if (command.getName().equalsIgnoreCase("gift")) {
if (p.hasPermission("prison.gift")) {
if (args.length != 1) {
return false;
}
Player target = Bukkit.getPlayerExact(args[0]);
if (!target.hasPlayedBefore()) {
p.sendMessage(ChatColor.RED + target.getDisplayName() + " §6не заходил на сервер!");
return true;
}
Location tloc = target.getLocation();
Location ploc = p.getLocation();
if (isInRadius(ploc, tloc, 5)) {
if (target.isOnline()) {
p.setItemInHand(new ItemStack (Material.AIR));
target.getInventory().addItem(p.getItemInHand());
p.sendMessage("§6Вы успешно отправили игроку §c" + target.getDisplayName() + "§6подарок!");
target.sendMessage("§aВы получили подарок от игрока §c" + p.getDisplayName());
target.playSound(target.getLocation(), Sound.LEVEL_UP, 2, 2);
return true;
} else {
p.sendMessage("§cДанный игрок сейчас не онлайн!");
}
} else {
p.sendMessage("§cДля того, чтобы подарить §6" + target.getDisplayName() + " §cподарок, нужно находиться рядом с ним!");
return true;
}
} else {
p.sendMessage("§cУ Вас ещё нет прав на это!");
return true;
}
}
return true;
}
public static boolean isEntityInRadius(Location center, double radius, Entity entity) {
return isInRadius(center, entity.getLocation(), radius);
}
public static boolean isInRadius(Location center, Location loc, double radius) {
if (!loc.getWorld().equals(center.getWorld())) {
return false;
}
return center.distanceSquared(loc) <= (radius * radius);
}
public static Set<Entity> getEntitiesNearLocation(Location center, int radius)
{
Set<Entity> entities = new HashSet<> ();
for (Entity entity : center.getWorld().getEntities())
{
if (isEntityInRadius(center, radius, entity))
{
entities.add(entity);
}
}
return entities;
}
}