From 169539de56310693ecc2843fb428227b72040619 Mon Sep 17 00:00:00 2001 From: mrCookieSlime Date: Sat, 17 Sep 2016 10:06:21 +0200 Subject: [PATCH] Immense AutoSaving Optimizations --- .../Slimefun/URID/AutoSavingTask.java | 28 ++++++-- .../Slimefun/api/BlockStorage.java | 64 +++++++++++++++---- .../Slimefun/api/inventory/BlockMenu.java | 7 ++ .../api/inventory/UniversalBlockMenu.java | 8 +++ 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/src/me/mrCookieSlime/Slimefun/URID/AutoSavingTask.java b/src/me/mrCookieSlime/Slimefun/URID/AutoSavingTask.java index fd05a2f8c..48babb7ff 100644 --- a/src/me/mrCookieSlime/Slimefun/URID/AutoSavingTask.java +++ b/src/me/mrCookieSlime/Slimefun/URID/AutoSavingTask.java @@ -1,19 +1,39 @@ package me.mrCookieSlime.Slimefun.URID; -import me.mrCookieSlime.Slimefun.SlimefunStartup; -import me.mrCookieSlime.Slimefun.api.BlockStorage; +import java.util.HashSet; +import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.World; +import me.mrCookieSlime.Slimefun.SlimefunStartup; +import me.mrCookieSlime.Slimefun.api.BlockStorage; + public class AutoSavingTask implements Runnable { @Override public void run() { - System.out.println("[Slimefun] Auto-Saving Data... (Next Auto-Save: " + SlimefunStartup.getCfg().getInt("options.auto-save-delay-in-minutes") + "m)"); + Set worlds = new HashSet(); + for (World world: Bukkit.getWorlds()) { - if (BlockStorage.isWorldRegistered(world.getName())) BlockStorage.getStorage(world).save(false); + if (BlockStorage.isWorldRegistered(world.getName())) { + BlockStorage storage = BlockStorage.getStorage(world); + storage.computeChanges(); + + if (storage.getChanges() > 0) { + worlds.add(storage); + } + } } + + if (!worlds.isEmpty()) { + System.out.println("[Slimefun] Auto-Saving Data... (Next Auto-Save: " + SlimefunStartup.getCfg().getInt("options.auto-save-delay-in-minutes") + "m)"); + + for (BlockStorage storage: worlds) { + storage.save(false); + } + } + } } diff --git a/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java index 7a2e837f5..9d0c911ce 100644 --- a/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -162,9 +162,38 @@ public class BlockStorage { } } } + + private static int chunk_changes = 0; + private int changes = 0; + + public void computeChanges() { + changes = cache_blocks.size() + chunk_changes; + + Map inventories2 = new HashMap(inventories); + for (Map.Entry entry: inventories2.entrySet()) { + changes += entry.getValue().changes; + } + + Map universal_inventories2 = new HashMap(universal_inventories); + for (Map.Entry entry: universal_inventories2.entrySet()) { + changes += entry.getValue().changes; + } + } + + public int getChanges() { + return changes; + } public void save(boolean remove) { - System.out.println("[Slimefun] Saving Blocks for World \"" + world.getName() + "\""); + this.save(true, remove); + } + + public void save(boolean computeChanges, boolean remove) { + if (computeChanges) computeChanges(); + + if (changes == 0) return; + + System.out.println("[Slimefun] Saving Blocks for World \"" + world.getName() + "\" (" + changes + " Changes queued)"); Map cache = new HashMap(cache_blocks); @@ -173,7 +202,7 @@ public class BlockStorage { Config cfg = entry.getValue(); if (cfg.getKeys().isEmpty()) cfg.getFile().delete(); else cfg.save(); - } + } Map inventories2 = new HashMap(inventories); @@ -181,22 +210,29 @@ public class BlockStorage { entry.getValue().save(entry.getKey()); } - for (Map.Entry entry: universal_inventories.entrySet()) { + Map universal_inventories2 = new HashMap(universal_inventories); + + for (Map.Entry entry: universal_inventories2.entrySet()) { entry.getValue().save(); } - File chunks = new File(path_chunks + "chunks.sfc"); - Config cfg = new Config("data-storage/Slimefun/temp.yml"); - - for (Map.Entry entry: map_chunks.entrySet()) { - cfg.setValue(entry.getKey(), entry.getValue()); + if (chunk_changes > 0) { + File chunks = new File(path_chunks + "chunks.sfc"); + Config cfg = new Config("data-storage/Slimefun/temp.yml"); + + for (Map.Entry entry: map_chunks.entrySet()) { + cfg.setValue(entry.getKey(), entry.getValue()); + } + + cfg.save(chunks); + + if (remove) { + worlds.remove(world.getName()); + } } - cfg.save(chunks); - - if (remove) { - worlds.remove(world.getName()); - } + changes = 0; + chunk_changes = 0; } public static void store(Block block, ItemStack item) { @@ -591,6 +627,8 @@ public class BlockStorage { } map_chunks.put(serializeChunk(chunk), json.toJSONString()); + + chunk_changes++; } public static String getChunkInfo(Chunk chunk, String key) { diff --git a/src/me/mrCookieSlime/Slimefun/api/inventory/BlockMenu.java b/src/me/mrCookieSlime/Slimefun/api/inventory/BlockMenu.java index ea6da893a..a1a9ea29f 100644 --- a/src/me/mrCookieSlime/Slimefun/api/inventory/BlockMenu.java +++ b/src/me/mrCookieSlime/Slimefun/api/inventory/BlockMenu.java @@ -16,6 +16,8 @@ public class BlockMenu extends ChestMenu { BlockMenuPreset preset; Location l; + public int changes = 0; + private ItemManipulationEvent event; private static String serializeLocation(Location l) { @@ -26,6 +28,7 @@ public class BlockMenu extends ChestMenu { super(preset.getTitle()); this.preset = preset; this.l = l; + changes = 1; preset.clone(this); @@ -55,6 +58,7 @@ public class BlockMenu extends ChestMenu { } public void save(Location l) { + if (changes == 0) return; // To force CS-CoreLib to build the Inventory this.getContents(); @@ -65,6 +69,8 @@ public class BlockMenu extends ChestMenu { cfg.setValue(String.valueOf(slot), getItemInSlot(slot)); } cfg.save(); + + changes = 0; } public void move(Block b) { @@ -104,6 +110,7 @@ public class BlockMenu extends ChestMenu { super.replaceExistingItem(slot, item); if (event && this.event != null) this.event.onEvent(slot, previous, item); + changes++; } public void close() { diff --git a/src/me/mrCookieSlime/Slimefun/api/inventory/UniversalBlockMenu.java b/src/me/mrCookieSlime/Slimefun/api/inventory/UniversalBlockMenu.java index 9322878c2..37b71df94 100644 --- a/src/me/mrCookieSlime/Slimefun/api/inventory/UniversalBlockMenu.java +++ b/src/me/mrCookieSlime/Slimefun/api/inventory/UniversalBlockMenu.java @@ -15,9 +15,12 @@ public class UniversalBlockMenu extends ChestMenu { BlockMenuPreset preset; ItemManipulationEvent event; + public int changes = 0; + public UniversalBlockMenu(BlockMenuPreset preset) { super(preset.getTitle()); this.preset = preset; + changes = 1; preset.clone(this); @@ -46,6 +49,7 @@ public class UniversalBlockMenu extends ChestMenu { } public void save() { + if (changes == 0) return; // To force CS-CoreLib to build the Inventory this.getContents(); @@ -56,6 +60,8 @@ public class UniversalBlockMenu extends ChestMenu { cfg.setValue(String.valueOf(slot), getItemInSlot(slot)); } cfg.save(); + + changes = 0; } public BlockMenuPreset getPreset() { @@ -76,6 +82,8 @@ public class UniversalBlockMenu extends ChestMenu { super.replaceExistingItem(slot, item); if (event && this.event != null) this.event.onEvent(slot, previous, item); + + changes++; } public void close() {