Advertisement
Guest User

Untitled

a guest
May 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. package me.hispanogamers;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import java.util.List;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.configuration.file.YamlConfiguration;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
  13. import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15. import java.io.File;
  16. import java.sql.Connection;
  17.  
  18. public class MainSemiIPBan extends JavaPlugin implements Listener{
  19.  
  20. private static Connection connection;
  21. private String host, database, username, password;
  22. private int port;
  23. List<String> uuid;
  24.  
  25. @Override
  26. public void onEnable() {
  27. Bukkit.getPluginManager().registerEvents(this, this);
  28.  
  29. File configf = new File(getDataFolder(), "config.yml"); // Archivo para almacenar las UUID de los usuarios a permitir
  30.  
  31. if (!configf.exists()) {
  32. configf.getParentFile().mkdirs();
  33. saveResource("config.yml", false);
  34. }
  35.  
  36. FileConfiguration configUUID = new YamlConfiguration();
  37.  
  38. try {
  39. configUUID.load(configf);
  40. } catch (Exception e) {}
  41.  
  42. uuid = configUUID.getStringList("UUID"); // Obtenemos la lista de UUIDs del fichero de configuración
  43. File file = new File(getDataFolder(), "../LiteBans/config.yml"); // Obtenemos la información de conexión a la base de datos desde la config de litebans
  44.  
  45. if (!file.exists()) {
  46.  
  47. getLogger().info("La configuración de LiteBans no existe, el plugin no funcionará correctamente");
  48.  
  49. } else {
  50.  
  51. FileConfiguration config = new YamlConfiguration();
  52. try {
  53. config.load(file);
  54. } catch (Exception e) {}
  55.  
  56. host = config.getString("sql.address").split(":")[0];
  57. port = Integer.valueOf(config.getString("sql.address").split(":")[1]);
  58. database = config.getString("sql.database");
  59. username = config.getString("sql.username");
  60. password = config.getString("sql.password");
  61.  
  62. try {
  63. openConnection();
  64. } catch (Exception e) {}
  65.  
  66. }
  67. }
  68.  
  69. @Override
  70. public void onDisable() {
  71. try {
  72. connection.close();
  73. } catch (Exception e) {}
  74. }
  75.  
  76. public void openConnection() throws Exception {
  77. if (connection != null && !connection.isClosed()) return;
  78.  
  79. synchronized (this) {
  80. if (connection != null && !connection.isClosed()) return;
  81.  
  82. Class.forName("com.mysql.jdbc.Driver");
  83. connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
  84. }
  85. }
  86.  
  87. @EventHandler()
  88. public void onPlayerJoin(AsyncPlayerPreLoginEvent e) {
  89. try {
  90. Statement statement = connection.createStatement();
  91. ResultSet result = statement.executeQuery("SELECT uuid,name,ip FROM litebans.litebans_history WHERE ip='" + e.getAddress().getHostAddress().toString() + "';"); // obtiene todos los jugadores baneados;
  92. boolean access=true; // Controlamos casos de jugadores no declarados bajo la uuid-whitelist
  93. while (result.next()) { // recorremos cada uuid de la misma ip que la del jugador conectado
  94. String name = result.getString("uuid");
  95. if(uuid.contains(name)){ // si alguna de las uuid está en la lista
  96. if (name.equals(e.getUniqueId().toString())) {
  97. return; // revisamos que la uuid del jugador conectado figure en la config de la lista
  98. } else access=false;
  99. }
  100. }
  101. if(!access)e.disallow(Result.KICK_WHITELIST, "Código [003]\n Usted no tiene permisos suficientes para poder acceder al servidor"); // Si llega aquí, hay otra ip de un jugador que tiene algún registro
  102. } catch (Exception ex) {ex.printStackTrace();}
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement