diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index a4c38521..a7806c34 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -1011,7 +1011,7 @@ public class MVWorld implements MultiverseWorld { this.plugin.log(Level.INFO, "New Spawn for '" + this.getName() + "' is Located at: " + LocationManipulation.locationToString(configLocation)); } else { // If it's a standard end world, let's check in a better place: - Location newerSpawn = null; + Location newerSpawn; newerSpawn = bs.getTopBlock(new Location(w, 0, 0, 0)); if (newerSpawn != null) { this.setSpawnLocation(newerSpawn); diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java index 60b47cdb..4bbd221a 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java @@ -1,3 +1,10 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2012. * + * Multiverse 2 is licensed under the BSD License. * + * For more information please check the README.md file included * + * with this project. * + ******************************************************************************/ + package com.onarandombox.MultiverseCore.test; import static org.junit.Assert.*; @@ -99,7 +106,7 @@ public class TestWorldProperties { assertTrue(mvWorld.canAnimalsSpawn()); assertTrue(mvWorld.canMonstersSpawn()); assertEquals(-1, mvWorld.getCurrency()); - assertEquals((Object) 0D, (Object) mvWorld.getPrice()); + assertEquals(0, mvWorld.getPrice(), 0); assertTrue(mvWorld.getHunger()); assertTrue(mvWorld.getAutoHeal()); assertTrue(mvWorld.getAdjustSpawn()); @@ -107,7 +114,7 @@ public class TestWorldProperties { assertTrue(mvWorld.isKeepingSpawnInMemory()); assertTrue(mvWorld.getBedRespawn()); assertTrue(mvWorld.getAutoLoad()); - assertEquals(new Location(mvWorld.getCBWorld(), 0, 0, 0), mvWorld.getSpawnLocation()); + assertEquals(new Location(mvWorld.getCBWorld(), 0, 64, 0), mvWorld.getSpawnLocation()); /* ****************************************** * * Call some events and verify behavior diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java index 707776c0..a2117a61 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java @@ -167,6 +167,40 @@ public class TestWorldStuff { verify(mockServer).createWorld(Matchers.argThat(matcher)); } + @Test + public void testNullWorld() { + // Pull a core instance from the server. + Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); + + // Make sure Core is not null + assertNotNull(plugin); + + // Make sure Core is enabled + assertTrue(plugin.isEnabled()); + + // Initialize a fake command + Command mockCommand = mock(Command.class); + when(mockCommand.getName()).thenReturn("mv"); + + // Ensure that there are no worlds imported. This is a fresh setup. + assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); + + // Create the NULL world + // The safe check is now BALLS SLOW. Use the -n to skip checking. + String[] normalArgs = new String[]{ "create", "nullworld", "normal", "-n" }; + plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); + + // We should now have one world! + assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size()); + + // Verify + verify(mockCommandSender).sendMessage("Starting creation of world 'nullworld'..."); + verify(mockCommandSender).sendMessage("Complete!"); + + WorldCreatorMatcher matcher = new WorldCreatorMatcher(new WorldCreator("nullworld")); + verify(mockServer).createWorld(Matchers.argThat(matcher)); + } + @Test public void testModifyGameMode() { // Pull a core instance from the server. diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java index 1f19e180..c0f22b03 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java @@ -40,7 +40,50 @@ public class MockWorldFactory { when(mockWorld.getName()).thenReturn(world); when(mockWorld.getEnvironment()).thenReturn(env); when(mockWorld.getWorldType()).thenReturn(type); - when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 0, 0)); + when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0)); + when(mockWorld.getWorldFolder()).thenAnswer(new Answer() { + public File answer(InvocationOnMock invocation) throws Throwable { + if (!(invocation.getMock() instanceof World)) + return null; + + World thiss = (World) invocation.getMock(); + return new File(TestInstanceCreator.serverDirectory, thiss.getName()); + } + }); + when(mockWorld.getBlockAt(any(Location.class))).thenAnswer(new Answer() { + public Block answer(InvocationOnMock invocation) throws Throwable { + Location loc; + try { + loc = (Location) invocation.getArguments()[0]; + } catch (Exception e) { + return null; + } + Material blockType = Material.AIR; + Block mockBlock = mock(Block.class); + if (loc.getBlockY() < 64) { + blockType = Material.DIRT; + } + + when(mockBlock.getType()).thenReturn(blockType); + when(mockBlock.getTypeId()).thenReturn(blockType.getId()); + when(mockBlock.getWorld()).thenReturn(loc.getWorld()); + when(mockBlock.getX()).thenReturn(loc.getBlockX()); + when(mockBlock.getY()).thenReturn(loc.getBlockY()); + when(mockBlock.getZ()).thenReturn(loc.getBlockZ()); + when(mockBlock.getLocation()).thenReturn(loc); + when(mockBlock.isEmpty()).thenReturn(blockType == Material.AIR); + return mockBlock; + } + }); + return mockWorld; + } + + private static World nullWorld(String world, World.Environment env, WorldType type) { + World mockWorld = mock(World.class); + when(mockWorld.getName()).thenReturn(world); + when(mockWorld.getEnvironment()).thenReturn(env); + when(mockWorld.getWorldType()).thenReturn(type); + when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0)); when(mockWorld.getWorldFolder()).thenAnswer(new Answer() { public File answer(InvocationOnMock invocation) throws Throwable { if (!(invocation.getMock() instanceof World)) @@ -60,7 +103,7 @@ public class MockWorldFactory { } Block mockBlock = mock(Block.class); - Material blockType = Material.AIR; // TODO we might use other materials, too + Material blockType = Material.AIR; when(mockBlock.getType()).thenReturn(blockType); when(mockBlock.getTypeId()).thenReturn(blockType.getId()); @@ -82,6 +125,12 @@ public class MockWorldFactory { return w; } + public static World makeNewNullMockWorld(String world, World.Environment env, WorldType type) { + World w = nullWorld(world, env, type); + registerWorld(w); + return w; + } + public static World makeNewMockWorld(String world, World.Environment env, WorldType type, long seed, ChunkGenerator generator) { World mockWorld = basics(world, env, type); diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java index c44ee0fe..716b5163 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java @@ -127,6 +127,11 @@ public class TestInstanceCreator { } catch (Exception e) { return null; } + // Add special case for creating null worlds. + // Not sure I like doing it this way, but this is a special case + if (arg.name().equalsIgnoreCase("nullworld")) { + return MockWorldFactory.makeNewNullMockWorld(arg.name(), arg.environment(), arg.type()); + } return MockWorldFactory.makeNewMockWorld(arg.name(), arg.environment(), arg.type()); } });