Changed how we deal with CB world reference. Partially fixes #923.

This commit is contained in:
Jeremy Wood 2012-11-05 13:02:03 -05:00
parent 73e394d5ec
commit f210851294
3 changed files with 75 additions and 34 deletions

View File

@ -42,8 +42,6 @@ import org.bukkit.permissions.PermissionDefault;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -92,7 +90,6 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
private MultiverseCore plugin; // Hold the Plugin Instance. private MultiverseCore plugin; // Hold the Plugin 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. private String name; // The Worlds Name, EG its folder name.
/** /**
@ -212,8 +209,11 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
public Boolean validateChange(String property, Boolean newValue, Boolean oldValue, public Boolean validateChange(String property, Boolean newValue, Boolean oldValue,
MVWorld object) throws ChangeDeniedException { MVWorld object) throws ChangeDeniedException {
if (!newValue) { if (!newValue) {
world.get().setStorm(false); final World world = getCBWorld();
world.get().setThundering(false); if (world != null) {
world.setStorm(false);
world.setThundering(false);
}
} }
return super.validateChange(property, newValue, oldValue, object); return super.validateChange(property, newValue, oldValue, object);
} }
@ -237,13 +237,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
} else { } else {
allowMonsters = true; allowMonsters = true;
} }
if (MVWorld.this.spawning.getAnimalSettings().getSpawnRate() != -1) { final World world = getCBWorld();
world.get().setTicksPerAnimalSpawns(MVWorld.this.spawning.getAnimalSettings().getSpawnRate()); if (world != null) {
if (MVWorld.this.spawning.getAnimalSettings().getSpawnRate() != -1) {
world.setTicksPerAnimalSpawns(MVWorld.this.spawning.getAnimalSettings().getSpawnRate());
}
if (MVWorld.this.spawning.getMonsterSettings().getSpawnRate() != -1) {
world.setTicksPerMonsterSpawns(MVWorld.this.spawning.getMonsterSettings().getSpawnRate());
}
world.setSpawnFlags(allowMonsters, allowAnimals);
} }
if (MVWorld.this.spawning.getMonsterSettings().getSpawnRate() != -1) {
world.get().setTicksPerMonsterSpawns(MVWorld.this.spawning.getMonsterSettings().getSpawnRate());
}
world.get().setSpawnFlags(allowMonsters, allowAnimals);
plugin.getMVWorldManager().getTheWorldPurger().purgeWorld(MVWorld.this); plugin.getMVWorldManager().getTheWorldPurger().purgeWorld(MVWorld.this);
return super.validateChange(property, newValue, oldValue, object); return super.validateChange(property, newValue, oldValue, object);
} }
@ -375,12 +378,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
private volatile VirtualProperty<Boolean> pvp = new VirtualProperty<Boolean>() { private volatile VirtualProperty<Boolean> pvp = new VirtualProperty<Boolean>() {
@Override @Override
public void set(Boolean newValue) { public void set(Boolean newValue) {
world.get().setPVP(newValue); final World world = getCBWorld();
if (world != null) {
world.setPVP(newValue);
}
} }
@Override @Override
public Boolean get() { public Boolean get() {
return world.get().getPVP(); final World world = getCBWorld();
return world != null ? world.getPVP() : null;
} }
}; };
@Property(validator = ScalePropertyValidator.class, description = "Scale must be a positive double value. ex: 2.3") @Property(validator = ScalePropertyValidator.class, description = "Scale must be a positive double value. ex: 2.3")
@ -394,12 +401,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
private volatile VirtualProperty<Difficulty> difficulty = new VirtualProperty<Difficulty>() { private volatile VirtualProperty<Difficulty> difficulty = new VirtualProperty<Difficulty>() {
@Override @Override
public void set(Difficulty newValue) { public void set(Difficulty newValue) {
world.get().setDifficulty(newValue); final World world = getCBWorld();
if (world != null) {
world.setDifficulty(newValue);
}
} }
@Override @Override
public Difficulty get() { public Difficulty get() {
return world.get().getDifficulty(); final World world = getCBWorld();
return world != null ? world.getDifficulty() : null;
} }
}; };
@Property(validator = SpawningPropertyValidator.class, description = "Sorry, 'animals' must either be: true or false.") @Property(validator = SpawningPropertyValidator.class, description = "Sorry, 'animals' must either be: true or false.")
@ -421,12 +432,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
private volatile VirtualProperty<Boolean> keepSpawnInMemory = new VirtualProperty<Boolean>() { private volatile VirtualProperty<Boolean> keepSpawnInMemory = new VirtualProperty<Boolean>() {
@Override @Override
public void set(Boolean newValue) { public void set(Boolean newValue) {
world.get().setKeepSpawnInMemory(newValue); final World world = getCBWorld();
if (world != null) {
world.setKeepSpawnInMemory(newValue);
}
} }
@Override @Override
public Boolean get() { public Boolean get() {
return world.get().getKeepSpawnInMemory(); final World world = getCBWorld();
return world != null ? world.getKeepSpawnInMemory() : null;
} }
}; };
@Property @Property
@ -461,12 +476,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
private volatile VirtualProperty<Long> time = new VirtualProperty<Long>() { private volatile VirtualProperty<Long> time = new VirtualProperty<Long>() {
@Override @Override
public void set(Long newValue) { public void set(Long newValue) {
world.get().setTime(newValue); final World world = getCBWorld();
if (world != null) {
world.setTime(newValue);
}
} }
@Override @Override
public Long get() { public Long get() {
return world.get().getTime(); final World world = getCBWorld();
return world != null ? world.getTime() : null;
} }
}; };
@Property @Property
@ -554,8 +573,6 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
public void init(World cbWorld, MultiverseCore thePlugin) { public void init(World cbWorld, MultiverseCore thePlugin) {
this.plugin = thePlugin; this.plugin = thePlugin;
// Weak reference so the CB-World can be unloaded even if this object still exists!
this.world = new WeakReference<World>(cbWorld);
this.environment = cbWorld.getEnvironment(); this.environment = cbWorld.getEnvironment();
this.seed = cbWorld.getSeed(); this.seed = cbWorld.getSeed();
this.name = cbWorld.getName(); this.name = cbWorld.getName();
@ -670,7 +687,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
this.adjustSpawn = true; this.adjustSpawn = true;
this.portalForm = AllowedPortalType.ALL; this.portalForm = AllowedPortalType.ALL;
this.gameMode = GameMode.SURVIVAL; this.gameMode = GameMode.SURVIVAL;
this.spawnLocation = (world != null) ? new SpawnLocation(world.get().getSpawnLocation()) : new NullLocation(); this.spawnLocation = (getCBWorld() != null) ? new SpawnLocation(getCBWorld().getSpawnLocation()) : new NullLocation();
this.autoLoad = true; this.autoLoad = true;
this.bedRespawn = true; this.bedRespawn = true;
this.worldBlacklist = new ArrayList<String>(); this.worldBlacklist = new ArrayList<String>();
@ -725,7 +742,14 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
*/ */
@Override @Override
public World getCBWorld() { public World getCBWorld() {
return this.world.get(); if (name == null) {
return null;
}
final World world = plugin.getServer().getWorld(name);
if (world == null) {
Logging.severe("Lost reference to bukkit world '%s'", name);
}
return world;
} }
/** /**
@ -884,7 +908,8 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
@Override @Override
public WorldType getWorldType() { public WorldType getWorldType() {
// This variable is not settable in-game, therefore does not get a property. // This variable is not settable in-game, therefore does not get a property.
return world.get().getWorldType(); final World world = getCBWorld();
return world != null ? world.getWorldType() : null;
} }
/** /**

View File

@ -82,6 +82,8 @@ import com.onarandombox.MultiverseCore.utils.WorldManager;
import com.pneumaticraft.commandhandler.CommandHandler; import com.pneumaticraft.commandhandler.CommandHandler;
import me.main__.util.SerializationConfig.SerializationConfig; import me.main__.util.SerializationConfig.SerializationConfig;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -641,10 +643,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// migrate gamemode // migrate gamemode
if (section.isString("gamemode")) { if (section.isString("gamemode")) {
try { final GameMode gameMode = GameMode.valueOf(section.getString("gamemode").toUpperCase());
world.setPropertyValue("gamemode", section.getString("gamemode")); if (gameMode != null) {
} catch (PropertyDoesNotExistException e) { world.setGameMode(gameMode);
throw new RuntimeException("Who forgot to update the migrator?", e);
} }
} }
@ -729,6 +730,19 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
world.setSpawnLocation(spawnLoc); world.setSpawnLocation(spawnLoc);
} }
// migrate difficulty
if (section.isString("difficulty")) {
final Difficulty difficulty = Difficulty.valueOf(section.getString("difficulty").toUpperCase());
if (difficulty != null) {
world.setDifficulty(difficulty);
}
}
// migrate keepspawninmemory
if (section.isBoolean("keepspawninmemory")) {
world.setKeepSpawnInMemory(section.getBoolean("keepspawninmemory"));
}
newValues.put(entry.getKey(), world); newValues.put(entry.getKey(), world);
wasChanged = true; wasChanged = true;
} else { } else {

View File

@ -20,6 +20,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty; import org.bukkit.Difficulty;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -332,17 +333,18 @@ public class TestWorldProperties {
} }
public void createEvents(MultiverseWorld mvWorld) { public void createEvents(MultiverseWorld mvWorld) {
final World world = mvWorld.getCBWorld();
//// Weather events //// Weather events
// weather change // weather change
weatherChangeOffEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), false); weatherChangeOffEvent = new WeatherChangeEvent(world, false);
weatherChangeOnEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), true); weatherChangeOnEvent = new WeatherChangeEvent(world, true);
// thunder change // thunder change
thunderChangeOffEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), false); thunderChangeOffEvent = new ThunderChangeEvent(world, false);
thunderChangeOnEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), true); thunderChangeOnEvent = new ThunderChangeEvent(world, true);
//// Player events //// Player events
// player chat // player chat
mockPlayer = mock(Player.class); mockPlayer = mock(Player.class);
when(mockPlayer.getWorld()).thenReturn(mvWorld.getCBWorld()); when(mockPlayer.getWorld()).thenReturn(world);
when(mockPlayer.hasPlayedBefore()).thenReturn(true); when(mockPlayer.hasPlayedBefore()).thenReturn(true);
when(mockPlayer.hasPermission("multiverse.access.world")).thenReturn(true); when(mockPlayer.hasPermission("multiverse.access.world")).thenReturn(true);
when(mockPlayer.getName()).thenReturn("MultiverseMan"); when(mockPlayer.getName()).thenReturn("MultiverseMan");
@ -368,7 +370,7 @@ public class TestWorldProperties {
// entity regain health // entity regain health
entityRegainHealthEvent = PowerMockito.mock(EntityRegainHealthEvent.class); entityRegainHealthEvent = PowerMockito.mock(EntityRegainHealthEvent.class);
when(entityRegainHealthEvent.getRegainReason()).thenReturn(RegainReason.REGEN); when(entityRegainHealthEvent.getRegainReason()).thenReturn(RegainReason.REGEN);
when(mockHumanEntity.getLocation()).thenReturn(new Location(mvWorld.getCBWorld(), 0, 0, 0)); when(mockHumanEntity.getLocation()).thenReturn(new Location(world, 0, 0, 0));
when(entityRegainHealthEvent.getEntity()).thenReturn(mockHumanEntity); when(entityRegainHealthEvent.getEntity()).thenReturn(mockHumanEntity);
// entity food level change event // entity food level change event
entityFoodLevelChangeEvent = PowerMockito.mock(FoodLevelChangeEvent.class); entityFoodLevelChangeEvent = PowerMockito.mock(FoodLevelChangeEvent.class);