Catch more structure export exceptions, add more information to exception messages.

This commit is contained in:
dfsek 2020-10-24 01:50:12 -07:00
parent 24c17f2ee6
commit b0a3cf9ffe
2 changed files with 50 additions and 31 deletions

View File

@ -1,7 +1,22 @@
package com.dfsek.terra.structure; package com.dfsek.terra.structure;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;
public class InitializationException extends Exception { public class InitializationException extends Exception {
public InitializationException(String message) { private final Location worldLoc;
public InitializationException(String message, @Nullable Location worldLoc) {
super(message); 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;
} }
} }

View File

@ -79,39 +79,43 @@ public class Structure implements Serializable {
StructureContainedBlock.Pull pull = StructureContainedBlock.Pull.NONE; StructureContainedBlock.Pull pull = StructureContainedBlock.Pull.NONE;
int pullOffset = 0; int pullOffset = 0;
StructureSpawnRequirement requirement = StructureSpawnRequirement.BLANK; StructureSpawnRequirement requirement = StructureSpawnRequirement.BLANK;
if(state instanceof Sign) { // Magic sign stuff try {
Sign s = (Sign) b.getState(); if(state instanceof Sign) { // Magic sign stuff
if(s.getLine(0).equals("[TERRA]")) { Sign s = (Sign) b.getState();
try { if(s.getLine(0).equals("[TERRA]")) {
d = Bukkit.createBlockData(s.getLine(2) + s.getLine(3)); try {
useState = false; d = Bukkit.createBlockData(s.getLine(2) + s.getLine(3));
if(s.getLine(1).equals("[CENTER]")) { useState = false;
centerX = x; if(s.getLine(1).equals("[CENTER]")) {
centerZ = z; centerX = x;
} else if(s.getLine(1).startsWith("[SPAWN=") && s.getLine(1).endsWith("]")) { centerZ = z;
String og = s.getLine(1); } else if(s.getLine(1).startsWith("[SPAWN=") && s.getLine(1).endsWith("]")) {
String spawn = og.substring(og.indexOf("=")+1, og.length()-1); String og = s.getLine(1);
try { String spawn = og.substring(og.indexOf("=") + 1, og.length() - 1);
requirement = StructureSpawnRequirement.valueOf(spawn.toUpperCase()); try {
} catch(IllegalArgumentException e) { requirement = StructureSpawnRequirement.valueOf(spawn.toUpperCase());
throw new InitializationException("Invalid spawn type: " + spawn); } 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("]")) { } catch(IllegalArgumentException e) {
String og = s.getLine(1); throw new InitializationException("Invalid Block Data on sign: \"" + s.getLine(2) + s.getLine(3) + "\"", b.getLocation());
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) + "\"");
} }
} }
} catch(StringIndexOutOfBoundsException e) {
throw new InitializationException("Invalid sign.", b.getLocation());
} }
StructureContainedBlock block = new StructureContainedBlock(x, y, z, useState ? state : null, d, requirement, pull, pullOffset); StructureContainedBlock block = new StructureContainedBlock(x, y, z, useState ? state : null, d, requirement, pull, pullOffset);
if(state instanceof BlockInventoryHolder) { 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)); structureInfo = new StructureInfo(l2.getBlockX()-l1.getBlockX()+1, l2.getBlockY()-l1.getBlockY()+1, l2.getBlockZ()-l1.getBlockZ()+1, new Vector2(centerX, centerZ));
} }