Synchronized getting/setting of mv config object.

This commit is contained in:
Jeremy Wood 2012-08-04 23:50:01 -04:00
parent 7b99130bfa
commit a31dc83635

View File

@ -113,6 +113,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
private AnchorManager anchorManager = new AnchorManager(this); private AnchorManager anchorManager = new AnchorManager(this);
// TODO please let's make this non-static // TODO please let's make this non-static
private MultiverseCoreConfiguration config; private MultiverseCoreConfiguration config;
private final Object configLock = new Object();
/** /**
* This method is used to find out who is teleporting a player. * This method is used to find out who is teleporting a player.
@ -293,9 +294,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
this.anchorManager.loadAnchors(); this.anchorManager.loadAnchors();
// Now set the firstspawnworld (after the worlds are loaded): // Now set the firstspawnworld (after the worlds are loaded):
this.worldManager.setFirstSpawnWorld(config.getFirstSpawnWorld()); this.worldManager.setFirstSpawnWorld(getMVConfig().getFirstSpawnWorld());
try { try {
config.setFirstSpawnWorld(this.worldManager.getFirstSpawnWorld().getName()); getMVConfig().setFirstSpawnWorld(this.worldManager.getFirstSpawnWorld().getName());
} catch (NullPointerException e) { } catch (NullPointerException e) {
// A test that had no worlds loaded was being run. This should never happen in production // A test that had no worlds loaded was being run. This should never happen in production
} }
@ -361,12 +362,14 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
} catch (Exception e) { } catch (Exception e) {
// We're just thinking "no risk no fun" and therefore have to catch and forget this exception // We're just thinking "no risk no fun" and therefore have to catch and forget this exception
} finally { } finally {
config = ((wantedConfig == null) ? new MultiverseCoreConfiguration() : wantedConfig); synchronized (configLock) {
config = ((wantedConfig == null) ? new MultiverseCoreConfiguration() : wantedConfig);
}
} }
this.migrateWorldConfig(); this.migrateWorldConfig();
this.worldManager.loadWorldConfig(new File(getDataFolder(), "worlds.yml")); this.worldManager.loadWorldConfig(new File(getDataFolder(), "worlds.yml"));
this.messaging.setCooldown(config.getMessageCooldown()); this.messaging.setCooldown(getMVConfig().getMessageCooldown());
// Remove old values. // Remove old values.
this.multiverseConfig.set("enforcegamemodes", null); this.multiverseConfig.set("enforcegamemodes", null);
@ -385,42 +388,42 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
private void migrate22Values() { private void migrate22Values() {
if (this.multiverseConfig.isSet("worldnameprefix")) { if (this.multiverseConfig.isSet("worldnameprefix")) {
this.log(Level.INFO, "Migrating 'worldnameprefix'..."); this.log(Level.INFO, "Migrating 'worldnameprefix'...");
this.config.setPrefixChat(this.multiverseConfig.getBoolean("worldnameprefix")); this.getMVConfig().setPrefixChat(this.multiverseConfig.getBoolean("worldnameprefix"));
this.multiverseConfig.set("worldnameprefix", null); this.multiverseConfig.set("worldnameprefix", null);
} }
if (this.multiverseConfig.isSet("firstspawnworld")) { if (this.multiverseConfig.isSet("firstspawnworld")) {
this.log(Level.INFO, "Migrating 'firstspawnworld'..."); this.log(Level.INFO, "Migrating 'firstspawnworld'...");
this.config.setFirstSpawnWorld(this.multiverseConfig.getString("firstspawnworld")); this.getMVConfig().setFirstSpawnWorld(this.multiverseConfig.getString("firstspawnworld"));
this.multiverseConfig.set("firstspawnworld", null); this.multiverseConfig.set("firstspawnworld", null);
} }
if (this.multiverseConfig.isSet("enforceaccess")) { if (this.multiverseConfig.isSet("enforceaccess")) {
this.log(Level.INFO, "Migrating 'enforceaccess'..."); this.log(Level.INFO, "Migrating 'enforceaccess'...");
this.config.setEnforceAccess(this.multiverseConfig.getBoolean("enforceaccess")); this.getMVConfig().setEnforceAccess(this.multiverseConfig.getBoolean("enforceaccess"));
this.multiverseConfig.set("enforceaccess", null); this.multiverseConfig.set("enforceaccess", null);
} }
if (this.multiverseConfig.isSet("displaypermerrors")) { if (this.multiverseConfig.isSet("displaypermerrors")) {
this.log(Level.INFO, "Migrating 'displaypermerrors'..."); this.log(Level.INFO, "Migrating 'displaypermerrors'...");
this.config.setDisplayPermErrors(this.multiverseConfig.getBoolean("displaypermerrors")); this.getMVConfig().setDisplayPermErrors(this.multiverseConfig.getBoolean("displaypermerrors"));
this.multiverseConfig.set("displaypermerrors", null); this.multiverseConfig.set("displaypermerrors", null);
} }
if (this.multiverseConfig.isSet("teleportintercept")) { if (this.multiverseConfig.isSet("teleportintercept")) {
this.log(Level.INFO, "Migrating 'teleportintercept'..."); this.log(Level.INFO, "Migrating 'teleportintercept'...");
this.config.setTeleportIntercept(this.multiverseConfig.getBoolean("teleportintercept")); this.getMVConfig().setTeleportIntercept(this.multiverseConfig.getBoolean("teleportintercept"));
this.multiverseConfig.set("teleportintercept", null); this.multiverseConfig.set("teleportintercept", null);
} }
if (this.multiverseConfig.isSet("firstspawnoverride")) { if (this.multiverseConfig.isSet("firstspawnoverride")) {
this.log(Level.INFO, "Migrating 'firstspawnoverride'..."); this.log(Level.INFO, "Migrating 'firstspawnoverride'...");
this.config.setFirstSpawnOverride(this.multiverseConfig.getBoolean("firstspawnoverride")); this.getMVConfig().setFirstSpawnOverride(this.multiverseConfig.getBoolean("firstspawnoverride"));
this.multiverseConfig.set("firstspawnoverride", null); this.multiverseConfig.set("firstspawnoverride", null);
} }
if (this.multiverseConfig.isSet("messagecooldown")) { if (this.multiverseConfig.isSet("messagecooldown")) {
this.log(Level.INFO, "Migrating 'messagecooldown'..."); this.log(Level.INFO, "Migrating 'messagecooldown'...");
this.config.setMessageCooldown(this.multiverseConfig.getInt("messagecooldown")); this.getMVConfig().setMessageCooldown(this.multiverseConfig.getInt("messagecooldown"));
this.multiverseConfig.set("messagecooldown", null); this.multiverseConfig.set("messagecooldown", null);
} }
if (this.multiverseConfig.isSet("debug")) { if (this.multiverseConfig.isSet("debug")) {
this.log(Level.INFO, "Migrating 'debug'..."); this.log(Level.INFO, "Migrating 'debug'...");
this.config.setGlobalDebug(this.multiverseConfig.getInt("debug")); this.getMVConfig().setGlobalDebug(this.multiverseConfig.getInt("debug"));
this.multiverseConfig.set("debug", null); this.multiverseConfig.set("debug", null);
} }
if (this.multiverseConfig.isSet("version")) { if (this.multiverseConfig.isSet("version")) {
@ -709,7 +712,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
if (this.playerSessions.containsKey(player.getName())) { if (this.playerSessions.containsKey(player.getName())) {
return this.playerSessions.get(player.getName()); return this.playerSessions.get(player.getName());
} else { } else {
this.playerSessions.put(player.getName(), new MVPlayerSession(player, config)); this.playerSessions.put(player.getName(), new MVPlayerSession(player, getMVConfig()));
return this.playerSessions.get(player.getName()); return this.playerSessions.get(player.getName());
} }
} }
@ -744,7 +747,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
} }
ArrayList<String> allArgs = new ArrayList<String>(Arrays.asList(args)); ArrayList<String> allArgs = new ArrayList<String>(Arrays.asList(args));
allArgs.add(0, command.getName()); allArgs.add(0, command.getName());
return this.commandHandler.locateAndRunCommand(sender, allArgs, config.getDisplayPermErrors()); return this.commandHandler.locateAndRunCommand(sender, allArgs, getMVConfig().getDisplayPermErrors());
} }
/** /**
@ -993,7 +996,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
*/ */
public boolean saveMVConfig() { public boolean saveMVConfig() {
try { try {
this.multiverseConfig.set("multiverse-configuration", config); this.multiverseConfig.set("multiverse-configuration", getMVConfig());
this.multiverseConfig.save(new File(getDataFolder(), "config.yml")); this.multiverseConfig.save(new File(getDataFolder(), "config.yml"));
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -1138,7 +1141,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
*/ */
@Override @Override
public MultiverseCoreConfig getMVConfig() { public MultiverseCoreConfig getMVConfig() {
return config; synchronized (configLock) {
return config;
}
} }
/** /**