Creates some new methods to get a location from a relative location which I can actually understand

This commit is contained in:
Kristian Knarvik 2021-10-08 15:28:12 +02:00
parent e7fc1daafe
commit 6d5c4802bc
7 changed files with 94 additions and 59 deletions

View File

@ -1,5 +1,6 @@
package net.knarcraft.stargate.container; package net.knarcraft.stargate.container;
import net.knarcraft.stargate.utility.DirectionHelper;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -8,6 +9,7 @@ import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Sign; import org.bukkit.block.data.type.Sign;
import org.bukkit.block.data.type.WallSign; import org.bukkit.block.data.type.WallSign;
import org.bukkit.util.Vector;
/** /**
* This class represents a block location * This class represents a block location
@ -81,6 +83,19 @@ public class BlockLocation extends Location {
return newLocation.add(x, y, z); return newLocation.add(x, y, z);
} }
/**
* Gets a location relative to this block location
*
* @param vector <p>The relative block vector describing the relative location</p>
* @param yaw <p>The yaw pointing in the distance direction</p>
* @return <p>A location relative to this location</p>
*/
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 * Makes a block location relative to the current origin according to given parameters
* *

View File

@ -217,7 +217,7 @@ public class PlayerEventListener implements Listener {
} }
//Cycle portal destination //Cycle portal destination
if ((!portal.isOpen()) && (!portal.isFixed())) { if ((!portal.isOpen()) && (!portal.getOptions().isFixed())) {
if (leftClick) { if (leftClick) {
portal.cycleDestination(player, -1); portal.cycleDestination(player, -1);
} else { } else {

View File

@ -314,7 +314,6 @@ public class Portal {
if (frame == null) { if (frame == null) {
frame = relativeBlockVectorsToBlockLocations(gate.getLayout().getBorder()); frame = relativeBlockVectorsToBlockLocations(gate.getLayout().getBorder());
} }
return frame; return frame;
} }
@ -398,7 +397,7 @@ public class Portal {
Portal destination = getDestination(); Portal destination = getDestination();
// Only open destination if it's not-fixed or points at this portal // 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.getDestinationName().equalsIgnoreCase(getName())) && !destination.isOpen()) {
destination.open(openFor, false); destination.open(openFor, false);
destination.setDestination(this); destination.setDestination(this);
@ -413,16 +412,23 @@ public class Portal {
* @param force <p>Whether to force this portal closed, even if it's set as always on</p> * @param force <p>Whether to force this portal closed, even if it's set as always on</p>
*/ */
public void close(boolean force) { public void close(boolean force) {
if (!isOpen) return; if (!isOpen) {
// Call the StargateCloseEvent return;
}
//Call the StargateCloseEvent
StargateCloseEvent event = new StargateCloseEvent(this, force); StargateCloseEvent event = new StargateCloseEvent(this, force);
Stargate.server.getPluginManager().callEvent(event); Stargate.server.getPluginManager().callEvent(event);
if (event.isCancelled()) return; if (event.isCancelled()) {
return;
}
force = event.getForce(); 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(); Material closedType = gate.getPortalClosedBlock();
for (BlockLocation inside : getEntrances()) { for (BlockLocation inside : getEntrances()) {
Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null)); Stargate.blockChangeRequestQueue.add(new BlockChangeRequest(inside, closedType, null));
@ -470,28 +476,6 @@ public class Portal {
return player != null && player.getName().equalsIgnoreCase(this.player.getName()); return player != null && player.getName().equalsIgnoreCase(this.player.getName());
} }
/**
* Gets whether this portal points to a fixed exit portal
*
* <p>A portal where portals can be chosen from a network is not fixed.</p>
*
* @return <p>True if this portal points to a fixed exit portal</p>
*/
public boolean isFixed() {
return options.isFixed();
}
/**
* Sets whether this portal points to a fixed exit portal
*
* <p>A portal where portals can be chosen from a network is not fixed.</p>
*
* @param fixed <p>True if this portal points to a fixed exit portal</p>
*/
public void setFixed(boolean fixed) {
options.setFixed(fixed);
}
/** /**
* Teleports a player to this portal * Teleports a player to this portal
* *
@ -693,22 +677,25 @@ public class Portal {
if (openingWidth > 1) { if (openingWidth > 1) {
newOffset -= 0.5; 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 //Move large entities further from the portal, especially if this portal will teleport them at once
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) {
if (options.isAlwaysOn()) { if (options.isAlwaysOn()) {
exitLocation = DirectionHelper.adjustLocation(exitLocation, 0, 0, (entityBoxSize / 2D), exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, (entityBoxSize / 2D),
getModX(), getModZ()); getYaw());
} else { } else {
exitLocation = DirectionHelper.adjustLocation(exitLocation, 0, 0, exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0,
(entitySize / 2D) - 1, getModX(), getModZ()); (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) { if (entity instanceof AbstractHorse) {
exitLocation = DirectionHelper.adjustLocation(exitLocation, 0, 0, 1, getModX(), getModZ()); exitLocation = DirectionHelper.moveLocation(exitLocation, 0, 0, 1, getYaw());
} }
return exitLocation; return exitLocation;
@ -772,8 +759,8 @@ public class Portal {
//Get the chunk in front of the gate corner //Get the chunk in front of the gate corner
Location cornerLocation = getBlockAt(vector).getLocation(); Location cornerLocation = getBlockAt(vector).getLocation();
int blockOffset = options.isBackwards() ? -5 : 5; int blockOffset = options.isBackwards() ? -5 : 5;
Location fiveBlocksForward = DirectionHelper.adjustLocation(cornerLocation, 0, 0, blockOffset, Location fiveBlocksForward = DirectionHelper.moveLocation(cornerLocation, 0, 0, blockOffset,
getModX(), getModZ()); getYaw());
Chunk forwardChunk = fiveBlocksForward.getChunk(); Chunk forwardChunk = fiveBlocksForward.getChunk();
//Load the chunks //Load the chunks
@ -923,7 +910,7 @@ public class Portal {
} }
Stargate.activePortalsQueue.remove(this); Stargate.activePortalsQueue.remove(this);
if (isFixed()) { if (options.isFixed()) {
return; return;
} }
destinations.clear(); destinations.clear();
@ -938,7 +925,7 @@ public class Portal {
* @return <p>Whether this portal is active</p> * @return <p>Whether this portal is active</p>
*/ */
public boolean isActive() { public boolean isActive() {
return isFixed() || (destinations.size() > 0); return options.isFixed() || (destinations.size() > 0);
} }
/** /**

View File

@ -85,7 +85,7 @@ public class PortalHandler {
continue; continue;
} }
//Check if destination is a fixed portal not pointing to this portal //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; continue;
} }
//Allow random use by non-players (Minecarts) //Allow random use by non-players (Minecarts)
@ -157,7 +157,7 @@ public class PortalHandler {
continue; continue;
} }
//Update the portal's sign //Update the portal's sign
if (origin.isFixed()) { if (origin.getOptions().isFixed()) {
origin.drawSign(); origin.drawSign();
} }
//Close portal without destination //Close portal without destination
@ -186,7 +186,8 @@ public class PortalHandler {
* @param portal <p>The portal to register</p> * @param portal <p>The portal to register</p>
*/ */
private static void registerPortal(Portal portal) { 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 portalName = portal.getName().toLowerCase();
String networkName = portal.getNetwork().toLowerCase(); String networkName = portal.getNetwork().toLowerCase();
@ -615,7 +616,7 @@ public class PortalHandler {
continue; continue;
} }
//Update sign of fixed gates pointing at this gate //Update sign of fixed gates pointing at this gate
if (origin.isFixed()) { if (origin.getOptions().isFixed()) {
origin.drawSign(); origin.drawSign();
} }
//Open any always on portal pointing at this portal //Open any always on portal pointing at this portal
@ -800,7 +801,7 @@ public class PortalHandler {
builder.append(portal.getYaw()).append(':'); builder.append(portal.getYaw()).append(':');
builder.append(portal.getTopLeft().toString()).append(':'); builder.append(portal.getTopLeft().toString()).append(':');
builder.append(portal.getGate().getFilename()).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(':'); builder.append(portal.getNetwork()).append(':');
UUID owner = portal.getOwnerUUID(); UUID owner = portal.getOwnerUUID();
if (owner != null) { if (owner != null) {
@ -1039,7 +1040,7 @@ public class PortalHandler {
portalCount++; portalCount++;
//Open the gate if it's set as always open or if it's a bungee gate //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.getDestination() != null && portal.getOptions().isAlwaysOn())) {
portal.open(true); portal.open(true);
openCount++; openCount++;

View File

@ -85,6 +85,18 @@ public final class DirectionHelper {
} }
} }
/**
* Gets a block location relative to another
*
* @param topLeft <p>The block location to start at (Usually the top-left block of a portal)</p>
* @param vector <p>The relative vector describing the relative location</p>
* @param yaw <p>The yaw pointing outwards from the portal</p>
* @return <p>A block location relative to the given location</p>
*/
public static BlockLocation getBlockAt(BlockLocation topLeft, RelativeBlockVector vector, double yaw) {
return topLeft.getRelativeLocation(vector, yaw);
}
/** /**
* Gets the block at a relative block vector location * 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 <p>The location to adjust</p> * @param location <p>The location to move</p>
* @param right <p>The amount of blocks to the right to adjust</p> * @param right <p>The amount to go right (When looking at the front of a portal)</p>
* @param depth <p>The amount of blocks upward to adjust</p> * @param depth <p>The amount to go downward (When looking at the front of a portal)</p>
* @param distance <p>The distance outward to adjust</p> * @param distance <p>The amount to go outward (When looking a the front of a portal)</p>
* @param modX <p>The x modifier to use</p> * @param yaw <p>The yaw when looking directly outwards from a portal</p>
* @param modZ <p>The z modifier to use</p> * @return <p>A location relative to the given location</p>
* @return <p>The altered location</p>
*/ */
public static Location adjustLocation(Location location, double right, double depth, double distance, int modX, public static Location moveLocation(Location location, double right, double depth, double distance, double yaw) {
int modZ) { return location.add(getCoordinateVectorFromRelativeVector(right, depth, distance, yaw));
return location.add(-right * modX + distance * modZ, depth, -right * modZ + -distance * modX); }
/**
* Gets a vector in Minecraft's normal X,Y,Z-space from a relative block vector
*
* @param right <p>The amount of right steps from the top-left origin</p>
* @param depth <p>The amount of downward steps from the top-left origin</p>
* @param distance <p>The distance outward from the top-left origin</p>
* @param yaw <p>The yaw when looking directly outwards from a portal</p>
* @return <p>A normal vector</p>
*/
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);
} }
/** /**

View File

@ -50,7 +50,7 @@ public final class PermissionHelper {
} }
//Gate that someone else is using -- Deny access //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")); Stargate.sendErrorMessage(player, Stargate.getString("denyMsg"));
return; return;
} }

View File

@ -28,7 +28,7 @@ public final class SignHelper {
if (portal.getOptions().isBungee()) { if (portal.getOptions().isBungee()) {
//Bungee sign //Bungee sign
drawBungeeSign(sign, portal); drawBungeeSign(sign, portal);
} else if (portal.isFixed()) { } else if (portal.getOptions().isFixed()) {
//Sign pointing at one other portal //Sign pointing at one other portal
drawFixedSign(sign, portal); drawFixedSign(sign, portal);
} else { } else {