From 6d5c4802bc5809676e5e02f04b3db05a3450cb73 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 8 Oct 2021 15:28:12 +0200 Subject: [PATCH] Creates some new methods to get a location from a relative location which I can actually understand --- .../stargate/container/BlockLocation.java | 15 +++++ .../listener/PlayerEventListener.java | 2 +- .../net/knarcraft/stargate/portal/Portal.java | 65 ++++++++----------- .../stargate/portal/PortalHandler.java | 13 ++-- .../stargate/utility/DirectionHelper.java | 54 +++++++++++---- .../stargate/utility/PermissionHelper.java | 2 +- .../stargate/utility/SignHelper.java | 2 +- 7 files changed, 94 insertions(+), 59 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/container/BlockLocation.java b/src/main/java/net/knarcraft/stargate/container/BlockLocation.java index 7ea37e1..7fec741 100644 --- a/src/main/java/net/knarcraft/stargate/container/BlockLocation.java +++ b/src/main/java/net/knarcraft/stargate/container/BlockLocation.java @@ -1,5 +1,6 @@ package net.knarcraft.stargate.container; +import net.knarcraft.stargate.utility.DirectionHelper; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -8,6 +9,7 @@ import org.bukkit.block.BlockFace; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.Sign; import org.bukkit.block.data.type.WallSign; +import org.bukkit.util.Vector; /** * This class represents a block location @@ -81,6 +83,19 @@ public class BlockLocation extends Location { return newLocation.add(x, y, z); } + /** + * Gets a location relative to this block location + * + * @param vector

The relative block vector describing the relative location

+ * @param yaw

The yaw pointing in the distance direction

+ * @return

A location relative to this location

+ */ + public BlockLocation getRelativeLocation(RelativeBlockVector vector, double yaw) { + Vector combined = DirectionHelper.getCoordinateVectorFromRelativeVector(vector.getRight(), vector.getDepth(), + vector.getDistance(), yaw); + return makeRelative(combined.getBlockX(), combined.getBlockY(), combined.getBlockZ()); + } + /** * Makes a block location relative to the current origin according to given parameters * diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index 9dcb908..885b0f7 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -217,7 +217,7 @@ public class PlayerEventListener implements Listener { } //Cycle portal destination - if ((!portal.isOpen()) && (!portal.isFixed())) { + if ((!portal.isOpen()) && (!portal.getOptions().isFixed())) { if (leftClick) { portal.cycleDestination(player, -1); } else { diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index bb47e36..0a635ae 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -314,7 +314,6 @@ public class Portal { if (frame == null) { frame = relativeBlockVectorsToBlockLocations(gate.getLayout().getBorder()); } - return frame; } @@ -398,7 +397,7 @@ public class Portal { Portal destination = getDestination(); // Only open destination if it's not-fixed or points at this portal - if (!options.isRandom() && destination != null && (!destination.isFixed() || + if (!options.isRandom() && destination != null && (!destination.options.isFixed() || destination.getDestinationName().equalsIgnoreCase(getName())) && !destination.isOpen()) { destination.open(openFor, false); destination.setDestination(this); @@ -413,16 +412,23 @@ public class Portal { * @param force

Whether to force this portal closed, even if it's set as always on

*/ public void close(boolean force) { - if (!isOpen) return; - // Call the StargateCloseEvent + if (!isOpen) { + return; + } + //Call the StargateCloseEvent StargateCloseEvent event = new StargateCloseEvent(this, force); Stargate.server.getPluginManager().callEvent(event); - if (event.isCancelled()) return; + if (event.isCancelled()) { + return; + } force = event.getForce(); - if (options.isAlwaysOn() && !force) return; // Only close always-open if forced + //Only close always-open if forced to + if (options.isAlwaysOn() && !force) { + return; + } - // Close this gate, then the dest gate. + //Close this gate, then the dest gate. Material closedType = gate.getPortalClosedBlock(); for (BlockLocation inside : getEntrances()) { Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null)); @@ -470,28 +476,6 @@ public class Portal { return player != null && player.getName().equalsIgnoreCase(this.player.getName()); } - /** - * Gets whether this portal points to a fixed exit portal - * - *

A portal where portals can be chosen from a network is not fixed.

- * - * @return

True if this portal points to a fixed exit portal

- */ - public boolean isFixed() { - return options.isFixed(); - } - - /** - * Sets whether this portal points to a fixed exit portal - * - *

A portal where portals can be chosen from a network is not fixed.

- * - * @param fixed

True if this portal points to a fixed exit portal

- */ - public void setFixed(boolean fixed) { - options.setFixed(fixed); - } - /** * Teleports a player to this portal * @@ -693,22 +677,25 @@ public class Portal { if (openingWidth > 1) { newOffset -= 0.5; } - exitLocation = DirectionHelper.adjustLocation(exitLocation, newOffset, 0, 0, getModX(), getModZ()); + exitLocation = DirectionHelper.moveLocation(exitLocation, newOffset, 0, 0, getYaw()); //Move large entities further from the portal, especially if this portal will teleport them at once double entitySize = EntityHelper.getEntityMaxSize(entity); int entityBoxSize = EntityHelper.getEntityMaxSizeInt(entity); if (entitySize > 1) { if (options.isAlwaysOn()) { - exitLocation = DirectionHelper.adjustLocation(exitLocation, 0, 0, (entityBoxSize / 2D), - getModX(), getModZ()); + exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, (entityBoxSize / 2D), + getYaw()); } else { - exitLocation = DirectionHelper.adjustLocation(exitLocation, 0, 0, - (entitySize / 2D) - 1, getModX(), getModZ()); + exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, + (entitySize / 2D) - 1, getYaw()); } } + + //If a horse has a player riding it, the player will spawn inside the roof of a standard portal unless it's + //moved one block out. if (entity instanceof AbstractHorse) { - exitLocation = DirectionHelper.adjustLocation(exitLocation, 0, 0, 1, getModX(), getModZ()); + exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, 1, getYaw()); } return exitLocation; @@ -772,8 +759,8 @@ public class Portal { //Get the chunk in front of the gate corner Location cornerLocation = getBlockAt(vector).getLocation(); int blockOffset = options.isBackwards() ? -5 : 5; - Location fiveBlocksForward = DirectionHelper.adjustLocation(cornerLocation, 0, 0, blockOffset, - getModX(), getModZ()); + Location fiveBlocksForward = DirectionHelper.moveLocation(cornerLocation, 0, 0, blockOffset, + getYaw()); Chunk forwardChunk = fiveBlocksForward.getChunk(); //Load the chunks @@ -923,7 +910,7 @@ public class Portal { } Stargate.activePortalsQueue.remove(this); - if (isFixed()) { + if (options.isFixed()) { return; } destinations.clear(); @@ -938,7 +925,7 @@ public class Portal { * @return

Whether this portal is active

*/ public boolean isActive() { - return isFixed() || (destinations.size() > 0); + return options.isFixed() || (destinations.size() > 0); } /** diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java index 067f12b..6336838 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java @@ -85,7 +85,7 @@ public class PortalHandler { continue; } //Check if destination is a fixed portal not pointing to this portal - if (portal.isFixed() && !portal.getDestinationName().equalsIgnoreCase(entrancePortal.getName())) { + if (portal.getOptions().isFixed() && !portal.getDestinationName().equalsIgnoreCase(entrancePortal.getName())) { continue; } //Allow random use by non-players (Minecarts) @@ -157,7 +157,7 @@ public class PortalHandler { continue; } //Update the portal's sign - if (origin.isFixed()) { + if (origin.getOptions().isFixed()) { origin.drawSign(); } //Close portal without destination @@ -186,7 +186,8 @@ public class PortalHandler { * @param portal

The portal to register

*/ private static void registerPortal(Portal portal) { - portal.setFixed(portal.getDestinationName().length() > 0 || portal.getOptions().isRandom() || portal.getOptions().isBungee()); + portal.getOptions().setFixed(portal.getDestinationName().length() > 0 || portal.getOptions().isRandom() || + portal.getOptions().isBungee()); String portalName = portal.getName().toLowerCase(); String networkName = portal.getNetwork().toLowerCase(); @@ -615,7 +616,7 @@ public class PortalHandler { continue; } //Update sign of fixed gates pointing at this gate - if (origin.isFixed()) { + if (origin.getOptions().isFixed()) { origin.drawSign(); } //Open any always on portal pointing at this portal @@ -800,7 +801,7 @@ public class PortalHandler { builder.append(portal.getYaw()).append(':'); builder.append(portal.getTopLeft().toString()).append(':'); builder.append(portal.getGate().getFilename()).append(':'); - builder.append(portal.isFixed() ? portal.getDestinationName() : "").append(':'); + builder.append(portal.getOptions().isFixed() ? portal.getDestinationName() : "").append(':'); builder.append(portal.getNetwork()).append(':'); UUID owner = portal.getOwnerUUID(); if (owner != null) { @@ -1039,7 +1040,7 @@ public class PortalHandler { portalCount++; //Open the gate if it's set as always open or if it's a bungee gate - if (portal.isFixed() && (Stargate.enableBungee && portal.getOptions().isBungee() || + if (portal.getOptions().isFixed() && (Stargate.enableBungee && portal.getOptions().isBungee() || portal.getDestination() != null && portal.getOptions().isAlwaysOn())) { portal.open(true); openCount++; diff --git a/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java b/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java index 9f747fc..aec2874 100644 --- a/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/DirectionHelper.java @@ -85,6 +85,18 @@ public final class DirectionHelper { } } + /** + * Gets a block location relative to another + * + * @param topLeft

The block location to start at (Usually the top-left block of a portal)

+ * @param vector

The relative vector describing the relative location

+ * @param yaw

The yaw pointing outwards from the portal

+ * @return

A block location relative to the given location

+ */ + public static BlockLocation getBlockAt(BlockLocation topLeft, RelativeBlockVector vector, double yaw) { + return topLeft.getRelativeLocation(vector, yaw); + } + /** * Gets the block at a relative block vector location * @@ -96,19 +108,39 @@ public final class DirectionHelper { } /** - * Adds a relative block vector to a location, accounting for direction + * Moves a location relatively * - * @param location

The location to adjust

- * @param right

The amount of blocks to the right to adjust

- * @param depth

The amount of blocks upward to adjust

- * @param distance

The distance outward to adjust

- * @param modX

The x modifier to use

- * @param modZ

The z modifier to use

- * @return

The altered location

+ * @param location

The location to move

+ * @param right

The amount to go right (When looking at the front of a portal)

+ * @param depth

The amount to go downward (When looking at the front of a portal)

+ * @param distance

The amount to go outward (When looking a the front of a portal)

+ * @param yaw

The yaw when looking directly outwards from a portal

+ * @return

A location relative to the given location

*/ - public static Location adjustLocation(Location location, double right, double depth, double distance, int modX, - int modZ) { - return location.add(-right * modX + distance * modZ, depth, -right * modZ + -distance * modX); + public static Location moveLocation(Location location, double right, double depth, double distance, double yaw) { + return location.add(getCoordinateVectorFromRelativeVector(right, depth, distance, yaw)); + } + + /** + * Gets a vector in Minecraft's normal X,Y,Z-space from a relative block vector + * + * @param right

The amount of right steps from the top-left origin

+ * @param depth

The amount of downward steps from the top-left origin

+ * @param distance

The distance outward from the top-left origin

+ * @param yaw

The yaw when looking directly outwards from a portal

+ * @return

A normal vector

+ */ + public static Vector getCoordinateVectorFromRelativeVector(double right, double depth, double distance, double yaw) { + Vector distanceVector = DirectionHelper.getDirectionVectorFromYaw(yaw); + distanceVector.multiply(distance); + + Vector rightVector = DirectionHelper.getDirectionVectorFromYaw(yaw - 90); + rightVector.multiply(right); + + Vector depthVector = new Vector(0, -1, 0); + depthVector.multiply(depth); + + return distanceVector.add(rightVector).add(depthVector); } /** diff --git a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java index bfe8444..588247a 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java @@ -50,7 +50,7 @@ public final class PermissionHelper { } //Gate that someone else is using -- Deny access - if ((!portal.isFixed()) && portal.isActive() && (portal.getActivePlayer() != player)) { + if ((!portal.getOptions().isFixed()) && portal.isActive() && (portal.getActivePlayer() != player)) { Stargate.sendErrorMessage(player, Stargate.getString("denyMsg")); return; } diff --git a/src/main/java/net/knarcraft/stargate/utility/SignHelper.java b/src/main/java/net/knarcraft/stargate/utility/SignHelper.java index d84fa21..2bcfc0e 100644 --- a/src/main/java/net/knarcraft/stargate/utility/SignHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/SignHelper.java @@ -28,7 +28,7 @@ public final class SignHelper { if (portal.getOptions().isBungee()) { //Bungee sign drawBungeeSign(sign, portal); - } else if (portal.isFixed()) { + } else if (portal.getOptions().isFixed()) { //Sign pointing at one other portal drawFixedSign(sign, portal); } else {