Now using ConcurrentHashMap instead of ReentrantLock for caching player world.

This commit is contained in:
Jeremy Wood 2012-08-21 15:06:33 -04:00
parent f2e1053b88
commit 80f6a0159c
3 changed files with 10 additions and 52 deletions

View File

@ -1,12 +1,9 @@
package com.onarandombox.MultiverseCore.listeners;
import java.util.logging.Level;
import org.bukkit.event.Listener;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.event.Listener;
/**
* Multiverse's {@link org.bukkit.event.Listener} for players.
@ -33,21 +30,10 @@ public abstract class MVChatListener implements Listener {
// Check whether the Server is set to prefix the chat with the World name.
// If not we do nothing, if so we need to check if the World has an Alias.
if (plugin.getMVConfig().getPrefixChat()) {
String world;
Thread thread = Thread.currentThread();
if (playerListener.getWorldsLock().isLocked()) {
plugin.log(Level.FINEST, "worldsLock is locked when attempting to handle player chat on thread: " + thread);
}
playerListener.getWorldsLock().lock();
try {
plugin.log(Level.FINEST, "Handling player chat on thread: " + thread);
world = playerListener.getPlayerWorld().get(event.getPlayer().getName());
if (world == null) {
world = event.getPlayer().getWorld().getName();
playerListener.getPlayerWorld().put(event.getPlayer().getName(), world);
}
} finally {
playerListener.getWorldsLock().unlock();
String world = playerListener.getPlayerWorld().get(event.getPlayer().getName());
if (world == null) {
world = event.getPlayer().getWorld().getName();
playerListener.getPlayerWorld().put(event.getPlayer().getName(), world);
}
String prefix = "";
// If we're not a MV world, don't do anything

View File

@ -27,9 +27,8 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
/**
@ -40,8 +39,7 @@ public class MVPlayerListener implements Listener {
private final MVWorldManager worldManager;
private final PermissionTools pt;
private final ReentrantLock worldsLock = new ReentrantLock();
private final Map<String, String> playerWorld = new HashMap<String, String>();
private final Map<String, String> playerWorld = new ConcurrentHashMap<String, String>();
public MVPlayerListener(MultiverseCore plugin) {
this.plugin = plugin;
@ -49,13 +47,6 @@ public class MVPlayerListener implements Listener {
pt = new PermissionTools(plugin);
}
/**
* @return the worldsLock
*/
public ReentrantLock getWorldsLock() {
return worldsLock;
}
/**
* @return the playerWorld-map
*/
@ -134,17 +125,7 @@ public class MVPlayerListener implements Listener {
}
// Handle the Players GameMode setting for the new world.
this.handleGameMode(event.getPlayer(), event.getPlayer().getWorld());
Thread thread = Thread.currentThread();
if (worldsLock.isLocked()) {
plugin.log(Level.FINEST, "worldsLock is locked when attempting to cache player world on thread: " + thread);
}
worldsLock.lock();
try {
plugin.log(Level.FINEST, "Caching player world on thread: " + thread);
playerWorld.put(p.getName(), p.getWorld().getName());
} finally {
worldsLock.unlock();
}
playerWorld.put(p.getName(), p.getWorld().getName());
}
/**
@ -155,17 +136,7 @@ public class MVPlayerListener implements Listener {
public void playerChangedWorld(PlayerChangedWorldEvent event) {
// Permissions now determine whether or not to handle a gamemode.
this.handleGameMode(event.getPlayer(), event.getPlayer().getWorld());
Thread thread = Thread.currentThread();
if (worldsLock.isLocked()) {
plugin.log(Level.FINEST, "worldsLock is locked when attempting to cache player world on thread: " + thread);
}
worldsLock.lock();
try {
plugin.log(Level.FINEST, "Caching player world on thread: " + thread);
playerWorld.put(event.getPlayer().getName(), event.getPlayer().getWorld().getName());
} finally {
worldsLock.unlock();
}
playerWorld.put(event.getPlayer().getName(), event.getPlayer().getWorld().getName());
}
/**

View File

@ -345,6 +345,7 @@ public class TestWorldProperties {
when(mockPlayer.getWorld()).thenReturn(mvWorld.getCBWorld());
when(mockPlayer.hasPlayedBefore()).thenReturn(true);
when(mockPlayer.hasPermission("multiverse.access.world")).thenReturn(true);
when(mockPlayer.getName()).thenReturn("MultiverseMan");
playerChatEvent = PowerMockito.mock(AsyncPlayerChatEvent.class);
when(playerChatEvent.getPlayer()).thenReturn(mockPlayer);
when(playerChatEvent.getFormat()).thenReturn("format");