Splits the preventExitSuffocation method and improves some more comments

This commit is contained in:
Kristian Knarvik 2021-10-29 15:08:44 +02:00
parent 945798916b
commit f52ba79ae9
3 changed files with 40 additions and 20 deletions

View File

@ -223,28 +223,29 @@ public class PortalCreator {
* @return <p>True if the portal is completely valid</p> * @return <p>True if the portal is completely valid</p>
*/ */
private boolean checkIfNewPortalIsValid(int cost, String portalName) { 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) { if (portal.getName().length() < 1 || portal.getName().length() > 11) {
Stargate.debug("createPortal", "Name length error"); Stargate.debug("createPortal", "Name length error");
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createNameLength")); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createNameLength"));
return false; return false;
} }
//Don't do network checks for bungee portals
if (portal.getOptions().isBungee()) { if (portal.getOptions().isBungee()) {
//Check if the bungee portal's name has been duplicated
if (PortalHandler.getBungeePortals().get(portal.getName().toLowerCase()) != null) { if (PortalHandler.getBungeePortals().get(portal.getName().toLowerCase()) != null) {
Stargate.debug("createPortal::Bungee", "Gate name duplicate"); Stargate.debug("createPortal::Bungee", "Gate name duplicate");
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createExists")); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createExists"));
return false; return false;
} }
} else { } else {
//Check if the portal name has been duplicated on the network
if (PortalHandler.getByName(portal.getName(), portal.getNetwork()) != null) { if (PortalHandler.getByName(portal.getName(), portal.getNetwork()) != null) {
Stargate.debug("createPortal", "Gate name duplicate"); Stargate.debug("createPortal", "Gate name duplicate");
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createExists")); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("createExists"));
return false; 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<String> networkList = PortalHandler.getAllPortalNetworks().get(portal.getNetwork().toLowerCase()); List<String> networkList = PortalHandler.getAllPortalNetworks().get(portal.getNetwork().toLowerCase());
int maxGates = Stargate.getGateConfig().maxGatesEachNetwork(); int maxGates = Stargate.getGateConfig().maxGatesEachNetwork();
if (maxGates > 0 && networkList != null && networkList.size() >= maxGates) { if (maxGates > 0 && networkList != null && networkList.size() >= maxGates) {
@ -254,6 +255,7 @@ public class PortalCreator {
} }
if (cost > 0) { if (cost > 0) {
//Deduct the required fee from the player
if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) { if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) {
EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost); EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost);
Stargate.debug("createPortal", "Insufficient Funds"); Stargate.debug("createPortal", "Insufficient Funds");

View File

@ -267,8 +267,8 @@ public class PortalRegistry {
if (!allPortalNetworks.get(networkName).contains(portalName)) { if (!allPortalNetworks.get(networkName).contains(portalName)) {
allPortalNetworks.get(networkName).add(portalName); allPortalNetworks.get(networkName).add(portalName);
} else { } else {
Stargate.logSevere(String.format("Portal %s was registered twice. Check your portal database for " + Stargate.logSevere(String.format("Portal %s on network %s was registered twice. Check your portal " +
"duplicates.", portal)); "database for duplicates.", portal.getName(), portal.getNetwork()));
} }
} }

View File

@ -62,10 +62,11 @@ public abstract class Teleporter {
*/ */
public Location getExit(Entity entity, Location traveller) { public Location getExit(Entity entity, Location traveller) {
Location exitLocation = null; Location exitLocation = null;
// Check if the gate has an exit block
RelativeBlockVector relativeExit = portal.getGate().getLayout().getExit(); RelativeBlockVector relativeExit = portal.getGate().getLayout().getExit();
if (relativeExit != null) { if (relativeExit != null) {
BlockLocation exit = portal.getBlockAt(relativeExit); BlockLocation exit = portal.getBlockAt(relativeExit);
//Move one block out to prevent exiting inside the portal
float portalYaw = portal.getYaw(); float portalYaw = portal.getYaw();
if (portal.getOptions().isBackwards()) { if (portal.getOptions().isBackwards()) {
portalYaw += 180; portalYaw += 180;
@ -74,6 +75,7 @@ public abstract class Teleporter {
if (entity != null) { if (entity != null) {
double entitySize = EntityHelper.getEntityMaxSize(entity); double entitySize = EntityHelper.getEntityMaxSize(entity);
//Prevent exit suffocation for players riding horses or similar
if (entitySize > 1) { if (entitySize > 1) {
exitLocation = preventExitSuffocation(relativeExit, exitLocation, entity); exitLocation = preventExitSuffocation(relativeExit, exitLocation, entity);
} }
@ -83,6 +85,7 @@ public abstract class Teleporter {
portal.getGate().getFilename())); portal.getGate().getFilename()));
} }
//Adjust pitch and height
return adjustExitLocation(traveller, exitLocation); return adjustExitLocation(traveller, exitLocation);
} }
@ -112,25 +115,34 @@ public abstract class Teleporter {
} }
exitLocation = DirectionHelper.moveLocation(exitLocation, newOffset, 0, 0, portal.getYaw()); 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 <p>The current exit location to adjust</p>
* @param entity <p>The entity to adjust the exit location for</p>
* @return <p>The adjusted exit location</p>
*/
private Location moveExitLocationOutwards(Location exitLocation, Entity entity) {
double entitySize = EntityHelper.getEntityMaxSize(entity); double entitySize = EntityHelper.getEntityMaxSize(entity);
int entityBoxSize = EntityHelper.getEntityMaxSizeInt(entity); int entityBoxSize = EntityHelper.getEntityMaxSizeInt(entity);
if (entitySize > 1) { if (entitySize > 1) {
double entityOffset;
if (portal.getOptions().isAlwaysOn()) { if (portal.getOptions().isAlwaysOn()) {
exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, (entityBoxSize / 2D), entityOffset = entityBoxSize / 2D;
portal.getYaw());
} else { } else {
exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, entityOffset = (entitySize / 2D) - 1;
(entitySize / 2D) - 1, portal.getYaw());
} }
}
//If a horse has a player riding it, the player will spawn inside the roof of a standard portal unless it's //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. // moved one block out.
if (entity instanceof AbstractHorse) { if (entity instanceof AbstractHorse) {
exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, 1, portal.getYaw()); entityOffset += 1;
}
exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, entityOffset, portal.getYaw());
} }
return exitLocation; return exitLocation;
} }
@ -143,6 +155,7 @@ public abstract class Teleporter {
*/ */
private RelativeBlockVector getPortalExitEdge(RelativeBlockVector relativeExit, int direction) { private RelativeBlockVector getPortalExitEdge(RelativeBlockVector relativeExit, int direction) {
RelativeBlockVector openingEdge = relativeExit; RelativeBlockVector openingEdge = relativeExit;
do { do {
RelativeBlockVector possibleOpening = new RelativeBlockVector(openingEdge.getRight() + direction, RelativeBlockVector possibleOpening = new RelativeBlockVector(openingEdge.getRight() + direction,
openingEdge.getDown(), openingEdge.getOut()); openingEdge.getDown(), openingEdge.getOut());
@ -152,11 +165,16 @@ public abstract class Teleporter {
break; break;
} }
} while (true); } while (true);
return openingEdge; return openingEdge;
} }
/** /**
* Adjusts an exit location with rotation and slab height incrementation * Adjusts an exit location by setting pitch and adjusting height
*
* <p>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.</p>
* *
* @param traveller <p>The location of the travelling entity</p> * @param traveller <p>The location of the travelling entity</p>
* @param exitLocation <p>The exit location generated</p> * @param exitLocation <p>The exit location generated</p>
@ -180,9 +198,9 @@ public abstract class Teleporter {
return exitLocation; return exitLocation;
} else { } else {
Stargate.logWarning("Unable to generate exit location"); Stargate.logWarning("Unable to generate exit location");
}
return traveller; return traveller;
} }
}
/** /**
* Loads the chunks outside the portal's entrance * Loads the chunks outside the portal's entrance