Panakotta00

SocketSystem.java

May 22nd, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package de.Panakotta00.BedWars;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintStream;
  8. import java.net.InetAddress;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11.  
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13.  
  14. import de.Panakotta00.BedWars.Arena.ArenaTemplate;
  15.  
  16. public class SocketSystem implements Runnable {
  17.     BedWars main;
  18.    
  19.     Thread thread;
  20.     boolean loop;
  21.     ServerSocket socket;
  22.     Socket lobby;
  23.     Socket bungee;
  24.    
  25.     int port;
  26.    
  27.     public SocketSystem(BedWars main, int port, String bungeehost, int bungeeport) {
  28.         this.main = main;
  29.        
  30.         this.port = port;
  31.        
  32.         try {
  33.             socket = new ServerSocket(port, 0, InetAddress.getByName(null));;
  34.             thread = new Thread(this);
  35.             thread.start();
  36.             System.out.println("ServerSocket auf Port " + port + " und IP " + socket.getInetAddress() + " gestartet!");
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         }
  40.         try {
  41.             bungee = new Socket("localhost", bungeeport);
  42.  
  43.             PrintStream out = new PrintStream(bungee.getOutputStream());
  44.            
  45.             out.println("arena");
  46.            
  47.             out.flush();
  48.            
  49.             System.out.println("BungeeSocket gestartet!");
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.    
  55.     @Override
  56.     public void run() {
  57.         while (loop) {
  58.             try {
  59.                 Socket socket = this.socket.accept();
  60.                
  61.                 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  62.                 String cmd = in.readLine();
  63.                
  64.                 if (cmd.equals("servercontroller")) {
  65.                     lobby = socket;
  66.                     main.id = Integer.valueOf(in.readLine());
  67.                    
  68.                     for (String s : (String[])((Object) in.readLine())) {
  69.                         main.arenas.put(s.intern(), loadTemplate(s.intern()));
  70.                     }
  71.                 }
  72.                
  73.                 main.nextArena();
  74.             } catch (IOException e) {
  75.                 e.printStackTrace();
  76.             }
  77.         }
  78.        
  79.         try {
  80.             this.socket.close();
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         }
  84.     }
  85.    
  86.     @SuppressWarnings("deprecation")
  87.     public void stop() {
  88.         if (loop) {
  89.             loop = false;
  90.         }
  91.        
  92.         if (!socket.isClosed()) {
  93.             try {
  94.                 socket.close();
  95.             } catch (IOException e) {
  96.                 e.printStackTrace();
  97.             }
  98.         }
  99.        
  100.         if (thread.isAlive()) {
  101.             thread.stop();
  102.         }
  103.     }
  104.    
  105.     private ArenaTemplate loadTemplate(String name) {
  106.         String path = main.config.cfg.getString("World_Dictonary");
  107.         File dir = new File(path);
  108.        
  109.         if (dir.isDirectory()) {
  110.             for (final File world : dir.listFiles()) {
  111.                 if (world.isDirectory() && world.getName().intern() == name.intern()) {
  112.                     try {
  113.                         YamlConfiguration cfg = YamlConfiguration.loadConfiguration(new File(world.getAbsolutePath() + "\\Arena.enabled.yml"));
  114.                         return new ArenaTemplate(main, world.getAbsolutePath(), world, cfg);
  115.                     } catch (Exception e) {
  116.                         e.printStackTrace();
  117.                         System.out.println("[BedWars] Cant load World \"" + world.getName() + "\" to ArenaTemplate!");
  118.                     }
  119.                 }
  120.             }
  121.             return null;
  122.         } else {
  123.             return null;
  124.         }
  125.     }
  126.    
  127.     public void signUpdate(int id, String[] lines) {
  128.         try {
  129.             PrintStream out = new PrintStream(lobby.getOutputStream());
  130.            
  131.             out.println("updateSign");
  132.             out.println(id);
  133.             out.println(lines[0].intern());
  134.             out.println(lines[1].intern());
  135.             out.println(lines[2].intern());
  136.             out.println(lines[3].intern());
  137.            
  138.             out.flush();
  139.         } catch (IOException e) {
  140.             e.printStackTrace();
  141.         }
  142.     }
  143. }
Add Comment
Please, Sign In to add comment