Skip to content

Commit 451756b

Browse files
committed
Added clean super-flat world setting and test
1 parent 4f9a68b commit 451756b

File tree

5 files changed

+259
-3
lines changed

5 files changed

+259
-3
lines changed

locales/en-US.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ protection:
320320
description: "Toggle teleportation"
321321
name: "Chorus fruits"
322322
hint: "No teleporting"
323+
CLEAN_SUPER_FLAT:
324+
description: |
325+
Enable to clean any
326+
super-flat chunks in
327+
island worlds
328+
name: "Clean Super Flat"
323329
COLLECT_LAVA:
324330
description: |
325331
Toggle collecting lava

src/main/java/us/tastybento/bskyblock/BSkyBlock.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ public class BSkyBlock extends JavaPlugin {
6262

6363
private HeadGetter headGetter;
6464

65+
private boolean isLoaded;
66+
6567
@Override
6668
public void onEnable(){
69+
// Not loaded
70+
setLoaded(false);
6771
// Store the current millis time so we can tell how many ms it took for BSB to fully load.
6872
final long startMillis = System.currentTimeMillis();
6973

@@ -142,6 +146,7 @@ public void onEnable(){
142146
instance.log("Thanks for using our plugin !");
143147
instance.log("- Tastybento and Poslovitch, 2017-2018");
144148
instance.log("#############################################");
149+
instance.setLoaded(true);
145150
});
146151
});
147152
}
@@ -182,7 +187,9 @@ public void onDisable() {
182187
islandsManager.shutdown();
183188
}
184189
// Save settings
185-
new BSBConfig<>(this, Settings.class).saveConfigObject(settings);
190+
if (isEnabled()) {
191+
new BSBConfig<>(this, Settings.class).saveConfigObject(settings);
192+
}
186193
}
187194

188195
private void registerCustomCharts(){
@@ -339,4 +346,22 @@ public SchemsManager getSchemsManager() {
339346
}
340347

341348

349+
350+
/**
351+
* @return the isLoaded
352+
*/
353+
public boolean isLoaded() {
354+
return isLoaded;
355+
}
356+
357+
358+
359+
/**
360+
* @param isLoaded the isLoaded to set
361+
*/
362+
public void setLoaded(boolean isLoaded) {
363+
this.isLoaded = isLoaded;
364+
}
365+
366+
342367
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
*
3+
*/
4+
package us.tastybento.bskyblock.listeners.flags;
5+
6+
import org.bukkit.Material;
7+
import org.bukkit.World;
8+
import org.bukkit.World.Environment;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.EventPriority;
11+
import org.bukkit.event.world.ChunkLoadEvent;
12+
13+
import us.tastybento.bskyblock.BSkyBlock;
14+
import us.tastybento.bskyblock.api.flags.AbstractFlagListener;
15+
import us.tastybento.bskyblock.lists.Flags;
16+
17+
/**
18+
* Cleans super-flat world chunks or normal nether chunks if they generate accidentally
19+
* due to lack of a generator being loaded
20+
* @author tastybento
21+
*
22+
*/
23+
public class CleanSuperFlatListener extends AbstractFlagListener {
24+
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
25+
public void onChunkLoad(ChunkLoadEvent e) {
26+
BSkyBlock plugin = BSkyBlock.getInstance();
27+
if (!plugin.isLoaded()) {
28+
return;
29+
}
30+
World world = e.getWorld();
31+
if (!e.getChunk().getBlock(0, 0, 0).getType().equals(Material.BEDROCK)
32+
|| !Flags.CLEAN_SUPER_FLAT.isSetForWorld(world)
33+
|| (world.getEnvironment().equals(Environment.NETHER) && (!plugin.getIWM().isNetherGenerate(world) || !plugin.getIWM().isNetherIslands(world)))
34+
|| (world.getEnvironment().equals(Environment.THE_END) && (!plugin.getIWM().isEndGenerate(world) || !plugin.getIWM().isEndIslands(world)))) {
35+
return;
36+
}
37+
world.regenerateChunk(e.getChunk().getX(), e.getChunk().getZ());
38+
plugin.logWarning("Regenerating superflat chunk at " + (e.getChunk().getX() * 16) + "," + (e.getChunk().getZ() * 16));
39+
40+
}
41+
42+
}

src/main/java/us/tastybento/bskyblock/lists/Flags.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import us.tastybento.bskyblock.listeners.flags.BreakBlocksListener;
1717
import us.tastybento.bskyblock.listeners.flags.BreedingListener;
1818
import us.tastybento.bskyblock.listeners.flags.BucketListener;
19+
import us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener;
1920
import us.tastybento.bskyblock.listeners.flags.EggListener;
2021
import us.tastybento.bskyblock.listeners.flags.EnderChestListener;
2122
import us.tastybento.bskyblock.listeners.flags.EnterExitListener;
@@ -52,7 +53,7 @@ private Flags() {}
5253
// TODO: add KEEP_INVENTORY - is it needed?
5354

5455
public static final Flag BREAK_BLOCKS = new FlagBuilder().id("BREAK_BLOCKS").icon(Material.STONE).listener(new BreakBlocksListener()).build();
55-
public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.BEDROCK).listener(new PlaceBlocksListener()).build();
56+
public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.GRASS).listener(new PlaceBlocksListener()).build();
5657

5758
// Block interactions - all use BlockInteractionListener()
5859
public static final Flag ANVIL = new FlagBuilder().id("ANVIL").icon(Material.ANVIL).listener(new BlockInteractionListener()).build();
@@ -180,7 +181,8 @@ private Flags() {}
180181
.listener(new IslandRespawnListener()).allowedByDefault(true).onClick(new WorldToggleClickListener("ISLAND_RESPAWN")).build();
181182
public static final Flag OFFLINE_REDSTONE = new FlagBuilder().id("OFFLINE_REDSTONE").icon(Material.REDSTONE_COMPARATOR).type(Type.WORLD_SETTING)
182183
.listener(new OfflineRedstoneListener()).allowedByDefault(true).onClick(new WorldToggleClickListener("OFFLINE_REDSTONE")).build();
183-
184+
public static final Flag CLEAN_SUPER_FLAT = new FlagBuilder().id("CLEAN_SUPER_FLAT").icon(Material.BEDROCK).type(Type.WORLD_SETTING)
185+
.listener(new CleanSuperFlatListener()).allowedByDefault(false).onClick(new WorldToggleClickListener("CLEAN_SUPER_FLAT")).build();
184186
/**
185187
* @return List of all the flags in this class
186188
*/
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
*
3+
*/
4+
package us.tastybento.bskyblock.listeners.flags;
5+
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import org.bukkit.Bukkit;
13+
import org.bukkit.Chunk;
14+
import org.bukkit.Material;
15+
import org.bukkit.World;
16+
import org.bukkit.block.Block;
17+
import org.bukkit.event.world.ChunkLoadEvent;
18+
import org.bukkit.inventory.ItemFactory;
19+
import org.bukkit.inventory.meta.ItemMeta;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mockito;
24+
import org.powermock.api.mockito.PowerMockito;
25+
import org.powermock.core.classloader.annotations.PrepareForTest;
26+
import org.powermock.modules.junit4.PowerMockRunner;
27+
import org.powermock.reflect.Whitebox;
28+
29+
import us.tastybento.bskyblock.BSkyBlock;
30+
import us.tastybento.bskyblock.api.configuration.WorldSettings;
31+
import us.tastybento.bskyblock.lists.Flags;
32+
import us.tastybento.bskyblock.managers.IslandWorldManager;
33+
import us.tastybento.bskyblock.util.Util;
34+
35+
/**
36+
* @author tastybento
37+
*
38+
*/
39+
@RunWith(PowerMockRunner.class)
40+
@PrepareForTest({Bukkit.class, BSkyBlock.class, Util.class })
41+
public class CleanSuperFlatListenerTest {
42+
43+
private World world;
44+
private Block block;
45+
private Chunk chunk;
46+
private IslandWorldManager iwm;
47+
private BSkyBlock plugin;
48+
49+
/**
50+
* @throws java.lang.Exception
51+
*/
52+
@Before
53+
public void setUp() throws Exception {
54+
55+
// Set up plugin
56+
plugin = mock(BSkyBlock.class);
57+
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
58+
59+
when(plugin.isLoaded()).thenReturn(true);
60+
61+
// World
62+
world = mock(World.class);
63+
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
64+
65+
PowerMockito.mockStatic(Util.class);
66+
when(Util.getWorld(Mockito.any())).thenReturn(world);
67+
68+
// World Settings
69+
iwm = mock(IslandWorldManager.class);
70+
when(plugin.getIWM()).thenReturn(iwm);
71+
WorldSettings ws = mock(WorldSettings.class);
72+
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
73+
Map<String, Boolean> worldFlags = new HashMap<>();
74+
when(ws.getWorldFlags()).thenReturn(worldFlags);
75+
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
76+
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
77+
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
78+
when(iwm.isEndIslands(Mockito.any())).thenReturn(true);
79+
80+
81+
PowerMockito.mockStatic(Bukkit.class);
82+
ItemFactory itemF = mock(ItemFactory.class);
83+
ItemMeta im = mock(ItemMeta.class);
84+
when(itemF.getItemMeta(Mockito.any())).thenReturn(im);
85+
when(Bukkit.getItemFactory()).thenReturn(itemF);
86+
87+
Flags.CLEAN_SUPER_FLAT.setSetting(world, true);
88+
89+
chunk = mock(Chunk.class);
90+
when(chunk.getWorld()).thenReturn(world);
91+
block = mock(Block.class);
92+
when(block.getType()).thenReturn(Material.BEDROCK);
93+
when(chunk.getBlock(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(block);
94+
95+
}
96+
97+
/**
98+
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
99+
*/
100+
@Test
101+
public void testOnChunkLoadNotBedrockNoFlsg() {
102+
when(block.getType()).thenReturn(Material.AIR);
103+
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
104+
105+
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
106+
new CleanSuperFlatListener().onChunkLoad(e);
107+
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
108+
}
109+
110+
/**
111+
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
112+
*/
113+
@Test
114+
public void testOnChunkLoadNotLoaded() {
115+
when(plugin.isLoaded()).thenReturn(false);
116+
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
117+
new CleanSuperFlatListener().onChunkLoad(e);
118+
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
119+
}
120+
121+
/**
122+
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
123+
*/
124+
@Test
125+
public void testOnChunkLoadBedrock() {
126+
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
127+
new CleanSuperFlatListener().onChunkLoad(e);
128+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
129+
}
130+
131+
/**
132+
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
133+
*/
134+
@Test
135+
public void testOnChunkLoadBedrockNoClean() {
136+
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
137+
138+
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
139+
new CleanSuperFlatListener().onChunkLoad(e);
140+
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
141+
}
142+
143+
/**
144+
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
145+
*/
146+
@Test
147+
public void testOnChunkLoadBedrockNether() {
148+
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
149+
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
150+
new CleanSuperFlatListener().onChunkLoad(e);
151+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
152+
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false);
153+
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
154+
new CleanSuperFlatListener().onChunkLoad(e);
155+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
156+
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
157+
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
158+
new CleanSuperFlatListener().onChunkLoad(e);
159+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
160+
}
161+
162+
/**
163+
* Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
164+
*/
165+
@Test
166+
public void testOnChunkLoadBedrockEnd() {
167+
when(world.getEnvironment()).thenReturn(World.Environment.THE_END);
168+
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
169+
new CleanSuperFlatListener().onChunkLoad(e);
170+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
171+
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false);
172+
when(iwm.isEndIslands(Mockito.any())).thenReturn(true);
173+
new CleanSuperFlatListener().onChunkLoad(e);
174+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
175+
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
176+
when(iwm.isEndIslands(Mockito.any())).thenReturn(false);
177+
new CleanSuperFlatListener().onChunkLoad(e);
178+
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
179+
}
180+
181+
}

0 commit comments

Comments
 (0)