Fixes some old bugs and renames rotX to yaw

Fixes the direction of minecarts sent through a portal. This prevent the minecarts to go back through the portal
and causing a lot of confusion
This commit is contained in:
Kristian Knarvik 2021-09-10 23:38:56 +02:00
parent b4059dd169
commit 93f8f715e5
6 changed files with 67 additions and 52 deletions

View File

@ -70,13 +70,13 @@ public class BlockLocation extends Location {
* @param x <p>The x position relative to this block's position</p> * @param x <p>The x position relative to this block's position</p>
* @param y <p>The y position relative to this block's position</p> * @param y <p>The y position relative to this block's position</p>
* @param z <p>The z position relative to this block's position</p> * @param z <p>The z position relative to this block's position</p>
* @param rotX <p>The x rotation of the location</p> * @param yaw <p>The yaw of the location</p>
* @param rotY <p>The y rotation of the location</p> * @param rotY <p>The y rotation of the location</p>
* @return <p>A new location</p> * @return <p>A new location</p>
*/ */
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(); Location newLocation = this.clone();
newLocation.setYaw(rotX); newLocation.setYaw(yaw);
newLocation.setPitch(rotY); newLocation.setPitch(rotY);
return newLocation.add(x, y, z); return newLocation.add(x, y, z);
} }
@ -105,15 +105,15 @@ public class BlockLocation extends Location {
* @param right <p></p> * @param right <p></p>
* @param depth <p>The y position relative to the current position</p> * @param depth <p>The y position relative to the current position</p>
* @param distance <p>The distance away from the previous location to the new location</p> * @param distance <p>The distance away from the previous location to the new location</p>
* @param rotX <p>The yaw of the location</p> * @param yaw <p>The yaw of the location</p>
* @param rotY <p>Unused</p> * @param rotY <p>Unused</p>
* @param modX <p>x modifier. Defines movement along the x-axis. 0 for no movement</p> * @param modX <p>x modifier. Defines movement along the x-axis. 0 for no movement</p>
* @param modY <p>Unused</p> * @param modY <p>Unused</p>
* @param modZ <p>z modifier. Defines movement along the z-axis. 0 for no movement</p> * @param modZ <p>z modifier. Defines movement along the z-axis. 0 for no movement</p>
* @return A new location relative to this block location * @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) { 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, rotX, 0); return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, yaw, 0);
} }
/** /**

View File

@ -8,6 +8,7 @@ import net.knarcraft.stargate.listener.BungeeCordListener;
import net.knarcraft.stargate.listener.EntityEventListener; import net.knarcraft.stargate.listener.EntityEventListener;
import net.knarcraft.stargate.listener.PlayerEventListener; import net.knarcraft.stargate.listener.PlayerEventListener;
import net.knarcraft.stargate.listener.PluginEventListener; import net.knarcraft.stargate.listener.PluginEventListener;
import net.knarcraft.stargate.listener.PortalEventListener;
import net.knarcraft.stargate.listener.VehicleEventListener; import net.knarcraft.stargate.listener.VehicleEventListener;
import net.knarcraft.stargate.listener.WorldEventListener; import net.knarcraft.stargate.listener.WorldEventListener;
import net.knarcraft.stargate.portal.Gate; import net.knarcraft.stargate.portal.Gate;
@ -540,6 +541,7 @@ public class Stargate extends JavaPlugin {
pm.registerEvents(new VehicleEventListener(), this); pm.registerEvents(new VehicleEventListener(), this);
pm.registerEvents(new EntityEventListener(), this); pm.registerEvents(new EntityEventListener(), this);
pm.registerEvents(new PortalEventListener(), this);
pm.registerEvents(new WorldEventListener(), this); pm.registerEvents(new WorldEventListener(), this);
pm.registerEvents(new PluginEventListener(this), this); pm.registerEvents(new PluginEventListener(this), this);

View File

@ -61,4 +61,5 @@ public class EntityEventListener implements Listener {
} }
} }
} }
} }

View File

@ -21,7 +21,6 @@ import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent; 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 * This event handler detects if a player moves into a portal
* *

View File

@ -48,7 +48,7 @@ public class Portal {
private final BlockLocation topLeft; private final BlockLocation topLeft;
private final int modX; private final int modX;
private final int modZ; private final int modZ;
private final float rotX; private final float yaw;
private final Axis rot; private final Axis rot;
// Block references // Block references
@ -83,7 +83,7 @@ public class Portal {
* @param topLeft <p>The top-left block of the portal. This is used to decide the positions of the rest of the portal</p> * @param topLeft <p>The top-left block of the portal. This is used to decide the positions of the rest of the portal</p>
* @param modX <p></p> * @param modX <p></p>
* @param modZ <p></p> * @param modZ <p></p>
* @param rotX <p></p> * @param yaw <p></p>
* @param id <p>The location of the portal's id block, which is the sign which activated the portal</p> * @param id <p>The location of the portal's id block, which is the sign which activated the portal</p>
* @param button <p>The location of the portal's open button</p> * @param button <p>The location of the portal's open button</p>
* @param destination <p>The destination defined on the sign's destination line</p> * @param destination <p>The destination defined on the sign's destination line</p>
@ -95,14 +95,14 @@ public class Portal {
* @param ownerName <p>The name of the gate's owner</p> * @param ownerName <p>The name of the gate's owner</p>
* @param options <p>A map containing all possible portal options</p> * @param options <p>A map containing all possible portal options</p>
*/ */
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 destination, String name, boolean verified, String network, Gate gate, UUID ownerUUID,
String ownerName, Map<PortalOption, Boolean> options) { String ownerName, Map<PortalOption, Boolean> options) {
this.topLeft = topLeft; this.topLeft = topLeft;
this.modX = modX; this.modX = modX;
this.modZ = modZ; this.modZ = modZ;
this.rotX = rotX; this.yaw = yaw;
this.rot = rotX == 0.0F || rotX == 180.0F ? Axis.X : Axis.Z; this.rot = yaw == 0.0F || yaw == 180.0F ? Axis.X : Axis.Z;
this.id = id; this.id = id;
this.destination = destination; this.destination = destination;
this.button = button; this.button = button;
@ -229,7 +229,7 @@ public class Portal {
* @return <p>The rotation of the portal</p> * @return <p>The rotation of the portal</p>
*/ */
public float getRotation() { public float getRotation() {
return rotX; return yaw;
} }
/** /**
@ -653,18 +653,18 @@ public class Portal {
Location exit = getExit(player, traveller); Location exit = getExit(player, traveller);
//Rotate the player to face out from the portal //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)) { if (!origin.equals(this)) {
StargatePortalEvent stargatePortalEvent = new StargatePortalEvent(player, origin, this, exit); StargatePortalEvent stargatePortalEvent = new StargatePortalEvent(player, origin, this, exit);
Stargate.server.getPluginManager().callEvent(stargatePortalEvent); Stargate.server.getPluginManager().callEvent(stargatePortalEvent);
// Teleport is cancelled //Teleport is cancelled. Teleport the player back to where it came from
if (stargatePortalEvent.isCancelled()) { if (stargatePortalEvent.isCancelled()) {
origin.teleport(player, origin, event); origin.teleport(player, origin, event);
return; return;
} }
// Update exit if needed //Update exit if needed
exit = stargatePortalEvent.getExit(); 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 no event is passed in, assume it's a teleport, and act as such
if (event == null) { if (event == null) {
exit.setYaw(this.getRotation());
player.teleport(exit); player.teleport(exit);
} else { } else {
// The new method to teleport in a move event is set the "to" field. // 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 * Adjusts the rotation of the player to face out from the portal
* *
* @param entry <p>The location the player entered from</p>
* @param exit <p>The location the player will exit from</p> * @param exit <p>The location the player will exit from</p>
* @param origin <p>The portal the player entered from</p> * @param origin <p>The portal the player entered from</p>
*/ */
private void adjustRotation(Location entry, Location exit, Portal origin) { private void adjustRotation(Location exit, Portal origin) {
int adjust = 180; int adjust = 0;
if (isBackwards() != origin.isBackwards()) { 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(); double velocity = vehicle.getVelocity().length();
// Stop and teleport //Stop and teleport
vehicle.setVelocity(new Vector()); vehicle.setVelocity(new Vector());
// Get new velocity //Get new velocity
final Vector newVelocity = new Vector(modX, 0.0F, modZ); Vector newVelocityDirection = getVectorFromYaw(this.getRotation());
//-z Vector newVelocity = newVelocityDirection.multiply(velocity);
Stargate.debug("teleport", modX + " " + modZ); adjustRotation(exit, origin);
newVelocity.multiply(velocity);
adjustRotation(traveller, exit, origin);
List<Entity> passengers = vehicle.getPassengers(); List<Entity> passengers = vehicle.getPassengers();
World vehicleWorld = exit.getWorld(); World vehicleWorld = exit.getWorld();
@ -739,6 +737,30 @@ public class Portal {
} }
} }
/**
* Gets a direction vector given a yaw
* @param yaw <p>The yaw to use</p>
* @return <p>The direction vector of the yaw</p>
*/
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 * Teleport a vehicle which is not a minecart or a boat
* *
@ -768,7 +790,8 @@ public class Portal {
vehicle.remove(); vehicle.remove();
vehicle.setRotation(exit.getYaw(), exit.getPitch()); vehicle.setRotation(exit.getYaw(), exit.getPitch());
handleVehiclePassengers(passengers, newVehicle); 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 <p>The rotation of this portal</p> * @return <p>The rotation of this portal</p>
*/ */
public float getRotX() { public float getYaw() {
return this.rotX; return this.yaw;
} }
/** /**

View File

@ -237,24 +237,24 @@ public class PortalHandler {
// Moved the layout check so as to avoid invalid messages when not making a gate // Moved the layout check so as to avoid invalid messages when not making a gate
int modX = 0; int modX = 0;
int modZ = 0; int modZ = 0;
float rotX = 0f; float yaw = 0f;
BlockFace buttonFacing = BlockFace.DOWN; BlockFace buttonFacing = BlockFace.DOWN;
if (idParent.getX() > id.getBlock().getX()) { if (idParent.getX() > id.getBlock().getX()) {
modZ -= 1; modZ -= 1;
rotX = 90f; yaw = 90f;
buttonFacing = BlockFace.WEST; buttonFacing = BlockFace.WEST;
} else if (idParent.getX() < id.getBlock().getX()) { } else if (idParent.getX() < id.getBlock().getX()) {
modZ += 1; modZ += 1;
rotX = 270f; yaw = 270f;
buttonFacing = BlockFace.EAST; buttonFacing = BlockFace.EAST;
} else if (idParent.getZ() > id.getBlock().getZ()) { } else if (idParent.getZ() > id.getBlock().getZ()) {
modX += 1; modX += 1;
rotX = 180f; yaw = 180f;
buttonFacing = BlockFace.NORTH; buttonFacing = BlockFace.NORTH;
} else if (idParent.getZ() < id.getBlock().getZ()) { } else if (idParent.getZ() < id.getBlock().getZ()) {
modX -= 1; modX -= 1;
rotX = 0f; yaw = 0f;
buttonFacing = BlockFace.SOUTH; buttonFacing = BlockFace.SOUTH;
} }
@ -376,7 +376,7 @@ public class PortalHandler {
BlockLocation button = null; BlockLocation button = null;
Portal portal; 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); gate, player.getUniqueId(), player.getName(), portalOptions);
int cost = Stargate.getCreateCost(player, gate); int cost = Stargate.getCreateCost(player, gate);
@ -654,7 +654,7 @@ public class PortalHandler {
builder.append(':'); builder.append(':');
builder.append(portal.getModZ()); builder.append(portal.getModZ());
builder.append(':'); builder.append(':');
builder.append(portal.getRotX()); builder.append(portal.getYaw());
builder.append(':'); builder.append(':');
builder.append(portal.getTopLeft().toString()); builder.append(portal.getTopLeft().toString());
builder.append(':'); builder.append(':');
@ -829,7 +829,7 @@ public class PortalHandler {
BlockLocation button = (portalData[2].length() > 0) ? new BlockLocation(world, portalData[2]) : null; BlockLocation button = (portalData[2].length() > 0) ? new BlockLocation(world, portalData[2]) : null;
int modX = Integer.parseInt(portalData[3]); int modX = Integer.parseInt(portalData[3]);
int modZ = Integer.parseInt(portalData[4]); 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]); BlockLocation topLeft = new BlockLocation(world, portalData[6]);
Gate gate = GateHandler.getGateByName(portalData[7]); Gate gate = GateHandler.getGateByName(portalData[7]);
if (gate == null) { if (gate == null) {
@ -864,7 +864,7 @@ public class PortalHandler {
} }
//Creates the new portal //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)); network, gate, ownerUUID, ownerName, getPortalOptions(portalData));
registerPortal(portal); registerPortal(portal);