From 9847235df1b8e72cf765ffd33cc17b312fe0c879 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 18 Aug 2018 09:53:21 -0700 Subject: [PATCH] Fixes MySQL exists and delete functions. --- .../database/mysql/MySQLDatabaseHandler.java | 9 ++-- .../bentobox/listeners/JoinLeaveListener.java | 3 +- .../bentobox/managers/PlayersManager.java | 23 ---------- .../bentobox/managers/PlayersManagerTest.java | 44 ++----------------- 4 files changed, 11 insertions(+), 68 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java index 6a99a86e6..c9bec3332 100644 --- a/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java @@ -165,7 +165,8 @@ public void deleteObject(T instance) { try (PreparedStatement preparedStatement = connection.prepareStatement(sb)) { Method getUniqueId = dataObject.getMethod("getUniqueId"); String uniqueId = (String) getUniqueId.invoke(instance); - preparedStatement.setString(1, uniqueId); + // UniqueId needs to be placed in quotes + preparedStatement.setString(1, "\"" + uniqueId + "\""); preparedStatement.execute(); } catch (Exception e) { plugin.logError("Could not delete object " + instance.getClass().getName() + " " + e.getMessage()); @@ -180,10 +181,12 @@ public boolean objectExists(String uniqueId) { "` WHERE `uniqueId` = ?), 1, 0)"; try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.setString(1, uniqueId); + // UniqueId needs to be placed in quotes + preparedStatement.setString(1, "\"" + uniqueId + "\""); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { - return resultSet.getBoolean(1); + boolean result = resultSet.getBoolean(1); + return result; } } } catch (SQLException e) { diff --git a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java index 2d0fa46fc..bfa08bc40 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java @@ -58,7 +58,6 @@ public void onPlayerJoin(final PlayerJoinEvent event) { @EventHandler(priority = EventPriority.NORMAL) public void onPlayerQuit(final PlayerQuitEvent event) { - players.removeOnlinePlayer(event.getPlayer().getUniqueId()); - User.removePlayer(event.getPlayer()); + players.save(event.getPlayer().getUniqueId()); } } diff --git a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java index d2d8d718d..99dd145fb 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java @@ -117,29 +117,6 @@ public void addPlayer(UUID playerUUID) { } } - /** - * Stores the player's info and removes the player from the cache - * - * @param playerUUID - the player - UUID of player - * - */ - public void removeOnlinePlayer(UUID playerUUID) { - save(playerUUID); - playerCache.remove(playerUUID); - } - - /** - * Saves all players on the server and clears the cache - */ - public void removeAllPlayers() { - playerCache.keySet().forEach(this::save); - playerCache.clear(); - } - - /* - * Player info query methods - */ - /** * Checks if the player is known or not. Will check not just the cache but if the object is * in the database too. diff --git a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java index 77d2e7d5e..6a0737525 100644 --- a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java @@ -85,7 +85,7 @@ public void setUp() throws Exception { when(end.getName()).thenReturn("world_the_end"); when(iwm.inWorld(any())).thenReturn(true); when(plugin.getIWM()).thenReturn(iwm); - + // Settings Settings s = mock(Settings.class); when(plugin.getSettings()).thenReturn(s); @@ -219,42 +219,6 @@ public void testAddPlayer() { pm.addPlayer(uuid); } - /** - * Test method for {@link world.bentobox.bentobox.managers.PlayersManager#removeOnlinePlayer(java.util.UUID)}. - */ - @Test - public void testRemoveOnlinePlayer() { - PlayersManager pm = new PlayersManager(plugin); - pm.setHandler(db); - // Unknown UUID - assertFalse(pm.isKnown(uuid)); - // Remove it just to check this does not fail - pm.removeOnlinePlayer(uuid); - // Should still be unknown - assertFalse(pm.isKnown(uuid)); - // Add the player - pm.addPlayer(uuid); - // Now should be known - assertTrue(pm.isKnown(uuid)); - // Remove the player - pm.removeOnlinePlayer(uuid); - // There's no check possible to confirm right now. - } - - /** - * Test method for {@link world.bentobox.bentobox.managers.PlayersManager#removeAllPlayers()}. - */ - @Test - public void testRemoveAllPlayers() { - PlayersManager pm = new PlayersManager(plugin); - pm.setHandler(db); - - pm.addPlayer(uuid); - pm.addPlayer(notUUID); - - pm.removeAllPlayers(); - } - /** * Test method for {@link world.bentobox.bentobox.managers.PlayersManager#isKnown(java.util.UUID)}. */ @@ -330,7 +294,7 @@ public void testGetUUIDOfflinePlayer() { // Add a player to the cache pm.addPlayer(uuid); UUID uuidResult = pm.getUUID("tastybento"); - assertEquals(uuid, uuidResult); + assertEquals(uuid, uuidResult); } /** @@ -343,7 +307,7 @@ public void testSetandGetPlayerName() { // Add a player pm.addPlayer(uuid); assertEquals("tastybento", pm.getName(user.getUniqueId())); - pm.setPlayerName(user); + pm.setPlayerName(user); assertEquals(user.getName(), pm.getName(user.getUniqueId())); } @@ -357,7 +321,7 @@ public void testGetUUIDUnknownPlayer() { // Add a player to the cache pm.addPlayer(uuid); // Unknown player should return null - assertNull(pm.getUUID("tastybento123")); + assertNull(pm.getUUID("tastybento123")); } /**