-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added clean super-flat world setting and test
- Loading branch information
1 parent
4f9a68b
commit 451756b
Showing
5 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/us/tastybento/bskyblock/listeners/flags/CleanSuperFlatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* | ||
*/ | ||
package us.tastybento.bskyblock.listeners.flags; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.World; | ||
import org.bukkit.World.Environment; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.world.ChunkLoadEvent; | ||
|
||
import us.tastybento.bskyblock.BSkyBlock; | ||
import us.tastybento.bskyblock.api.flags.AbstractFlagListener; | ||
import us.tastybento.bskyblock.lists.Flags; | ||
|
||
/** | ||
* Cleans super-flat world chunks or normal nether chunks if they generate accidentally | ||
* due to lack of a generator being loaded | ||
* @author tastybento | ||
* | ||
*/ | ||
public class CleanSuperFlatListener extends AbstractFlagListener { | ||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) | ||
public void onChunkLoad(ChunkLoadEvent e) { | ||
BSkyBlock plugin = BSkyBlock.getInstance(); | ||
if (!plugin.isLoaded()) { | ||
return; | ||
} | ||
World world = e.getWorld(); | ||
if (!e.getChunk().getBlock(0, 0, 0).getType().equals(Material.BEDROCK) | ||
|| !Flags.CLEAN_SUPER_FLAT.isSetForWorld(world) | ||
|| (world.getEnvironment().equals(Environment.NETHER) && (!plugin.getIWM().isNetherGenerate(world) || !plugin.getIWM().isNetherIslands(world))) | ||
|| (world.getEnvironment().equals(Environment.THE_END) && (!plugin.getIWM().isEndGenerate(world) || !plugin.getIWM().isEndIslands(world)))) { | ||
return; | ||
} | ||
world.regenerateChunk(e.getChunk().getX(), e.getChunk().getZ()); | ||
plugin.logWarning("Regenerating superflat chunk at " + (e.getChunk().getX() * 16) + "," + (e.getChunk().getZ() * 16)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/test/java/us/tastybento/bskyblock/listeners/flags/CleanSuperFlatListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/** | ||
* | ||
*/ | ||
package us.tastybento.bskyblock.listeners.flags; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.Material; | ||
import org.bukkit.World; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.event.world.ChunkLoadEvent; | ||
import org.bukkit.inventory.ItemFactory; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.reflect.Whitebox; | ||
|
||
import us.tastybento.bskyblock.BSkyBlock; | ||
import us.tastybento.bskyblock.api.configuration.WorldSettings; | ||
import us.tastybento.bskyblock.lists.Flags; | ||
import us.tastybento.bskyblock.managers.IslandWorldManager; | ||
import us.tastybento.bskyblock.util.Util; | ||
|
||
/** | ||
* @author tastybento | ||
* | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Bukkit.class, BSkyBlock.class, Util.class }) | ||
public class CleanSuperFlatListenerTest { | ||
|
||
private World world; | ||
private Block block; | ||
private Chunk chunk; | ||
private IslandWorldManager iwm; | ||
private BSkyBlock plugin; | ||
|
||
/** | ||
* @throws java.lang.Exception | ||
*/ | ||
@Before | ||
public void setUp() throws Exception { | ||
|
||
// Set up plugin | ||
plugin = mock(BSkyBlock.class); | ||
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); | ||
|
||
when(plugin.isLoaded()).thenReturn(true); | ||
|
||
// World | ||
world = mock(World.class); | ||
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL); | ||
|
||
PowerMockito.mockStatic(Util.class); | ||
when(Util.getWorld(Mockito.any())).thenReturn(world); | ||
|
||
// World Settings | ||
iwm = mock(IslandWorldManager.class); | ||
when(plugin.getIWM()).thenReturn(iwm); | ||
WorldSettings ws = mock(WorldSettings.class); | ||
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); | ||
Map<String, Boolean> worldFlags = new HashMap<>(); | ||
when(ws.getWorldFlags()).thenReturn(worldFlags); | ||
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); | ||
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true); | ||
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); | ||
when(iwm.isEndIslands(Mockito.any())).thenReturn(true); | ||
|
||
|
||
PowerMockito.mockStatic(Bukkit.class); | ||
ItemFactory itemF = mock(ItemFactory.class); | ||
ItemMeta im = mock(ItemMeta.class); | ||
when(itemF.getItemMeta(Mockito.any())).thenReturn(im); | ||
when(Bukkit.getItemFactory()).thenReturn(itemF); | ||
|
||
Flags.CLEAN_SUPER_FLAT.setSetting(world, true); | ||
|
||
chunk = mock(Chunk.class); | ||
when(chunk.getWorld()).thenReturn(world); | ||
block = mock(Block.class); | ||
when(block.getType()).thenReturn(Material.BEDROCK); | ||
when(chunk.getBlock(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(block); | ||
|
||
} | ||
|
||
/** | ||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. | ||
*/ | ||
@Test | ||
public void testOnChunkLoadNotBedrockNoFlsg() { | ||
when(block.getType()).thenReturn(Material.AIR); | ||
Flags.CLEAN_SUPER_FLAT.setSetting(world, false); | ||
|
||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); | ||
} | ||
|
||
/** | ||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. | ||
*/ | ||
@Test | ||
public void testOnChunkLoadNotLoaded() { | ||
when(plugin.isLoaded()).thenReturn(false); | ||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); | ||
} | ||
|
||
/** | ||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. | ||
*/ | ||
@Test | ||
public void testOnChunkLoadBedrock() { | ||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); | ||
} | ||
|
||
/** | ||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. | ||
*/ | ||
@Test | ||
public void testOnChunkLoadBedrockNoClean() { | ||
Flags.CLEAN_SUPER_FLAT.setSetting(world, false); | ||
|
||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); | ||
} | ||
|
||
/** | ||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. | ||
*/ | ||
@Test | ||
public void testOnChunkLoadBedrockNether() { | ||
when(world.getEnvironment()).thenReturn(World.Environment.NETHER); | ||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); | ||
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false); | ||
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once | ||
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); | ||
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once | ||
} | ||
|
||
/** | ||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. | ||
*/ | ||
@Test | ||
public void testOnChunkLoadBedrockEnd() { | ||
when(world.getEnvironment()).thenReturn(World.Environment.THE_END); | ||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); | ||
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false); | ||
when(iwm.isEndIslands(Mockito.any())).thenReturn(true); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once | ||
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true); | ||
when(iwm.isEndIslands(Mockito.any())).thenReturn(false); | ||
new CleanSuperFlatListener().onChunkLoad(e); | ||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once | ||
} | ||
|
||
} |