Skip to content

Commit

Permalink
Rework the chunk deletion (#1897)
Browse files Browse the repository at this point in the history
* delete island one by one

* register before IslandDeletionManager

* optimize imports

* setting

* just some indents

* config

* run synchronously

* a bit reformat before recoding

* proper delete chunks

* comment

* combine the task call

* expose the NMS Handler

* don't have to try-catch this

* we know that this is final

* expose copy chunk data so that it can be overridden

* Don't have to use Vector

* set block from minimum height

* remove NMS and use fallback if not set

* only get the height once

* fix test
  • Loading branch information
HSGamer committed Jan 2, 2022
1 parent c9c9ea0 commit ce1d8e5
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 140 deletions.
13 changes: 8 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@
<id>maven-snapshots</id>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
</repository>

<repository>
<id>minecraft-repo</id>
<url>https://libraries.minecraft.net/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -190,11 +193,11 @@
<version>${paper.version}</version>
<scope>provided</scope>
</dependency>
<!-- Spigot NMS. Used for Head Getter and chunk deletion. -->
<!-- AuthLib. Used for Head Getter. -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>${spigot.version}</version>
<groupId>com.mojang</groupId>
<artifactId>authlib</artifactId>
<version>3.2.38</version>
<scope>provided</scope>
</dependency>
<!-- Metrics -->
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@
import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.listeners.PortalTeleportationListener;
import world.bentobox.bentobox.listeners.StandardSpawnProtectionListener;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.BlueprintsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.HooksManager;
import world.bentobox.bentobox.managers.IslandDeletionManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.WebManager;
import world.bentobox.bentobox.managers.*;
import world.bentobox.bentobox.util.heads.HeadGetter;
import world.bentobox.bentobox.versions.ServerCompatibility;

Expand All @@ -69,6 +57,7 @@ public class BentoBox extends JavaPlugin {
private HooksManager hooksManager;
private PlaceholdersManager placeholdersManager;
private IslandDeletionManager islandDeletionManager;
private IslandChunkDeletionManager islandChunkDeletionManager;
private WebManager webManager;

// Settings
Expand Down Expand Up @@ -294,6 +283,7 @@ private void registerListeners() {
// Death counter
manager.registerEvents(new DeathListener(this), this);
// Island Delete Manager
islandChunkDeletionManager = new IslandChunkDeletionManager(this);
islandDeletionManager = new IslandDeletionManager(this);
manager.registerEvents(islandDeletionManager, this);
}
Expand Down Expand Up @@ -523,6 +513,13 @@ public IslandDeletionManager getIslandDeletionManager() {
return islandDeletionManager;
}

/**
* @return the islandChunkDeletionManager
*/
public IslandChunkDeletionManager getIslandChunkDeletionManager() {
return islandChunkDeletionManager;
}

/**
* @return an optional of the Bstats instance
* @since 1.1
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/world/bentobox/bentobox/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ public class Settings implements ConfigObject {
@ConfigEntry(path = "island.deletion.keep-previous-island-on-reset", since = "1.13.0")
private boolean keepPreviousIslandOnReset = false;

@ConfigComment("Toggles how the islands are deleted.")
@ConfigComment("* If set to 'false', all islands will be deleted at once.")
@ConfigComment(" This is fast but may cause an impact on the performance")
@ConfigComment(" as it'll load all the chunks of the in-deletion islands.")
@ConfigComment("* If set to 'true', the islands will be deleted one by one.")
@ConfigComment(" This is slower but will not cause any impact on the performance.")
@ConfigEntry(path = "island.deletion.slow-deletion", since = "1.19.1")
private boolean slowDeletion = false;

@ConfigComment("By default, If the destination is not safe, the plugin will try to search for a safe spot around the destination,")
@ConfigComment("then it will try to expand the y-coordinate up and down from the destination.")
@ConfigComment("This setting limits how far the y-coordinate will be expanded.")
Expand Down Expand Up @@ -905,4 +914,12 @@ public int getSafeSpotSearchVerticalRange() {
public void setSafeSpotSearchVerticalRange(int safeSpotSearchVerticalRange) {
this.safeSpotSearchVerticalRange = safeSpotSearchVerticalRange;
}

public boolean isSlowDeletion() {
return slowDeletion;
}

public void setSlowDeletion(boolean slowDeletion) {
this.slowDeletion = slowDeletion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package world.bentobox.bentobox.managers;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.util.DeleteIslandChunks;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicReference;

/**
* Manages the queue of island chunks to be deleted.
*/
public class IslandChunkDeletionManager implements Runnable {
private final boolean slowDeletion;
private final BentoBox plugin;
private final AtomicReference<DeleteIslandChunks> currentTask;
private final Queue<IslandDeletion> queue;

public IslandChunkDeletionManager(BentoBox plugin) {
this.plugin = plugin;
this.currentTask = new AtomicReference<>();
this.queue = new LinkedList<>();
this.slowDeletion = plugin.getSettings().isSlowDeletion();

if (slowDeletion) {
plugin.getServer().getScheduler().runTaskTimer(plugin, this, 0L, 20L);
}
}

@Override
public void run() {
if (queue.isEmpty()) {
return;
}
DeleteIslandChunks task = this.currentTask.get();
if (task != null && !task.isCompleted()) {
return;
}
IslandDeletion islandDeletion = queue.remove();
currentTask.set(startDeleteTask(islandDeletion));
}

private DeleteIslandChunks startDeleteTask(IslandDeletion islandDeletion) {
return new DeleteIslandChunks(plugin, islandDeletion);
}

/**
* Adds an island deletion to the queue.
*
* @param islandDeletion island deletion
*/
public void add(IslandDeletion islandDeletion) {
if (slowDeletion) {
queue.add(islandDeletion);
} else {
startDeleteTask(islandDeletion);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import world.bentobox.bentobox.api.events.island.IslandDeletedEvent;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.util.DeleteIslandChunks;
import world.bentobox.bentobox.util.Util;

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ public void onBentoBoxReady(BentoBoxReadyEvent e) {
} 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);
plugin.getIslandChunkDeletionManager().add(di);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import world.bentobox.bentobox.database.objects.IslandDeletion;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.island.IslandCache;
import world.bentobox.bentobox.util.DeleteIslandChunks;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;

Expand Down Expand Up @@ -346,7 +345,7 @@ public void deleteIsland(@NonNull Island island, boolean removeBlocks, @Nullable
// Remove players from island
removePlayersFromIsland(island);
// Remove blocks from world
new DeleteIslandChunks(plugin, new IslandDeletion(island));
plugin.getIslandChunkDeletionManager().add(new IslandDeletion(island));
}
}

Expand Down
30 changes: 29 additions & 1 deletion src/main/java/world/bentobox/bentobox/nms/NMSAbstraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@

import org.bukkit.Chunk;
import org.bukkit.block.data.BlockData;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.util.BoundingBox;

public interface NMSAbstraction {

/**
* Copy the chunk data and biome grid to the given chunk.
* @param chunk - chunk to copy to
* @param chunkData - chunk data to copy
* @param biomeGrid - biome grid to copy to
* @param limitBox - bounding box to limit the copying
*/
default void copyChunkDataToChunk(Chunk chunk, ChunkGenerator.ChunkData chunkData, ChunkGenerator.BiomeGrid biomeGrid, BoundingBox limitBox) {
double baseX = chunk.getX() << 4;
double baseZ = chunk.getZ() << 4;
int minHeight = chunk.getWorld().getMinHeight();
int maxHeight = chunk.getWorld().getMaxHeight();
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
if (!limitBox.contains(baseX + x, 0, baseZ + z)) {
continue;
}
for (int y = minHeight; y < maxHeight; y++) {
setBlockInNativeChunk(chunk, x, y, z, chunkData.getBlockData(x, y, z), false);
// 3D biomes, 4 blocks separated
if (x % 4 == 0 && y % 4 == 0 && z % 4 == 0) {
chunk.getBlock(x, y, z).setBiome(biomeGrid.getBiome(x, y, z));
}
}
}
}
}

/**
* Update the low-level chunk information for the given block to the new block ID and data. This
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/world/bentobox/bentobox/nms/v1_18_R1/NMSHandler.java

This file was deleted.

0 comments on commit ce1d8e5

Please sign in to comment.