Guest User

SocketSystem.java

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