diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 75824b2..fe26130 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -82,11 +82,7 @@ public class Portal { /** * Instantiates a new portal * - * @param topLeft
The top-left block of the portal. This is used to decide the positions of the rest of the portal
- * @param modX - * @param modZ - * @param yaw - * @param idThe location of the portal's id block, which is the sign which activated the portal
+ * @param portalLocationObject containing locations of all relevant blocks
* @param buttonThe location of the portal's open button
* @param destinationThe destination defined on the sign's destination line
* @param nameThe name of the portal defined on the sign's first line
@@ -97,15 +93,15 @@ public class Portal { * @param ownerNameThe name of the gate's owner
* @param optionsA map containing all possible portal options
*/ - Portal(BlockLocation topLeft, int modX, int modZ, float yaw, BlockLocation id, BlockLocation button, + Portal(PortalLocation portalLocation, BlockLocation button, String destination, String name, boolean verified, String network, Gate gate, UUID ownerUUID, String ownerName, MapThe created portal
*/ public static Portal createPortal(SignChangeEvent event, Player player) { - BlockLocation id = new BlockLocation(event.getBlock()); - Block idParent = id.getParent(); - if (idParent == null) { - return null; - } - - if (GateHandler.getGatesByControlBlock(idParent).length == 0) { + BlockLocation signLocation = new BlockLocation(event.getBlock()); + Block idParent = signLocation.getParent(); + + //Return early if the sign is not placed on a block, or the block is not a control block + if (idParent == null || GateHandler.getGatesByControlBlock(idParent).length == 0) { return null; } + //The control block is already part of another portal if (getByBlock(idParent) != null) { Stargate.debug("createPortal", "idParent belongs to existing stargate"); return null; } //Get necessary information from the gate's sign - BlockLocation parent = new BlockLocation(player.getWorld(), idParent.getX(), idParent.getY(), idParent.getZ()); - BlockLocation topLeft = null; String portalName = filterName(event.getLine(0)); String destinationName = filterName(event.getLine(1)); String network = filterName(event.getLine(2)); @@ -266,88 +263,48 @@ public class PortalHandler { MapThe enabled portal options
+ * @param playerThe player trying to create the new portal
+ * @param destinationNameThe name of the portal's destination
+ * @param networkThe name of the portal's network
+ * @returnFalse if the portal is an invalid bungee portal. True otherwise
+ */ + private static boolean isValidBungeePortal(MapThe location data for the new portal
+ * @param playerThe player trying to create the new portal
+ * @returnThe matching gate type, or null if no such gate could be found
+ */ + private static Gate findMatchingGate(PortalLocation portalLocation, Player player) { + Block signParent = portalLocation.getSignLocation().getParent(); + BlockLocation parent = new BlockLocation(player.getWorld(), signParent.getX(), signParent.getY(), + signParent.getZ()); + + //Get all gates with the used type of control blocks + Gate[] possibleGates = GateHandler.getGatesByControlBlock(signParent); + int modX = portalLocation.getModX(); + int modZ = portalLocation.getModZ(); + Gate gate = null; + + for (Gate possibleGate : possibleGates) { + //Get gate controls + RelativeBlockVector[] vectors = possibleGate.getLayout().getControls(); + + portalLocation.setButtonVector(null); + for (RelativeBlockVector controlVector : vectors) { + //Assuming the top-left location is pointing to the gate's top-left location, check if it's a match + BlockLocation possibleTopLocation = parent.modRelative(-controlVector.getRight(), + -controlVector.getDepth(), -controlVector.getDistance(), modX, 1, modZ); + if (possibleGate.matches(possibleTopLocation, modX, modZ, true)) { + gate = possibleGate; + portalLocation.setTopLeft(possibleTopLocation); + } else { + portalLocation.setButtonVector(controlVector); + } } } - Portal portal; - portal = new Portal(topLeft, modX, modZ, yaw, id, null, destinationName, portalName, false, network, - gate, player.getUniqueId(), player.getName(), portalOptions); + return gate; + } - int cost = EconomyHandler.getCreateCost(player, gate); + /** + * Checks whether the new portal conflicts with an existing portal + * + * @param gateThe gate type of the new portal
+ * @param topLeftThe top-left block of the new portal
+ * @param modXThe x-modifier for the new portal
+ * @param modZThe new z-modifier for the new portal
+ * @param playerThe player creating the new portal
+ * @returnTrue if a conflict was found. False otherwise
+ */ + private static boolean conflictsWithExistingPortal(Gate gate, BlockLocation topLeft, int modX, int modZ, + Player player) { + //TODO: Make a quicker check. Could just check for control block conflicts if all code is changed to account for + // getting several hits at a single location when checking for the existence of a portal. May make + // everything slower overall? Would make for cooler gates though. + for (RelativeBlockVector borderVector : gate.getLayout().getBorder()) { + BlockLocation borderBlockLocation = topLeft.modRelative(borderVector.getRight(), borderVector.getDepth(), + borderVector.getDistance(), modX, 1, modZ); + if (getByBlock(borderBlockLocation.getBlock()) != null) { + Stargate.debug("createPortal", "Gate conflicts with existing gate"); + Stargate.sendErrorMessage(player, Stargate.getString("createConflict")); + return true; + } + } + return false; + } + + /** + * Creates and validates a new portal + * + * @param destinationNameThe name of the portal's destination
+ * @param portalNameThe name of the new portal
+ * @param networkThe name of the new portal's network
+ * @param gateThe gate type used in the physical construction of the new portal
+ * @param playerThe player creating the new portal
+ * @param portalOptionsA map of enabled and disabled portal options
+ * @param denyMessageThe deny message to display if the portal creation was denied
+ * @param linesAll the lines of the sign which initiated the portal creation
+ * @param denyWhether to deny the creation of the new portal
+ * @returnA new portal, or null if the input cases the creation to be denied
+ */ + private static Portal createAndValidateNewPortal(PortalLocation portalLocation, String destinationName, + String portalName, String network, Gate gate, Player player, + MapThe portal to validate
- * @param playerThe player creating the portal
- * @param costThe cost of creating the portal
+ * + * @param portalThe portal to validate
+ * @param playerThe player creating the portal
+ * @param costThe cost of creating the portal
* @param portalNameThe name of the newly created portal
* @returnTrue if the portal is completely valid
*/ @@ -884,12 +953,13 @@ public class PortalHandler { private static void loadPortal(String[] portalData, World world, int lineIndex) { //Load min. required portal data String name = portalData[0]; - BlockLocation sign = new BlockLocation(world, portalData[1]); + PortalLocation portalLocation = new PortalLocation(); + portalLocation.setSignLocation(new BlockLocation(world, portalData[1])); BlockLocation button = (portalData[2].length() > 0) ? new BlockLocation(world, portalData[2]) : null; - int modX = Integer.parseInt(portalData[3]); - int modZ = Integer.parseInt(portalData[4]); - float yaw = Float.parseFloat(portalData[5]); - BlockLocation topLeft = new BlockLocation(world, portalData[6]); + portalLocation.setModX(Integer.parseInt(portalData[3])); + portalLocation.setModZ(Integer.parseInt(portalData[4])); + portalLocation.setYaw(Float.parseFloat(portalData[5])); + portalLocation.setTopLeft(new BlockLocation(world, portalData[6])); Gate gate = GateHandler.getGateByName(portalData[7]); if (gate == null) { Stargate.logger.info(Stargate.getString("prefix") + "Gate layout on line " + lineIndex + @@ -923,7 +993,7 @@ public class PortalHandler { } //Creates the new portal - Portal portal = new Portal(topLeft, modX, modZ, yaw, sign, button, destination, name, false, + Portal portal = new Portal(portalLocation, button, destination, name, false, network, gate, ownerUUID, ownerName, getPortalOptions(portalData)); registerPortal(portal);