Skip to content

Commit

Permalink
Fixed some teleport patch bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Oct 23, 2023
1 parent 3ed2170 commit 42dafbc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ default void disconnect(String s) {
default void teleport(double d0, double d1, double d2, float f, float f1, PlayerTeleportEvent.TeleportCause cause) {
}

default void teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set, PlayerTeleportEvent.TeleportCause cause) { // CraftBukkit - Return event status
default boolean teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set, PlayerTeleportEvent.TeleportCause cause) { // CraftBukkit - Return event status
return false;
}

default void teleport(Location dest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,13 @@ public Entity changeDimension(ServerLevel worldserver, PlayerTeleportEvent.Telep

@Inject(method = "teleportTo(Lnet/minecraft/server/level/ServerLevel;DDDLjava/util/Set;FF)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;teleport(DDDFFLjava/util/Set;)V"))
private void banner$forwardReason(ServerLevel level, double x, double y, double z, Set<RelativeMovement> relativeMovements, float yRot, float xRot, CallbackInfoReturnable<Boolean> cir) {
var teleportCause = banner$changeDimensionCause.getAndSet(PlayerTeleportEvent.TeleportCause.UNKNOWN);
this.connection.pushTeleportCause(teleportCause);
this.connection.pushTeleportCause(banner$changeDimensionCause.getAndSet(PlayerTeleportEvent.TeleportCause.UNKNOWN));
}

@Inject(method = "teleportTo(Lnet/minecraft/server/level/ServerLevel;DDDFF)V", cancellable = true, at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/server/level/ServerPlayer;stopRiding()V"))
private void arclight$handleBy(ServerLevel world, double x, double y, double z, float yaw, float pitch, CallbackInfo ci) {
this.getBukkitEntity().teleport(new Location(world.getWorld(), x, y, z, yaw, pitch), banner$changeDimensionCause.getAndSet(PlayerTeleportEvent.TeleportCause.UNKNOWN));
ci.cancel();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -395,7 +396,6 @@ public void handleMoveVehicle(final ServerboundMoveVehiclePacket packetplayinveh
}

entity.move(MoverType.PLAYER, new Vec3(d6, d7, d8));
double d11 = d7;

d6 = d3 - entity.getX();
d7 = d4 - entity.getY();
Expand Down Expand Up @@ -481,7 +481,7 @@ public void handleMoveVehicle(final ServerboundMoveVehiclePacket packetplayinveh

this.player.serverLevel().getChunkSource().move(this.player);
this.player.checkMovementStatistics(this.player.getX() - d0, this.player.getY() - d1, this.player.getZ() - d2);
this.clientVehicleIsFloating = d11 >= -0.03125D && !flag1 && !this.server.isFlightAllowed() && !entity.isNoGravity() && this.noBlocksAround(entity);
this.clientVehicleIsFloating = d7 >= -0.03125D && !flag1 && !this.server.isFlightAllowed() && !entity.isNoGravity() && this.noBlocksAround(entity);
this.vehicleLastGoodX = entity.getX();
this.vehicleLastGoodY = entity.getY();
this.vehicleLastGoodZ = entity.getZ();
Expand Down Expand Up @@ -1809,51 +1809,61 @@ public void handlePlayerAbilities(ServerboundPlayerAbilitiesPacket packet) {
}

private transient PlayerTeleportEvent.TeleportCause banner$cause;
private AtomicBoolean cancelledMoveEvent = new AtomicBoolean(false);

/**
* @author wdog5
* @reason bukkit
*/
@Overwrite
public void teleport(double x, double y, double z, float yaw, float pitch, Set<RelativeMovement> set) {
public void teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set) {
PlayerTeleportEvent.TeleportCause cause = banner$cause == null ? PlayerTeleportEvent.TeleportCause.UNKNOWN : banner$cause;
banner$cause = null;

double x = d0;
double y = d1;
double z = d2;
float yaw = f;
float pitch = f1;

org.bukkit.entity.Player player = this.getCraftPlayer();
Location from = player.getLocation();

Location to = new Location(this.getCraftPlayer().getWorld(), x, y, z, yaw, pitch);
// SPIGOT-5171: Triggered on join
if (!from.equals(to)) {
PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause);
this.cserver.getPluginManager().callEvent(event);

if (event.isCancelled() || !to.equals(event.getTo())) {
set = Collections.emptySet(); // Banner TODO
to = event.isCancelled() ? event.getFrom() : event.getTo();
x = to.getX();
y = to.getY();
z = to.getZ();
yaw = to.getYaw();
pitch = to.getPitch();
}
}
if (Float.isNaN(yaw)) {
yaw = 0.0f;
if (from.equals(to)) {
this.internalTeleport(d0, d1, d2, f, f1, set);
cancelledMoveEvent.set(false);
return; // CraftBukkit - Return event status
}
if (Float.isNaN(pitch)) {
pitch = 0.0f;

PlayerTeleportEvent event = new PlayerTeleportEvent(player, from.clone(), to.clone(), cause);
this.cserver.getPluginManager().callEvent(event);

if (event.isCancelled() || !to.equals(event.getTo())) {
set = Collections.emptySet(); // Banner TODO
to = event.isCancelled() ? event.getFrom() : event.getTo();
d0 = to.getX();
d1 = to.getY();
d2 = to.getZ();
f = to.getYaw();
f1 = to.getPitch();
}
this.internalTeleport(x, y, z, yaw, pitch, set);

this.internalTeleport(d0, d1, d2, f, f1, set);
this.cancelledMoveEvent.set(event.isCancelled()); // CraftBukkit - Return event status
}

@Override
public void teleport(double d0, double d1, double d2, float f, float f1, PlayerTeleportEvent.TeleportCause cause) {
this.teleport(d0, d1, d2, f, f1, Collections.emptySet(), cause);
}

public void teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set, PlayerTeleportEvent.TeleportCause cause) {
@Override
public boolean teleport(double d0, double d1, double d2, float f, float f1, Set<RelativeMovement> set, PlayerTeleportEvent.TeleportCause cause) {
pushTeleportCause(cause);
this.teleport(d0, d1, d2, f, f1, set);
return cancelledMoveEvent.get();
}

@Override
Expand All @@ -1880,14 +1890,6 @@ public void internalTeleport(double d0, double d1, double d2, float f, float f1,
this.awaitingTeleport = 0;
}

// CraftBukkit start - update last location
this.lastPosX = this.awaitingPositionFromClient.x;
this.lastPosY = this.awaitingPositionFromClient.y;
this.lastPosZ = this.awaitingPositionFromClient.z;
this.lastYaw = f;
this.lastPitch = f1;
// CraftBukkit end

this.awaitingTeleportTime = this.tickCount;
this.player.absMoveTo(d0, d1, d2, f, f1);
this.player.connection.send(new ClientboundPlayerPositionPacket(d0 - d3, d1 - d4, d2 - d5, f - f2, f1 - f3, set, this.awaitingTeleport));
Expand Down

0 comments on commit 42dafbc

Please sign in to comment.