Lobby/src/eu/univento/lobby/utils/StatusSignUtil.java

109 lines
3.1 KiB
Java

package eu.univento.lobby.utils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.bukkit.Location;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
public class StatusSignUtil{
private Location location;
private Sign sign;
private String name;
private String ip;
private int port;
private static File file = new File("plugins/Lobby/signs.yml");
private static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
public StatusSignUtil(Location location, String name, String ip, int port) {
this.location = location;
this.sign = ((Sign)location.getBlock().getState());
this.name = name;
this.ip = ip;
this.port = port;
}
public Location getLocation() {
return this.location;
}
public String getName() {
return this.name;
}
public String getIP() {
return this.ip;
}
public int getPort() {
return this.port;
}
public void update() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(this.ip, this.port), 1000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
DataInputStream in = new DataInputStream(socket.getInputStream());
out.write(254);
StringBuilder str = new StringBuilder();
int b;
while ((b = in.read()) != -1)
{
if ((b != 1) && (b > 16) && (b != 255) && (b != 23) && (b != 24)) {
str.append((char)b);
}
}
String[] data = str.toString().split("§");
String motd = data[0];
int onlinePlayers = Integer.valueOf(data[1]).intValue();
int maxPlayers = Integer.valueOf(data[2]).intValue();
socket.close();
this.sign.setLine(0, "§a[Beitreten]");
this.sign.setLine(1, "§9" + this.name);
this.sign.setLine(2, "§6" + motd);
this.sign.setLine(3, "§1" + onlinePlayers + "/" + maxPlayers);
}
catch (Exception e)
{
this.sign.setLine(0, "§4████████");
this.sign.setLine(1, "§4Neustart..");
this.sign.setLine(2, "§9" + this.name);
this.sign.setLine(3, "§4████████");
}
this.sign.update();
}
public static void save(StatusSignUtil sign) {
int size = cfg.getKeys(false).size() + 1;
cfg.set(size + ".loc.world", sign.getLocation().getWorld().getName());
cfg.set(size + ".loc.x", Double.valueOf(sign.getLocation().getX()));
cfg.set(size + ".loc.y", Double.valueOf(sign.getLocation().getY()));
cfg.set(size + ".loc.z", Double.valueOf(sign.getLocation().getZ()));
cfg.set(size + ".name", sign.getName());
cfg.set(size + ".ip", sign.getIP());
cfg.set(size + ".port", Integer.valueOf(sign.getPort()));
try
{
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}