13
0

Implement teleport in TeleportPortal

Dieser Commit ist enthalten in:
yoyosource 2021-10-04 15:26:10 +02:00
Ursprung fe6e963d4c
Commit 8b1c74e31d
2 geänderte Dateien mit 46 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -32,6 +32,7 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
private static final Map<String, Portal> portals = new HashMap<>(); private static final Map<String, Portal> portals = new HashMap<>();
private static final Map<Vector, Portal> posMap = new HashMap<>(); private static final Map<Vector, Portal> posMap = new HashMap<>();
public static final Vector NORMAL = new Vector(0, 0, 1);
public static List<Portal> getPortals() { public static List<Portal> getPortals() {
return new ArrayList<>(portals.values()); return new ArrayList<>(portals.values());
@ -55,6 +56,11 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
private final PortalType type; private final PortalType type;
private final PortalHandler handler; private final PortalHandler handler;
public Vector v1;
public Vector v2;
public Vector dv;
public double degree;
public Portal(Map<String, Object> map) { public Portal(Map<String, Object> map) {
this.id = (String) map.get("id"); this.id = (String) map.get("id");
this.pos1 = (Location) map.get("pos1"); this.pos1 = (Location) map.get("pos1");
@ -87,6 +93,11 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
} }
} }
} }
v1 = getPos1().toVector();
v2 = getPos2().toVector();
dv = v2.clone().subtract(v1);
this.degree = NORMAL.angle(dv);
} }
@Override @Override

Datei anzeigen

@ -22,6 +22,8 @@ package de.steamwar.lobby.portal;
import de.steamwar.lobby.listener.Portals; import de.steamwar.lobby.listener.Portals;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.util.Vector;
import java.util.*; import java.util.*;
@ -62,7 +64,39 @@ public class TeleportPortal implements PortalHandler {
} }
protected void teleport(Player player, Location from, Portal target) { protected void teleport(Player player, Location from, Portal target) {
player.sendMessage("Symbolisierter teleport zu " + target.getId()); Vector vector = from.toVector();
double yaw = from.getYaw();
Vector lookVector = Portal.NORMAL.clone().rotateAroundY(Math.toRadians(yaw)).normalize();
double portalDegreeDelta = portal.degree - Portal.NORMAL.angle(lookVector);
// System.out.println(Math.toDegrees(degree) + " " + Math.toDegrees(portalDegreeDelta) + " " + Math.toDegrees(destination.degree));
double relativeSectionX = vector.clone().subtract(portal.v1).getX() / portal.dv.getX();
double relativeSectionZ = vector.clone().subtract(portal.v1).getZ() / portal.dv.getZ();
double relativeHeight = (vector.getY() - portal.v1.getY()) / portal.dv.getY();
Vector destinationSection = target.v1.clone();
Vector now = target.dv.clone();
if (Double.isFinite(relativeSectionX)) {
now.setX(now.getX() * relativeSectionX);
}
if (Double.isFinite(relativeSectionZ)) {
now.setZ(now.getZ() * relativeSectionZ);
}
destinationSection.add(now);
Vector v = destinationSection.clone();
v.setX(v.getBlockX());
v.setY(target.v1.getY());
v.setZ(v.getBlockZ());
double destinationHeight = portal.dv.getY() * relativeHeight;
Location location = destinationSection.toLocation(from.getWorld());
location.setPitch(from.getPitch());
location.setYaw((float) Math.toDegrees(target.degree + portalDegreeDelta));
location.add(0, destinationHeight, 0);
player.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
} }
@Override @Override