9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-30 20:29:18 +00:00

Implements #20

This commit is contained in:
SamB440
2020-08-14 17:39:09 +01:00
parent a2ea012140
commit ffb1efd2a3
3 changed files with 65 additions and 19 deletions

View File

@@ -82,7 +82,11 @@ public class RPGRegionsCommand extends BaseCommand {
@CommandCompletion("@regions")
public void onRemove(CommandSender sender, ConfiguredRegion configuredRegion) {
if (configuredRegion != null) {
configuredRegion.delete(plugin);
try {
configuredRegion.delete(plugin);
} catch (IOException e) {
e.printStackTrace();
}
plugin.getManagers().getRegionsCache().removeConfiguredRegion(configuredRegion.getId());
sender.sendMessage(StringUtils.colour("&cRemoved configured region " + configuredRegion.getId() + "!"));
} else {

View File

@@ -34,11 +34,14 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class RPGRegionsManagers {
@@ -99,20 +102,33 @@ public class RPGRegionsManagers {
e.printStackTrace();
}
for (File file : folder.listFiles()) {
// Exclude non-json files
if (file.getName().endsWith(".json")) {
try {
Reader reader = new FileReader(file);
ConfiguredRegion region = plugin.getGson().fromJson(reader, ConfiguredRegion.class);
if (!region.getId().equals("exampleconfig")) regionsCache.addConfiguredRegion(region);
if (region.getRequirements() != null) region.getRequirements().forEach(regionRequirement -> plugin.getLogger().warning("Warning: Region " + region.getId() + " uses requirements. These are highly experimental and there may be bypasses."));
reader.close();
} catch (Exception e) {
plugin.getLogger().severe("Error loading region config " + file.getName() + ":");
e.printStackTrace();
}
}
try {
Files.walk(Paths.get(folder.getPath()))
.filter(Files::isRegularFile)
.collect(Collectors.toList())
.forEach(path -> {
File file = path.toFile();
if (regionsCache.getConfiguredRegions().containsKey(file.getName().replace(".json", ""))) {
plugin.getLogger().severe("Duplicate region files have been found for " + file.getName() + ". " +
"In order to protect your data, the plugin will NOT load the duplicate region config.");
return;
}
// Exclude non-json files
if (file.getName().endsWith(".json")) {
try {
Reader reader = new FileReader(file);
ConfiguredRegion region = plugin.getGson().fromJson(reader, ConfiguredRegion.class);
if (!region.getId().equals("exampleconfig")) regionsCache.addConfiguredRegion(region);
if (region.getRequirements() != null) region.getRequirements().forEach(regionRequirement -> plugin.getLogger().warning("Warning: Region " + region.getId() + " uses requirements. These are highly experimental and there may be bypasses."));
reader.close();
} catch (Exception e) {
plugin.getLogger().severe("Error loading region config " + file.getName() + ":");
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
this.regenerationManager = new RegenerationManager(plugin);

View File

@@ -27,10 +27,14 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class ConfiguredRegion {
@@ -298,7 +302,8 @@ public class ConfiguredRegion {
}
public void save(RPGRegions plugin) throws IOException {
File file = new File(plugin.getDataFolder() + "/regions/" + this.id + ".json");
File file = this.findFile(plugin);
if (file == null) file = new File(plugin.getDataFolder() + "/regions/" + this.id + ".json");
Writer writer = new FileWriter(file);
Gson gson = plugin.getGson();
gson.toJson(this, writer);
@@ -306,8 +311,29 @@ public class ConfiguredRegion {
writer.close();
}
public void delete(RPGRegions plugin) {
File file = new File(plugin.getDataFolder() + "/regions/" + this.id + ".json");
file.delete();
public boolean delete(RPGRegions plugin) throws IOException {
File file = this.findFile(plugin);
if (file == null) file = new File(plugin.getDataFolder() + "/regions/" + this.id + ".json");
return file.delete();
}
@Nullable
private File findFile(RPGRegions plugin) throws IOException {
File folder = new File(plugin.getDataFolder() + "/regions/");
List<Path> valid = Files.walk(Paths.get(folder.getPath()))
.filter(path -> Files.isRegularFile(path) && path.toFile().getName().equals(this.id + ".json"))
.collect(Collectors.toList());
if (valid.isEmpty()) return null;
if (valid.size() > 1) {
plugin.getLogger().severe("Duplicate region files have been found for " + this.id + ". " +
"In order to protect your data, the plugin will NOT save the region config.");
throw new IOException("Duplicate region file");
}
File file = null;
for (Path path : valid) {
file = path.toFile();
}
return file;
}
}