From 0abdf99c857ff454aceb5b6ed5cf33cbd22f9d00 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Sun, 28 Jul 2019 12:39:31 +0200 Subject: [PATCH] Class register should be in the main thread --- src/main/java/com/gamingmesh/jobs/Jobs.java | 15 +++++ .../jobs/Placeholders/Placeholder.java | 4 +- .../com/gamingmesh/jobs/PlayerManager.java | 12 +--- .../jobs/commands/list/editpoints.java | 2 +- .../gamingmesh/jobs/commands/list/points.java | 2 +- .../gamingmesh/jobs/config/ShopManager.java | 4 +- .../jobs/dao/JobsConnectionPool.java | 63 ++++++++++--------- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 6 +- .../com/gamingmesh/jobs/dao/JobsMySQL.java | 1 - .../gamingmesh/jobs/economy/PointsData.java | 4 ++ .../jobs/tasks/BufferedPaymentTask.java | 2 +- 11 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index a4d1ff05..c33a3f6a 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -95,6 +95,7 @@ import com.gamingmesh.jobs.economy.BufferedEconomy; import com.gamingmesh.jobs.economy.BufferedPayment; import com.gamingmesh.jobs.economy.Economy; import com.gamingmesh.jobs.economy.PaymentData; +import com.gamingmesh.jobs.economy.PointsData; import com.gamingmesh.jobs.McMMO.McMMO1_X_listener; import com.gamingmesh.jobs.McMMO.McMMO2_X_listener; import com.gamingmesh.jobs.McMMO.McMMOManager; @@ -184,6 +185,8 @@ public class Jobs extends JavaPlugin { protected static SelectionManager smanager = null; + private static PointsData pointsDatabase = null; + private void setMcMMOlistener() { try { Class.forName("com.gmail.nossr50.datatypes.skills.SuperAbilityType"); @@ -319,6 +322,17 @@ public class Jobs extends JavaPlugin { return DBManager; } + public static void setPointsDatabase() { + pointsDatabase = new PointsData(); + } + + public static PointsData getPointsData() { + if (pointsDatabase == null) + pointsDatabase = new PointsData(); + + return pointsDatabase; + } + public static void setShopManager() { shopManager = new ShopManager(); } @@ -904,6 +918,7 @@ public class Jobs extends JavaPlugin { setPermissionHandler(new PermissionHandler(this)); setPluginLogger(getLogger()); setJobsClassloader(); + setPointsDatabase(); setDBManager(); setLanguage(); setGUIManager(); diff --git a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java index 46df647c..12a17937 100644 --- a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java +++ b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java @@ -397,12 +397,12 @@ public class Placeholder { case user_totallevels: return Integer.toString(user.getTotalLevels()); case user_points: - PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(user.getPlayerUUID()); + PlayerPoints pointInfo = Jobs.getPointsData().getPlayerPointsInfo(user.getPlayerUUID()); NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH); return format.format(pointInfo.getCurrentPoints()); case user_total_points: format = NumberFormat.getInstance(Locale.ENGLISH); - pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(user.getPlayerUUID()); + pointInfo = Jobs.getPointsData().getPlayerPointsInfo(user.getPlayerUUID()); return format.format(pointInfo.getTotalPoints()); case user_issaved: return convert(user.isSaved()); diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index de5e47ad..011b56e9 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -62,7 +62,6 @@ import com.gamingmesh.jobs.container.PlayerPoints; import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.dao.JobsDAOData; import com.gamingmesh.jobs.economy.PaymentData; -import com.gamingmesh.jobs.economy.PointsData; import com.gamingmesh.jobs.stuff.PerformCommands; import com.gamingmesh.jobs.stuff.Util; @@ -73,17 +72,12 @@ public class PlayerManager { private ConcurrentHashMap players = new ConcurrentHashMap<>(); private ConcurrentHashMap playersUUID = new ConcurrentHashMap<>(); - private PointsData PointsDatabase = new PointsData(); private final String mobSpawnerMetadata = "jobsMobSpawner"; private HashMap PlayerUUIDMap = new HashMap<>(); private HashMap PlayerIDMap = new HashMap<>(); private HashMap PlayerNameMap = new HashMap<>(); - public PointsData getPointsData() { - return PointsDatabase; - } - public int getMapSize() { return PlayerUUIDMap.size(); } @@ -324,9 +318,9 @@ public class PlayerManager { } if (points != null) - getPointsData().addPlayer(jPlayer.getPlayerUUID(), points); + Jobs.getPointsData().addPlayer(jPlayer.getPlayerUUID(), points); else - getPointsData().addPlayer(jPlayer.getPlayerUUID()); + Jobs.getPointsData().addPlayer(jPlayer.getPlayerUUID()); if (logs != null) jPlayer.setLog(logs); @@ -549,7 +543,7 @@ public class PlayerManager { if (Jobs.getGCManager().LevelChangeChat) player.sendMessage(line); } - } + } jPlayer.reloadHonorific(); Jobs.getPermissionHandler().recalculatePermissions(jPlayer); diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java b/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java index 64a8ec37..2dab84a9 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java @@ -33,7 +33,7 @@ public class editpoints implements Cmd { return true; } - PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(jPlayer.getPlayerUUID()); + PlayerPoints pointInfo = Jobs.getPointsData().getPlayerPointsInfo(jPlayer.getPlayerUUID()); if (pointInfo == null) { sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfoByPlayer", "%playername%", jPlayer.getUserName())); diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/points.java b/src/main/java/com/gamingmesh/jobs/commands/list/points.java index ffd585e3..e05677e1 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/points.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/points.java @@ -38,7 +38,7 @@ public class points implements Cmd { return true; } - PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(jPlayer.getPlayerUUID()); + PlayerPoints pointInfo = Jobs.getPointsData().getPlayerPointsInfo(jPlayer.getPlayerUUID()); sender.sendMessage(Jobs.getLanguage().getMessage("general.info.toplineseparator", "%playername%", jPlayer.getUserName())); sender.sendMessage(Jobs.getLanguage().getMessage("command.points.currentpoints", "%currentpoints%", (int) (pointInfo.getCurrentPoints() * 100) / 100D)); diff --git a/src/main/java/com/gamingmesh/jobs/config/ShopManager.java b/src/main/java/com/gamingmesh/jobs/config/ShopManager.java index 51f8c80a..102e91f2 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ShopManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ShopManager.java @@ -71,7 +71,7 @@ public class ShopManager { return; ShopItem item = ls.get(slot); - PlayerPoints pointsInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getUniqueId()); + PlayerPoints pointsInfo = Jobs.getPointsData().getPlayerPointsInfo(player.getUniqueId()); //if (!player.hasPermission("jobs.items.bypass")) { for (String onePerm : item.getRequiredPerm()) { @@ -193,7 +193,7 @@ public class ShopManager { if (title.length() > 32) title = title.substring(0, 30) + ".."; - PlayerPoints pointsInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(player.getUniqueId()); + PlayerPoints pointsInfo = Jobs.getPointsData().getPlayerPointsInfo(player.getUniqueId()); double points = 0D; if (pointsInfo != null) points = (int) (pointsInfo.getCurrentPoints() * 100.0) / 100.0; diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java b/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java index a7f5e2b5..8f0ac872 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsConnectionPool.java @@ -1,50 +1,51 @@ package com.gamingmesh.jobs.dao; -import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import com.gamingmesh.jobs.Jobs; - public class JobsConnectionPool { + private JobsConnection connection; private String url; private String username; private String password; - public JobsConnectionPool(String driverName, String url, String username, String password) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { - Driver driver = (Driver) Class.forName(driverName, true, Jobs.getJobsClassloader()).newInstance(); - JobsDrivers jDriver = new JobsDrivers(driver); - DriverManager.registerDriver(jDriver); - this.url = url; - this.username = username; - this.password = password; + + public JobsConnectionPool(String driverName, String url, String username, String password) + throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { + Driver driver = (Driver) Class.forName(driverName, true, Jobs.getJobsClassloader()).newInstance(); + JobsDrivers jDriver = new JobsDrivers(driver); + DriverManager.registerDriver(jDriver); + + this.url = url; + this.username = username; + this.password = password; } - + public synchronized JobsConnection getConnection() throws SQLException { - if (connection != null && (connection.isClosed() || !connection.isValid(1))) { - try { - connection.closeConnection(); - } catch (SQLException e) {} - connection = null; - } - - if (connection == null) { - Connection conn = DriverManager.getConnection(url, username, password); - connection = new JobsConnection(conn); - } - - return connection; + if (connection != null && (connection.isClosed() || !connection.isValid(1))) { + try { + connection.closeConnection(); + } catch (SQLException e) {} + connection = null; + } + + if (connection == null) { + connection = new JobsConnection(DriverManager.getConnection(url, username, password)); + } + + return connection; } - + public synchronized void closeConnection() { - if (connection != null) { - try { - connection.closeConnection(); - } catch (SQLException e) { - e.printStackTrace(); - } - } + if (connection != null) { + try { + connection.closeConnection(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 2dbe45e2..2b7f5876 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -1430,7 +1430,7 @@ public abstract class JobsDAO { PreparedStatement prest = null; try { - PlayerPoints pointInfo = Jobs.getPlayerManager().getPointsData().getPlayerPointsInfo(jPlayer.getPlayerUUID()); + PlayerPoints pointInfo = Jobs.getPointsData().getPlayerPointsInfo(jPlayer.getPlayerUUID()); prest = conn.prepareStatement("INSERT INTO `" + prefix + "points` (`totalpoints`, `currentpoints`, `userid`) VALUES (?, ?, ?);"); prest.setDouble(1, pointInfo.getTotalPoints()); prest.setDouble(2, pointInfo.getCurrentPoints()); @@ -1455,9 +1455,9 @@ public abstract class JobsDAO { res = prest.executeQuery(); if (res.next()) { - Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID(), res.getDouble("currentpoints"), res.getDouble("totalpoints")); + Jobs.getPointsData().addPlayer(player.getPlayerUUID(), res.getDouble("currentpoints"), res.getDouble("totalpoints")); } else { - Jobs.getPlayerManager().getPointsData().addPlayer(player.getPlayerUUID()); + Jobs.getPointsData().addPlayer(player.getPlayerUUID()); } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java index cadec4c1..3a2c1c33 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsMySQL.java @@ -130,7 +130,6 @@ public class JobsMySQL extends JobsDAO { statement.close(); } catch (SQLException e) { Jobs.consoleMsg("&cCould not create table, SQLException: " + e.getMessage()); - close(statement); return false; } finally { close(statement); diff --git a/src/main/java/com/gamingmesh/jobs/economy/PointsData.java b/src/main/java/com/gamingmesh/jobs/economy/PointsData.java index 1111b06b..57bf8055 100644 --- a/src/main/java/com/gamingmesh/jobs/economy/PointsData.java +++ b/src/main/java/com/gamingmesh/jobs/economy/PointsData.java @@ -10,6 +10,9 @@ public class PointsData { private HashMap Pointbase = new HashMap<>(); + public PointsData() { + } + public HashMap getPointBase() { return Pointbase; } @@ -29,6 +32,7 @@ public class PointsData { public void addPlayer(UUID uuid, PlayerPoints points) { if (Jobs.getGCManager().MultiServerCompatability() && Pointbase.containsKey(uuid)) Pointbase.remove(uuid); + if (!Pointbase.containsKey(uuid)) Pointbase.put(uuid, points); } diff --git a/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java b/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java index 5289e041..062bebe3 100644 --- a/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java +++ b/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java @@ -46,6 +46,6 @@ public class BufferedPaymentTask implements Runnable { } if (payment.getPoints() != 0D) - Jobs.getPlayerManager().getPointsData().addPoints(payment.getOfflinePlayer().getUniqueId(), payment.getPoints()); + Jobs.getPointsData().addPoints(payment.getOfflinePlayer().getUniqueId(), payment.getPoints()); } } \ No newline at end of file