package de.Panakotta00.BedWars; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import org.bukkit.configuration.file.YamlConfiguration; import de.Panakotta00.BedWars.Arena.ArenaTemplate; public class SocketSystem implements Runnable { BedWars main; Thread thread; boolean loop; ServerSocket socket; Socket lobby; Socket bungee; int port; public SocketSystem(BedWars main, int port, String bungeehost, int bungeeport) { this.main = main; this.port = port; try { socket = new ServerSocket(port, 0, InetAddress.getByName(null));; thread = new Thread(this); thread.start(); System.out.println("ServerSocket auf Port " + port + " und IP " + socket.getInetAddress() + " gestartet!"); } catch (IOException e) { e.printStackTrace(); } try { bungee = new Socket("localhost", bungeeport); PrintStream out = new PrintStream(bungee.getOutputStream()); out.println("arena"); out.flush(); System.out.println("BungeeSocket gestartet!"); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { while (loop) { try { Socket socket = this.socket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String cmd = in.readLine(); if (cmd.equals("servercontroller")) { lobby = socket; main.id = Integer.valueOf(in.readLine()); for (String s : (String[])((Object) in.readLine())) { main.arenas.put(s.intern(), loadTemplate(s.intern())); } } main.nextArena(); } catch (IOException e) { e.printStackTrace(); } } try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("deprecation") public void stop() { if (loop) { loop = false; } if (!socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (thread.isAlive()) { thread.stop(); } } private ArenaTemplate loadTemplate(String name) { String path = main.config.cfg.getString("World_Dictonary"); File dir = new File(path); if (dir.isDirectory()) { for (final File world : dir.listFiles()) { if (world.isDirectory() && world.getName().intern() == name.intern()) { try { YamlConfiguration cfg = YamlConfiguration.loadConfiguration(new File(world.getAbsolutePath() + "\\Arena.enabled.yml")); return new ArenaTemplate(main, world.getAbsolutePath(), world, cfg); } catch (Exception e) { e.printStackTrace(); System.out.println("[BedWars] Cant load World \"" + world.getName() + "\" to ArenaTemplate!"); } } } return null; } else { return null; } } public void signUpdate(int id, String[] lines) { try { PrintStream out = new PrintStream(lobby.getOutputStream()); out.println("updateSign"); out.println(id); out.println(lines[0].intern()); out.println(lines[1].intern()); out.println(lines[2].intern()); out.println(lines[3].intern()); out.flush(); } catch (IOException e) { e.printStackTrace(); } } }