From 21b8c384fbf717552060e9bcb9d806776a532549 Mon Sep 17 00:00:00 2001 From: Xzavier0722 Date: Tue, 16 Jan 2024 21:12:09 +0800 Subject: [PATCH] feat(controller): add remove all data in chunk if --- .../controller/BlockDataController.java | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java b/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java index cc6cd38a82..c71b6a4103 100644 --- a/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java +++ b/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java @@ -540,6 +540,23 @@ public Set getAllLoadedChunkData() { return new HashSet<>(loadedChunk.values()); } + public void removeAllDataInChunk(Chunk chunk) { + var cKey = LocationUtils.getChunkKey(chunk); + var cache = loadedChunk.remove(cKey); + + if (cache != null && cache.isDataLoaded()) { + cache.getAllBlockData().forEach(this::clearBlockCacheAndTasks); + } + deleteChunkAndBlockDataDirectly(cKey); + } + + public void removeAllDataInChunkAsync(Chunk chunk, Runnable onFinishedCallback) { + scheduleWriteTask(() -> { + removeAllDataInChunk(chunk); + onFinishedCallback.run(); + }); + } + public void removeAllDataInWorld(World world) { // 1. remove block cache var loadedBlockData = new HashSet(); @@ -549,29 +566,11 @@ public void removeAllDataInWorld(World world) { } // 2. remove ticker and delayed tasks - for (var blockData : loadedBlockData) { - var l = blockData.getLocation(); - if (blockData.isDataLoaded() - && Slimefun.getRegistry().getTickerBlocks().contains(blockData.getSfId())) { - Slimefun.getTickerTask().disableTicker(l); - } - Slimefun.getNetworkManager().updateAllNetworks(l); - - var scopeKey = new LocationKey(DataScope.NONE, l); - removeDelayedBlockDataUpdates(scopeKey); - abortScopeTask(scopeKey); - } + loadedBlockData.forEach(this::clearBlockCacheAndTasks); // 3. remove from database var prefix = world.getName() + ";"; - var condition = prefix + "%"; - var req = new RecordKey(DataScope.BLOCK_DATA); - req.addCondition(FieldKey.CHUNK, condition); - deleteData(req); - - req = new RecordKey(DataScope.CHUNK_DATA); - req.addCondition(FieldKey.CHUNK, condition); - deleteData(req); + deleteChunkAndBlockDataDirectly(prefix + "%"); // 4. remove chunk cache loadedChunk.entrySet().removeIf(entry -> entry.getKey().startsWith(prefix)); @@ -729,4 +728,26 @@ private SlimefunChunkData getChunkDataCache(Chunk chunk, boolean createOnNotExis }) : loadedChunk.get(LocationUtils.getChunkKey(chunk)); } + + private void deleteChunkAndBlockDataDirectly(String cKey) { + var req = new RecordKey(DataScope.BLOCK_DATA); + req.addCondition(FieldKey.CHUNK, cKey); + deleteData(req); + + req = new RecordKey(DataScope.CHUNK_DATA); + req.addCondition(FieldKey.CHUNK, cKey); + deleteData(req); + } + + private void clearBlockCacheAndTasks(SlimefunBlockData blockData) { + var l = blockData.getLocation(); + if (blockData.isDataLoaded() && Slimefun.getRegistry().getTickerBlocks().contains(blockData.getSfId())) { + Slimefun.getTickerTask().disableTicker(l); + } + Slimefun.getNetworkManager().updateAllNetworks(l); + + var scopeKey = new LocationKey(DataScope.NONE, l); + removeDelayedBlockDataUpdates(scopeKey); + abortScopeTask(scopeKey); + } }