Last active 1449780539

Steven Smith revised this gist 1358477234. Go to revision

2 files changed, 207 insertions

OnlinePlayersSQLConfig.java(file created)

@@ -0,0 +1,65 @@
1 + package net.rayherring;
2 +
3 + public class OnlinePlayersSQLConfig
4 + {
5 + OnlinePlayersSQL plugin;
6 +
7 + public OnlinePlayersSQLConfig(OnlinePlayersSQL plugin)
8 + {
9 + this.plugin = plugin;
10 + }
11 +
12 + public void loadConfiguration() {
13 + String mySQLServer = "MySQLServer";
14 + String mySQLPort = "MySQLPort";
15 + String mySQLUsername = "MySQLUsername";
16 + String mySQLPassword = "MySQLPassword";
17 + String mySQLDatabase = "MySQLDatabase";
18 + String mySQLTable = "MySQLTable";
19 +
20 + this.plugin.getConfig().addDefault(mySQLServer, "localhost");
21 + this.plugin.getConfig().addDefault(mySQLPort, "3306");
22 + this.plugin.getConfig().addDefault(mySQLUsername, "root");
23 + this.plugin.getConfig().addDefault(mySQLPassword, "");
24 + this.plugin.getConfig().addDefault(mySQLDatabase, "db");
25 + this.plugin.getConfig().addDefault(mySQLTable, "online_players");
26 + this.plugin.getConfig().addDefault("showDebug", Boolean.valueOf(false));
27 + this.plugin.getConfig().addDefault("op_only_resync", Boolean.valueOf(false));
28 +
29 + this.plugin.getConfig().options().copyDefaults(true);
30 +
31 + this.plugin.saveConfig();
32 + }
33 +
34 + public boolean opOnlyResync() {
35 + return this.plugin.getConfig().getBoolean("op_only_resync");
36 + }
37 +
38 + public String getMySQLServer() {
39 + return this.plugin.getConfig().getString("MySQLServer");
40 + }
41 +
42 + public String getMySQLPort() {
43 + return this.plugin.getConfig().getString("MySQLPort");
44 + }
45 +
46 + public String getMySQLUsername() {
47 + return this.plugin.getConfig().getString("MySQLUsername");
48 + }
49 +
50 + public String getMySQLPassword() {
51 + return this.plugin.getConfig().getString("MySQLPassword");
52 + }
53 +
54 + public String getMySQLDatabase() {
55 + return this.plugin.getConfig().getString("MySQLDatabase");
56 + }
57 +
58 + public String getMySQLTable() {
59 + return this.plugin.getConfig().getString("MySQLTable");
60 + }
61 +
62 + public boolean isShowDebug() {
63 + return this.plugin.getConfig().getBoolean("showDebug");
64 + }
65 + }

OnlinePlayersSQLLib.java(file created)

@@ -0,0 +1,142 @@
1 + package net.rayherring;
2 +
3 + import java.sql.Connection;
4 + import java.sql.DriverManager;
5 + import java.sql.PreparedStatement;
6 + import java.sql.ResultSet;
7 + import java.sql.SQLException;
8 + import java.util.logging.Logger;
9 +
10 + public class OnlinePlayersSQLLib
11 + {
12 + Logger log = Logger.getLogger("Minecraft");
13 + String url;
14 + Connection conn = null;
15 + PreparedStatement myQuery = null;
16 + OnlinePlayersSQL plugin;
17 +
18 + public OnlinePlayersSQLLib(OnlinePlayersSQL plugin)
19 + {
20 + this.plugin = plugin;
21 + this.url = ("jdbc:mysql://" + plugin.opConfig.getMySQLServer() + ":" + plugin.opConfig.getMySQLPort() + "/" + plugin.opConfig.getMySQLDatabase());
22 + }
23 +
24 + public Connection SQLConnect() throws SQLException {
25 + Connection conn = DriverManager.getConnection(this.url, this.plugin.opConfig.getMySQLUsername(), this.plugin.opConfig.getMySQLPassword());
26 + return conn;
27 + }
28 +
29 + public void SQLDisconnect() throws SQLException {
30 + this.myQuery.close();
31 + this.conn.close();
32 + }
33 +
34 + public void updateTableSchema() throws SQLException {
35 + this.log.info("Updating Schema information for table.");
36 + if (!columnExists(this.plugin.opConfig.getMySQLDatabase(), this.plugin.opConfig.getMySQLTable(), "online")) {
37 + this.log.info("Creating additional 'online' column for table.");
38 + runUpdateQuery("ALTER TABLE " + this.plugin.opConfig.getMySQLTable() + " ADD COLUMN online boolean default false;");
39 + }
40 +
41 + if (!columnExists(this.plugin.opConfig.getMySQLDatabase(), this.plugin.opConfig.getMySQLTable(), "last_logout")) {
42 + this.log.info("Creating additional 'last_logout' column for table.");
43 + runUpdateQuery("ALTER TABLE " + this.plugin.opConfig.getMySQLTable() + " ADD COLUMN last_logout int;");
44 + }
45 +
46 + if (!columnExists(this.plugin.opConfig.getMySQLDatabase(), this.plugin.opConfig.getMySQLTable(), "first_login")) {
47 + this.log.info("Creating additional 'first_login' column for table.");
48 + runUpdateQuery("ALTER TABLE " + this.plugin.opConfig.getMySQLTable() + " ADD COLUMN first_login int;");
49 + }
50 + }
51 +
52 + public void runUpdateQuery(String query) {
53 + try {
54 + this.conn = SQLConnect();
55 + this.myQuery = this.conn.prepareStatement(query);
56 +
57 + this.myQuery.executeUpdate();
58 + SQLDisconnect();
59 + }
60 + catch (SQLException e1) {
61 + e1.printStackTrace();
62 + }
63 + }
64 +
65 + public ResultSet runSearchQuery(String query) {
66 + ResultSet result = null;
67 + try
68 + {
69 + this.conn = SQLConnect();
70 + this.myQuery = this.conn.prepareStatement(query);
71 +
72 + result = this.myQuery.executeQuery();
73 + } catch (SQLException el) {
74 + el.printStackTrace();
75 + }
76 +
77 + return result;
78 + }
79 +
80 + public void createSqlTable() throws SQLException
81 + {
82 + runUpdateQuery("CREATE TABLE " + this.plugin.opConfig.getMySQLTable() +
83 + "(player varchar(255) not null, " +
84 + "previous_world varchar(255), " +
85 + "current_world varchar(255), " +
86 + "ip_address varchar(16), " +
87 + "logon_time int(11), " +
88 + "permission_group varchar(255), " +
89 + "online boolean default false, " +
90 + "last_logout int(11), " +
91 + "first_login int(11))");
92 + }
93 +
94 + public boolean tableExists(String db, String tbl) {
95 + ResultSet result = null;
96 + Boolean recordExists = Boolean.valueOf(false);
97 +
98 + String query = "SELECT * FROM Information_Schema.TABLES WHERE Information_Schema.TABLES.TABLE_NAME = '" +
99 + tbl + "' " +
100 + "AND Information_Schema.TABLES.TABLE_SCHEMA = '" + db + "'";
101 +
102 + result = runSearchQuery(query);
103 + try
104 + {
105 + recordExists = Boolean.valueOf(result.isBeforeFirst());
106 + SQLDisconnect();
107 +
108 + return recordExists.booleanValue();
109 + } catch (SQLException e) {
110 + e.printStackTrace();
111 + }
112 +
113 + return false;
114 + }
115 +
116 + public boolean columnExists(String db, String tbl, String column) {
117 + ResultSet result = null;
118 + Boolean recordExists = Boolean.valueOf(false);
119 +
120 + String query = "SELECT * FROM Information_Schema.COLUMNS WHERE Information_Schema.COLUMNS.COLUMN_NAME = '" +
121 + column + "' " +
122 + "AND Information_Schema.COLUMNS.TABLE_NAME = '" + tbl + "' " +
123 + "AND Information_Schema.COLUMNS.TABLE_SCHEMA = '" + db + "'";
124 +
125 + result = runSearchQuery(query);
126 + try
127 + {
128 + this.log.info("Result of column " + column + " check: " + result.isBeforeFirst());
129 +
130 + recordExists = Boolean.valueOf(result.isBeforeFirst());
131 +
132 + SQLDisconnect();
133 +
134 + return recordExists.booleanValue();
135 + }
136 + catch (SQLException e) {
137 + e.printStackTrace();
138 + }
139 +
140 + return false;
141 + }
142 + }
Newer Older