Added defensive code to avoid infinite loop

CleanSuperFlat can infinite loop if the generator isn't working.
This commit is contained in:
tastybento 2019-01-07 20:43:37 -08:00
parent dc8b672286
commit 8a63bf80f9
2 changed files with 20 additions and 0 deletions

View File

@ -3,6 +3,9 @@
*/
package world.bentobox.bentobox.listeners.flags;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
@ -13,6 +16,7 @@ import org.bukkit.event.world.ChunkLoadEvent;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Pair;
/**
* Cleans super-flat world chunks or normal nether chunks if they generate accidentally
@ -21,17 +25,26 @@ import world.bentobox.bentobox.lists.Flags;
*
*/
public class CleanSuperFlatListener extends FlagListener {
private Set<Pair<Integer, Integer>> regeneratedChunk = new HashSet<>();
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent e) {
BentoBox plugin = BentoBox.getInstance();
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;
}
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;
}
// 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
world.regenerateChunk(e.getChunk().getX(), e.getChunk().getZ());
plugin.logWarning("Regenerating superflat chunk in " + world.getName() + " at blocks " + (e.getChunk().getX() << 4) + "," + (e.getChunk().getZ() << 4));

View File

@ -1,6 +1,13 @@
package world.bentobox.bentobox.util;
/**
* Class to store pairs of objects, e.g. coordinates
* @author tastybento
*
* @param <X> the x part of the pair
* @param <Z> the z part of the pair
*/
public class Pair<X, Z> {
public final X x;
public final Z z;