1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Immense AutoSaving Optimizations

This commit is contained in:
mrCookieSlime 2016-09-17 10:06:21 +02:00
parent 329a191d3c
commit 169539de56
4 changed files with 90 additions and 17 deletions

View File

@ -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<BlockStorage> worlds = new HashSet<BlockStorage>();
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);
}
}
}
}

View File

@ -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<Location, BlockMenu> inventories2 = new HashMap<Location, BlockMenu>(inventories);
for (Map.Entry<Location, BlockMenu> entry: inventories2.entrySet()) {
changes += entry.getValue().changes;
}
Map<String, UniversalBlockMenu> universal_inventories2 = new HashMap<String, UniversalBlockMenu>(universal_inventories);
for (Map.Entry<String, UniversalBlockMenu> 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<String, Config> cache = new HashMap<String, Config>(cache_blocks);
@ -173,7 +202,7 @@ public class BlockStorage {
Config cfg = entry.getValue();
if (cfg.getKeys().isEmpty()) cfg.getFile().delete();
else cfg.save();
}
}
Map<Location, BlockMenu> inventories2 = new HashMap<Location, BlockMenu>(inventories);
@ -181,22 +210,29 @@ public class BlockStorage {
entry.getValue().save(entry.getKey());
}
for (Map.Entry<String, UniversalBlockMenu> entry: universal_inventories.entrySet()) {
Map<String, UniversalBlockMenu> universal_inventories2 = new HashMap<String, UniversalBlockMenu>(universal_inventories);
for (Map.Entry<String, UniversalBlockMenu> 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<String, String> 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<String, String> 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) {

View File

@ -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() {

View File

@ -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() {