mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-28 05:35:44 +01:00
Implements async superflat cleaning
Should avoid crashes when there's a lot of cleaning to do. https://github.com/BentoBoxWorld/BentoBox/issues/431
This commit is contained in:
parent
e4e6525995
commit
fa8c0ec0c8
@ -3,17 +3,20 @@
|
|||||||
*/
|
*/
|
||||||
package world.bentobox.bentobox.listeners.flags;
|
package world.bentobox.bentobox.listeners.flags;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Set;
|
import java.util.Queue;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.World.Environment;
|
import org.bukkit.World.Environment;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.world.ChunkLoadEvent;
|
import org.bukkit.event.world.ChunkLoadEvent;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
|
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
|
||||||
import world.bentobox.bentobox.api.flags.FlagListener;
|
import world.bentobox.bentobox.api.flags.FlagListener;
|
||||||
import world.bentobox.bentobox.lists.Flags;
|
import world.bentobox.bentobox.lists.Flags;
|
||||||
import world.bentobox.bentobox.util.Pair;
|
import world.bentobox.bentobox.util.Pair;
|
||||||
@ -25,28 +28,41 @@ import world.bentobox.bentobox.util.Pair;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CleanSuperFlatListener extends FlagListener {
|
public class CleanSuperFlatListener extends FlagListener {
|
||||||
|
|
||||||
private Set<Pair<Integer, Integer>> regeneratedChunk = new HashSet<>();
|
private Queue<Pair<Integer, Integer>> chunkQ = new ArrayDeque<>();
|
||||||
|
private BukkitTask task;
|
||||||
|
private boolean ready;
|
||||||
|
|
||||||
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
|
public void onBentoBoxReady(BentoBoxReadyEvent e) {
|
||||||
|
ready = true;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
public void onChunkLoad(ChunkLoadEvent e) {
|
public void onChunkLoad(ChunkLoadEvent e) {
|
||||||
BentoBox plugin = BentoBox.getInstance();
|
if (!ready) {
|
||||||
World world = e.getWorld();
|
|
||||||
if (regeneratedChunk.contains(new Pair<Integer, Integer>(e.getChunk().getX(), e.getChunk().getZ()))) {
|
|
||||||
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
|
|
||||||
plugin.logError("World generator for " + world.getName() + " is broken and superflat regen cannot occur!!! Disabling regen.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
BentoBox plugin = BentoBox.getInstance();
|
||||||
|
World world = e.getWorld();
|
||||||
if (!e.getChunk().getBlock(0, 0, 0).getType().equals(Material.BEDROCK)
|
if (!e.getChunk().getBlock(0, 0, 0).getType().equals(Material.BEDROCK)
|
||||||
|| !Flags.CLEAN_SUPER_FLAT.isSetForWorld(world)
|
|| !Flags.CLEAN_SUPER_FLAT.isSetForWorld(world)
|
||||||
|| (world.getEnvironment().equals(Environment.NETHER) && (!plugin.getIWM().isNetherGenerate(world) || !plugin.getIWM().isNetherIslands(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)))) {
|
|| (world.getEnvironment().equals(Environment.THE_END) && (!plugin.getIWM().isEndGenerate(world) || !plugin.getIWM().isEndIslands(world)))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// This deprecation is OK because all it means is that things like tree leaves may not be the same in the chunk when it is generated
|
// Add to queue
|
||||||
world.regenerateChunk(e.getChunk().getX(), e.getChunk().getZ());
|
chunkQ.add(new Pair<>(e.getChunk().getX(), e.getChunk().getZ()));
|
||||||
plugin.logWarning("Regenerating superflat chunk in " + world.getName() + " at blocks " + (e.getChunk().getX() << 4) + "," + (e.getChunk().getZ() << 4));
|
if (task == null) {
|
||||||
|
task = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
|
||||||
|
if (!chunkQ.isEmpty()) {
|
||||||
|
Pair<Integer, Integer> chunkXZ = chunkQ.poll();
|
||||||
|
world.regenerateChunk(chunkXZ.x, chunkXZ.z);
|
||||||
|
plugin.logWarning(chunkQ.size() + " Regenerating superflat chunk " + world.getName() + " " + chunkXZ.x + ", " + chunkXZ.z);
|
||||||
|
}
|
||||||
|
}, 0L, 1L);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ import org.bukkit.block.Block;
|
|||||||
import org.bukkit.event.world.ChunkLoadEvent;
|
import org.bukkit.event.world.ChunkLoadEvent;
|
||||||
import org.bukkit.inventory.ItemFactory;
|
import org.bukkit.inventory.ItemFactory;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -28,6 +29,7 @@ import org.powermock.reflect.Whitebox;
|
|||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.configuration.WorldSettings;
|
import world.bentobox.bentobox.api.configuration.WorldSettings;
|
||||||
|
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
|
||||||
import world.bentobox.bentobox.lists.Flags;
|
import world.bentobox.bentobox.lists.Flags;
|
||||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||||
import world.bentobox.bentobox.util.Util;
|
import world.bentobox.bentobox.util.Util;
|
||||||
@ -44,6 +46,8 @@ public class CleanSuperFlatListenerTest {
|
|||||||
private Block block;
|
private Block block;
|
||||||
private Chunk chunk;
|
private Chunk chunk;
|
||||||
private IslandWorldManager iwm;
|
private IslandWorldManager iwm;
|
||||||
|
private CleanSuperFlatListener l;
|
||||||
|
private BukkitScheduler scheduler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws java.lang.Exception
|
* @throws java.lang.Exception
|
||||||
@ -91,84 +95,86 @@ public class CleanSuperFlatListenerTest {
|
|||||||
when(block.getType()).thenReturn(Material.BEDROCK);
|
when(block.getType()).thenReturn(Material.BEDROCK);
|
||||||
when(chunk.getBlock(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(block);
|
when(chunk.getBlock(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(block);
|
||||||
|
|
||||||
|
// Fire the ready event
|
||||||
|
l = new CleanSuperFlatListener();
|
||||||
|
l.onBentoBoxReady(mock(BentoBoxReadyEvent.class));
|
||||||
|
|
||||||
|
// Scheduler
|
||||||
|
scheduler = mock(BukkitScheduler.class);
|
||||||
|
when(Bukkit.getScheduler()).thenReturn(scheduler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnChunkLoadNotBedrockNoFlsg() {
|
public void testOnChunkLoadNotBedrockNoFlsg() {
|
||||||
when(block.getType()).thenReturn(Material.AIR);
|
when(block.getType()).thenReturn(Material.AIR);
|
||||||
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
|
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
|
||||||
|
|
||||||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
|
Mockito.verify(scheduler, Mockito.never()).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnChunkLoadBedrock() {
|
public void testOnChunkLoadBedrock() {
|
||||||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnChunkLoadBedrockNoClean() {
|
public void testOnChunkLoadBedrockNoClean() {
|
||||||
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
|
Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
|
||||||
|
|
||||||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
|
Mockito.verify(scheduler, Mockito.never()).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnChunkLoadBedrockNether() {
|
public void testOnChunkLoadBedrockNether() {
|
||||||
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
|
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
|
||||||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false);
|
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false);
|
||||||
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
|
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
|
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
|
||||||
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
|
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
* Test method for {@link world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnChunkLoadBedrockEnd() {
|
public void testOnChunkLoadBedrockEnd() {
|
||||||
when(world.getEnvironment()).thenReturn(World.Environment.THE_END);
|
when(world.getEnvironment()).thenReturn(World.Environment.THE_END);
|
||||||
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
ChunkLoadEvent e = new ChunkLoadEvent(chunk, false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt());
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false);
|
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false);
|
||||||
when(iwm.isEndIslands(Mockito.any())).thenReturn(true);
|
when(iwm.isEndIslands(Mockito.any())).thenReturn(true);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
|
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
|
||||||
when(iwm.isEndIslands(Mockito.any())).thenReturn(false);
|
when(iwm.isEndIslands(Mockito.any())).thenReturn(false);
|
||||||
new CleanSuperFlatListener().onChunkLoad(e);
|
l.onChunkLoad(e);
|
||||||
Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once
|
Mockito.verify(scheduler).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user