From 200490c1b4e4a9c2052a84b5737871f0a6807b21 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 7 May 2020 13:21:51 +0200 Subject: [PATCH] auto reconnect to MySQL and added gm ability to translate messages send to player --- config.yml | 3 +++ src/fr/topeka/levelSync/Main.java | 12 ++++++++---- .../topeka/levelSync/listener/EventsListener.java | 8 ++++---- src/fr/topeka/levelSync/sql/Sql.java | 15 +++++++++------ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/config.yml b/config.yml index de1edec..b594ee5 100644 --- a/config.yml +++ b/config.yml @@ -5,3 +5,6 @@ mysql: password: "password" database: "minecraft" table: "playerLevel" +message: + syncError: "Couln't sync your experience" + dbError: "An error occured while trying to save your experience to database" \ No newline at end of file diff --git a/src/fr/topeka/levelSync/Main.java b/src/fr/topeka/levelSync/Main.java index f61159c..061cc12 100644 --- a/src/fr/topeka/levelSync/Main.java +++ b/src/fr/topeka/levelSync/Main.java @@ -14,16 +14,20 @@ import fr.topeka.levelSync.sql.Sql; */ public class Main extends JavaPlugin{ - Sql sql; + private Sql _sql; + public String msg_syncError, msg_dbError; + @Override public void onEnable() { try { this.saveDefaultConfig(); - sql = new Sql(this); + _sql = new Sql(this); PluginManager pm = getServer().getPluginManager(); - pm.registerEvents(new EventsListener(this, sql), this); + pm.registerEvents(new EventsListener(this, _sql), this); this.getLogger().info("[levelSync] Plugin enabled"); + msg_syncError = getConfig().getString("message.syncError"); + msg_dbError = getConfig().getString("message.dbError"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); this.getLogger().warning("An error occured during loading, disabling plugin..."); @@ -33,7 +37,7 @@ public class Main extends JavaPlugin{ @Override public void onDisable() { - sql.closeConnection(); + _sql.closeConnection(); this.getLogger().info("Plugin disabled"); } } diff --git a/src/fr/topeka/levelSync/listener/EventsListener.java b/src/fr/topeka/levelSync/listener/EventsListener.java index 7286778..2d5854a 100644 --- a/src/fr/topeka/levelSync/listener/EventsListener.java +++ b/src/fr/topeka/levelSync/listener/EventsListener.java @@ -38,7 +38,7 @@ public class EventsListener implements Listener { p.setLevel((int) values[0]); } catch (SQLException e) { 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); }else { - p.sendMessage("Couln't sync your experience"); + p.sendMessage("[LevelSync] " + plugin.msg_syncError); } } @@ -61,11 +61,11 @@ public class EventsListener implements Listener { try { sql.setPlayerLevel(p); }catch(SQLException e) { - p.sendMessage("An error occured while trying to save your experience to database"); + p.sendMessage("[LevelSync] " + plugin.msg_dbError); e.printStackTrace(); } }else { - p.sendMessage("An error occured while trying to save your experience to database"); + p.sendMessage("[LevelSync] " + plugin.msg_dbError); } } diff --git a/src/fr/topeka/levelSync/sql/Sql.java b/src/fr/topeka/levelSync/sql/Sql.java index 9f7fb5b..dd2bf72 100644 --- a/src/fr/topeka/levelSync/sql/Sql.java +++ b/src/fr/topeka/levelSync/sql/Sql.java @@ -16,15 +16,19 @@ public class Sql { private Main plugin; private String host, database, table, username, password; - private int port = 3306; + private int port; public Sql(Main plugin) throws ClassNotFoundException, SQLException{ this.plugin = plugin; this.loadConfig(); Class.forName("com.mysql.jdbc.Driver"); - connection = DriverManager.getConnection("jdbc:mysql://"+ host + ":" + port + "/" + database, username, password); - statement = connection.createStatement(); - statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + this.table + "`(" + this.connect(); + } + + 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," + "`experience` FLOAT(2,1)," + "`level` INT(6)" @@ -84,8 +88,7 @@ public class Sql { public boolean reConnect() { try { - connection = DriverManager.getConnection("jdbc:mysql://"+ host + ":" + port + "/" + database, username, password); - statement = connection.createStatement(); + this.connect(); return true; }catch(SQLException e) { e.printStackTrace();