Refactored the Gate class: removed the fill and empty methods.
This commit is contained in:
		| @@ -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(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -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(); |  | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
| @@ -159,20 +141,13 @@ public class Gate implements ConfigurationSerializable | |||||||
| 	{ | 	{ | ||||||
| 		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."); |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Tobias Ottenweller
					Tobias Ottenweller