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