diff --git a/src/main/java/net/knarcraft/stargate/BlockLocation.java b/src/main/java/net/knarcraft/stargate/BlockLocation.java index 639072e..c81ec41 100644 --- a/src/main/java/net/knarcraft/stargate/BlockLocation.java +++ b/src/main/java/net/knarcraft/stargate/BlockLocation.java @@ -70,13 +70,13 @@ public class BlockLocation extends Location { * @param x

The x position relative to this block's position

* @param y

The y position relative to this block's position

* @param z

The z position relative to this block's position

- * @param rotX

The x rotation of the location

+ * @param yaw

The yaw of the location

* @param rotY

The y rotation of the location

* @return

A new location

*/ - public Location makeRelativeLoc(double x, double y, double z, float rotX, float rotY) { + public Location makeRelativeLoc(double x, double y, double z, float yaw, float rotY) { Location newLocation = this.clone(); - newLocation.setYaw(rotX); + newLocation.setYaw(yaw); newLocation.setPitch(rotY); return newLocation.add(x, y, z); } @@ -105,15 +105,15 @@ public class BlockLocation extends Location { * @param right

* @param depth

The y position relative to the current position

* @param distance

The distance away from the previous location to the new location

- * @param rotX

The yaw of the location

+ * @param yaw

The yaw of the location

* @param rotY

Unused

* @param modX

x modifier. Defines movement along the x-axis. 0 for no movement

* @param modY

Unused

* @param modZ

z modifier. Defines movement along the z-axis. 0 for no movement

* @return A new location relative to this block location */ - public Location modRelativeLoc(double right, double depth, double distance, float rotX, float rotY, int modX, int modY, int modZ) { - return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, rotX, 0); + public Location modRelativeLoc(double right, double depth, double distance, float yaw, float rotY, int modX, int modY, int modZ) { + return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, yaw, 0); } /** diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index f51b64b..5686cf7 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -8,6 +8,7 @@ import net.knarcraft.stargate.listener.BungeeCordListener; import net.knarcraft.stargate.listener.EntityEventListener; import net.knarcraft.stargate.listener.PlayerEventListener; import net.knarcraft.stargate.listener.PluginEventListener; +import net.knarcraft.stargate.listener.PortalEventListener; import net.knarcraft.stargate.listener.VehicleEventListener; import net.knarcraft.stargate.listener.WorldEventListener; import net.knarcraft.stargate.portal.Gate; @@ -540,6 +541,7 @@ public class Stargate extends JavaPlugin { pm.registerEvents(new VehicleEventListener(), this); pm.registerEvents(new EntityEventListener(), this); + pm.registerEvents(new PortalEventListener(), this); pm.registerEvents(new WorldEventListener(), this); pm.registerEvents(new PluginEventListener(this), this); diff --git a/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java b/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java index 6fdeab6..43f5fcb 100644 --- a/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/EntityEventListener.java @@ -61,4 +61,5 @@ public class EntityEventListener implements Listener { } } } + } diff --git a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java index e0f60f1..e4c2934 100644 --- a/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/PlayerEventListener.java @@ -21,7 +21,6 @@ import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; -import org.bukkit.event.entity.EntityTeleportEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; @@ -84,16 +83,6 @@ public class PlayerEventListener implements Listener { } } - @EventHandler - public void onEntityTeleport(EntityTeleportEvent event) { - //Prevent any entities from teleporting through stargates - Portal entryPortal = PortalHandler.getByAdjacentEntrance(event.getFrom()); - Portal exitPortal = PortalHandler.getByAdjacentEntrance(event.getTo()); - if (!event.isCancelled() && entryPortal != null && exitPortal != null && exitPortal == entryPortal.getDestination()) { - event.setCancelled(true); - } - } - /** * This event handler detects if a player moves into a portal * diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index ee3c2c6..5f87928 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -48,7 +48,7 @@ public class Portal { private final BlockLocation topLeft; private final int modX; private final int modZ; - private final float rotX; + private final float yaw; private final Axis rot; // Block references @@ -83,7 +83,7 @@ public class 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 rotX

+ * @param yaw

* @param id

The location of the portal's id block, which is the sign which activated the portal

* @param button

The location of the portal's open button

* @param destination

The destination defined on the sign's destination line

@@ -95,14 +95,14 @@ public class Portal { * @param ownerName

The name of the gate's owner

* @param options

A map containing all possible portal options

*/ - Portal(BlockLocation topLeft, int modX, int modZ, float rotX, BlockLocation id, BlockLocation button, + Portal(BlockLocation topLeft, int modX, int modZ, float yaw, BlockLocation id, BlockLocation button, String destination, String name, boolean verified, String network, Gate gate, UUID ownerUUID, String ownerName, Map options) { this.topLeft = topLeft; this.modX = modX; this.modZ = modZ; - this.rotX = rotX; - this.rot = rotX == 0.0F || rotX == 180.0F ? Axis.X : Axis.Z; + this.yaw = yaw; + this.rot = yaw == 0.0F || yaw == 180.0F ? Axis.X : Axis.Z; this.id = id; this.destination = destination; this.button = button; @@ -229,7 +229,7 @@ public class Portal { * @return

The rotation of the portal

*/ public float getRotation() { - return rotX; + return yaw; } /** @@ -653,18 +653,18 @@ public class Portal { Location exit = getExit(player, traveller); //Rotate the player to face out from the portal - adjustRotation(traveller, exit, origin); + adjustRotation(exit, origin); - // Call the StargatePortalEvent to allow plugins to change destination + //Call the StargatePortalEvent to allow plugins to change destination if (!origin.equals(this)) { StargatePortalEvent stargatePortalEvent = new StargatePortalEvent(player, origin, this, exit); Stargate.server.getPluginManager().callEvent(stargatePortalEvent); - // Teleport is cancelled + //Teleport is cancelled. Teleport the player back to where it came from if (stargatePortalEvent.isCancelled()) { origin.teleport(player, origin, event); return; } - // Update exit if needed + //Update exit if needed exit = stargatePortalEvent.getExit(); } @@ -672,7 +672,6 @@ public class Portal { // If no event is passed in, assume it's a teleport, and act as such if (event == null) { - exit.setYaw(this.getRotation()); player.teleport(exit); } else { // The new method to teleport in a move event is set the "to" field. @@ -683,16 +682,17 @@ public class Portal { /** * Adjusts the rotation of the player to face out from the portal * - * @param entry

The location the player entered from

* @param exit

The location the player will exit from

* @param origin

The portal the player entered from

*/ - private void adjustRotation(Location entry, Location exit, Portal origin) { - int adjust = 180; + private void adjustRotation(Location exit, Portal origin) { + int adjust = 0; if (isBackwards() != origin.isBackwards()) { - adjust = 0; + adjust = 180; } - exit.setYaw(entry.getYaw() - origin.getRotation() + this.getRotation() + adjust); + float newYaw = (this.getRotation() + adjust) % 360; + Stargate.debug("Portal::adjustRotation", "Setting exit yaw to " + newYaw); + exit.setYaw(newYaw); } /** @@ -707,15 +707,13 @@ public class Portal { double velocity = vehicle.getVelocity().length(); - // Stop and teleport + //Stop and teleport vehicle.setVelocity(new Vector()); - // Get new velocity - final Vector newVelocity = new Vector(modX, 0.0F, modZ); - //-z - Stargate.debug("teleport", modX + " " + modZ); - newVelocity.multiply(velocity); - adjustRotation(traveller, exit, origin); + //Get new velocity + Vector newVelocityDirection = getVectorFromYaw(this.getRotation()); + Vector newVelocity = newVelocityDirection.multiply(velocity); + adjustRotation(exit, origin); List passengers = vehicle.getPassengers(); World vehicleWorld = exit.getWorld(); @@ -739,6 +737,30 @@ public class Portal { } } + /** + * Gets a direction vector given a yaw + * @param yaw

The yaw to use

+ * @return

The direction vector of the yaw

+ */ + private Vector getVectorFromYaw(double yaw) { + while (yaw < 0) { + yaw += 360; + } + yaw = yaw % 360; + + if (yaw == 0) { + return new Vector(0, 0, 1); + } else if (yaw == 90) { + return new Vector(-1, 0, 0); + } else if (yaw == 180) { + return new Vector(0, 0, -1); + } else if (yaw == 270) { + return new Vector(1, 0, 0); + } else { + throw new IllegalArgumentException("Invalid yaw given"); + } + } + /** * Teleport a vehicle which is not a minecart or a boat * @@ -768,7 +790,8 @@ public class Portal { vehicle.remove(); vehicle.setRotation(exit.getYaw(), exit.getPitch()); handleVehiclePassengers(passengers, newVehicle); - Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, () -> newVehicle.setVelocity(newVelocity), 1); + Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, + () -> newVehicle.setVelocity(newVelocity), 1); } /** @@ -950,8 +973,8 @@ public class Portal { * * @return

The rotation of this portal

*/ - public float getRotX() { - return this.rotX; + public float getYaw() { + return this.yaw; } /** diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java index cb61cbd..89c277a 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalHandler.java @@ -237,24 +237,24 @@ public class PortalHandler { // Moved the layout check so as to avoid invalid messages when not making a gate int modX = 0; int modZ = 0; - float rotX = 0f; + float yaw = 0f; BlockFace buttonFacing = BlockFace.DOWN; if (idParent.getX() > id.getBlock().getX()) { modZ -= 1; - rotX = 90f; + yaw = 90f; buttonFacing = BlockFace.WEST; } else if (idParent.getX() < id.getBlock().getX()) { modZ += 1; - rotX = 270f; + yaw = 270f; buttonFacing = BlockFace.EAST; } else if (idParent.getZ() > id.getBlock().getZ()) { modX += 1; - rotX = 180f; + yaw = 180f; buttonFacing = BlockFace.NORTH; } else if (idParent.getZ() < id.getBlock().getZ()) { modX -= 1; - rotX = 0f; + yaw = 0f; buttonFacing = BlockFace.SOUTH; } @@ -376,7 +376,7 @@ public class PortalHandler { BlockLocation button = null; Portal portal; - portal = new Portal(topLeft, modX, modZ, rotX, id, button, destinationName, name, false, network, + portal = new Portal(topLeft, modX, modZ, yaw, id, button, destinationName, name, false, network, gate, player.getUniqueId(), player.getName(), portalOptions); int cost = Stargate.getCreateCost(player, gate); @@ -654,7 +654,7 @@ public class PortalHandler { builder.append(':'); builder.append(portal.getModZ()); builder.append(':'); - builder.append(portal.getRotX()); + builder.append(portal.getYaw()); builder.append(':'); builder.append(portal.getTopLeft().toString()); builder.append(':'); @@ -829,7 +829,7 @@ public class PortalHandler { 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 rotX = Float.parseFloat(portalData[5]); + float yaw = Float.parseFloat(portalData[5]); BlockLocation topLeft = new BlockLocation(world, portalData[6]); Gate gate = GateHandler.getGateByName(portalData[7]); if (gate == null) { @@ -864,7 +864,7 @@ public class PortalHandler { } //Creates the new portal - Portal portal = new Portal(topLeft, modX, modZ, rotX, sign, button, destination, name, false, + Portal portal = new Portal(topLeft, modX, modZ, yaw, sign, button, destination, name, false, network, gate, ownerUUID, ownerName, getPortalOptions(portalData)); registerPortal(portal);