MVWorld properties now volatile instead of using thread lock.

This commit is contained in:
Jeremy Wood 2012-08-21 14:25:11 -04:00
parent a778d353c4
commit e975968637
3 changed files with 44 additions and 100 deletions

View File

@ -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> world = new WeakReference<World>(null); // A reference to the World Instance.
private volatile Reference<World> world = new WeakReference<World>(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<Boolean> pvp = new VirtualProperty<Boolean>() {
private volatile VirtualProperty<Boolean> pvp = new VirtualProperty<Boolean>() {
@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> difficulty = new VirtualProperty<Difficulty>() {
private volatile VirtualProperty<Difficulty> difficulty = new VirtualProperty<Difficulty>() {
@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<Boolean> keepSpawnInMemory = new VirtualProperty<Boolean>() {
private volatile VirtualProperty<Boolean> keepSpawnInMemory = new VirtualProperty<Boolean>() {
@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<Location> spawn = new VirtualProperty<Location>() {
private volatile VirtualProperty<Location> spawn = new VirtualProperty<Location>() {
@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<String> worldBlacklist;
private volatile List<String> worldBlacklist;
@Property(serializor = TimePropertySerializor.class, virtualType = Long.class,
description = "Set the time to whatever you want! (Will NOT freeze time)")
private VirtualProperty<Long> time = new VirtualProperty<Long>() {
private volatile VirtualProperty<Long> time = new VirtualProperty<Long>() {
@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();
}
/**

View File

@ -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.

View File

@ -38,8 +38,6 @@ public class MultiverseCoreConfiguration extends SerializationConfig implements
return instance;
}
//private final ReentrantLock propertyLock = new ReentrantLock();
@Property
private volatile boolean enforceaccess;
@Property