Save immediately on shutdown

This commit is contained in:
fullwall 2012-09-08 23:30:18 +08:00
parent 2b08e7dbeb
commit 8f1b6d7c33
3 changed files with 53 additions and 47 deletions

View File

@ -171,7 +171,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
tearDownScripting();
// Don't bother with this part if MC versions are not compatible
if (compatible) {
save();
save(true);
despawnNPCs();
npcRegistry = null;
}
@ -276,12 +276,16 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
getServer().getPluginManager().callEvent(new CitizensReloadEvent());
}
public void save() {
public void save(boolean immediate) {
if (saves == null)
return;
for (NPC npc : npcRegistry)
((CitizensNPC) npc).save(saves.getKey("npc." + npc.getId()));
if (immediate) {
saves.save();
return;
}
new Thread() {
@Override
public void run() {
@ -294,7 +298,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
save();
save(false);
}
});
}

View File

@ -179,48 +179,6 @@ public class CommandManager {
return cmds;
}
public static class CommandInfo {
private final Command commandAnnotation;
private final Requirements requirements;
public CommandInfo(Command commandAnnotation, Requirements requirements) {
this.commandAnnotation = commandAnnotation;
this.requirements = requirements;
}
public Command getCommandAnnotation() {
return commandAnnotation;
}
public Requirements getRequirements() {
return requirements;
}
@Override
public int hashCode() {
return 31 + ((commandAnnotation == null) ? 0 : commandAnnotation.hashCode());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CommandInfo other = (CommandInfo) obj;
if (commandAnnotation == null) {
if (other.commandAnnotation != null) {
return false;
}
} else if (!commandAnnotation.equals(other.commandAnnotation)) {
return false;
}
return true;
}
}
// Get the usage string for a command.
private String getUsage(String[] args, Command cmd) {
StringBuilder command = new StringBuilder();
@ -323,6 +281,48 @@ public class CommandManager {
this.injector = injector;
}
public static class CommandInfo {
private final Command commandAnnotation;
private final Requirements requirements;
public CommandInfo(Command commandAnnotation, Requirements requirements) {
this.commandAnnotation = commandAnnotation;
this.requirements = requirements;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CommandInfo other = (CommandInfo) obj;
if (commandAnnotation == null) {
if (other.commandAnnotation != null) {
return false;
}
} else if (!commandAnnotation.equals(other.commandAnnotation)) {
return false;
}
return true;
}
public Command getCommandAnnotation() {
return commandAnnotation;
}
public Requirements getRequirements() {
return requirements;
}
@Override
public int hashCode() {
return 31 + ((commandAnnotation == null) ? 0 : commandAnnotation.hashCode());
}
}
// Logger for general errors.
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
}

View File

@ -52,15 +52,17 @@ public class AdminCommands {
@Command(
aliases = { "citizens" },
usage = "save",
usage = "save (-a)",
desc = "Save NPCs",
modifiers = { "save" },
min = 1,
max = 1,
flags = "a",
permission = "admin")
public void save(CommandContext args, CommandSender sender, NPC npc) {
Messaging.send(sender, "<e>Saving Citizens...");
plugin.save();
boolean async = args.hasFlag('a');
plugin.save(!async);
Messaging.send(sender, "<e>Citizens saved.");
}
}