BaseGate will throw exceptions when something goes wrong

This commit is contained in:
Tobias Ottenweller 2012-05-17 16:43:21 +02:00
parent 2ee725dc21
commit 69571ea511

View File

@ -9,7 +9,6 @@ import org.bukkit.block.Block;
import org.mcteam.ancientgates.util.FloodUtil; import org.mcteam.ancientgates.util.FloodUtil;
public abstract class BaseGate public abstract class BaseGate
{ {
/* /*
@ -34,12 +33,12 @@ public abstract class BaseGate
} }
public void setLocation(Location location) public void setLocation(Location location) throws Exception
{ {
this.location = location; this.location = location;
if (isOpen) { if (isOpen) {
findPortalBlocks(); fillGate();
validate(); validate();
} }
} }
@ -51,7 +50,7 @@ public abstract class BaseGate
} }
public void setExit(Location exit) public void setExit(Location exit) throws Exception
{ {
this.exit = exit; this.exit = exit;
validate(); validate();
@ -64,7 +63,7 @@ public abstract class BaseGate
} }
public void setHidden(boolean isHidden) public void setHidden(boolean isHidden) throws Exception
{ {
this.isHidden = isHidden; this.isHidden = isHidden;
@ -85,7 +84,7 @@ public abstract class BaseGate
} }
public void setOpen(boolean isOpen) public void setOpen(boolean isOpen) throws Exception
{ {
if (isOpen == true && this.isOpen == false) { if (isOpen == true && this.isOpen == false) {
findPortalBlocks(); findPortalBlocks();
@ -116,6 +115,9 @@ public abstract class BaseGate
protected void fillGate() protected void fillGate()
{ {
emptyGate();
findPortalBlocks();
// This is not to do an effect // This is not to do an effect
// It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P // It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P
for (Location l : gateBlockLocations) { for (Location l : gateBlockLocations) {
@ -140,14 +142,10 @@ public abstract class BaseGate
protected void findPortalBlocks() protected void findPortalBlocks()
{ {
gateBlockLocations = new HashSet<Location>();
Set<Block> gateBlocks = FloodUtil.getGateFrameBlocks(location.getBlock()); Set<Block> gateBlocks = FloodUtil.getGateFrameBlocks(location.getBlock());
if (gateBlocks == null) { if (gateBlocks != null) {
gateBlockLocations = null;
}
else {
gateBlockLocations = new HashSet<Location>();
for (Block b : gateBlocks) { for (Block b : gateBlocks) {
gateBlockLocations.add(b.getLocation()); gateBlockLocations.add(b.getLocation());
} }
@ -162,24 +160,33 @@ public abstract class BaseGate
/** /**
* Checks if valus attributes do add up; will close gate on wrong values. * Checks if valus attributes do add up; will close gate on wrong values.
*/ */
public void validate() public void validate() throws Exception
{ {
if (!isOpen) { if (!isOpen) {
return; return;
} }
if (location == null || exit == null) { if (location == null) {
isOpen = false; setOpen(false);
emptyGate(); throw new Exception("Gate got closed. It has no location.");
return;
} }
if (exit == null) {
setOpen(false);
throw new Exception("Gate got closed. It has no exit.");
}
if (gateBlockLocations.size() == 0) {
setOpen(false);
throw new Exception("Gate got closed. The frame is missing or broken.");
}
if (isHidden == false) { if (isHidden == false) {
for (Location l : gateBlockLocations) { for (Location l : gateBlockLocations) {
if (l.getBlock().getType() != Material.PORTAL) { if (l.getBlock().getType() == Material.AIR) {
isOpen = false; setOpen(false);
emptyGate(); throw new Exception("Gate got closed. The frame is missing or broken.");
return;
} }
} }
} }