Add events for player changing spectator target (#1498)

* Add events for player changing spectator target

- Add PlayerStartSpectatingEntityEvent
- Add PlayerStopSpectatingEntityEvent
This commit is contained in:
Caleb Bassham 2018-10-18 15:53:10 -05:00 committed by Zach
parent 42772e476a
commit 3b358452ba
3 changed files with 196 additions and 2 deletions

View file

@ -1,4 +1,4 @@
From 4ceb745753f3bb90b932daacc58c3fc104e8a795 Mon Sep 17 00:00:00 2001
From 418c1570fa8d0784cfaadd1a7070143ef2de5241 Mon Sep 17 00:00:00 2001
From: pkt77 <parkerkt77@gmail.com>
Date: Fri, 10 Nov 2017 23:45:59 -0500
Subject: [PATCH] Add PlayerArmorChangeEvent
@ -146,5 +146,5 @@ index 00000000..0783ac82
+ }
+}
--
2.18.0
2.19.1

View file

@ -0,0 +1,135 @@
From 44a47707ed4cc9e39a4e054061e8b58c6eb02790 Mon Sep 17 00:00:00 2001
From: Caleb Bassham <caleb.bassham@gmail.com>
Date: Fri, 28 Sep 2018 02:30:56 -0500
Subject: [PATCH] Add spectator target events
- PlayerStartSpectatingEntityEvent
- PlayerStopSpectatingEntityEvent
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerStartSpectatingEntityEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerStartSpectatingEntityEvent.java
new file mode 100644
index 00000000..d3b7cc27
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerStartSpectatingEntityEvent.java
@@ -0,0 +1,62 @@
+package com.destroystokyo.paper.event.player;
+
+import org.bukkit.entity.Entity;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+
+/**
+ * Triggered when a player starts spectating an entity in spectator mode.
+ */
+public class PlayerStartSpectatingEntityEvent extends PlayerEvent implements Cancellable {
+
+ private static final HandlerList handlers = new HandlerList();
+ private boolean cancelled;
+ private final Entity currentSpectatorTarget;
+ private final Entity newSpectatorTarget;
+
+ public PlayerStartSpectatingEntityEvent(Player player, Entity currentSpectatorTarget, Entity newSpectatorTarget) {
+ super(player);
+ this.currentSpectatorTarget = currentSpectatorTarget;
+ this.newSpectatorTarget = newSpectatorTarget;
+ }
+
+ /**
+ * Gets the entity that the player is currently spectating or themselves if they weren't spectating anything
+ *
+ * @return The entity the player is currently spectating (before they start spectating the new target).
+ */
+ public Entity getCurrentSpectatorTarget() {
+ return currentSpectatorTarget;
+ }
+
+ /**
+ * Gets the new entity that the player will now be spectating
+ *
+ * @return The entity the player is now going to be spectating.
+ */
+ public Entity getNewSpectatorTarget() {
+ return newSpectatorTarget;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
+
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerStopSpectatingEntityEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerStopSpectatingEntityEvent.java
new file mode 100644
index 00000000..ce23caaf
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerStopSpectatingEntityEvent.java
@@ -0,0 +1,50 @@
+package com.destroystokyo.paper.event.player;
+
+import org.bukkit.entity.Entity;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+
+/**
+ * Triggered when a player stops spectating an entity in spectator mode.
+ */
+public class PlayerStopSpectatingEntityEvent extends PlayerEvent implements Cancellable {
+
+ private static final HandlerList handlers = new HandlerList();
+ private boolean cancelled;
+ private final Entity spectatorTarget;
+
+ public PlayerStopSpectatingEntityEvent(Player player, Entity spectatorTarget) {
+ super(player);
+ this.spectatorTarget = spectatorTarget;
+ }
+
+ /**
+ * Gets the entity that the player is spectating
+ *
+ * @return The entity the player is currently spectating (before they will stop).
+ */
+ public Entity getSpectatorTarget() {
+ return spectatorTarget;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
--
2.19.1

View file

@ -0,0 +1,59 @@
From 7b0e1ee3eb6ff96297023b8d32b32dc1627bfd9f 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
diff --git a/src/main/java/net/minecraft/server/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java
index 5ab4e01ed..4ca5cfe9a 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
@@ -62,7 +62,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
private EntityHuman.EnumChatVisibility cs;
private boolean ct = true;
private long cu = SystemUtils.b();
- private Entity cv;
+ private Entity cv; private void setSpectatorTargetField(Entity e) { this.cv = e; } // Paper - OBFHELPER
public boolean worldChangeInvuln;
private boolean cx; private void setHasSeenCredits(boolean has) { this.cx = has; } // Paper - OBFHELPER
private final RecipeBookServer cy;
@@ -1373,15 +1373,33 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
return (Entity) (this.cv == null ? this : this.cv);
}
- public void setSpectatorTarget(Entity entity) {
+ public void setSpectatorTarget(Entity newSpectatorTarget) {
Entity entity1 = this.getSpecatorTarget();
- this.cv = (Entity) (entity == null ? this : entity);
- if (entity1 != this.cv) {
+ if (newSpectatorTarget == null) {
+ newSpectatorTarget = this;
+ }
+
+ if (entity1 != newSpectatorTarget) {
+ 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;
+ }
+ }
+
this.playerConnection.sendPacket(new PacketPlayOutCamera(this.cv));
this.playerConnection.a(this.cv.locX, this.cv.locY, this.cv.locZ, this.yaw, this.pitch, TeleportCause.SPECTATE); // CraftBukkit
}
+ setSpectatorTargetField(newSpectatorTarget);
}
protected void E() {
--
2.19.1