Skip to content

Commit

Permalink
Handles island deletion gracefully if use-own-generator is set to true.
Browse files Browse the repository at this point in the history
Also handles situations where islands are pending deletion in the
database but the world they refer to has gone.

#797
  • Loading branch information
tastybento committed Jun 28, 2019
1 parent 006305f commit a6dd175
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ public Optional<CompositeCommand> getAdminCommand() {
* Defines the world generator for this game mode
* @param worldName - name of world that this applies to
* @param id - id if any
* @return Chunk generator
* @return Chunk generator or null if one does not exist, e.g. the use own generator setting is true
* @since 1.2.0
*/
@NonNull
@Nullable
public abstract ChunkGenerator getDefaultWorldGenerator(String worldName, String id);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.bentobox.managers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -46,14 +47,23 @@ public IslandDeletionManager(BentoBox plugin) {
public void onBentoBoxReady(BentoBoxReadyEvent e) {
// Load list of islands that were mid deletion and delete them
List<IslandDeletion> toBeDeleted = handler.loadObjects();
List<IslandDeletion> toBeRemoved = new ArrayList<>();
if (!toBeDeleted.isEmpty()) {
plugin.log("There are " + toBeDeleted.size() + " islands pending deletion.");
toBeDeleted.forEach(di -> {
plugin.log("Resuming deletion of island at " + di.getLocation().getWorld().getName() + " " + Util.xyz(di.getLocation().toVector()));
inDeletion.add(di.getLocation());
new DeleteIslandChunks(plugin, di);
if (di.getLocation() == null || di.getLocation().getWorld() == null) {
plugin.logError("Island queued for deletion refers to a non-existant game world. Skipping...");
toBeRemoved.add(di);
} else {
plugin.log("Resuming deletion of island at " + di.getLocation().getWorld().getName() + " " + Util.xyz(di.getLocation().toVector()));
inDeletion.add(di.getLocation());
new DeleteIslandChunks(plugin, di);
}
});
}
// Remove the islands from the database so they don't come back
toBeRemoved.forEach(i -> plugin.logDebug("Island - " + i.toString()));
toBeRemoved.forEach(handler::deleteObject);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/world/bentobox/bentobox/util/DeleteIslandChunks.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import org.bukkit.generator.ChunkGenerator.ChunkData;
import org.bukkit.inventory.InventoryHolder;
Expand Down Expand Up @@ -81,15 +82,19 @@ private void regerateChunk(GameModeAddon gm, Chunk chunk) {
.forEach(te -> ((InventoryHolder)te).getInventory().clear());
// Reset blocks
MyBiomeGrid grid = new MyBiomeGrid(chunk.getWorld().getEnvironment());
ChunkData cd = gm.getDefaultWorldGenerator(chunk.getWorld().getName(), "").generateChunkData(chunk.getWorld(), new Random(), chunk.getX(), chunk.getZ(), grid);
int baseX = chunk.getX() << 4;
int baseZ = chunk.getZ() << 4;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
if (di.inBounds(baseX + x, baseZ + z)) {
chunk.getBlock(x, 0, z).setBiome(grid.getBiome(x, z));
for (int y = 0; y < chunk.getWorld().getMaxHeight(); y++) {
chunk.getBlock(x, y, z).setBlockData(cd.getBlockData(x, y, z));
ChunkGenerator cg = gm.getDefaultWorldGenerator(chunk.getWorld().getName(), "");
// Will be null if use-own-generator is set to true
if (cg != null) {
ChunkData cd = cg.generateChunkData(chunk.getWorld(), new Random(), chunk.getX(), chunk.getZ(), grid);
int baseX = chunk.getX() << 4;
int baseZ = chunk.getZ() << 4;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
if (di.inBounds(baseX + x, baseZ + z)) {
chunk.getBlock(x, 0, z).setBiome(grid.getBiome(x, z));
for (int y = 0; y < chunk.getWorld().getMaxHeight(); y++) {
chunk.getBlock(x, y, z).setBlockData(cd.getBlockData(x, y, z));
}
}
}
}
Expand Down

0 comments on commit a6dd175

Please sign in to comment.