mirror of
https://github.com/NLthijs48/AreaShop.git
synced 2024-11-16 15:25:11 +01:00
File loading improvements to be more robust
This commit is contained in:
parent
bae3c0a199
commit
54b6e84558
@ -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);
|
||||
destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Dummy method a Feature implementation can override
|
||||
*/
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
* @param location The location to get the key for
|
||||
|
@ -857,7 +857,7 @@ public class FileManager {
|
||||
*/
|
||||
public void loadRegionFiles() {
|
||||
regions.clear();
|
||||
File file = new File(regionsPath);
|
||||
final File file = new File(regionsPath);
|
||||
if(!file.exists()) {
|
||||
if(!file.mkdirs()) {
|
||||
AreaShop.warn("Could not create region files directory: "+file.getAbsolutePath());
|
||||
@ -865,57 +865,82 @@ public class FileManager {
|
||||
}
|
||||
plugin.setReady(true);
|
||||
} else if(file.isDirectory()) {
|
||||
File[] regionFiles = file.listFiles();
|
||||
if(regionFiles == null) {
|
||||
return;
|
||||
}
|
||||
for(File regionFile : regionFiles) {
|
||||
if(regionFile.exists() && regionFile.isFile()) {
|
||||
// Load the region file from disk in UTF8 mode
|
||||
YamlConfiguration config;
|
||||
try(
|
||||
InputStreamReader reader = new InputStreamReader(new FileInputStream(regionFile), Charsets.UTF_8)
|
||||
) {
|
||||
config = YamlConfiguration.loadConfiguration(reader);
|
||||
if(config.getKeys(false).size() == 0) {
|
||||
AreaShop.warn("Region file '"+regionFile.getName()+"' is empty, check for errors in the log.");
|
||||
}
|
||||
} catch(IOException e) {
|
||||
AreaShop.warn("Something went wrong reading region file: "+regionFile.getAbsolutePath());
|
||||
continue;
|
||||
}
|
||||
// Construct the correct type of region
|
||||
if(RegionType.RENT.getValue().equals(config.getString("general.type"))) {
|
||||
RentRegion rent = new RentRegion(plugin, config);
|
||||
addRentNoSave(rent);
|
||||
} else if(RegionType.BUY.getValue().equals(config.getString("general.type"))) {
|
||||
BuyRegion buy = new BuyRegion(plugin, config);
|
||||
addBuyNoSave(buy);
|
||||
}
|
||||
}
|
||||
}
|
||||
plugin.setReady(true);
|
||||
new BukkitRunnable() {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
File[] regionFiles = file.listFiles();
|
||||
if(regionFiles == null) {
|
||||
plugin.setReady(true);
|
||||
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(GeneralRegion region : AreaShop.getInstance().getFileManager().getRegions()) {
|
||||
// Add broken regions to a list
|
||||
if(region != null) {
|
||||
if(region.getWorld() == null) {
|
||||
for(File regionFile : regionFiles) {
|
||||
if(regionFile.exists() && regionFile.isFile()) {
|
||||
|
||||
// Load the region file from disk in UTF8 mode
|
||||
YamlConfiguration config;
|
||||
try(
|
||||
InputStreamReader reader = new InputStreamReader(new FileInputStream(regionFile), Charsets.UTF_8)
|
||||
) {
|
||||
config = YamlConfiguration.loadConfiguration(reader);
|
||||
if(config.getKeys(false).size() == 0) {
|
||||
AreaShop.warn("Region file '"+regionFile.getName()+"' is empty, check for errors in the log.");
|
||||
}
|
||||
} catch(IOException e) {
|
||||
AreaShop.warn("Something went wrong reading region file: "+regionFile.getAbsolutePath());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Construct the correct type of region
|
||||
String type = config.getString("general.type");
|
||||
GeneralRegion region;
|
||||
if(RegionType.RENT.getValue().equals(type)) {
|
||||
region = new RentRegion(plugin, config);
|
||||
} else if(RegionType.BUY.getValue().equals(type)) {
|
||||
region = new BuyRegion(plugin, config);
|
||||
} else {
|
||||
noNamePaths.add(regionFile.getPath());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check consistency
|
||||
boolean added = false;
|
||||
if(region.getName() == null) {
|
||||
noNamePaths.add(regionFile.getPath());
|
||||
} else if(region.getWorld() == null) {
|
||||
noWorld.add(region);
|
||||
}
|
||||
if(region.getRegion() == null) {
|
||||
} else if(region.getRegion() == null) {
|
||||
noRegion.add(region);
|
||||
}
|
||||
if(region.isRentRegion() && !Utils.checkTimeFormat(((RentRegion)region).getDurationString())) {
|
||||
} else if(region instanceof RentRegion && !Utils.checkTimeFormat(((RentRegion)region).getDurationString())) {
|
||||
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()) {
|
||||
List<String> noRegionNames = new ArrayList<>();
|
||||
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("Remove these regions from AreaShop with '/as del' or recreate their regions in WorldGuard.");
|
||||
}
|
||||
|
||||
boolean noWorldRegions = !noWorld.isEmpty();
|
||||
while(!noWorld.isEmpty()) {
|
||||
List<GeneralRegion> toDisplay = new ArrayList<>();
|
||||
@ -944,6 +970,7 @@ public class FileManager {
|
||||
if(noWorldRegions) {
|
||||
AreaShop.warn("Remove these regions from AreaShop with '/as del' or load the world(s) on the server again.");
|
||||
}
|
||||
|
||||
if(!incorrectDuration.isEmpty()) {
|
||||
List<String> incorrectDurationNames = new ArrayList<>();
|
||||
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));
|
||||
}
|
||||
plugin.setReady(true);
|
||||
}
|
||||
}.runTask(plugin);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import me.wiefferink.areashop.AreaShop;
|
||||
import me.wiefferink.areashop.Utils;
|
||||
import me.wiefferink.areashop.events.NotifyRegionEvent;
|
||||
import me.wiefferink.areashop.events.notify.UpdateRegionEvent;
|
||||
import me.wiefferink.areashop.features.Feature;
|
||||
import me.wiefferink.areashop.features.FriendsFeature;
|
||||
import me.wiefferink.areashop.features.SignsFeature;
|
||||
import me.wiefferink.areashop.interfaces.GeneralRegionInterface;
|
||||
@ -42,6 +43,8 @@ public abstract class GeneralRegion implements GeneralRegionInterface, Comparabl
|
||||
private FriendsFeature friendsFeature;
|
||||
private SignsFeature signsFeature;
|
||||
|
||||
private Map<Class, Feature> features;
|
||||
|
||||
// Enum for region types
|
||||
public enum RegionType {
|
||||
RENT("rent"),
|
||||
@ -146,10 +149,32 @@ public abstract class GeneralRegion implements GeneralRegionInterface, Comparabl
|
||||
setupFeatures();
|
||||
}
|
||||
|
||||
// Create instance of the features
|
||||
public void setupFeatures() {
|
||||
friendsFeature = new FriendsFeature(this);
|
||||
signsFeature = new SignsFeature(this);
|
||||
/**
|
||||
* Deregister everything
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user