From bfde05eb5c407588d78c27068ff4271eec46cdfc Mon Sep 17 00:00:00 2001 From: Drakia Date: Thu, 10 Mar 2011 01:14:52 -0800 Subject: [PATCH] Cleaned up code a bit Added more output for when gates can't be loaded --- src/net/TheDgtl/Stargate/Gate.java | 19 ++-- src/net/TheDgtl/Stargate/Portal.java | 132 +++++++++++---------------- 2 files changed, 58 insertions(+), 93 deletions(-) diff --git a/src/net/TheDgtl/Stargate/Gate.java b/src/net/TheDgtl/Stargate/Gate.java index 0632f07..29a8b9c 100644 --- a/src/net/TheDgtl/Stargate/Gate.java +++ b/src/net/TheDgtl/Stargate/Gate.java @@ -363,16 +363,17 @@ public class Gate { } public static void populateDefaults(String gateFolder) { + int Obsidian = Material.OBSIDIAN.getId(); Integer[][] layout = new Integer[][] { - {ANYTHING, Portal.OBSIDIAN, Portal.OBSIDIAN, ANYTHING}, - {Portal.OBSIDIAN, ENTRANCE, ENTRANCE, Portal.OBSIDIAN}, + {ANYTHING, Obsidian,Obsidian, ANYTHING}, + {Obsidian, ENTRANCE, ENTRANCE, Obsidian}, {CONTROL, ENTRANCE, ENTRANCE, CONTROL}, - {Portal.OBSIDIAN, EXIT, ENTRANCE, Portal.OBSIDIAN}, - {ANYTHING, Portal.OBSIDIAN, Portal.OBSIDIAN, ANYTHING}, + {Obsidian, EXIT, ENTRANCE, Obsidian}, + {ANYTHING, Obsidian, Obsidian, ANYTHING}, }; HashMap types = new HashMap(); - types.put('X', Portal.OBSIDIAN); - types.put('-', Portal.OBSIDIAN); + types.put('X', Obsidian); + types.put('-', Obsidian); Gate gate = new Gate("nethergate.gate", layout, types); gate.save(gateFolder); @@ -401,10 +402,4 @@ public class Gate { return name.endsWith(".gate"); } } - - public enum CostFor { - Using, - Activating, - Creating - }; } diff --git a/src/net/TheDgtl/Stargate/Portal.java b/src/net/TheDgtl/Stargate/Portal.java index 4e1eaa4..2df506d 100644 --- a/src/net/TheDgtl/Stargate/Portal.java +++ b/src/net/TheDgtl/Stargate/Portal.java @@ -6,7 +6,6 @@ import java.io.FileWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; -import java.util.Set; import java.util.logging.Level; import org.bukkit.Location; @@ -28,40 +27,41 @@ import org.bukkit.util.Vector; * @author Dinnerbone */ public class Portal { - public static final int OBSIDIAN = 49; - public static final int FIRE = 51; - public static final int SIGN = 68; - public static final int BUTTON = 77; + // Variables used to store portal lists private static final HashMap lookupBlocks = new HashMap(); private static final HashMap lookupEntrances = new HashMap(); private static final ArrayList allPortals = new ArrayList(); private static final HashMap> allPortalsNet = new HashMap>(); private static final HashMap> lookupNamesNet = new HashMap>(); + + // Gate location block info private Blox topLeft; private int modX; private int modZ; private float rotX; + // Block references private SignPost id; - private String name; - private String destination; private Blox button; - private Player player; private Blox[] frame; private Blox[] entrances; - private boolean verified; - private boolean fixed; - private ArrayList destinations = new ArrayList(); + // Gate information + private String name; + private String destination; private String network; private Gate gate; - private boolean isOpen = false; private String owner = ""; - private HashMap exits; - private HashMap reverseExits; private boolean hidden = false; - private Player activePlayer; private boolean alwaysOn = false; private boolean priv = false; private World world; + // Gate options + private boolean verified; + private boolean fixed; + // In-use information + private Player player; + private Player activePlayer; + private ArrayList destinations = new ArrayList(); + private boolean isOpen = false; private long openTime; private Portal(Blox topLeft, int modX, int modZ, @@ -211,7 +211,7 @@ public class Portal { Location traveller = new Location(this.world, vehicle.getLocation().getX(), vehicle.getLocation().getY(), vehicle.getLocation().getZ()); Location exit = getExit(traveller, origin); - double velocity = vehicle.getVelocity().length(); + //double velocity = vehicle.getVelocity().length(); // Stop and teleport vehicle.setVelocity(new Vector()); @@ -454,53 +454,6 @@ public class Portal { return frame; } - public HashMap getExits() { - if (exits == null) { - exits = new HashMap(); - reverseExits = new HashMap(); - HashMap relativeExits = gate.getExits(); - Set exitBlocks = relativeExits.keySet(); - - for (RelativeBlockVector vector : exitBlocks) { - Blox block = getBlockAt(vector); - Integer position = relativeExits.get(vector); - exits.put(block, position); - reverseExits.put(position, block); - } - } - - return exits; - } - - public HashMap getReverseExits() { - if (reverseExits == null) { - getExits(); - } - - return reverseExits; - } - - @Override - public int hashCode() { - return getName().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - - Portal portal = (Portal) obj; - return this.getName().equalsIgnoreCase(portal.getName()); - } - public void unregister() { Stargate.log.info("[Stargate] Unregistering gate " + getName()); close(true); @@ -540,18 +493,10 @@ public class Portal { saveAllGates(world); } - private Blox getBlockAt(int right, int depth) { - return getBlockAt(right, depth, 0); - } - private Blox getBlockAt(RelativeBlockVector vector) { return topLeft.modRelative(vector.getRight(), vector.getDepth(), vector.getDistance(), modX, 1, modZ); } - private Blox getBlockAt(int right, int depth, int distance) { - return topLeft.modRelative(right, depth, distance, modX, 1, modZ); - } - private void register() { if (!lookupNamesNet.containsKey(getNetwork().toLowerCase())) lookupNamesNet.put(getNetwork().toLowerCase(), new HashMap()); @@ -577,11 +522,6 @@ public class Portal { allPortalsNet.get(getNetwork().toLowerCase()).add(getName().toLowerCase()); } - @Override - public String toString() { - return String.format("Portal [id=%s, name=%s, type=%s]", id, name, gate.getFilename()); - } - public static Portal createPortal(SignPost id, Player player) { Block idParent = id.getParent(); if (idParent == null) return null; @@ -672,7 +612,7 @@ public class Portal { // No button on an always-open gate. if (!alwaysOn) { button = topleft.modRelative(buttonVector.getRight(), buttonVector.getDepth(), buttonVector.getDistance() + 1, modX, 1, modZ); - button.setType(BUTTON); + button.setType(Material.STONE_BUTTON.getId()); button.setData(facing); } portal = new Portal(topleft, modX, modZ, rotX, id, button, destName, name, true, network, gate, player.getName(), hidden, alwaysOn, priv); @@ -721,7 +661,8 @@ public class Portal { BufferedWriter bw = new BufferedWriter(new FileWriter(loc, false)); for (Portal portal : allPortals) { - if (!portal.world.getName().equals(world.getName())) continue; + String wName = portal.world.getName(); + if (!wName.equalsIgnoreCase(world.getName())) continue; StringBuilder builder = new StringBuilder(); Blox sign = new Blox(portal.id.getBlock()); Blox button = portal.button; @@ -756,7 +697,6 @@ public class Portal { builder.append(':'); builder.append(portal.world.getName()); - bw.append(builder.toString()); bw.newLine(); } @@ -793,6 +733,7 @@ public class Portal { } String[] split = line.split(":"); if (split.length < 8) { + Stargate.log.info("[Stargate] Invalid line - " + l); continue; } String name = split[0]; @@ -808,6 +749,10 @@ public class Portal { float rotX = Float.parseFloat(split[5]); Blox topLeft = new Blox(world, split[6]); Gate gate = (split[7].contains(";")) ? Gate.getGateByName("nethergate.gate") : Gate.getGateByName(split[7]); + if (gate == null) { + Stargate.log.info("[Stargate] Invalid gate layout on line " + l + " [" + split[7] + "]"); + continue; + } String dest = (split.length > 8) ? split[8] : ""; String network = (split.length > 9) ? split[9] : Stargate.getDefaultNetwork(); @@ -821,7 +766,6 @@ public class Portal { portal.close(true); // Verify portal integrity/register portal if (!portal.isVerified() || !portal.checkIntegrity()) { - portal.close(true); portal.unregister(); Stargate.log.info("[Stargate] Destroying stargate at " + portal.toString()); } else { @@ -859,7 +803,6 @@ public class Portal { Stargate.log.info("Closing all stargates."); for (Portal p : allPortals) { if (p == null) continue; - //if (!p.isLoaded()) continue; p.close(true); } } @@ -867,4 +810,31 @@ public class Portal { public static String filterName(String input) { return input.replaceAll("[\\|:#]", "").trim(); } + + @Override + public String toString() { + return String.format("Portal [id=%s, network=%s name=%s, type=%s]", id, network, name, gate.getFilename()); + } + + @Override + public int hashCode() { + return getName().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + + Portal portal = (Portal) obj; + return (this.getNetwork().equalsIgnoreCase(portal.getNetwork()) && + this.getName().equalsIgnoreCase(portal.getName())); + } }