Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package com.kNoAPP.Clara.aspects;
  2.  
  3. import java.io.File;
  4. import java.sql.PreparedStatement;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.scheduler.BukkitRunnable;
  11.  
  12. import com.kNoAPP.Clara.Clara;
  13. import com.kNoAPP.Clara.data.Data;
  14. import com.kNoAPP.Clara.data.MySQL;
  15. import com.kNoAPP.Clara.data.Table;
  16. import com.kNoAPP.Clara.utils.Tools;
  17.  
  18. import net.md_5.bungee.api.ChatColor;
  19.  
  20. public class Server {
  21.  
  22.     public static List<Server> servers = new ArrayList<Server>();
  23.    
  24.     private String name;
  25.     private int port;
  26.    
  27.     public Server(String name, int port) {
  28.         this.name = name;
  29.         this.port = port;
  30.     }
  31.    
  32.     public String getName() {
  33.         return name;
  34.     }
  35.    
  36.     public int getPort() {
  37.         return port;
  38.     }
  39.    
  40.     public boolean isOnline() {
  41.         return Tools.convertBoolean(MySQL.getInt(Table.SERVER.getName(), "online", "name", name));
  42.     }
  43.    
  44.     public void setOnline(boolean b) {
  45.         MySQL.update(Table.SERVER.getName(), "online", Tools.convertInt(b), "name", name);
  46.     }
  47.    
  48.     public void setOnline(boolean b, boolean s) {
  49.         if(s) MySQL.specialUpdate(Table.SERVER.getName(), "online", Tools.convertInt(b), "name", name);
  50.         else MySQL.update(Table.SERVER.getName(), "online", Tools.convertInt(b), "name", name);
  51.     }
  52.    
  53.     public void logToDB() {
  54.         if(MySQL.getString(Table.SERVER.getName(), "name", "name", name) == null) {
  55.             //Add Server
  56.             Server s = this;
  57.             new BukkitRunnable() {
  58.                 public void run() {
  59.                     try {
  60.                         PreparedStatement ps = MySQL.getConnection().prepareStatement("INSERT INTO `" + Table.SERVER.getName() + "` values('" + s.getName() + "', " + s.getPort() + ", 1);");
  61.                         ps.execute();
  62.                         ps.close();
  63.                     } catch(Exception e) {
  64.                         e.printStackTrace();
  65.                     }
  66.                 }
  67.             }.runTaskAsynchronously(Clara.getPlugin());
  68.         } else if(MySQL.getInt(Table.SERVER.getName(), "port", "name", name) != port) {
  69.             //Port Updater
  70.             MySQL.update(Table.SERVER.getName(), "port", port, "name", name);
  71.         }
  72.     }
  73.    
  74.     public void removeFromDB() {
  75.         MySQL.delete(Table.SERVER.getName(), "name", name);
  76.     }
  77.    
  78.     public static Server getServer(String name) {
  79.         for(Server s : servers) {
  80.             if(s.getName().equals(name)) {
  81.                 return s;
  82.             }
  83.         }
  84.         return null;
  85.     }
  86.    
  87.     public static Server getServer(int port) {
  88.         for(Server s : servers) {
  89.             if(s.getPort() == port) {
  90.                 return s;
  91.             }
  92.         }
  93.         return null;
  94.     }
  95.    
  96.     public static Server getThisServer() {
  97.         return getServer(Bukkit.getPort());
  98.     }
  99.    
  100.     public static void importServers() {
  101.         servers.clear();
  102.        
  103.         FileConfiguration fc = Tools.getYML(new File(Data.MAIN.getFileConfig().getString("Bungee.path"), "config.yml"));
  104.         if(fc != null) {
  105.             //Servers
  106.             for(String s : fc.getConfigurationSection("servers").getKeys(false)) {
  107.                 try {
  108.                     int port = Integer.parseInt(fc.getString("servers." + s + ".address").replaceFirst("localhost:", ""));
  109.                     servers.add(new Server(s, port));
  110.                 } catch(Exception e) {
  111.                     e.printStackTrace();
  112.                 }
  113.             }
  114.         } else {
  115.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[" + Clara.getPlugin().getName() + "] Bungee config.yml path incorrect; fix and try again!");
  116.             Clara.failed = true;
  117.         }
  118.     }
  119.    
  120.     public static void checkSetup() {
  121.         for(String s : MySQL.getStringList(Table.SERVER.getName(), "name")) {
  122.             if(getServer(s) == null) {
  123.                 MySQL.delete(Table.SERVER.getName(), "name", s);
  124.             }
  125.         }
  126.     }
  127.    
  128.     public static Server transferServer(Server from) {
  129.         for(Server s : servers) {
  130.             if(s != from && s.isOnline()) {
  131.                 return s;
  132.             }
  133.         }
  134.         return null;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement