2020-05-14 23:05:00 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Caleb Bassham <caleb.bassham@gmail.com>
|
|
|
|
Date: Fri, 28 Sep 2018 02:32:19 -0500
|
|
|
|
Subject: [PATCH] Call player spectator target events and improve
|
|
|
|
implementation
|
|
|
|
|
|
|
|
Use a proper teleport for teleporting to entities in different
|
|
|
|
worlds.
|
|
|
|
|
|
|
|
Implementation improvements authored by Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
|
|
Validate that the target entity is valid and deny spectate
|
|
|
|
requests from frozen players.
|
|
|
|
|
|
|
|
Also, make sure the entity is spawned to the client before
|
|
|
|
sending the camera packet. If the entity isn't spawned clientside
|
|
|
|
when it receives the camera packet, then the client will not
|
|
|
|
spectate the target entity.
|
|
|
|
|
|
|
|
Co-authored-by: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
|
|
|
2021-03-16 07:19:45 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/EntityPlayer.java b/src/main/java/net/minecraft/server/level/EntityPlayer.java
|
2021-04-22 01:31:45 +00:00
|
|
|
index 1271de75743356090050763ff2fb67d28b48cb23..46d1e766e234bf49d31583e9e59aeb33c719b1ec 100644
|
2021-03-16 07:19:45 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/level/EntityPlayer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/level/EntityPlayer.java
|
2021-04-22 01:31:45 +00:00
|
|
|
@@ -1811,15 +1811,59 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
|
2020-05-14 23:05:00 +00:00
|
|
|
return (Entity) (this.spectatedEntity == null ? this : this.spectatedEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
- public void setSpectatorTarget(Entity entity) {
|
|
|
|
+ public void setSpectatorTarget(Entity newSpectatorTarget) {
|
|
|
|
+ // Paper start - Add PlayerStartSpectatingEntityEvent and PlayerStopSpectatingEntity Event and improve implementation
|
|
|
|
Entity entity1 = this.getSpecatorTarget();
|
|
|
|
|
|
|
|
- this.spectatedEntity = (Entity) (entity == null ? this : entity);
|
|
|
|
- if (entity1 != this.spectatedEntity) {
|
|
|
|
- this.playerConnection.sendPacket(new PacketPlayOutCamera(this.spectatedEntity));
|
|
|
|
- this.playerConnection.a(this.spectatedEntity.locX(), this.spectatedEntity.locY(), this.spectatedEntity.locZ(), this.yaw, this.pitch, TeleportCause.SPECTATE); // CraftBukkit
|
|
|
|
+ if (newSpectatorTarget == null) {
|
|
|
|
+ newSpectatorTarget = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ if (entity1 == newSpectatorTarget) return; // new spec target is the current spec target
|
|
|
|
+
|
|
|
|
+ if (newSpectatorTarget == this) {
|
|
|
|
+ com.destroystokyo.paper.event.player.PlayerStopSpectatingEntityEvent playerStopSpectatingEntityEvent = new com.destroystokyo.paper.event.player.PlayerStopSpectatingEntityEvent(this.getBukkitEntity(), entity1.getBukkitEntity());
|
|
|
|
+
|
|
|
|
+ if (!playerStopSpectatingEntityEvent.callEvent()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ com.destroystokyo.paper.event.player.PlayerStartSpectatingEntityEvent playerStartSpectatingEntityEvent = new com.destroystokyo.paper.event.player.PlayerStartSpectatingEntityEvent(this.getBukkitEntity(), entity1.getBukkitEntity(), newSpectatorTarget.getBukkitEntity());
|
|
|
|
+
|
|
|
|
+ if (!playerStartSpectatingEntityEvent.callEvent()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Validate
|
|
|
|
+ if (newSpectatorTarget != this) {
|
|
|
|
+ if (newSpectatorTarget.dead || newSpectatorTarget.shouldBeRemoved || !newSpectatorTarget.valid || newSpectatorTarget.world == null) {
|
|
|
|
+ MinecraftServer.LOGGER.info("Blocking player " + this.toString() + " from spectating invalid entity " + newSpectatorTarget.toString());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (this.isFrozen()) {
|
|
|
|
+ // use debug: clients might maliciously spam this
|
|
|
|
+ MinecraftServer.LOGGER.debug("Blocking frozen player " + this.toString() + " from spectating entity " + newSpectatorTarget.toString());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.spectatedEntity = newSpectatorTarget; // only set after validating state
|
|
|
|
+
|
|
|
|
+ if (newSpectatorTarget != this) {
|
|
|
|
+ // Make sure we're in the right place
|
|
|
|
+ this.ejectPassengers(); // teleport can fail if we have passengers...
|
|
|
|
+ this.getBukkitEntity().teleport(new Location(newSpectatorTarget.getWorld().getWorld(), newSpectatorTarget.locX(), newSpectatorTarget.locY(), newSpectatorTarget.locZ(), this.yaw, this.pitch), TeleportCause.SPECTATE); // Correctly handle cross-world entities from api calls by using CB teleport
|
|
|
|
+
|
|
|
|
+ // Make sure we're tracking the entity before sending
|
|
|
|
+ PlayerChunkMap.EntityTracker tracker = ((WorldServer)newSpectatorTarget.world).getChunkProvider().playerChunkMap.trackedEntities.get(newSpectatorTarget.getId());
|
|
|
|
+ if (tracker != null) { // dumb plugins...
|
|
|
|
+ tracker.updatePlayer(this);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ this.playerConnection.teleport(this.spectatedEntity.locX(), this.spectatedEntity.locY(), this.spectatedEntity.locZ(), this.yaw, this.pitch, TeleportCause.SPECTATE); // CraftBukkit
|
|
|
|
+ }
|
|
|
|
+ this.playerConnection.sendPacket(new PacketPlayOutCamera(newSpectatorTarget));
|
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-16 07:19:45 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/network/PlayerConnection.java b/src/main/java/net/minecraft/server/network/PlayerConnection.java
|
2021-04-18 09:02:48 +00:00
|
|
|
index 74942bf7641eafab85202fd6a521780ce4cc1f65..5ea1354fb9d1c570603fc6814198639a741a9e34 100644
|
2021-03-16 07:19:45 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/network/PlayerConnection.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/network/PlayerConnection.java
|
2021-04-18 09:02:48 +00:00
|
|
|
@@ -1355,6 +1355,7 @@ public class PlayerConnection implements PacketListenerPlayIn {
|
2020-05-14 23:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CraftBukkit start - Delegate to teleport(Location)
|
|
|
|
+ public final void teleport(double d0, double d1, double d2, float f, float f1, PlayerTeleportEvent.TeleportCause cause) { this.a(d0, d1, d2, f, f1, cause); } // Paper - OBFHELPER
|
|
|
|
public void a(double d0, double d1, double d2, float f, float f1, PlayerTeleportEvent.TeleportCause cause) {
|
|
|
|
this.a(d0, d1, d2, f, f1, Collections.<PacketPlayOutPosition.EnumPlayerTeleportFlags>emptySet(), cause);
|
|
|
|
}
|