109 lines
2.7 KiB
Java
109 lines
2.7 KiB
Java
package eu.univento.core.api;
|
|
|
|
import com.google.common.collect.Iterables;
|
|
import com.google.common.io.ByteArrayDataOutput;
|
|
import com.google.common.io.ByteStreams;
|
|
|
|
import eu.univento.core.Core;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
|
|
/**
|
|
* sends player to other bungeecord servers
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
public class Servers {
|
|
|
|
|
|
/**
|
|
* sends player to server
|
|
* @param p player
|
|
* @param server name of server
|
|
*/
|
|
public static void connectServer(Player p, String server) {
|
|
if ((p instanceof Player)) {
|
|
ByteArrayOutputStream b = new ByteArrayOutputStream();
|
|
DataOutputStream out = new DataOutputStream(b);
|
|
try {
|
|
out.writeUTF("Connect");
|
|
out.writeUTF(server);
|
|
} catch (IOException el) {
|
|
el.printStackTrace();
|
|
}
|
|
p.sendPluginMessage(Core.getInstance(), "BungeeCord", b.toByteArray());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* connects all player to server
|
|
* @param server name of server
|
|
*/
|
|
public static void connectAllToServer(String server) {
|
|
for (Player all : Bukkit.getOnlinePlayers())
|
|
connectServer(all, server);
|
|
}
|
|
|
|
/**
|
|
* gets player count of server
|
|
* @param server name of server
|
|
* @return player count
|
|
*/
|
|
public static int getOnlinePlayers(String server) {
|
|
int online = 0;
|
|
|
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
|
|
out.writeUTF("PlayerCount");
|
|
out.writeUTF(server);
|
|
|
|
Player player = (Player) Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
|
|
player.sendPluginMessage(Core.getInstance(), "BungeeCord", out.toByteArray());
|
|
|
|
return online;
|
|
}
|
|
|
|
/**
|
|
* gets ip of server from bungeecord
|
|
* @param server server name
|
|
* @return server ip
|
|
*/
|
|
public static String getServerIP(String server) {
|
|
String ip = null;
|
|
|
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
|
|
out.writeUTF("ServerInfo");
|
|
out.writeUTF(server);
|
|
|
|
Player player = (Player) Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
|
|
player.sendPluginMessage(Core.getInstance(), "BungeeCord", out.toByteArray());
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
/**
|
|
* gets port of server from bungeecord
|
|
* @param server server name
|
|
* @return server port
|
|
*/
|
|
public static int getServerPort(String server) {
|
|
int port = 0;
|
|
|
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
|
|
out.writeUTF("ServerInfo");
|
|
out.writeUTF(server);
|
|
|
|
Player player = (Player) Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
|
|
player.sendPluginMessage(Core.getInstance(), "BungeeCord", out.toByteArray());
|
|
|
|
return port;
|
|
|
|
}
|
|
} |