auto reconnect to MySQL and added gm ability to translate messages send to player

This commit is contained in:
Quentin Legot 2020-05-07 13:21:51 +02:00
parent e2fabea733
commit 200490c1b4
4 changed files with 24 additions and 14 deletions

View File

@ -5,3 +5,6 @@ mysql:
password: "password" password: "password"
database: "minecraft" database: "minecraft"
table: "playerLevel" table: "playerLevel"
message:
syncError: "Couln't sync your experience"
dbError: "An error occured while trying to save your experience to database"

View File

@ -14,16 +14,20 @@ import fr.topeka.levelSync.sql.Sql;
*/ */
public class Main extends JavaPlugin{ public class Main extends JavaPlugin{
Sql sql; private Sql _sql;
public String msg_syncError, msg_dbError;
@Override @Override
public void onEnable() { public void onEnable() {
try { try {
this.saveDefaultConfig(); this.saveDefaultConfig();
sql = new Sql(this); _sql = new Sql(this);
PluginManager pm = getServer().getPluginManager(); PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new EventsListener(this, sql), this); pm.registerEvents(new EventsListener(this, _sql), this);
this.getLogger().info("[levelSync] Plugin enabled"); this.getLogger().info("[levelSync] Plugin enabled");
msg_syncError = getConfig().getString("message.syncError");
msg_dbError = getConfig().getString("message.dbError");
} catch (ClassNotFoundException | SQLException e) { } catch (ClassNotFoundException | SQLException e) {
e.printStackTrace(); e.printStackTrace();
this.getLogger().warning("An error occured during loading, disabling plugin..."); this.getLogger().warning("An error occured during loading, disabling plugin...");
@ -33,7 +37,7 @@ public class Main extends JavaPlugin{
@Override @Override
public void onDisable() { public void onDisable() {
sql.closeConnection(); _sql.closeConnection();
this.getLogger().info("Plugin disabled"); this.getLogger().info("Plugin disabled");
} }
} }

View File

@ -38,7 +38,7 @@ public class EventsListener implements Listener {
p.setLevel((int) values[0]); p.setLevel((int) values[0]);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
p.sendMessage("Couln't sync your experience"); p.sendMessage("[LevelSync] " + plugin.msg_syncError);
} }
} }
} }
@ -46,7 +46,7 @@ public class EventsListener implements Listener {
} }
}, 20L); }, 20L);
}else { }else {
p.sendMessage("Couln't sync your experience"); p.sendMessage("[LevelSync] " + plugin.msg_syncError);
} }
} }
@ -61,11 +61,11 @@ public class EventsListener implements Listener {
try { try {
sql.setPlayerLevel(p); sql.setPlayerLevel(p);
}catch(SQLException e) { }catch(SQLException e) {
p.sendMessage("An error occured while trying to save your experience to database"); p.sendMessage("[LevelSync] " + plugin.msg_dbError);
e.printStackTrace(); e.printStackTrace();
} }
}else { }else {
p.sendMessage("An error occured while trying to save your experience to database"); p.sendMessage("[LevelSync] " + plugin.msg_dbError);
} }
} }

View File

@ -16,15 +16,19 @@ public class Sql {
private Main plugin; private Main plugin;
private String host, database, table, username, password; private String host, database, table, username, password;
private int port = 3306; private int port;
public Sql(Main plugin) throws ClassNotFoundException, SQLException{ public Sql(Main plugin) throws ClassNotFoundException, SQLException{
this.plugin = plugin; this.plugin = plugin;
this.loadConfig(); this.loadConfig();
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://"+ host + ":" + port + "/" + database, username, password); this.connect();
statement = connection.createStatement(); }
statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + this.table + "`("
private void connect() throws SQLException{
this.connection = DriverManager.getConnection("jdbc:mysql://"+ host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
this.statement = connection.createStatement();
this.statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + this.table + "`("
+ "`uuid` VARCHAR(36) NOT NULL," + "`uuid` VARCHAR(36) NOT NULL,"
+ "`experience` FLOAT(2,1)," + "`experience` FLOAT(2,1),"
+ "`level` INT(6)" + "`level` INT(6)"
@ -84,8 +88,7 @@ public class Sql {
public boolean reConnect() { public boolean reConnect() {
try { try {
connection = DriverManager.getConnection("jdbc:mysql://"+ host + ":" + port + "/" + database, username, password); this.connect();
statement = connection.createStatement();
return true; return true;
}catch(SQLException e) { }catch(SQLException e) {
e.printStackTrace(); e.printStackTrace();