Synchronized all access to worlds map in WorldManager.

This commit is contained in:
Jeremy Wood 2012-08-05 00:00:46 -04:00
parent 16e42f6469
commit b2f3b74062

View File

@ -45,10 +45,10 @@ import java.util.logging.Logger;
* Public facing API to add/remove Multiverse worlds.
*/
public class WorldManager implements MVWorldManager {
private MultiverseCore plugin;
private WorldPurger worldPurger;
private final MultiverseCore plugin;
private final WorldPurger worldPurger;
private final Map<String, MultiverseWorld> worlds;
private Map<String, MVWorld> worldsFromTheConfig;
private Map<String, MultiverseWorld> worlds;
private FileConfiguration configWorlds = null;
private Map<String, String> defaultGens;
private String firstSpawn;
@ -225,7 +225,9 @@ public class WorldManager implements MVWorldManager {
}
// set generator (special case because we can't read it from org.bukkit.World)
synchronized (worlds) {
this.worlds.get(name).setGenerator(generator);
}
this.saveWorldsConfig();
return true;
@ -304,6 +306,7 @@ public class WorldManager implements MVWorldManager {
*/
@Override
public boolean unloadWorld(String name) {
synchronized (worlds) {
if (this.worlds.containsKey(name)) {
if (this.unloadWorldFromBukkit(name, true)) {
this.worlds.remove(name);
@ -322,6 +325,7 @@ public class WorldManager implements MVWorldManager {
} else {
this.plugin.log(Level.INFO, "Multiverse does not know about " + name + " and it's not loaded by Bukkit.");
}
}
return false;
}
@ -331,9 +335,11 @@ public class WorldManager implements MVWorldManager {
@Override
public boolean loadWorld(String name) {
// Check if the World is already loaded
synchronized (worlds) {
if (this.worlds.containsKey(name)) {
return true;
}
}
// Check that the world is in the config
if (worldsFromTheConfig.containsKey(name)) {
@ -367,8 +373,10 @@ public class WorldManager implements MVWorldManager {
String worldName = creator.name();
if (!worldsFromTheConfig.containsKey(worldName))
throw new IllegalArgumentException("That world doesn't exist!");
synchronized (worlds) {
if (worlds.containsKey(worldName))
throw new IllegalArgumentException("That world is already loaded!");
}
MVWorld mvworld = worldsFromTheConfig.get(worldName);
World cbworld;
try {
@ -380,7 +388,9 @@ public class WorldManager implements MVWorldManager {
}
mvworld.init(cbworld, plugin);
this.worldPurger.purgeWorld(mvworld);
synchronized (worlds) {
this.worlds.put(worldName, mvworld);
}
return true;
}
@ -477,17 +487,21 @@ public class WorldManager implements MVWorldManager {
*/
@Override
public Collection<MultiverseWorld> getMVWorlds() {
synchronized (worlds) {
return this.worlds.values();
}
}
/**
* {@inheritDoc}
*/
@Override
public MultiverseWorld getMVWorld(String name) {
synchronized (worlds) {
if (this.worlds.containsKey(name)) {
return this.worlds.get(name);
}
}
return this.getMVWorldByAlias(name);
}
@ -509,11 +523,13 @@ public class WorldManager implements MVWorldManager {
* @return A {@link MVWorld} or null.
*/
private MultiverseWorld getMVWorldByAlias(String alias) {
synchronized (worlds) {
for (MultiverseWorld w : this.worlds.values()) {
if (w.getAlias().equalsIgnoreCase(alias)) {
return w;
}
}
}
return null;
}
@ -522,8 +538,10 @@ public class WorldManager implements MVWorldManager {
*/
@Override
public boolean isMVWorld(String name) {
synchronized (worlds) {
return (this.worlds.containsKey(name) || isMVWorldAlias(name));
}
}
/**
* {@inheritDoc}
@ -540,11 +558,13 @@ public class WorldManager implements MVWorldManager {
* @return True if the world exists, false if not.
*/
private boolean isMVWorldAlias(String alias) {
synchronized (worlds) {
for (MultiverseWorld w : this.worlds.values()) {
if (w.getAlias().equalsIgnoreCase(alias)) {
return true;
}
}
}
return false;
}
@ -589,6 +609,7 @@ public class WorldManager implements MVWorldManager {
// Remove all world permissions.
Permission allAccess = this.plugin.getServer().getPluginManager().getPermission("multiverse.access.*");
Permission allExempt = this.plugin.getServer().getPluginManager().getPermission("multiverse.exempt.*");
synchronized (worlds) {
for (MultiverseWorld w : this.worlds.values()) {
// Remove this world from the master list
if (allAccess != null) {
@ -602,15 +623,20 @@ public class WorldManager implements MVWorldManager {
// Special namespace for gamemodes
this.plugin.getServer().getPluginManager().removePermission("mv.bypass.gamemode." + w.getName());
}
}
// Recalc the all permission
this.plugin.getServer().getPluginManager().recalculatePermissionDefaults(allAccess);
this.plugin.getServer().getPluginManager().recalculatePermissionDefaults(allExempt);
synchronized (worlds) {
this.worlds.clear();
}
}
for (Map.Entry<String, MVWorld> entry : worldsFromTheConfig.entrySet()) {
synchronized (worlds) {
if (worlds.containsKey(entry.getKey()))
continue;
}
if (!entry.getValue().getAutoLoad())
continue;
@ -676,9 +702,11 @@ public class WorldManager implements MVWorldManager {
String worldName = key.replaceAll(String.valueOf(SEPARATOR), ".");
if (this.worldsFromTheConfig.containsKey(worldName)) {
// Object-Recycling :D
synchronized (worlds) {
MVWorld oldMVWorld = (MVWorld) this.worlds.get(worldName);
oldMVWorld.copyValues((MVWorld) obj);
newWorldsFromTheConfig.put(worldName, oldMVWorld);
}
} else {
// we have to use a new one
World cbworld = this.plugin.getServer().getWorld(worldName);
@ -696,7 +724,9 @@ public class WorldManager implements MVWorldManager {
}
}
this.worldsFromTheConfig = newWorldsFromTheConfig;
synchronized (worlds) {
this.worlds.keySet().retainAll(this.worldsFromTheConfig.keySet());
}
return this.configWorlds;
}
@ -733,7 +763,9 @@ public class WorldManager implements MVWorldManager {
@Override
public List<String> getUnloadedWorlds() {
List<String> allNames = new ArrayList<String>(this.worldsFromTheConfig.keySet());
synchronized (worlds) {
allNames.removeAll(worlds.keySet());
}
return allNames;
}