File loading improvements to be more robust

This commit is contained in:
Thijs Wiefferink 2016-12-04 21:14:22 +01:00
parent bae3c0a199
commit 54b6e84558
4 changed files with 117 additions and 48 deletions

View File

@ -17,10 +17,17 @@ public class Feature implements Listener {
} }
/** /**
* Stop listening to events * Destroy the feature and deregister everything
*/ */
public void stopListen() { public void destroyFeature() {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
destroy();
}
/**
* Dummy method a Feature implementation can override
*/
public void destroy() {
} }
} }

View File

@ -48,6 +48,14 @@ public class SignsFeature extends Feature {
} }
} }
@Override
public void destroy() {
// Deregister signs from the registry
for(String key : signs.keySet()) {
allSigns.remove(key);
}
}
/** /**
* Convert a location to a string to use as map key * Convert a location to a string to use as map key
* @param location The location to get the key for * @param location The location to get the key for

View File

@ -857,7 +857,7 @@ public class FileManager {
*/ */
public void loadRegionFiles() { public void loadRegionFiles() {
regions.clear(); regions.clear();
File file = new File(regionsPath); final File file = new File(regionsPath);
if(!file.exists()) { if(!file.exists()) {
if(!file.mkdirs()) { if(!file.mkdirs()) {
AreaShop.warn("Could not create region files directory: "+file.getAbsolutePath()); AreaShop.warn("Could not create region files directory: "+file.getAbsolutePath());
@ -865,12 +865,23 @@ public class FileManager {
} }
plugin.setReady(true); plugin.setReady(true);
} else if(file.isDirectory()) { } else if(file.isDirectory()) {
new BukkitRunnable() {
@Override
public void run() {
File[] regionFiles = file.listFiles(); File[] regionFiles = file.listFiles();
if(regionFiles == null) { if(regionFiles == null) {
plugin.setReady(true);
return; return;
} }
List<String> noRegionType = new ArrayList<>();
List<String> noNamePaths = new ArrayList<>();
List<GeneralRegion> noWorld = new ArrayList<>();
List<GeneralRegion> noRegion = new ArrayList<>();
List<GeneralRegion> incorrectDuration = new ArrayList<>();
for(File regionFile : regionFiles) { for(File regionFile : regionFiles) {
if(regionFile.exists() && regionFile.isFile()) { if(regionFile.exists() && regionFile.isFile()) {
// Load the region file from disk in UTF8 mode // Load the region file from disk in UTF8 mode
YamlConfiguration config; YamlConfiguration config;
try( try(
@ -884,38 +895,52 @@ public class FileManager {
AreaShop.warn("Something went wrong reading region file: "+regionFile.getAbsolutePath()); AreaShop.warn("Something went wrong reading region file: "+regionFile.getAbsolutePath());
continue; continue;
} }
// Construct the correct type of region // Construct the correct type of region
if(RegionType.RENT.getValue().equals(config.getString("general.type"))) { String type = config.getString("general.type");
RentRegion rent = new RentRegion(plugin, config); GeneralRegion region;
addRentNoSave(rent); if(RegionType.RENT.getValue().equals(type)) {
} else if(RegionType.BUY.getValue().equals(config.getString("general.type"))) { region = new RentRegion(plugin, config);
BuyRegion buy = new BuyRegion(plugin, config); } else if(RegionType.BUY.getValue().equals(type)) {
addBuyNoSave(buy); region = new BuyRegion(plugin, config);
} else {
noNamePaths.add(regionFile.getPath());
continue;
} }
}
} // Check consistency
plugin.setReady(true); boolean added = false;
new BukkitRunnable() { if(region.getName() == null) {
@Override noNamePaths.add(regionFile.getPath());
public void run() { } else if(region.getWorld() == null) {
List<GeneralRegion> noWorld = new ArrayList<>();
List<GeneralRegion> noRegion = new ArrayList<>();
List<GeneralRegion> incorrectDuration = new ArrayList<>();
for(GeneralRegion region : AreaShop.getInstance().getFileManager().getRegions()) {
// Add broken regions to a list
if(region != null) {
if(region.getWorld() == null) {
noWorld.add(region); noWorld.add(region);
} } else if(region.getRegion() == null) {
if(region.getRegion() == null) {
noRegion.add(region); noRegion.add(region);
} } else if(region instanceof RentRegion && !Utils.checkTimeFormat(((RentRegion)region).getDurationString())) {
if(region.isRentRegion() && !Utils.checkTimeFormat(((RentRegion)region).getDurationString())) {
incorrectDuration.add(region); incorrectDuration.add(region);
} else {
added = true;
if(region instanceof RentRegion) {
addRentNoSave((RentRegion)region);
} else if(region instanceof BuyRegion) {
addBuyNoSave((BuyRegion)region);
}
}
if(!added) {
region.destroy();
} }
} }
} }
// All files are loaded, print possible problems to the console
// All files are loaded, print problems to the console
if(!noRegionType.isEmpty()) {
AreaShop.warn("The following region files do no have a region type: "+Utils.createCommaSeparatedList(noRegionType));
}
if(!noNamePaths.isEmpty()) {
AreaShop.warn("The following region files do no have a name in their file: "+Utils.createCommaSeparatedList(noNamePaths));
}
if(!noRegion.isEmpty()) { if(!noRegion.isEmpty()) {
List<String> noRegionNames = new ArrayList<>(); List<String> noRegionNames = new ArrayList<>();
for(GeneralRegion region : noRegion) { for(GeneralRegion region : noRegion) {
@ -924,6 +949,7 @@ public class FileManager {
AreaShop.warn("AreaShop regions that are missing their WorldGuard region: "+Utils.createCommaSeparatedList(noRegionNames)); AreaShop.warn("AreaShop regions that are missing their WorldGuard region: "+Utils.createCommaSeparatedList(noRegionNames));
AreaShop.warn("Remove these regions from AreaShop with '/as del' or recreate their regions in WorldGuard."); AreaShop.warn("Remove these regions from AreaShop with '/as del' or recreate their regions in WorldGuard.");
} }
boolean noWorldRegions = !noWorld.isEmpty(); boolean noWorldRegions = !noWorld.isEmpty();
while(!noWorld.isEmpty()) { while(!noWorld.isEmpty()) {
List<GeneralRegion> toDisplay = new ArrayList<>(); List<GeneralRegion> toDisplay = new ArrayList<>();
@ -944,6 +970,7 @@ public class FileManager {
if(noWorldRegions) { if(noWorldRegions) {
AreaShop.warn("Remove these regions from AreaShop with '/as del' or load the world(s) on the server again."); AreaShop.warn("Remove these regions from AreaShop with '/as del' or load the world(s) on the server again.");
} }
if(!incorrectDuration.isEmpty()) { if(!incorrectDuration.isEmpty()) {
List<String> incorrectDurationNames = new ArrayList<>(); List<String> incorrectDurationNames = new ArrayList<>();
for(GeneralRegion region : incorrectDuration) { for(GeneralRegion region : incorrectDuration) {
@ -951,8 +978,10 @@ public class FileManager {
} }
AreaShop.warn("The following regions have an incorrect time format as duration: "+Utils.createCommaSeparatedList(incorrectDurationNames)); AreaShop.warn("The following regions have an incorrect time format as duration: "+Utils.createCommaSeparatedList(incorrectDurationNames));
} }
plugin.setReady(true);
} }
}.runTask(plugin); }.runTask(plugin);
} }
} }

View File

@ -7,6 +7,7 @@ import me.wiefferink.areashop.AreaShop;
import me.wiefferink.areashop.Utils; import me.wiefferink.areashop.Utils;
import me.wiefferink.areashop.events.NotifyRegionEvent; import me.wiefferink.areashop.events.NotifyRegionEvent;
import me.wiefferink.areashop.events.notify.UpdateRegionEvent; import me.wiefferink.areashop.events.notify.UpdateRegionEvent;
import me.wiefferink.areashop.features.Feature;
import me.wiefferink.areashop.features.FriendsFeature; import me.wiefferink.areashop.features.FriendsFeature;
import me.wiefferink.areashop.features.SignsFeature; import me.wiefferink.areashop.features.SignsFeature;
import me.wiefferink.areashop.interfaces.GeneralRegionInterface; import me.wiefferink.areashop.interfaces.GeneralRegionInterface;
@ -42,6 +43,8 @@ public abstract class GeneralRegion implements GeneralRegionInterface, Comparabl
private FriendsFeature friendsFeature; private FriendsFeature friendsFeature;
private SignsFeature signsFeature; private SignsFeature signsFeature;
private Map<Class, Feature> features;
// Enum for region types // Enum for region types
public enum RegionType { public enum RegionType {
RENT("rent"), RENT("rent"),
@ -146,10 +149,32 @@ public abstract class GeneralRegion implements GeneralRegionInterface, Comparabl
setupFeatures(); setupFeatures();
} }
// Create instance of the features /**
public void setupFeatures() { * Deregister everything
friendsFeature = new FriendsFeature(this); */
signsFeature = new SignsFeature(this); public void destroy() {
destroyFeatures();
}
/**
* Setup the features of this class
*/
private void setupFeatures() {
addFeature(new FriendsFeature(this));
addFeature(new SignsFeature(this));
}
private void addFeature(Feature feature) {
features.put(feature.getClass(), feature);
}
/**
* Destroy the features created for this region
*/
private void destroyFeatures() {
friendsFeature.destroyFeature();
signsFeature.destroyFeature();
} }
/** /**