Initial async saving on auto save and force save.

This commit is contained in:
drtshock 2015-08-06 17:41:37 -05:00
parent f76fc190fd
commit 1a94e89558
9 changed files with 68 additions and 18 deletions

View File

@ -80,5 +80,7 @@ public abstract class Board {
public abstract boolean forceSave();
public abstract boolean forceSave(boolean sync);
public abstract boolean load();
}

View File

@ -31,6 +31,8 @@ public abstract class FPlayers {
public abstract void forceSave();
public abstract void forceSave(boolean sync);
public abstract FPlayer getByOfflinePlayer(OfflinePlayer player);
public abstract FPlayer getById(String string);

View File

@ -37,6 +37,8 @@ public abstract class Factions {
public abstract void forceSave();
public abstract void forceSave(boolean sync);
public static Factions getInstance() {
return instance;
}

View File

@ -166,7 +166,7 @@ public class P extends MPlugin {
@Override
public void postAutoSave() {
Board.getInstance().forceSave();
//Board.getInstance().forceSave(); Not sure why this was there as it's called after the board is already saved.
Conf.save();
}

View File

@ -28,9 +28,9 @@ public class CmdSaveAll extends FCommand {
@Override
public void perform() {
FPlayers.getInstance().forceSave();
Factions.getInstance().forceSave();
Board.getInstance().forceSave();
FPlayers.getInstance().forceSave(false);
Factions.getInstance().forceSave(false);
Board.getInstance().forceSave(false);
Conf.save();
msg(TL.COMMAND_SAVEALL_SUCCESS);
}

View File

@ -21,9 +21,9 @@ public class SaveTask implements Runnable {
}
running = true;
p.preAutoSave();
Factions.getInstance().forceSave();
FPlayers.getInstance().forceSave();
Board.getInstance().forceSave();
Factions.getInstance().forceSave(false);
FPlayers.getInstance().forceSave(false);
Board.getInstance().forceSave(false);
p.postAutoSave();
running = false;
}

View File

@ -6,6 +6,7 @@ import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.P;
import com.massivecraft.factions.zcore.persist.MemoryBoard;
import com.massivecraft.factions.zcore.util.DiscUtil;
import org.bukkit.Bukkit;
import java.io.File;
import java.lang.reflect.Type;
@ -63,14 +64,30 @@ public class JSONBoard extends MemoryBoard {
}
public boolean forceSave() {
//Factions.log("Saving board to disk");
return forceSave(true);
}
try {
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) {
e.printStackTrace();
P.p.log("Failed to save the board to disk.");
return false;
public boolean forceSave(boolean sync) {
if (sync) {
try {
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) {
e.printStackTrace();
P.p.log("Failed to save the board to disk.");
return false;
}
} else {
Bukkit.getScheduler().runTaskAsynchronously(P.p, new Runnable() {
@Override
public void run() {
try {
DiscUtil.write(file, P.p.gson.toJson(dumpAsSaveFormat()));
} catch (Exception e) {
e.printStackTrace();
P.p.log("Failed to save the board to disk.");
}
}
});
}
return true;

View File

@ -51,13 +51,27 @@ public class JSONFPlayers extends MemoryFPlayers {
}
public void forceSave() {
Map<String, JSONFPlayer> entitiesThatShouldBeSaved = new HashMap<String, JSONFPlayer>();
forceSave(true);
}
public void forceSave(boolean sync) {
final Map<String, JSONFPlayer> entitiesThatShouldBeSaved = new HashMap<String, JSONFPlayer>();
for (FPlayer entity : this.fPlayers.values()) {
if (((MemoryFPlayer) entity).shouldBeSaved()) {
entitiesThatShouldBeSaved.put(entity.getId(), (JSONFPlayer) entity);
}
}
this.saveCore(this.file, entitiesThatShouldBeSaved);
if (sync) {
saveCore(file, entitiesThatShouldBeSaved);
} else {
Bukkit.getScheduler().runTaskAsynchronously(P.p, new Runnable() {
@Override
public void run() {
saveCore(file, entitiesThatShouldBeSaved);
}
});
}
}
private boolean saveCore(File target, Map<String, JSONFPlayer> data) {

View File

@ -53,12 +53,25 @@ public class JSONFactions extends MemoryFactions {
}
public void forceSave() {
Map<String, JSONFaction> entitiesThatShouldBeSaved = new HashMap<String, JSONFaction>();
forceSave(true);
}
public void forceSave(boolean sync) {
final Map<String, JSONFaction> entitiesThatShouldBeSaved = new HashMap<String, JSONFaction>();
for (Faction entity : this.factions.values()) {
entitiesThatShouldBeSaved.put(entity.getId(), (JSONFaction) entity);
}
this.saveCore(this.file, entitiesThatShouldBeSaved);
if (sync) {
saveCore(file, entitiesThatShouldBeSaved);
} else {
Bukkit.getScheduler().runTaskAsynchronously(P.p, new Runnable() {
@Override
public void run() {
saveCore(file, entitiesThatShouldBeSaved);
}
});
}
}
private boolean saveCore(File target, Map<String, JSONFaction> entities) {