Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.2 kB
2
Indexable
Never
var PlayerCommandPreprocessEvent = Java.type("org.bukkit.event.player.PlayerCommandPreprocessEvent");
var AsyncPlayerChatEvent = Java.type("org.bukkit.event.player.AsyncPlayerChatEvent");
var Bukkit = Java.type("org.bukkit.Bukkit");

function onInit() {
    API.registerListener(PlayerCommandPreprocessEvent.class, function(event) {
        var player = event.getPlayer();
 
        if (event.getMessage().trim().equalsIgnoreCase("/tprandom region")) {
            var regionId = player.getName() + "-region-random-player";
 
            API.setTempData(regionId, true);
            API.message(player, "&aIndica la región que quieras escanear...");
 
            API.runLater(function() {
                if (API.containsTempData(regionId)) {
                    API.message(player, "&c¡Tiempo límite excedido!");
                    API.removeTempData(regionId); 
                }
            }, 300);
            event.setCancelled(true);
        }
    });
 
    API.registerListener(AsyncPlayerChatEvent.class, function(event) {
        var player = event.getPlayer();
        var regionId = player.getName() + "-region-random-player";

        if (API.containsTempData(regionId)) {
            var players = Bukkit.getOnlinePlayers();
            var found = [];
 
            for (var i = 0; i < players.length; i++) {
                var jugador = players[i];
 
                if (API.isInRegion(jugador, event.getMessage())) {   
                    found.push(jugador);         
                }
            }

            if (found.length == 0) {
                API.message(player, "&c¡No se encontraron jugadores en la región &a" + event.getMessage() + "&c!");
            } else {
                var random = found[Math.floor(Math.random() * found.length)];
                random.teleport(random.getLocation());
            }
 
            var task = API.getTempData(regionId);
            if (task != null) {
                try { 
                    task.cancel(); 
                } catch (ignored) {

                }
            }
            API.removeTempData(regionId); 
            event.setCancelled(true);
        }
    });
}