From f52ba79ae9f6295589b58bbc71edcb1817f1845b Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 29 Oct 2021 15:08:44 +0200 Subject: [PATCH] Splits the preventExitSuffocation method and improves some more comments --- .../stargate/portal/PortalCreator.java | 8 ++-- .../stargate/portal/PortalRegistry.java | 4 +- .../knarcraft/stargate/portal/Teleporter.java | 48 +++++++++++++------ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java index e5c06e7..7d26981 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java @@ -223,28 +223,29 @@ public class PortalCreator { * @return

True if the portal is completely valid

*/ private boolean checkIfNewPortalIsValid(int cost, String portalName) { - // Name & Network can be changed in the event, so do these checks here. + //Check if the portal name can fit on the sign with padding (>name<) if (portal.getName().length() < 1 || portal.getName().length() > 11) { Stargate.debug("createPortal", "Name length error"); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createNameLength")); return false; } - //Don't do network checks for bungee portals if (portal.getOptions().isBungee()) { + //Check if the bungee portal's name has been duplicated if (PortalHandler.getBungeePortals().get(portal.getName().toLowerCase()) != null) { Stargate.debug("createPortal::Bungee", "Gate name duplicate"); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createExists")); return false; } } else { + //Check if the portal name has been duplicated on the network if (PortalHandler.getByName(portal.getName(), portal.getNetwork()) != null) { Stargate.debug("createPortal", "Gate name duplicate"); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createExists")); return false; } - //Check if there are too many gates in this network + //Check if the number of portals in the network has been surpassed List networkList = PortalHandler.getAllPortalNetworks().get(portal.getNetwork().toLowerCase()); int maxGates = Stargate.getGateConfig().maxGatesEachNetwork(); if (maxGates > 0 && networkList != null && networkList.size() >= maxGates) { @@ -254,6 +255,7 @@ public class PortalCreator { } if (cost > 0) { + //Deduct the required fee from the player if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) { EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost); Stargate.debug("createPortal", "Insufficient Funds"); diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java index da9476a..7a557bc 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalRegistry.java @@ -267,8 +267,8 @@ public class PortalRegistry { if (!allPortalNetworks.get(networkName).contains(portalName)) { allPortalNetworks.get(networkName).add(portalName); } else { - Stargate.logSevere(String.format("Portal %s was registered twice. Check your portal database for " + - "duplicates.", portal)); + Stargate.logSevere(String.format("Portal %s on network %s was registered twice. Check your portal " + + "database for duplicates.", portal.getName(), portal.getNetwork())); } } diff --git a/src/main/java/net/knarcraft/stargate/portal/Teleporter.java b/src/main/java/net/knarcraft/stargate/portal/Teleporter.java index 2e11a83..5002150 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Teleporter.java +++ b/src/main/java/net/knarcraft/stargate/portal/Teleporter.java @@ -62,10 +62,11 @@ public abstract class Teleporter { */ public Location getExit(Entity entity, Location traveller) { Location exitLocation = null; - // Check if the gate has an exit block RelativeBlockVector relativeExit = portal.getGate().getLayout().getExit(); if (relativeExit != null) { BlockLocation exit = portal.getBlockAt(relativeExit); + + //Move one block out to prevent exiting inside the portal float portalYaw = portal.getYaw(); if (portal.getOptions().isBackwards()) { portalYaw += 180; @@ -74,6 +75,7 @@ public abstract class Teleporter { if (entity != null) { double entitySize = EntityHelper.getEntityMaxSize(entity); + //Prevent exit suffocation for players riding horses or similar if (entitySize > 1) { exitLocation = preventExitSuffocation(relativeExit, exitLocation, entity); } @@ -83,6 +85,7 @@ public abstract class Teleporter { portal.getGate().getFilename())); } + //Adjust pitch and height return adjustExitLocation(traveller, exitLocation); } @@ -112,25 +115,34 @@ public abstract class Teleporter { } exitLocation = DirectionHelper.moveLocation(exitLocation, newOffset, 0, 0, portal.getYaw()); - //Move large entities further from the portal, especially if this teleporter's portal will teleport them at once + //Move large entities further from the portal + return moveExitLocationOutwards(exitLocation, entity); + } + + /** + * Moves the exit location out from the portal to prevent the entity from entering a teleportation loop + * + * @param exitLocation

The current exit location to adjust

+ * @param entity

The entity to adjust the exit location for

+ * @return

The adjusted exit location

+ */ + private Location moveExitLocationOutwards(Location exitLocation, Entity entity) { double entitySize = EntityHelper.getEntityMaxSize(entity); int entityBoxSize = EntityHelper.getEntityMaxSizeInt(entity); if (entitySize > 1) { + double entityOffset; if (portal.getOptions().isAlwaysOn()) { - exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, (entityBoxSize / 2D), - portal.getYaw()); + entityOffset = entityBoxSize / 2D; } else { - exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, - (entitySize / 2D) - 1, portal.getYaw()); + entityOffset = (entitySize / 2D) - 1; } + //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) { + entityOffset += 1; + } + exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, entityOffset, portal.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.moveLocation(exitLocation, 0, 0, 1, portal.getYaw()); - } - return exitLocation; } @@ -143,6 +155,7 @@ public abstract class Teleporter { */ private RelativeBlockVector getPortalExitEdge(RelativeBlockVector relativeExit, int direction) { RelativeBlockVector openingEdge = relativeExit; + do { RelativeBlockVector possibleOpening = new RelativeBlockVector(openingEdge.getRight() + direction, openingEdge.getDown(), openingEdge.getOut()); @@ -152,11 +165,16 @@ public abstract class Teleporter { break; } } while (true); + return openingEdge; } /** - * Adjusts an exit location with rotation and slab height incrementation + * Adjusts an exit location by setting pitch and adjusting height + * + *

If the exit location is a slab or water, the exit location will be changed to arrive one block above. The + * slab check is necessary to prevent the player from clipping through the slab and spawning beneath it. The water + * check is necessary when teleporting boats to prevent it from becoming a submarine.

* * @param traveller

The location of the travelling entity

* @param exitLocation

The exit location generated

@@ -180,8 +198,8 @@ public abstract class Teleporter { return exitLocation; } else { Stargate.logWarning("Unable to generate exit location"); + return traveller; } - return traveller; } /**