From b0a3cf9ffeddbe8c536cc0f5f1153ad2a82ef54b Mon Sep 17 00:00:00 2001 From: dfsek Date: Sat, 24 Oct 2020 01:50:12 -0700 Subject: [PATCH] Catch more structure export exceptions, add more information to exception messages. --- .../structure/InitializationException.java | 17 ++++- .../com/dfsek/terra/structure/Structure.java | 64 ++++++++++--------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/dfsek/terra/structure/InitializationException.java b/src/main/java/com/dfsek/terra/structure/InitializationException.java index 8289a3a83..afb72f8d4 100644 --- a/src/main/java/com/dfsek/terra/structure/InitializationException.java +++ b/src/main/java/com/dfsek/terra/structure/InitializationException.java @@ -1,7 +1,22 @@ package com.dfsek.terra.structure; +import org.bukkit.Location; +import org.jetbrains.annotations.Nullable; + public class InitializationException extends Exception { - public InitializationException(String message) { + private final Location worldLoc; + public InitializationException(String message, @Nullable Location worldLoc) { super(message); + this.worldLoc = worldLoc == null ? null :worldLoc.clone(); + } + + @Override + public String getMessage() { + return super.getMessage() + " (at location: " + worldLoc.toString() + ")"; + } + + @Nullable + public Location getWorldLoc() { + return worldLoc; } } diff --git a/src/main/java/com/dfsek/terra/structure/Structure.java b/src/main/java/com/dfsek/terra/structure/Structure.java index 713b72305..6120cd784 100644 --- a/src/main/java/com/dfsek/terra/structure/Structure.java +++ b/src/main/java/com/dfsek/terra/structure/Structure.java @@ -79,39 +79,43 @@ public class Structure implements Serializable { StructureContainedBlock.Pull pull = StructureContainedBlock.Pull.NONE; int pullOffset = 0; StructureSpawnRequirement requirement = StructureSpawnRequirement.BLANK; - if(state instanceof Sign) { // Magic sign stuff - Sign s = (Sign) b.getState(); - if(s.getLine(0).equals("[TERRA]")) { - try { - d = Bukkit.createBlockData(s.getLine(2) + s.getLine(3)); - useState = false; - if(s.getLine(1).equals("[CENTER]")) { - centerX = x; - centerZ = z; - } else if(s.getLine(1).startsWith("[SPAWN=") && s.getLine(1).endsWith("]")) { - String og = s.getLine(1); - String spawn = og.substring(og.indexOf("=")+1, og.length()-1); - try { - requirement = StructureSpawnRequirement.valueOf(spawn.toUpperCase()); - } catch(IllegalArgumentException e) { - throw new InitializationException("Invalid spawn type: " + spawn); + try { + if(state instanceof Sign) { // Magic sign stuff + Sign s = (Sign) b.getState(); + if(s.getLine(0).equals("[TERRA]")) { + try { + d = Bukkit.createBlockData(s.getLine(2) + s.getLine(3)); + useState = false; + if(s.getLine(1).equals("[CENTER]")) { + centerX = x; + centerZ = z; + } else if(s.getLine(1).startsWith("[SPAWN=") && s.getLine(1).endsWith("]")) { + String og = s.getLine(1); + String spawn = og.substring(og.indexOf("=") + 1, og.length() - 1); + try { + requirement = StructureSpawnRequirement.valueOf(spawn.toUpperCase()); + } catch(IllegalArgumentException e) { + throw new InitializationException("Invalid spawn type: " + spawn, b.getLocation()); + } + } else if(s.getLine(1).startsWith("[PULL=") && s.getLine(1).endsWith("]")) { + String og = s.getLine(1); + String spawn = og.substring(og.indexOf("=") + 1, og.indexOf("_")); + pullOffset = Integer.parseInt(og.substring(og.indexOf("_") + 1, og.length() - 1)); + try { + pull = StructureContainedBlock.Pull.valueOf(spawn); + } catch(IllegalArgumentException e) { + throw new InitializationException("Invalid pull type: " + spawn, b.getLocation()); + } + } else { + throw new InitializationException("Invalid Magic Sign: \"" + s.getLine(1) + "\"", b.getLocation()); } - } else if(s.getLine(1).startsWith("[PULL=") && s.getLine(1).endsWith("]")) { - String og = s.getLine(1); - String spawn = og.substring(og.indexOf("=")+1, og.indexOf("_")); - pullOffset = Integer.parseInt(og.substring(og.indexOf("_")+1, og.length()-1)); - try { - pull = StructureContainedBlock.Pull.valueOf(spawn); - } catch(IllegalArgumentException e) { - throw new InitializationException("Invalid pull type: " + spawn); - } - } else { - throw new InitializationException("Invalid Magic Sign: \"" + s.getLine(1) + "\""); + } catch(IllegalArgumentException e) { + throw new InitializationException("Invalid Block Data on sign: \"" + s.getLine(2) + s.getLine(3) + "\"", b.getLocation()); } - } catch(IllegalArgumentException e) { - throw new InitializationException("Invalid Block Data on sign: \"" + s.getLine(2) + s.getLine(3) + "\""); } } + } catch(StringIndexOutOfBoundsException e) { + throw new InitializationException("Invalid sign.", b.getLocation()); } StructureContainedBlock block = new StructureContainedBlock(x, y, z, useState ? state : null, d, requirement, pull, pullOffset); if(state instanceof BlockInventoryHolder) { @@ -122,7 +126,7 @@ public class Structure implements Serializable { } } } - if(centerX < 0 || centerZ < 0) throw new InitializationException("No structure center specified."); + if(centerX < 0 || centerZ < 0) throw new InitializationException("No structure center specified.", null); structureInfo = new StructureInfo(l2.getBlockX()-l1.getBlockX()+1, l2.getBlockY()-l1.getBlockY()+1, l2.getBlockZ()-l1.getBlockZ()+1, new Vector2(centerX, centerZ)); }