Save WorldGuard regions periodically instead of directly

Now uses the saveChanges() method on RegionManager of WorldGuard, that
will only save regions that are actually changed.
This commit is contained in:
Thijs Wiefferink 2014-12-01 21:26:06 +01:00
parent cf3139f373
commit 46abc8f83d
4 changed files with 50 additions and 17 deletions

View File

@ -35,7 +35,7 @@ useColorsInConsole: false
## Post error messages in the console when a command run from the config fails (from the 'runCommands' section for example)
postCommandErrors: true
## Enables / disables debug messages in the console, could be useful to figure out where errors come from
debug: true
debug: false
## Version of the config, do not change!
version: 2.1.0

View File

@ -13,7 +13,9 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import nl.evolutioncoding.areashop.exceptions.RegionCreateException;
import nl.evolutioncoding.areashop.regions.BuyRegion;
@ -40,6 +42,7 @@ import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.bukkit.selections.Selection;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
public class FileManager {
@ -58,10 +61,11 @@ public class FileManager {
private String defaultPath = null;
private YamlConfiguration defaultConfig = null;
private boolean saveGroupsRequired = false;
private Set<String> worldRegionsRequireSaving;
private HashMap<String,Integer> versions = null;
private String versionPath = null;
private String schemFolder = null;
private String schemFolder = null;
/**
* Constructor, initialize variabeles
@ -77,6 +81,7 @@ public class FileManager {
defaultPath = plugin.getDataFolder() + File.separator + AreaShop.defaultFile;
versionPath = plugin.getDataFolder().getPath() + File.separator + AreaShop.versionFile;
schemFolder = plugin.getDataFolder() + File.separator + AreaShop.schematicFolder;
worldRegionsRequireSaving = new HashSet<String>();
File schemFile = new File(schemFolder);
if(!schemFile.exists()) {
schemFile.mkdirs();
@ -258,7 +263,7 @@ public class FileManager {
for(RegionGroup group : groups) {
group.removeMember(rent);
}
saveGroupsRequired();
saveGroupsIsRequired();
rent.resetRegionFlags();
regions.remove(rent.getLowerCaseName());
File file = new File(plugin.getDataFolder() + File.separator + AreaShop.regionsFolder + File.separator + rent.getLowerCaseName() + ".yml");
@ -322,7 +327,7 @@ public class FileManager {
for(RegionGroup group : getGroups()) {
group.removeMember(buy);
}
saveGroupsRequired();
saveGroupsIsRequired();
// Deleting the file
File file = new File(plugin.getDataFolder() + File.separator + AreaShop.regionsFolder + File.separator + buy.getLowerCaseName() + ".yml");
@ -351,7 +356,7 @@ public class FileManager {
public void removeGroup(RegionGroup group) {
groups.remove(group.getLowerCaseName());
groupsConfig.set(group.getLowerCaseName(), null);
saveGroupsRequired();
saveGroupsIsRequired();
}
/**
@ -472,7 +477,7 @@ public class FileManager {
/**
* Save the group file to disk
*/
public void saveGroupsRequired() {
public void saveGroupsIsRequired() {
saveGroupsRequired = true;
}
public boolean isSaveGroupsRequired() {
@ -496,6 +501,7 @@ public class FileManager {
if(isSaveGroupsRequired()) {
saveGroupsNow();
}
this.saveWorldGuardRegions();
final List<GeneralRegion> regions = new ArrayList<GeneralRegion>(getRegions());
new BukkitRunnable() {
@ -530,8 +536,40 @@ public class FileManager {
region.saveNow();
}
}
this.saveWorldGuardRegions();
}
/**
* Indicates that a/multiple WorldGuard regions need to be saved
* @param worldName The world where the regions that should be saved is in
*/
public void saveIsRequiredForRegionWorld(String worldName) {
worldRegionsRequireSaving.add(worldName);
}
/**
* Save all worldGuard regions that need saving
*/
public void saveWorldGuardRegions() {
for(String world : worldRegionsRequireSaving) {
World bukkitWorld = Bukkit.getWorld(world);
if(bukkitWorld != null) {
RegionManager manager = plugin.getWorldGuard().getRegionManager(bukkitWorld);
if(manager != null) {
try {
manager.saveChanges();
} catch(StorageException e) {
plugin.getLogger().info("Error: WorldGuard regions in world " + world + " could not be saved");
}
}
}
}
}
/**
* Get the folder the region files are located in
* @return The folder where the <region>.yml files are in
*/
public String getRegionFolder() {
return regionsPath;
}
@ -559,6 +597,9 @@ public class FileManager {
}.runTaskTimer(plugin, 1, 1);
}
/**
* Check all regions and unrent/sell them if the player is inactive for too long
*/
public void checkForInactiveRegions() {
final List<GeneralRegion> regions = new ArrayList<GeneralRegion>(getRegions());
new BukkitRunnable() {

View File

@ -44,7 +44,6 @@ import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.InvalidFlagFormat;
import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException;
@ -864,9 +863,6 @@ public abstract class GeneralRegion {
* @return
*/
protected boolean setRegionFlags(ConfigurationSection flags) {
// TODO A lot of testing + delete lines below
// https://github.com/sk89q/WorldGuard/blob/master/src/main/java/com/sk89q/worldguard/bukkit/commands/region/RegionCommands.java#L461
// https://github.com/sk89q/WorldGuard/blob/master/src/main/java/com/sk89q/worldguard/bukkit/commands/region/RegionCommandsBase.java#L393
boolean result = true;
if(flags == null) {
AreaShop.debug("Flags section is null");
@ -1007,12 +1003,8 @@ public abstract class GeneralRegion {
}
}
}
try {
// TODO include into periodic saving program
worldGuard.getRegionManager(getWorld()).save();
} catch (StorageException e) {
plugin.getLogger().info("Error: regions could not be saved");
}
// Indicate that the regions needs to be saved
plugin.getFileManager().saveIsRequiredForRegionWorld(getWorldName());
return result;
}

View File

@ -144,7 +144,7 @@ public class RegionGroup {
* Indicates this file needs to be saved, will actually get saved later by a task
*/
public void saveRequired() {
plugin.getFileManager().saveGroupsRequired();
plugin.getFileManager().saveGroupsIsRequired();
}
/**