AoElite / AutoReconnect

A bungeecord plugin that automatically reconnect players when a server restarts.
MIT License
14 stars 7 forks source link

Suggestion | Onlinechecker #2

Closed xXSchrandXx closed 4 years ago

xXSchrandXx commented 5 years ago

Hi,

I love this plugin and I wonder if you could improve it with this suggestion:

Add an (optional) SQL-based online checker. Create a spigot plugin that sets an SQL statement to true every few seconds and / or at server startup. And what sets the same statement to false if the server disables the plugin.

This can work too: You can send a PluginMessage from the BungeeCord to update the Spigots SQL statement when the connection is reestablished.

Witch great wishes xXSchrandXx

xXSchrandXx commented 5 years ago

If you need some help:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Universal {

  public final static String host = "localhost";
  public final static String port = "3306";
  public final static String database = "minecraft";
  public final static String table = "minecraft";
  public final static String username = "minecraft";
  public final static String password = "minecraft";

  private static Connection connection;

  public static Connection getConnection() throws SQLException, ClassNotFoundException {
    if (connection != null && !connection.isClosed()) {
      return connection;
    }
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://" + Universal.host + ":" + Universal.port + "/" + Universal.database, Universal.username, Universal.password);
    return connection;
  }

  public static void sendOnline(String servername , boolean isOnline) {
    try {
      int intboolean = 0;
      if (isOnline)
        intboolean = 1;
      getConnection();
      Statement statement = connection.createStatement();
      statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + Universal.database + "`.`" + Universal.table + "` ( `Server` VARCHAR(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `isOnline` BOOLEAN NOT NULL , PRIMARY KEY (`Server`(16))) ENGINE = InnoDB;");
      statement.executeUpdate("INSERT INTO `" + Universal.database + "`.`" + Universal.table + "`(`Server`, `isOnline`) VALUES ('" + servername + "', '" + intboolean + "') ON DUPLICATE KEY UPDATE `isOnline` = '" + intboolean + "'");
    } catch (SQLException | ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static boolean isOnline(String servername) {
    boolean isOnline = false;
    try {
      getConnection();
      Statement statement = connection.createStatement();
      statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + Universal.database + "`.`" + Universal.table + "` ( `Server` VARCHAR(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `isOnline` BOOLEAN NOT NULL , PRIMARY KEY (`Server`(16))) ENGINE = InnoDB;");
      ResultSet result = statement.executeQuery("SELECT `Server`, `isOnline` FROM `gamestrike` WHERE `isOnline` = '1'");
      List<String> servers = new ArrayList<String>();
      while (result.next()) {
        String name = result.getString("Server");
        servers.add(name);
      }
      if (servers.contains(servername))
        isOnline = true;
    } catch (SQLException | ClassNotFoundException e) {
      e.printStackTrace();
    }
    return isOnline;
  }
}

PS: I don't realy know SQL! Change parts that wont work!