From f210851294e645d72409596afe65638b2b0feb89 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 5 Nov 2012 13:02:03 -0500 Subject: [PATCH] Changed how we deal with CB world reference. Partially fixes #923. --- .../onarandombox/MultiverseCore/MVWorld.java | 73 +++++++++++++------ .../MultiverseCore/MultiverseCore.java | 22 +++++- .../test/TestWorldProperties.java | 14 ++-- 3 files changed, 75 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index b19e83dc..ece2cc00 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -42,8 +42,6 @@ import org.bukkit.permissions.PermissionDefault; import org.bukkit.util.Vector; import org.json.simple.JSONObject; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -92,7 +90,6 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private MultiverseCore plugin; // Hold the Plugin Instance. - private volatile Reference world = new WeakReference(null); // A reference to the World Instance. 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, MVWorld object) throws ChangeDeniedException { if (!newValue) { - world.get().setStorm(false); - world.get().setThundering(false); + final World world = getCBWorld(); + if (world != null) { + world.setStorm(false); + world.setThundering(false); + } } return super.validateChange(property, newValue, oldValue, object); } @@ -237,13 +237,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { } else { allowMonsters = true; } - if (MVWorld.this.spawning.getAnimalSettings().getSpawnRate() != -1) { - world.get().setTicksPerAnimalSpawns(MVWorld.this.spawning.getAnimalSettings().getSpawnRate()); + final World world = getCBWorld(); + 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); return super.validateChange(property, newValue, oldValue, object); } @@ -375,12 +378,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private volatile VirtualProperty pvp = new VirtualProperty() { @Override public void set(Boolean newValue) { - world.get().setPVP(newValue); + final World world = getCBWorld(); + if (world != null) { + world.setPVP(newValue); + } } @Override 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") @@ -394,12 +401,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private volatile VirtualProperty difficulty = new VirtualProperty() { @Override public void set(Difficulty newValue) { - world.get().setDifficulty(newValue); + final World world = getCBWorld(); + if (world != null) { + world.setDifficulty(newValue); + } } @Override 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.") @@ -421,12 +432,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private volatile VirtualProperty keepSpawnInMemory = new VirtualProperty() { @Override public void set(Boolean newValue) { - world.get().setKeepSpawnInMemory(newValue); + final World world = getCBWorld(); + if (world != null) { + world.setKeepSpawnInMemory(newValue); + } } @Override public Boolean get() { - return world.get().getKeepSpawnInMemory(); + final World world = getCBWorld(); + return world != null ? world.getKeepSpawnInMemory() : null; } }; @Property @@ -461,12 +476,16 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private volatile VirtualProperty time = new VirtualProperty() { @Override public void set(Long newValue) { - world.get().setTime(newValue); + final World world = getCBWorld(); + if (world != null) { + world.setTime(newValue); + } } @Override public Long get() { - return world.get().getTime(); + final World world = getCBWorld(); + return world != null ? world.getTime() : null; } }; @Property @@ -554,8 +573,6 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { public void init(World cbWorld, MultiverseCore thePlugin) { this.plugin = thePlugin; - // Weak reference so the CB-World can be unloaded even if this object still exists! - this.world = new WeakReference(cbWorld); this.environment = cbWorld.getEnvironment(); this.seed = cbWorld.getSeed(); this.name = cbWorld.getName(); @@ -670,7 +687,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { this.adjustSpawn = true; this.portalForm = AllowedPortalType.ALL; 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.bedRespawn = true; this.worldBlacklist = new ArrayList(); @@ -725,7 +742,14 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { */ @Override 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 public WorldType getWorldType() { // 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; } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 810bdf58..13bed049 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -82,6 +82,8 @@ import com.onarandombox.MultiverseCore.utils.WorldManager; import com.pneumaticraft.commandhandler.CommandHandler; import me.main__.util.SerializationConfig.SerializationConfig; import org.bukkit.ChatColor; +import org.bukkit.Difficulty; +import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.World.Environment; import org.bukkit.command.Command; @@ -641,10 +643,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // migrate gamemode if (section.isString("gamemode")) { - try { - world.setPropertyValue("gamemode", section.getString("gamemode")); - } catch (PropertyDoesNotExistException e) { - throw new RuntimeException("Who forgot to update the migrator?", e); + final GameMode gameMode = GameMode.valueOf(section.getString("gamemode").toUpperCase()); + if (gameMode != null) { + world.setGameMode(gameMode); } } @@ -729,6 +730,19 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { 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); wasChanged = true; } else { diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java index 0db2d5f1..ca75a04b 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java @@ -20,6 +20,7 @@ import org.bukkit.ChatColor; import org.bukkit.Difficulty; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; @@ -332,17 +333,18 @@ public class TestWorldProperties { } public void createEvents(MultiverseWorld mvWorld) { + final World world = mvWorld.getCBWorld(); //// Weather events // weather change - weatherChangeOffEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), false); - weatherChangeOnEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), true); + weatherChangeOffEvent = new WeatherChangeEvent(world, false); + weatherChangeOnEvent = new WeatherChangeEvent(world, true); // thunder change - thunderChangeOffEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), false); - thunderChangeOnEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), true); + thunderChangeOffEvent = new ThunderChangeEvent(world, false); + thunderChangeOnEvent = new ThunderChangeEvent(world, true); //// Player events // player chat mockPlayer = mock(Player.class); - when(mockPlayer.getWorld()).thenReturn(mvWorld.getCBWorld()); + when(mockPlayer.getWorld()).thenReturn(world); when(mockPlayer.hasPlayedBefore()).thenReturn(true); when(mockPlayer.hasPermission("multiverse.access.world")).thenReturn(true); when(mockPlayer.getName()).thenReturn("MultiverseMan"); @@ -368,7 +370,7 @@ public class TestWorldProperties { // entity regain health entityRegainHealthEvent = PowerMockito.mock(EntityRegainHealthEvent.class); 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); // entity food level change event entityFoodLevelChangeEvent = PowerMockito.mock(FoodLevelChangeEvent.class);