From e975968637d1911f156c168f9451c9ae0e91a1dc Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Tue, 21 Aug 2012 14:25:11 -0400 Subject: [PATCH] MVWorld properties now volatile instead of using thread lock. --- .../onarandombox/MultiverseCore/MVWorld.java | 141 ++++++------------ .../MultiverseCore/MultiverseCore.java | 1 - .../MultiverseCoreConfiguration.java | 2 - 3 files changed, 44 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index baea6766..9c31591e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -47,7 +47,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -88,7 +87,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private MultiverseCore plugin; // Hold the Plugin Instance. - private Reference world = new WeakReference(null); // A reference to the World Instance. + private volatile Reference world = new WeakReference(null); // A reference to the World Instance. private String name; // The Worlds Name, EG its folder name. /** @@ -351,20 +350,18 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } } - private final ReentrantLock propertyLock = new ReentrantLock(); - // -------------------------------------------------------------- // Begin properties @Property(description = "Sorry, 'hidden' must either be: true or false.") - private boolean hidden; + private volatile boolean hidden; @Property(description = "Alias must be a valid string.") - private String alias; + private volatile String alias; @Property(serializor = EnumPropertySerializor.class, description = "Sorry, 'color' must be a valid color-name.") - private EnglishChatColor color; + private volatile EnglishChatColor color; @Property(serializor = EnumPropertySerializor.class, description = "Sorry, 'style' must be a valid style-name.") - private EnglishChatStyle style; + private volatile EnglishChatStyle style; @Property(description = "Sorry, 'pvp' must either be: true or false.", virtualType = Boolean.class, persistVirtual = true) - private VirtualProperty pvp = new VirtualProperty() { + private volatile VirtualProperty pvp = new VirtualProperty() { @Override public void set(Boolean newValue) { world.get().setPVP(newValue); @@ -376,14 +373,14 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } }; @Property(validator = ScalePropertyValidator.class, description = "Scale must be a positive double value. ex: 2.3") - private double scale; + private volatile double scale; @Property(validator = RespawnWorldPropertyValidator.class, description = "You must set this to the NAME not alias of a world.") - private String respawnWorld; + private volatile String respawnWorld; @Property(validator = AllowWeatherPropertyValidator.class, description = "Sorry, this must either be: true or false.") - private boolean allowWeather; + private volatile boolean allowWeather; @Property(serializor = DifficultyPropertySerializor.class, virtualType = Difficulty.class, persistVirtual = true, description = "Difficulty must be set as one of the following: peaceful easy normal hard") - private VirtualProperty difficulty = new VirtualProperty() { + private volatile VirtualProperty difficulty = new VirtualProperty() { @Override public void set(Difficulty newValue) { world.get().setDifficulty(newValue); @@ -395,22 +392,22 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } }; @Property(validator = SpawningPropertyValidator.class, description = "Sorry, 'animals' must either be: true or false.") - private SpawnSettings spawning; + private volatile SpawnSettings spawning; @Property - private EntryFee entryfee; + private volatile EntryFee entryfee; @Property(description = "Sorry, 'hunger' must either be: true or false.") - private boolean hunger; + private volatile boolean hunger; @Property(description = "Sorry, 'autoheal' must either be: true or false.") - private boolean autoHeal; + private volatile boolean autoHeal; @Property(description = "Sorry, 'adjustspawn' must either be: true or false.") - private boolean adjustSpawn; + private volatile boolean adjustSpawn; @Property(serializor = EnumPropertySerializor.class, description = "Allow portal forming must be NONE, ALL, NETHER or END.") - private AllowedPortalType portalForm; + private volatile AllowedPortalType portalForm; @Property(serializor = GameModePropertySerializor.class, validator = GameModePropertyValidator.class, description = "GameMode must be set as one of the following: survival creative") - private GameMode gameMode; + private volatile GameMode gameMode; @Property(description = "Sorry, this must either be: true or false.", virtualType = Boolean.class, persistVirtual = true) - private VirtualProperty keepSpawnInMemory = new VirtualProperty() { + private volatile VirtualProperty keepSpawnInMemory = new VirtualProperty() { @Override public void set(Boolean newValue) { world.get().setKeepSpawnInMemory(newValue); @@ -422,10 +419,10 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } }; @Property - private SpawnLocation spawnLocation; + private volatile SpawnLocation spawnLocation; @Property(validator = SpawnLocationPropertyValidator.class, virtualType = Location.class, description = "There is no help available for this variable. Go bug Rigby90 about it.") - private VirtualProperty spawn = new VirtualProperty() { + private volatile VirtualProperty spawn = new VirtualProperty() { @Override public void set(Location newValue) { if (getCBWorld() != null) @@ -443,14 +440,14 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } }; @Property(description = "Set this to false ONLY if you don't want this world to load itself on server restart.") - private boolean autoLoad; + private volatile boolean autoLoad; @Property(description = "If a player dies in this world, shoudld they go to their bed?") - private boolean bedRespawn; + private volatile boolean bedRespawn; @Property - private List worldBlacklist; + private volatile List worldBlacklist; @Property(serializor = TimePropertySerializor.class, virtualType = Long.class, description = "Set the time to whatever you want! (Will NOT freeze time)") - private VirtualProperty time = new VirtualProperty() { + private volatile VirtualProperty time = new VirtualProperty() { @Override public void set(Long newValue) { world.get().setTime(newValue); @@ -462,11 +459,11 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } }; @Property - private Environment environment; + private volatile Environment environment; @Property - private long seed; + private volatile long seed; @Property - private String generator; + private volatile String generator; // End of properties // -------------------------------------------------------------- @@ -715,30 +712,20 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { */ @Override public String getColoredWorldString() { - Thread thread = Thread.currentThread(); - if (propertyLock.isLocked()) { - plugin.log(Level.FINEST, "propertyLock is locked when attempting to get ColoredWorldString on thread: " + thread); + if (alias.length() == 0) { + alias = this.getName(); } - propertyLock.lock(); - try { - plugin.log(Level.FINEST, "Getting ColoredWorldString on thread: " + thread); - if (alias.length() == 0) { - alias = this.getName(); - } - if ((color == null) || (color.getColor() == null)) { - this.setPropertyValueUnchecked("color", EnglishChatColor.WHITE); - } - - StringBuilder nameBuilder = new StringBuilder().append(color.getColor()); - if (style.getColor() != null) - nameBuilder.append(style.getColor()); - nameBuilder.append(alias).append(ChatColor.WHITE).toString(); - - return nameBuilder.toString(); - } finally { - propertyLock.unlock(); + if ((color == null) || (color.getColor() == null)) { + this.setPropertyValueUnchecked("color", EnglishChatColor.WHITE); } + + StringBuilder nameBuilder = new StringBuilder().append(color.getColor()); + if (style.getColor() != null) + nameBuilder.append(style.getColor()); + nameBuilder.append(alias).append(ChatColor.WHITE).toString(); + + return nameBuilder.toString(); } /** @@ -949,20 +936,10 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { */ @Override public String getAlias() { - Thread thread = Thread.currentThread(); - if (propertyLock.isLocked()) { - plugin.log(Level.FINEST, "propertyLock is locked when attempting to get alias on thread: " + thread); - } - propertyLock.lock(); - try { - plugin.log(Level.FINEST, "Getting alias on thread: " + thread); - if (this.alias == null || this.alias.length() == 0) { - return this.name; - } - return this.alias; - } finally { - propertyLock.unlock(); + if (this.alias == null || this.alias.length() == 0) { + return this.name; } + return this.alias; } /** @@ -1044,17 +1021,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { */ @Override public boolean isHidden() { - Thread thread = Thread.currentThread(); - if (propertyLock.isLocked()) { - plugin.log(Level.FINEST, "propertyLock is locked when attempting to get hidden on thread: " + thread); - } - propertyLock.lock(); - try { - plugin.log(Level.FINEST, "Getting hidden on thread: " + thread); - return this.hidden; - } finally { - propertyLock.unlock(); - } + return this.hidden; } /** @@ -1113,17 +1080,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { */ @Override public ChatColor getColor() { - Thread thread = Thread.currentThread(); - if (propertyLock.isLocked()) { - plugin.log(Level.FINEST, "propertyLock is locked when attempting to get color on thread: " + thread); - } - propertyLock.lock(); - try { - plugin.log(Level.FINEST, "Getting color on thread: " + thread); - return this.color.getColor(); - } finally { - propertyLock.unlock(); - } + return this.color.getColor(); } /** @@ -1427,17 +1384,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { */ @Override public ChatColor getStyle() { - Thread thread = Thread.currentThread(); - if (propertyLock.isLocked()) { - plugin.log(Level.FINEST, "propertyLock is locked when attempting to get style on thread: " + thread); - } - propertyLock.lock(); - try { - plugin.log(Level.FINEST, "Getting style on thread: " + thread); - return style.getColor(); - } finally { - propertyLock.unlock(); - } + return style.getColor(); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index ef765d15..8a419e12 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -119,7 +119,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { private AnchorManager anchorManager = new AnchorManager(this); // TODO please let's make this non-static private volatile MultiverseCoreConfiguration config; - //private final ReentrantLock configLock = new ReentrantLock(); /** * This method is used to find out who is teleporting a player. diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java index 7af440bf..289e4d06 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java @@ -38,8 +38,6 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements return instance; } - //private final ReentrantLock propertyLock = new ReentrantLock(); - @Property private volatile boolean enforceaccess; @Property