Refactored the Gate class: removed the fill and empty methods.

This commit is contained in:
Tobias Ottenweller 2013-05-26 14:03:18 +02:00
parent 3cac4e70cc
commit 30d15c141e

View File

@ -1,26 +1,16 @@
package de.craftinc.gates; package de.craftinc.gates;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import de.craftinc.gates.util.FloodUtil; import de.craftinc.gates.util.FloodUtil;
import de.craftinc.gates.util.LocationUtil; import de.craftinc.gates.util.LocationUtil;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import java.util.*;
public class Gate implements ConfigurationSerializable public class Gate implements ConfigurationSerializable
{ {
/*
* ATTRIBUTES
*/
protected Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */ protected Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */
protected Set<Location> gateBlockLocations = new HashSet<Location>(); /* Locations of the blocks inside the gate */ protected Set<Location> gateBlockLocations = new HashSet<Location>(); /* Locations of the blocks inside the gate */
@ -73,7 +63,7 @@ public class Gate implements ConfigurationSerializable
this.location = location; this.location = location;
if (isOpen) { if (isOpen) {
fillGate(); findPortalBlocks();
validate(); validate();
} }
} }
@ -96,7 +86,7 @@ public class Gate implements ConfigurationSerializable
* supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an * supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an
* exception is thrown. * exception is thrown.
*/ */
public void setExit(Location exit) throws Exception public void setExit(Location exit) throws Exception
{ {
this.exit = exit; this.exit = exit;
validate(); validate();
@ -137,15 +127,7 @@ public class Gate implements ConfigurationSerializable
public void setHidden(boolean isHidden) throws Exception public void setHidden(boolean isHidden) throws Exception
{ {
this.isHidden = isHidden; this.isHidden = isHidden;
this.validate();
if (isHidden) {
emptyGate();
}
else if (isOpen()) {
fillGate();
}
validate();
} }
@ -158,21 +140,14 @@ public class Gate implements ConfigurationSerializable
public void setOpen(boolean isOpen) throws Exception public void setOpen(boolean isOpen) throws Exception
{ {
if (isOpen && !this.isOpen) { if (isOpen && !this.isOpen) {
findPortalBlocks(); findPortalBlocks();
if (!isHidden) {
fillGate();
}
}
else if (!isOpen && this.isOpen) {
emptyGate();
} }
this.isOpen = isOpen; this.isOpen = isOpen;
validate(); validate();
} }
/** /**
* *
* @return Will never return 'null' but might return an empty Set. * @return Will never return 'null' but might return an empty Set.
@ -183,37 +158,6 @@ public class Gate implements ConfigurationSerializable
} }
/*
* GATE BLOCK HANDLING
*/
protected void fillGate()
{
emptyGate();
findPortalBlocks();
// 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
for (Location l : gateBlockLocations) {
l.getBlock().setType(Material.GLOWSTONE);
}
for (Location l : gateBlockLocations) {
l.getBlock().setType(Material.PORTAL);
}
}
protected void emptyGate()
{
for (Location l : gateBlockLocations) {
if (l.getBlock().getType() == Material.PORTAL) {
l.getBlock().setType(Material.AIR);
}
}
}
protected void findPortalBlocks() protected void findPortalBlocks()
{ {
gateBlockLocations = new HashSet<Location>(); gateBlockLocations = new HashSet<Location>();
@ -227,10 +171,6 @@ public class Gate implements ConfigurationSerializable
} }
/*
* VALIDATION
*/
/** /**
* Checks if values attributes do add up; will close gate on wrong values. * Checks if values attributes do add up; will close gate on wrong values.
*/ */
@ -250,20 +190,16 @@ public class Gate implements ConfigurationSerializable
throw new Exception("Gate got closed. It has no exit."); throw new Exception("Gate got closed. It has no exit.");
} }
if (!isHidden) {
findPortalBlocks();
}
if (gateBlockLocations.size() == 0) { if (gateBlockLocations.size() == 0) {
setOpen(false); setOpen(false);
throw new Exception("Gate got closed. The frame is missing or broken."); throw new Exception("Gate got closed. The frame is missing or broken.");
} }
if (!isHidden) {
for (Location l : gateBlockLocations) {
if (l.getBlock().getType() == Material.AIR) {
setOpen(false);
throw new Exception("Gate got closed. The frame is missing or broken.");
}
}
}
} }
@ -285,7 +221,7 @@ public class Gate implements ConfigurationSerializable
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Gate(Map<String, Object> map) public Gate(Map<String, Object> map)
{ {
try { try {
id = map.get(idKey).toString(); id = map.get(idKey).toString();
if (id == null) { if (id == null) {