Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.hispanogamers;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
- import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.io.File;
- import java.sql.Connection;
- public class MainSemiIPBan extends JavaPlugin implements Listener{
- private static Connection connection;
- private String host, database, username, password;
- private int port;
- List<String> uuid;
- @Override
- public void onEnable() {
- Bukkit.getPluginManager().registerEvents(this, this);
- File configf = new File(getDataFolder(), "config.yml"); // Archivo para almacenar las UUID de los usuarios a permitir
- if (!configf.exists()) {
- configf.getParentFile().mkdirs();
- saveResource("config.yml", false);
- }
- FileConfiguration configUUID = new YamlConfiguration();
- try {
- configUUID.load(configf);
- } catch (Exception e) {}
- uuid = configUUID.getStringList("UUID"); // Obtenemos la lista de UUIDs del fichero de configuración
- 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
- if (!file.exists()) {
- getLogger().info("La configuración de LiteBans no existe, el plugin no funcionará correctamente");
- } else {
- FileConfiguration config = new YamlConfiguration();
- try {
- config.load(file);
- } catch (Exception e) {}
- host = config.getString("sql.address").split(":")[0];
- port = Integer.valueOf(config.getString("sql.address").split(":")[1]);
- database = config.getString("sql.database");
- username = config.getString("sql.username");
- password = config.getString("sql.password");
- try {
- openConnection();
- } catch (Exception e) {}
- }
- }
- @Override
- public void onDisable() {
- try {
- connection.close();
- } catch (Exception e) {}
- }
- public void openConnection() throws Exception {
- if (connection != null && !connection.isClosed()) return;
- synchronized (this) {
- if (connection != null && !connection.isClosed()) return;
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
- }
- }
- @EventHandler()
- public void onPlayerJoin(AsyncPlayerPreLoginEvent e) {
- try {
- Statement statement = connection.createStatement();
- ResultSet result = statement.executeQuery("SELECT uuid,name,ip FROM litebans.litebans_history WHERE ip='" + e.getAddress().getHostAddress().toString() + "';"); // obtiene todos los jugadores baneados;
- boolean access=true; // Controlamos casos de jugadores no declarados bajo la uuid-whitelist
- while (result.next()) { // recorremos cada uuid de la misma ip que la del jugador conectado
- String name = result.getString("uuid");
- if(uuid.contains(name)){ // si alguna de las uuid está en la lista
- if (name.equals(e.getUniqueId().toString())) {
- return; // revisamos que la uuid del jugador conectado figure en la config de la lista
- } else access=false;
- }
- }
- 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
- } catch (Exception ex) {ex.printStackTrace();}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement