mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-22 10:36:06 +01:00
Now using ConcurrentHashMap instead of ReentrantLock for caching player world.
This commit is contained in:
parent
f2e1053b88
commit
80f6a0159c
@ -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
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user