Fixes super-flat clearing

Superflat was not working properly. It was always clearing just 1 (first) chunk because it referenced the chunk from the first event that created the clearing task.
The issue should be fixed.
This commit is contained in:
BONNe 2022-11-01 23:04:59 +02:00 committed by GitHub
parent d9ad9f158b
commit acb984ee90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk;
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;
@ -61,51 +62,82 @@ public class CleanSuperFlatListener extends FlagListener {
} }
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent e) { public void onChunkLoad(ChunkLoadEvent e)
{
World world = e.getWorld(); World world = e.getWorld();
if (noClean(world, e)) {
if (this.noClean(world, e))
{
return; return;
} }
MyBiomeGrid grid = new MyBiomeGrid(world.getEnvironment()); MyBiomeGrid grid = new MyBiomeGrid(world.getEnvironment());
ChunkGenerator cg = plugin.getAddonsManager().getDefaultWorldGenerator(world.getName(), ""); ChunkGenerator cg = plugin.getAddonsManager().getDefaultWorldGenerator(world.getName(), "");
if (cg == null) {
if (cg == null)
{
Flags.CLEAN_SUPER_FLAT.setSetting(world, false); Flags.CLEAN_SUPER_FLAT.setSetting(world, false);
plugin.logWarning("Could not enable Clean Super Flat for " + world.getName()); this.plugin.logWarning("Could not enable Clean Super Flat for " + world.getName());
plugin.logWarning("There is no world generator assigned to this world."); this.plugin.logWarning("There is no world generator assigned to this world.");
plugin.logWarning("This is often caused by the 'use-own-generator' being set to 'true' in the gamemode's" + this.plugin.logWarning("This is often caused by the 'use-own-generator' being set to 'true' in the gamemode's" +
" configuration while there hasn't been any custom world generator assigned to the world."); " configuration while there hasn't been any custom world generator assigned to the world.");
plugin.logWarning("Either revert the changes in the gamemode's config.yml or assign your custom world generator to the world."); this.plugin.logWarning("Either revert the changes in the gamemode's config.yml or assign your custom world generator to the world.");
return; return;
} }
// Add to queue // Add to queue
chunkQueue.add(new Pair<>(e.getChunk().getX(), e.getChunk().getZ())); this.chunkQueue.add(new Pair<>(e.getChunk().getX(), e.getChunk().getZ()));
if (task == null || task.isCancelled()) {
task = Bukkit.getScheduler().runTaskTimer(plugin, () -> cleanChunk(e, world, cg, grid), 0L, 1L); if (this.task == null || this.task.isCancelled())
{
this.task = Bukkit.getScheduler().runTaskTimer(this.plugin, () -> this.cleanChunk(world, cg, grid), 0L, 1L);
} }
} }
private void cleanChunk(ChunkLoadEvent e, World world, ChunkGenerator cg, MyBiomeGrid grid) {
/**
* This method clears the chunk from queue in the given world
* @param world The world that must be cleared.
* @param cg Chunk generator.
* @param grid Biome Grid.
*/
private void cleanChunk(World world, ChunkGenerator cg, MyBiomeGrid grid)
{
SecureRandom random = new SecureRandom(); SecureRandom random = new SecureRandom();
if (!chunkQueue.isEmpty()) {
Pair<Integer, Integer> chunkXZ = chunkQueue.poll(); if (!this.chunkQueue.isEmpty())
ChunkData cd = cg.generateChunkData(world, random, e.getChunk().getX(), e.getChunk().getZ(), grid); {
for (int x = 0; x < 16; x++) { Pair<Integer, Integer> chunkXZ = this.chunkQueue.poll();
for (int z = 0; z < 16; z++) {
for (int y = 0; y < world.getMaxHeight(); y++) { ChunkData cd = cg.generateChunkData(world, random, chunkXZ.getKey(), chunkXZ.getValue(), grid);
e.getChunk().getBlock(x, y, z).setBlockData(cd.getBlockData(x, y, z), false); Chunk chunk = world.getChunkAt(chunkXZ.getKey(), chunkXZ.getValue());
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
for (int y = world.getMinHeight(); y < world.getMaxHeight(); y++)
{
chunk.getBlock(x, y, z).setBlockData(cd.getBlockData(x, y, z), false);
} }
} }
} }
// Run populators // Run populators
cg.getDefaultPopulators(world).forEach(pop -> pop.populate(world, random, e.getChunk())); cg.getDefaultPopulators(world).forEach(pop -> pop.populate(world, random, chunk));
if (plugin.getSettings().isLogCleanSuperFlatChunks()) {
plugin.log("Regenerating superflat chunk in " + world.getName() + " at (" + chunkXZ.x + ", " + chunkXZ.z + ") " + if (this.plugin.getSettings().isLogCleanSuperFlatChunks())
"(" + chunkQueue.size() + " chunk(s) remaining in the queue)"); {
this.plugin.log("Regenerating superflat chunk in " + world.getName() +
" at (" + chunkXZ.x + ", " + chunkXZ.z + ") " +
"(" + this.chunkQueue.size() + " chunk(s) remaining in the queue)");
} }
} else { }
task.cancel(); else
{
this.task.cancel();
} }
} }
@ -125,9 +157,9 @@ public class CleanSuperFlatListener extends FlagListener {
if (!this.getIWM().inWorld(world) || if (!this.getIWM().inWorld(world) ||
!Flags.CLEAN_SUPER_FLAT.isSetForWorld(world) || !Flags.CLEAN_SUPER_FLAT.isSetForWorld(world) ||
world.getEnvironment().equals(Environment.NETHER) && world.getEnvironment().equals(Environment.NETHER) &&
(!plugin.getIWM().isNetherGenerate(world) || !plugin.getIWM().isNetherIslands(world)) || (!this.plugin.getIWM().isNetherGenerate(world) || !this.plugin.getIWM().isNetherIslands(world)) ||
world.getEnvironment().equals(Environment.THE_END) && world.getEnvironment().equals(Environment.THE_END) &&
(!plugin.getIWM().isEndGenerate(world) || !plugin.getIWM().isEndIslands(world))) (!this.plugin.getIWM().isEndGenerate(world) || !this.plugin.getIWM().isEndIslands(world)))
{ {
return true; return true;
} }