From 190aaf6f7636d5e5b129be1b9e3447c715634627 Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Wed, 26 Aug 2020 16:39:13 +0200 Subject: [PATCH] fix data deletion on saving error, fix reading griefprevention data --- gradle.properties | 2 +- .../flemmli97/flan/claim/ClaimStorage.java | 26 +++++++++++++------ .../flemmli97/flan/commands/CommandClaim.java | 3 ++- .../flan/player/PlayerClaimData.java | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/gradle.properties b/gradle.properties index 994bbdf..c670667 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx2G loader_version=0.9.1+build.205 # Mod Properties - mod_version = 1.0.0 + mod_version = 1.0.1 maven_group = com.flemmli97.flan archives_base_name = flan diff --git a/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java b/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java index b9549bc..2075537 100644 --- a/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java +++ b/src/main/java/com/flemmli97/flan/claim/ClaimStorage.java @@ -240,9 +240,9 @@ public class ClaimStorage { } } if (dirty) { - FileWriter writer = new FileWriter(file); JsonArray arr = new JsonArray(); e.getValue().forEach(claim -> arr.add(claim.toJson(new JsonObject()))); + FileWriter writer = new FileWriter(file); ConfigHandler.GSON.toJson(arr, writer); writer.close(); } @@ -252,7 +252,7 @@ public class ClaimStorage { } } - public static List readGriefPreventionData(MinecraftServer server) { + public static Set readGriefPreventionData(MinecraftServer server) { Yaml yml = new Yaml(); File griefPrevention = server.getSavePath(WorldSavePath.ROOT).resolve("plugins/GriefPreventionData/ClaimData").toFile(); if (!griefPrevention.exists()) @@ -262,15 +262,24 @@ public class ClaimStorage { try { //Get all parent claims + Set failedClaimsFile = Sets.newHashSet(); for (File f : griefPrevention.listFiles()) { + System.out.println("f " + f.getName()); if (f.getName().endsWith(".yml")) { FileReader reader = new FileReader(f); Map values = yml.load(reader); if (values.get("Parent Claim ID").equals(-1)) { - intFileMap.put(Integer.valueOf(values.get("Parent Claim ID").toString()), f); + try { + intFileMap.put(Integer.valueOf(f.getName().replace(".yml", "")), f); + } + catch (NumberFormatException e){ + failedClaimsFile.add(f.getName()); + } } } } + System.out.println(subClaimMap); + System.out.println(intFileMap); //Map child to parent claims for (File f : griefPrevention.listFiles()) { if (f.getName().endsWith(".yml")) { @@ -279,13 +288,12 @@ public class ClaimStorage { if (!values.get("Parent Claim ID").equals(-1)) { subClaimMap.merge(intFileMap.get(Integer.valueOf(values.get("Parent Claim ID").toString())) , Lists.newArrayList(f), (key, val) -> { - val.add(f); - return val; + key.add(f); + return key; }); } } } - List failedClaimsFile = Lists.newArrayList(); for (File parent : intFileMap.values()) { Pair parentClaim = parseFromYaml(parent, yml, server); List childs = subClaimMap.get(parent); @@ -294,8 +302,10 @@ public class ClaimStorage { parentClaim.second.addSubClaimGriefprevention(parseFromYaml(childF, yml, server).second); } ClaimStorage storage = ClaimStorage.get(parentClaim.first); - if (storage.conflicts(parentClaim.second, null).isEmpty()) + if (storage.conflicts(parentClaim.second, null).isEmpty()) { + parentClaim.second.setClaimID(storage.generateUUID()); storage.addClaim(parentClaim.second); + } else failedClaimsFile.add(parent.getName()); } @@ -316,7 +326,7 @@ public class ClaimStorage { String[] greaterCorner = values.get("Greater Boundary Corner").toString().split(";"); ServerWorld world = server.getWorld(worldRegFromString(lesserCorner[0])); Claim claim = new Claim(Integer.parseInt(lesserCorner[1]), Integer.parseInt(greaterCorner[1]), - Integer.parseInt(lesserCorner[3]), Integer.parseInt(greaterCorner[3]), + Integer.parseInt(lesserCorner[3]), Integer.parseInt(greaterCorner[3]), ConfigHandler.config.defaultClaimDepth == 255?0: Integer.parseInt(lesserCorner[2]), owner, world); return Pair.of(world, claim); } diff --git a/src/main/java/com/flemmli97/flan/commands/CommandClaim.java b/src/main/java/com/flemmli97/flan/commands/CommandClaim.java index eb389df..17824c8 100644 --- a/src/main/java/com/flemmli97/flan/commands/CommandClaim.java +++ b/src/main/java/com/flemmli97/flan/commands/CommandClaim.java @@ -37,6 +37,7 @@ import net.minecraft.world.World; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.CompletableFuture; public class CommandClaim { @@ -318,7 +319,7 @@ public class CommandClaim { private static int readGriefPreventionData(CommandContext context) { ServerCommandSource src = context.getSource(); src.sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionData, Formatting.GOLD), true); - List errors = ClaimStorage.readGriefPreventionData(src.getMinecraftServer()); + Set errors = ClaimStorage.readGriefPreventionData(src.getMinecraftServer()); PlayerClaimData.readGriefPreventionPlayerData(src.getMinecraftServer()); if (errors == null) src.sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.readGriefpreventionDataSuccess, Formatting.GOLD), true); diff --git a/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java b/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java index 83b0d03..81431ee 100644 --- a/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java +++ b/src/main/java/com/flemmli97/flan/player/PlayerClaimData.java @@ -184,10 +184,10 @@ public class PlayerClaimData { File file = new File(dir, this.player.getUuid() + ".json"); if (!file.exists()) file.createNewFile(); - FileWriter writer = new FileWriter(file); JsonObject obj = new JsonObject(); obj.addProperty("ClaimBlocks", this.claimBlocks); obj.addProperty("AdditionalBlocks", this.additionalClaimBlocks); + FileWriter writer = new FileWriter(file); ConfigHandler.GSON.toJson(obj, writer); writer.close(); } catch (IOException e) {