Clears player cache by name and UUID

Previously only UUID was checked, but joining with a name and different
UUID is possible. This clears out the cache so commands that look up
names like ban do not use the old UUID.

https://github.com/BentoBoxWorld/BentoBox/issues/1468
This commit is contained in:
tastybento 2020-07-25 14:37:45 -07:00
parent 22724077bb
commit 1e4056e33a
3 changed files with 20 additions and 1 deletions

View File

@ -118,12 +118,13 @@ public class User {
}
/**
* Removes this player from the User cache
* Removes this player from the User cache and player manager cache
* @param player the player
*/
public static void removePlayer(Player player) {
if (player != null) {
users.remove(player.getUniqueId());
BentoBox.getInstance().getPlayers().removePlayer(player);
}
}

View File

@ -12,6 +12,7 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@ -479,4 +480,17 @@ public class PlayersManager {
addPlayer(playerUUID);
return playerCache.get(playerUUID).getFlagsDisplayMode();
}
/**
* Remove player from cache. Clears players with the same name or UUID
* @param player player to remove
*/
public void removePlayer(Player player) {
// Clear any players with the same name
playerCache.values().removeIf(p -> p.getPlayerName().equalsIgnoreCase(player.getName()));
// Remove if the player's UUID is the same
playerCache.values().removeIf(p -> p.getUniqueId().equals(player.getUniqueId().toString()));
}
}

View File

@ -78,6 +78,8 @@ public class UserTest {
private CommandSender sender;
@Mock
private Server server;
@Mock
private PlayersManager pm;
@Before
public void setUp() throws Exception {
@ -117,6 +119,7 @@ public class UserTest {
// This will just return the value of the second argument of replacePlaceholders. i.e., it won't change anything
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getPlayers()).thenReturn(pm);
}
@After
@ -154,6 +157,7 @@ public class UserTest {
// Return null and check if instance is null will show that the player is not in the cache
when(Bukkit.getPlayer(any(UUID.class))).thenReturn(null);
assertNull(User.getInstance(uuid).getPlayer());
verify(pm).removePlayer(player);
}
@Test