13
0
Dieser Commit ist enthalten in:
Lixfel 2022-01-02 21:02:49 +01:00
Ursprung 95cc8326d7
Commit 5dddb69484
7 geänderte Dateien mit 65 neuen und 69 gelöschten Zeilen

Datei anzeigen

@ -4,6 +4,7 @@ import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.regions.RegionSelector;
import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper; import de.steamwar.command.TypeMapper;
@ -126,15 +127,14 @@ public class PortalCommand extends SWCommand {
.getRegionSelector(BukkitAdapter.adapt(player.getWorld())); .getRegionSelector(BukkitAdapter.adapt(player.getWorld()));
try { try {
BlockVector3 min = regionSelector.getRegion().getMinimumPoint(); CuboidRegion region = (CuboidRegion)regionSelector.getRegion();
BlockVector3 max = regionSelector.getRegion().getMaximumPoint(); return new Tuple<>(adapt(player.getWorld(), region.getPos1()), adapt(player.getWorld(), region.getPos2()));
return new Tuple<>(adapt(player.getWorld(), min), adapt(player.getWorld(), max));
} catch (IncompleteRegionException e) { } catch (IncompleteRegionException e) {
return null; return null;
} }
} }
private Location adapt(World world, BlockVector3 blockVector3) { private Location adapt(World world, BlockVector3 blockVector3) {
return new Location(world, blockVector3.getBlockX(), blockVector3.getBlockY(), blockVector3.getBlockZ()); return new Location(world, blockVector3.getBlockX() + 0.5, blockVector3.getBlockY(), blockVector3.getBlockZ() + 0.5);
} }
} }

Datei anzeigen

@ -157,11 +157,11 @@ public class Hologram implements ConfigurationSerializable {
public void delete() { public void delete() {
display.delete(); display.delete();
if(id != null) if(id != null)
holograms.remove(this); holograms.remove(id);
} }
@Override @Override
public String toString() { public String toString() {
return id + " " + location + " " + text; return id + " " + text;
} }
} }

Datei anzeigen

@ -47,10 +47,11 @@ public class Portals extends BasicListener {
@EventHandler @EventHandler
public void onMove(PlayerMoveEvent e) { public void onMove(PlayerMoveEvent e) {
Location from = e.getFrom();
Location to = e.getTo(); Location to = e.getTo();
assert to != null; assert to != null;
Portal portal = Portal.getPortal(to); Portal portal = Portal.getPortal(e.getPlayer(), from, to);
if (portal == null) if (portal == null)
return; return;
@ -59,7 +60,7 @@ public class Portals extends BasicListener {
if(!lastPortals.isEmpty() && lastPortals.peek() == portal) if(!lastPortals.isEmpty() && lastPortals.peek() == portal)
return; return;
portal.handle(player, to); portal.handle(player, from, to);
} }
@EventHandler @EventHandler

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.lobby.portal; package de.steamwar.lobby.portal;
import de.steamwar.comms.packets.ExecuteCommandPacket;
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;
@ -75,7 +76,7 @@ public class CommandPortal implements PortalHandler {
cmd.append(pieces[i]).append(parts[i+1].substring(1)); cmd.append(pieces[i]).append(parts[i+1].substring(1));
} }
player.sendMessage("Folgender Command wäre jetzt ausgeführt worden, könnte der Bungee das schon: " + cmd); new ExecuteCommandPacket(player, cmd.toString()).send(player);
} }
@Override @Override

Datei anzeigen

@ -32,8 +32,6 @@ import java.util.function.Function;
public class Portal implements PortalHandler, ConfigurationSerializable { 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<>();
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());
@ -43,35 +41,46 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
return portals.keySet(); return portals.keySet();
} }
public static Portal getPortal(Location loc) { public static Portal getPortal(Player player, Location from, Location to) {
return posMap.get(new Vector(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); Vector movement = to.toVector().subtract(from.toVector());
for(Portal portal : portals.values()) {
double d = portal.orientation.getX() * movement.getZ() - portal.orientation.getZ() * movement.getX();
if(d == 0 || !insidePortal(to.getY(), portal.pos1.getY(), portal.pos2.getY()))
continue;
double t = ((portal.pos2.getX() - to.getX()) * movement.getZ() - (portal.pos2.getZ() - to.getZ()) * movement.getX()) / d;
double u = ((portal.pos2.getX() - to.getX()) * portal.orientation.getZ() - (portal.pos2.getZ() - to.getZ()) * portal.orientation.getX()) / d;
if(0 <= t && t <= 1 && 0 <= u && u <= 1)
return portal;
}
return null;
} }
public static Portal getPortal(String id) { public static Portal getPortal(String id) {
return portals.get(id); return portals.get(id);
} }
private static boolean insidePortal(double x, double p1, double p2) {
return x < p1 ^ x < p2;
}
private final Location pos1; private final Location pos1;
private final Location pos2; private final Location pos2;
private final Vector orientation;
private final String id; private final String id;
private final PortalType type; private final PortalType type;
private final PortalHandler handler; private final PortalHandler handler;
final Vector v1;
final Vector v2;
final Vector dv;
final 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");
this.pos2 = (Location) map.get("pos2"); this.pos2 = (Location) map.get("pos2");
this.orientation = pos2.toVector().subtract(pos1.toVector());
this.type = PortalType.valueOf((String) map.get("type")); this.type = PortalType.valueOf((String) map.get("type"));
this.handler = type.deserialize(map, this); this.handler = type.deserialize(map, this);
v1 = getPos1().toVector();
v2 = getPos2().toVector();
dv = v2.clone().subtract(v1);
degree = NORMAL.angle(dv);
init(); init();
} }
@ -80,12 +89,9 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
this.id = id; this.id = id;
this.pos1 = pos1; this.pos1 = pos1;
this.pos2 = pos2; this.pos2 = pos2;
this.orientation = pos2.toVector().subtract(pos1.toVector());
this.handler = handlerConstructor.apply(this); this.handler = handlerConstructor.apply(this);
this.type = handler.type(); this.type = handler.type();
v1 = pos1.toVector();
v2 = pos2.toVector();
dv = v2.clone().subtract(v1);
degree = NORMAL.angle(dv);
init(); init();
LobbySystem.config().save(); LobbySystem.config().save();
@ -93,18 +99,11 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
private void init() { private void init() {
portals.put(id, this); portals.put(id, this);
for(int x = pos1.getBlockX(); x <= pos2.getBlockX(); x++) {
for(int y = pos1.getBlockY(); y <= pos2.getBlockY(); y++) {
for(int z = pos1.getBlockZ(); z <= pos2.getBlockZ(); z++) {
posMap.put(new Vector(x, y, z), this);
}
}
}
} }
@Override @Override
public void handle(Player player, Location from) { public void handle(Player player, Location from, Location to) {
handler.handle(player, from); handler.handle(player, from, to);
Portals.getStack(player).push(this); Portals.getStack(player).push(this);
} }
@ -126,7 +125,6 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
public void delete() { public void delete() {
handler.delete(); handler.delete();
portals.remove(id); portals.remove(id);
posMap.values().removeIf(this::equals);
LobbySystem.config().save(); LobbySystem.config().save();
} }
@ -145,6 +143,10 @@ public class Portal implements PortalHandler, ConfigurationSerializable {
return pos2; return pos2;
} }
public Vector getOrientation() {
return orientation;
}
public String getId() { public String getId() {
return id; return id;
} }

Datei anzeigen

@ -25,7 +25,13 @@ import org.bukkit.entity.Player;
import java.util.Map; import java.util.Map;
public interface PortalHandler { public interface PortalHandler {
void handle(Player player, Location from); default void handle(Player player, Location to) {
throw new UnsupportedOperationException();
}
default void handle(Player player, Location from, Location to) {
handle(player, to);
}
void serialize(Map<String, Object> map); void serialize(Map<String, Object> map);

Datei anzeigen

@ -55,50 +55,36 @@ public class TeleportPortal implements PortalHandler {
} }
@Override @Override
public void handle(Player player, Location loc) { public void handle(Player player, Location from, Location to) {
Deque<Portal> stack = Portals.getStack(player); Deque<Portal> stack = Portals.getStack(player);
if(!stack.isEmpty() && sources.contains(stack.peek().getHandler())) { if(!stack.isEmpty() && sources.contains(stack.peek().getHandler())) {
teleport(player, loc, stack.pop()); teleport(player, from, to, stack.pop());
} else { } else {
teleport(player, loc, Portal.getPortal(target)); teleport(player, from, to, Portal.getPortal(target));
} }
} }
protected void teleport(Player player, Location from, Portal target) { private void teleport(Player player, Location from, Location to, Portal target) {
player.sendMessage("teleport " + portal.getId() + " -> " + target.getId()); player.sendMessage("teleport " + portal.getId() + " -> " + target.getId());
Vector vector = from.toVector();
double yaw = from.getYaw(); Vector ownOrientation = portal.getOrientation().clone();
Vector lookVector = Portal.NORMAL.clone().rotateAroundY(Math.toRadians(yaw)).normalize(); Vector targetOrientation = target.getOrientation().clone();
double portalDegreeDelta = portal.degree - Portal.NORMAL.angle(lookVector); double rotation = Math.atan2(targetOrientation.getZ(), targetOrientation.getX()) - Math.atan2(ownOrientation.getZ(), ownOrientation.getX());
// System.out.println(Math.toDegrees(degree) + " " + Math.toDegrees(portalDegreeDelta) + " " + Math.toDegrees(destination.degree)); rotation = Math.atan2(Math.sin(rotation), Math.cos(rotation));
double relativeSectionX = vector.clone().subtract(portal.v1).getX() / portal.dv.getX(); Vector pos = to.toVector()
double relativeSectionZ = vector.clone().subtract(portal.v1).getZ() / portal.dv.getZ(); .subtract(portal.getPos1().toVector()).divide(absPlusOne(ownOrientation))
double relativeHeight = (vector.getY() - portal.v1.getY()) / portal.dv.getY(); .rotateAroundY(-rotation)
.multiply(absPlusOne(targetOrientation)).add(target.getPos1().toVector());
player.teleport(pos.toLocation(from.getWorld(), (float) (to.getYaw() + Math.toDegrees(rotation)), to.getPitch()), PlayerTeleportEvent.TeleportCause.PLUGIN);
}
Vector destinationSection = target.v1.clone(); private Vector absPlusOne(Vector v) {
Vector now = target.dv.clone(); v.setX(Math.abs(v.getX()) + 1);
if (Double.isFinite(relativeSectionX)) { v.setY(Math.abs(v.getY()) + 1);
now.setX(now.getX() * relativeSectionX); v.setZ(Math.abs(v.getZ()) + 1);
} return v;
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