package players.around; import net.md_5.bungee.api.ChatColor; import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; public class MainClass extends JavaPlugin{ private final String COMMAND = "around"; private final String PERMUSE = "around.use"; private final String DISTANCE = "maxDistance"; private final String PERMALL = "around.*"; @Override public void onEnable() { this.getLogger().info("PLAYERS AROUND duoc bat thanh cong!"); loadConfig(); } @Override public void onDisable() { this.getLogger().info("PLAYERS AROUND da bi vo hieu hoa!"); } public void loadConfig() { this.getConfig().options().copyDefaults(true); this.saveConfig(); } public void setDistance(double distance) { this.getConfig().set(DISTANCE, distance); this.saveConfig(); } public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) { if (cmd.getName().equalsIgnoreCase(COMMAND)) { if (args.length == 0) { sender.sendMessage("/around check - Check yourself"); if (!(sender instanceof Player) || sender.hasPermission(PERMALL)) { sender.sendMessage("/around check - Check PLAYER"); sender.sendMessage("/around set - Set the distance"); } } else { // Console command if (!(sender instanceof Player)) { // Check someone if (args[0].equalsIgnoreCase("check")) { if (args.length == 1) { sender.sendMessage(ChatColor.YELLOW + "Nhap ten mot nguoi choi"); } else { Player p = getServer().getPlayer(args[1]); if (p == null) { sender.sendMessage("Nguoi choi offline hoac khong ton tai!"); } else { Location pLocation = p.getLocation(); double distance = this.getConfig().getDouble(DISTANCE); int i = 0; for (Player player : getServer().getOnlinePlayers()) { Location targetLocation = player.getLocation(); double distanceX = Math.abs(targetLocation.getX() - pLocation.getX()); double distanceZ = Math.abs(targetLocation.getZ() - pLocation.getZ()); if ((distanceX <= distance || distanceZ <= distance) && player != p) { i++; sender.sendMessage(ChatColor.YELLOW + "" + i + ". " + player.getName()); } } sender.sendMessage(ChatColor.GREEN + "Tong cong co " + i + " nguoi"); } } } // Set distance if (args[0].equalsIgnoreCase("set")) { try { double distance = Double.parseDouble(args[1]); this.getConfig().set(DISTANCE, distance); this.saveConfig(); sender.sendMessage(ChatColor.GREEN + "Da cap nhat thanh cong khoang cach la " + distance); } catch(Exception e) { sender.sendMessage(ChatColor.RED + "Xay ra loi!"); } } if (!args[0].equalsIgnoreCase("check") && !args[0].equalsIgnoreCase("set")) { sender.sendMessage(ChatColor.RED + "Lenh khong ton tai!"); } } /////////////// /////////////// // Player command if (sender instanceof Player) { Player p = (Player) sender; if (args[0].equalsIgnoreCase("check") ) { ////////////////////// if (args.length == 2 && p.hasPermission(PERMALL)) { Player pl = getServer().getPlayer(args[1]); if (pl == null) { sender.sendMessage(ChatColor.RED + "Người chơi offline hoặc không tồn tại"); } else { Location pLocation = pl.getLocation(); double distance = this.getConfig().getDouble(DISTANCE); int i = 0; for (Player player : getServer().getOnlinePlayers()) { Location targetLocation = player.getLocation(); double distanceX = Math.abs(targetLocation.getX() - pLocation.getX()); double distanceZ = Math.abs(targetLocation.getZ() - pLocation.getZ()); if ((distanceX <= distance || distanceZ <= distance) && player != pl) { i++; p.sendMessage(ChatColor.YELLOW + "" + i + ". " + player.getName()); } } p.sendMessage(ChatColor.GREEN + "Tổng cộng có " + i + " người"); } } //////////////////////// else if (args.length == 1 && p.hasPermission(PERMUSE)){ Location pLocation = p.getLocation(); double distance = this.getConfig().getDouble(DISTANCE); //p.sendMessage(ChatColor.GREEN + "Những người chơi gần bạn:"); int i = 0; for (Player player : getServer().getOnlinePlayers()) { Location targetLocation = player.getLocation(); double distanceX = Math.abs(targetLocation.getX() - pLocation.getX()); double distanceZ = Math.abs(targetLocation.getZ() - pLocation.getZ()); if ((distanceX <= distance || distanceZ <= distance) && player != p) { i++; p.sendMessage(ChatColor.YELLOW + "" + i + ". " + player.getName()); } } sender.sendMessage(ChatColor.GREEN + "Tổng cộng có " + i + " người"); } } else { p.sendMessage(ChatColor.RED + "Bạn không có quyền để sử dụng lệnh này hoặc lệnh không tồn tại!"); } // Set distance if (args[0].equalsIgnoreCase("set") && p.hasPermission(PERMALL)) { try { double distance = Double.parseDouble(args[1]); this.getConfig().set(DISTANCE, distance); this.saveConfig(); p.sendMessage(ChatColor.GREEN + "Đã cập nhật thành công khoảng cách là " + distance); } catch(Exception e) { sender.sendMessage("Xảy ra lỗi!"); } } if (!args[0].equalsIgnoreCase("check") && !args[0].equalsIgnoreCase("set")) { sender.sendMessage(ChatColor.RED + "Lệnh không tồn tại!"); } } } } return false; } }