Skip to content

Commit

Permalink
Fixes MySQL exists and delete functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Aug 18, 2018
1 parent 46c1ff4 commit 9847235
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
23 changes: 0 additions & 23 deletions src/main/java/world/bentobox/bentobox/managers/PlayersManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)}.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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()));
}

Expand All @@ -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"));
}

/**
Expand Down

0 comments on commit 9847235

Please sign in to comment.