· 5 years ago · Apr 18, 2020, 08:02 PM
1package org.minelegends.kittop;
2
3import org.bukkit.Bukkit;
4import org.bukkit.ChatColor;
5import org.bukkit.plugin.java.JavaPlugin;
6import org.minelegends.kittop.commands.UUID;
7import org.minelegends.kittop.events.OnNewPlayerJoin;
8import org.minelegends.kittop.events.OnPlayerEvent;
9
10import java.sql.*;
11
12public final class Main extends JavaPlugin {
13
14 //Variables to setup the database connection.
15 private Connection connection;
16 public String host, database, username, password, table;
17 public int port;
18
19 //Load the config.yml file.
20 public void loadConfig() {
21 getConfig().options().copyDefaults(true);
22 saveConfig();
23 }
24
25 @Override
26 public void onEnable() {
27
28 loadConfig(); // Loading the config.yml file
29 mysqlSetup(); // Setting up MySQL
30
31 // Registering the events and commands.
32 this.getServer().getPluginManager().registerEvents(new OnPlayerEvent(), this);
33 getServer().getPluginManager().registerEvents(new OnNewPlayerJoin(), this);
34 getCommand("uuid").setExecutor(new UUID());
35 }
36
37 public void mysqlSetup() {
38
39 //Database information from the config file.
40 host = this.getConfig().getString("host");
41 port = this.getConfig().getInt("port");
42 database = this.getConfig().getString("database");
43 username = this.getConfig().getString("username");
44 table = this.getConfig().getString("table");
45
46 try {
47 synchronized (this) {
48 //Checking if the connection exists. If it does, then return.
49 if(getConnection() != null && !getConnection().isClosed()) { return; }
50
51 Class.forName("com.mysql.jdbc.Driver");
52 //Officially setting the connection to be the database.
53 setConnection(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password));
54
55 try {
56 DatabaseMetaData dbm = connection.getMetaData();
57 ResultSet tables = dbm.getTables(null, null, this.table, null);
58
59 // Check if the table does not exist, if it does not, then create one.
60 if(!tables.next()) {
61 PreparedStatement statement = connection.prepareStatement("CREATE TABLE " + this.table +
62 " (UUID TEXT," +
63 " NAME TEXT," +
64 " KILLS INTEGER," +
65 " DEATHS INTEGER," +
66 " CURRENT_STREAK INTEGER," +
67 " BEST_STREAK INTEGER," +
68 " KDR INTEGER)");
69 statement.execute();
70 String message = ChatColor.translateAlternateColorCodes('&', getConfig().getString("CONSOLE_CreatedMySQLTable"));
71 Bukkit.getConsoleSender().sendMessage(message);
72 }
73 } catch(SQLException e) {
74 e.printStackTrace();
75 }
76
77 String message = ChatColor.translateAlternateColorCodes('&', getConfig().getString("CONSOLE_MySQLSuccessful"));
78 Bukkit.getConsoleSender().sendMessage(message);
79 }
80
81 } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); }
82
83 }
84
85 public Connection getConnection() { return connection; } //Getting the connection from the instance at the top of the class.
86 public void setConnection(Connection connection) { this.connection = connection; } //Setting the connection.
87
88
89}