From 8bab4540cd834b1c25d6502956f7c6760c5d0bc1 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 22 Mar 2011 22:31:04 +0100 Subject: [PATCH] Better save system --- src/com/bukkit/mcteam/factions/Board.java | 4 +--- src/com/bukkit/mcteam/factions/Conf.java | 3 ++- src/com/bukkit/mcteam/factions/FPlayer.java | 6 +---- src/com/bukkit/mcteam/factions/Faction.java | 22 +----------------- src/com/bukkit/mcteam/factions/Factions.java | 23 +++++++++++++++---- src/com/bukkit/mcteam/factions/SaveTask.java | 12 ++++++++++ .../factions/commands/FCommandCreate.java | 2 -- .../factions/commands/FCommandDeinvite.java | 1 - .../commands/FCommandDescription.java | 1 + .../factions/commands/FCommandInvite.java | 1 - .../factions/commands/FCommandJoin.java | 2 -- .../factions/commands/FCommandKick.java | 2 -- 12 files changed, 37 insertions(+), 42 deletions(-) create mode 100644 src/com/bukkit/mcteam/factions/SaveTask.java diff --git a/src/com/bukkit/mcteam/factions/Board.java b/src/com/bukkit/mcteam/factions/Board.java index 60478b10..eeea067a 100644 --- a/src/com/bukkit/mcteam/factions/Board.java +++ b/src/com/bukkit/mcteam/factions/Board.java @@ -51,7 +51,6 @@ public class Board { } worldCoordIds.get(flocation.getWorldName()).put(flocation.getCoordString(), id); - save(); } public static void setFactionAt(Faction faction, FLocation flocation) { @@ -63,7 +62,6 @@ public class Board { return; } worldCoordIds.get(flocation.getWorldName()).remove(flocation.getCoordString()); - save(); } // Is this coord NOT completely surrounded by coords claimed by the same faction? @@ -171,7 +169,7 @@ public class Board { // -------------------------------------------- // public static boolean save() { - Factions.log("Saving board to disk"); + //Factions.log("Saving board to disk"); try { DiscUtil.write(file, Factions.gson.toJson(worldCoordIds)); diff --git a/src/com/bukkit/mcteam/factions/Conf.java b/src/com/bukkit/mcteam/factions/Conf.java index fc1a2e20..77fe2a13 100644 --- a/src/com/bukkit/mcteam/factions/Conf.java +++ b/src/com/bukkit/mcteam/factions/Conf.java @@ -67,7 +67,8 @@ public class Conf { // -------------------------------------------- // public static boolean save() { - Factions.log("Saving config to disk."); + //Factions.log("Saving config to disk."); + try { DiscUtil.write(file, Factions.gson.toJson(new Conf())); } catch (IOException e) { diff --git a/src/com/bukkit/mcteam/factions/FPlayer.java b/src/com/bukkit/mcteam/factions/FPlayer.java index e713d74c..fd57b16b 100644 --- a/src/com/bukkit/mcteam/factions/FPlayer.java +++ b/src/com/bukkit/mcteam/factions/FPlayer.java @@ -164,7 +164,6 @@ public class FPlayer { public void setTitle(String title) { this.title = title; - save(); } public String getName() { @@ -335,7 +334,6 @@ public class FPlayer { int millisPerMinute = 60*1000; this.alterPower(millisPassed * Conf.powerPerMinute / millisPerMinute); - //this.save(); // This would save to often. So we save this on player quit instead. } public void onDeath() { @@ -386,8 +384,6 @@ public class FPlayer { } Faction.delete(myFaction.getId()); } - FPlayer.save(); - FPlayer.save(); } // -------------------------------------------- // @@ -454,7 +450,7 @@ public class FPlayer { } public static boolean save() { - Factions.log("Saving players to disk"); + //Factions.log("Saving players to disk"); // We only wan't to save the vplayers with non default values Map vplayersToSave = new HashMap(); diff --git a/src/com/bukkit/mcteam/factions/Faction.java b/src/com/bukkit/mcteam/factions/Faction.java index baa53493..f74f7d26 100644 --- a/src/com/bukkit/mcteam/factions/Faction.java +++ b/src/com/bukkit/mcteam/factions/Faction.java @@ -59,7 +59,6 @@ public class Faction { public void setOpen(boolean isOpen) { open = isOpen; - save(); } public String getTag() { @@ -79,7 +78,6 @@ public class Faction { str = str.toUpperCase(); } this.tag = str; - save(); } public String getDescription() { @@ -88,7 +86,6 @@ public class Faction { public void setDescription(String value) { this.description = value; - save(); } // ------------------------------- @@ -124,7 +121,6 @@ public class Faction { } else { this.relationWish.put(otherFaction.getId(), relation); } - Faction.save(); } public Relation getRelation(Faction otherFaction) { @@ -215,18 +211,6 @@ public class Faction { return ret; } - /* - public void removeFollower(FPlayer follower) { - if (this.id != follower.factionId) { - return; // safety check - } - - this.invites.remove(follower.id); - follower.resetFactionData(); - follower.save(); - this.save(); - }*/ - public ArrayList getOnlinePlayers() { ArrayList ret = new ArrayList(); for (Player player: Factions.instance.getServer().getOnlinePlayers()) { @@ -314,7 +298,7 @@ public class Faction { //----------------------------------------------// public static boolean save() { - Factions.log("Saving factions to disk"); + //Factions.log("Saving factions to disk"); try { DiscUtil.write(file, Factions.gson.toJson(instances)); @@ -403,9 +387,5 @@ public class Faction { // Clean the fplayers FPlayer.clean(); - - // SAVE files - Board.save(); - FPlayer.save(); } } diff --git a/src/com/bukkit/mcteam/factions/Factions.java b/src/com/bukkit/mcteam/factions/Factions.java index 2674781a..c3b1943a 100644 --- a/src/com/bukkit/mcteam/factions/Factions.java +++ b/src/com/bukkit/mcteam/factions/Factions.java @@ -52,6 +52,9 @@ import com.nijikokun.bukkit.Permissions.Permissions; import me.taylorkelly.help.Help; +/** + * The data is saved to disk every 30min and on plugin disable. + */ public class Factions extends JavaPlugin { // -------------------------------------------- // // Fields @@ -134,16 +137,18 @@ public class Factions extends JavaPlugin { pm.registerEvent(Event.Type.ENTITY_EXPLODE, this.entityListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.BLOCK_DAMAGED, this.blockListener, Event.Priority.Normal, this); pm.registerEvent(Event.Type.BLOCK_PLACED, this.blockListener, Event.Priority.Normal, this); - pm.registerEvent(Event.Type.BLOCK_INTERACT, this.blockListener, Event.Priority.Normal, this); + pm.registerEvent(Event.Type.BLOCK_INTERACT, this.blockListener, Event.Priority.Normal, this); + + // Register recurring tasks + long saveTicks = 20 * 60 * 30; // Approximately every 30 min + this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new SaveTask(), saveTicks, saveTicks); log("=== INIT DONE (Took "+(System.currentTimeMillis()-timeInitStart)+"ms) ==="); } @Override public void onDisable() { - FPlayer.save(); - Faction.save(); - Board.save(); + saveAll(); log("Disabled"); } @@ -232,5 +237,15 @@ public class Factions extends JavaPlugin { public static void log(Level level, String msg) { Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg); } + + // -------------------------------------------- // + // Save all + // -------------------------------------------- // + + public static void saveAll() { + FPlayer.save(); + Faction.save(); + Board.save(); + } } diff --git a/src/com/bukkit/mcteam/factions/SaveTask.java b/src/com/bukkit/mcteam/factions/SaveTask.java new file mode 100644 index 00000000..21966417 --- /dev/null +++ b/src/com/bukkit/mcteam/factions/SaveTask.java @@ -0,0 +1,12 @@ +package com.bukkit.mcteam.factions; + +public class SaveTask implements Runnable { + + //TODO are they removed on disable? + + @Override + public void run() { + Factions.saveAll(); + } + +} diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java b/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java index 2e7ce1c5..0e642f28 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandCreate.java @@ -47,8 +47,6 @@ public class FCommandCreate extends FBaseCommand { faction.setTag(tag); me.setRole(Role.ADMIN); me.setFaction(faction); - Faction.save(); - FPlayer.save(); for (FPlayer follower : FPlayer.getAllOnline()) { follower.sendMessage(me.getNameAndRelevant(follower)+Conf.colorSystem+" created a new faction "+faction.getTag(follower)); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java b/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java index 4d72b128..c0f360f2 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandDeinvite.java @@ -50,7 +50,6 @@ public class FCommandDeinvite extends FBaseCommand { } myFaction.deinvite(you); - Faction.save(); you.sendMessage(me.getNameAndRelevant(you)+Conf.colorSystem+" revoked your invitation to "+myFaction.getTag(you)); myFaction.sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" revoked "+you.getNameAndRelevant(me)+"'s"+Conf.colorSystem+" invitation."); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandDescription.java b/src/com/bukkit/mcteam/factions/commands/FCommandDescription.java index 920de432..556b9f40 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandDescription.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandDescription.java @@ -34,6 +34,7 @@ public class FCommandDescription extends FBaseCommand { } me.getFaction().setDescription(TextUtil.implode(parameters)); + // Broadcast the description to everyone for (FPlayer fplayer : FPlayer.getAllOnline()) { fplayer.sendMessage("The faction "+fplayer.getRelationColor(me)+me.getFaction().getTag()+Conf.colorSystem+" changed their description to:"); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java b/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java index 98e79e3f..85383faa 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandInvite.java @@ -50,7 +50,6 @@ public class FCommandInvite extends FBaseCommand { } myFaction.invite(you); - Faction.save(); you.sendMessage(me.getNameAndRelevant(you)+Conf.colorSystem+" invited you to "+myFaction.getTag(you)); myFaction.sendMessage(me.getNameAndRelevant(me)+Conf.colorSystem+" invited "+you.getNameAndRelevant(me)+Conf.colorSystem+" to your faction."); diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java b/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java index 2fbb1b92..6d31a29b 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandJoin.java @@ -3,7 +3,6 @@ package com.bukkit.mcteam.factions.commands; import java.util.ArrayList; import com.bukkit.mcteam.factions.Conf; -import com.bukkit.mcteam.factions.FPlayer; import com.bukkit.mcteam.factions.Faction; public class FCommandJoin extends FBaseCommand { @@ -53,7 +52,6 @@ public class FCommandJoin extends FBaseCommand { me.resetFactionData(); me.setFaction(faction); faction.deinvite(me); - FPlayer.save(); } } diff --git a/src/com/bukkit/mcteam/factions/commands/FCommandKick.java b/src/com/bukkit/mcteam/factions/commands/FCommandKick.java index 0b1c7efe..81ea1fcd 100644 --- a/src/com/bukkit/mcteam/factions/commands/FCommandKick.java +++ b/src/com/bukkit/mcteam/factions/commands/FCommandKick.java @@ -51,8 +51,6 @@ public class FCommandKick extends FBaseCommand { myFaction.deinvite(you); you.resetFactionData(); - FPlayer.save(); - Faction.save(); myFaction.sendMessage(me.getNameAndRelevant(myFaction)+Conf.colorSystem+" kicked "+you.getNameAndRelevant(myFaction)+Conf.colorSystem+" from the faction! :O"); you.sendMessage(me.getNameAndRelevant(you)+Conf.colorSystem+" kicked you from "+myFaction.getTag(you)+Conf.colorSystem+"! :O");