Paper/patches/server/0651-Add-PlayerSetSpawnEvent.patch
Jake Potrebic 2fa8efce9b
Updated Upstream (Bukkit/CraftBukkit) (#9485)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
82af5dc6 SPIGOT-7396: Add PlayerSignOpenEvent
3f0281ca SPIGOT-7063, PR-763: Add DragonBattle#initiateRespawn with custom EnderCrystals
f83c8df4 PR-873: Add PlayerRecipeBookClickEvent
14560d39 SPIGOT-7435: Add TeleportCause#EXIT_BED
2cc6db92 SPIGOT-7422, PR-887: Add API to set sherds on decorated pots
36022f02 PR-883: Add ItemFactory#getSpawnEgg
12eb5c46 PR-881: Update Scoreboard Javadocs, remove explicit exception throwing
f6d8d44a PR-882: Add modern time API methods to ban API
21a7b710 Upgrade some Maven plugins to reduce warnings
11fd1225 PR-886: Deprecate the SmithingRecipe constructor as it now does nothing
dbd1761d SPIGOT-7406: Improve documentation for getDragonBattle

CraftBukkit Changes:
d548daac2 SPIGOT-7446: BlockState#update not updating a spawner's type to null
70e0bc050 SPIGOT-7447: Fix --forceUpgrade
6752f1d63 SPIGOT-7396: Add PlayerSignOpenEvent
847b4cad5 SPIGOT-7063, PR-1071: Add DragonBattle#initiateRespawn with custom EnderCrystals
c335a555f PR-1212: Add PlayerRecipeBookClickEvent
4be756ecb SPIGOT-7445: Fix opening smithing inventory
db70bd6ed SPIGOT-7441: Fix issue placing certain items in creative/op
f7fa6d993 SPIGOT-7435: Add TeleportCause#EXIT_BED
b435e8e8d SPIGOT-7349: Player#setDisplayName not working when message/format unmodified
a2fafdd1d PR-1232: Re-add fix for player rotation
7cf863de1 PR-1233: Remove some old MC bug fixes now fixed in vanilla
08ec344ad Fix ChunkGenerator#generateCaves never being called
5daeb502a SPIGOT-7422, PR-1228: Add API to set sherds on decorated pots
52faa6b32 PR-1224: Add ItemFactory#getSpawnEgg
01cae71b7 SPIGOT-7429: Fix LEFT_CLICK_AIR not working for passable entities and spectators
a94277a18 PR-1223: Remove non-existent scoreboard display name/prefix/suffix limits
36b107660 PR-1225: Add modern time API methods to ban API
59ead25bc Upgrade some Maven plugins to reduce warnings
202fc5c4e Increase outdated build delay
ce545de57 SPIGOT-7398: TextDisplay#setInterpolationDuration incorrectly updates the line width

Spigot Changes:
b41c46db Rebuild patches
3374045a SPIGOT-7431: Fix EntityMountEvent returning opposite entities
0ca4eb66 Rebuild patches
2023-08-05 17:21:59 -07:00

205 lines
13 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 19 May 2021 18:59:10 -0700
Subject: [PATCH] Add PlayerSetSpawnEvent
diff --git a/src/main/java/net/minecraft/server/commands/SetSpawnCommand.java b/src/main/java/net/minecraft/server/commands/SetSpawnCommand.java
index a2d0699e8427b2262a2396495111125eccafbb66..d797637f61bdf8a424f56fbb48e28b7c9117d604 100644
--- a/src/main/java/net/minecraft/server/commands/SetSpawnCommand.java
+++ b/src/main/java/net/minecraft/server/commands/SetSpawnCommand.java
@@ -38,24 +38,34 @@ public class SetSpawnCommand {
ResourceKey<Level> resourcekey = source.getLevel().dimension();
Iterator iterator = targets.iterator();
+ final Collection<ServerPlayer> actualTargets = new java.util.ArrayList<>(); // Paper
while (iterator.hasNext()) {
ServerPlayer entityplayer = (ServerPlayer) iterator.next();
- entityplayer.setRespawnPosition(resourcekey, pos, angle, true, false, org.bukkit.event.player.PlayerSpawnChangeEvent.Cause.COMMAND); // CraftBukkit
+ // Paper start - PlayerSetSpawnEvent
+ if (entityplayer.setRespawnPosition(resourcekey, pos, angle, true, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.COMMAND)) {
+ actualTargets.add(entityplayer);
+ }
+ // Paper end
}
+ // Paper start
+ if (actualTargets.isEmpty()) {
+ return 0;
+ }
+ // Paper end
String s = resourcekey.location().toString();
- if (targets.size() == 1) {
+ if (actualTargets.size() == 1) { // Paper
source.sendSuccess(() -> {
- return Component.translatable("commands.spawnpoint.success.single", pos.getX(), pos.getY(), pos.getZ(), angle, s, ((ServerPlayer) targets.iterator().next()).getDisplayName());
+ return Component.translatable("commands.spawnpoint.success.single", pos.getX(), pos.getY(), pos.getZ(), angle, s, ((ServerPlayer) actualTargets.iterator().next()).getDisplayName()); // Paper
}, true);
} else {
source.sendSuccess(() -> {
- return Component.translatable("commands.spawnpoint.success.multiple", pos.getX(), pos.getY(), pos.getZ(), angle, s, targets.size());
+ return Component.translatable("commands.spawnpoint.success.multiple", pos.getX(), pos.getY(), pos.getZ(), angle, s, actualTargets.size()); // Paper
}, true);
}
- return targets.size();
+ return actualTargets.size(); // Paper
}
}
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index deaf1fcb189ba1382f2b52d12afa12463874f7ec..c06e81d5448364154d9fb4fb68c1867d04daeb97 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -1349,7 +1349,7 @@ public class ServerPlayer extends Player {
} else if (this.bedBlocked(blockposition, enumdirection)) {
return Either.left(Player.BedSleepingProblem.OBSTRUCTED);
} else {
- this.setRespawnPosition(this.level().dimension(), blockposition, this.getYRot(), false, true, PlayerSpawnChangeEvent.Cause.BED); // CraftBukkit
+ this.setRespawnPosition(this.level().dimension(), blockposition, this.getYRot(), false, true, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.BED); // Paper - PlayerSetSpawnEvent
if (this.level().isDay()) {
return Either.left(Player.BedSleepingProblem.NOT_POSSIBLE_NOW);
} else {
@@ -2196,44 +2196,50 @@ public class ServerPlayer extends Player {
return this.respawnForced;
}
+ @Deprecated // Paper
public void setRespawnPosition(ResourceKey<Level> dimension, @Nullable BlockPos pos, float angle, boolean forced, boolean sendMessage) {
- // CraftBukkit start
- this.setRespawnPosition(dimension, pos, angle, forced, sendMessage, PlayerSpawnChangeEvent.Cause.UNKNOWN);
- }
-
- public void setRespawnPosition(ResourceKey<Level> resourcekey, @Nullable BlockPos blockposition, float f, boolean flag, boolean flag1, PlayerSpawnChangeEvent.Cause cause) {
- ServerLevel newWorld = this.server.getLevel(resourcekey);
- Location newSpawn = (blockposition != null) ? CraftLocation.toBukkit(blockposition, newWorld.getWorld(), f, 0) : null;
-
- PlayerSpawnChangeEvent event = new PlayerSpawnChangeEvent(this.getBukkitEntity(), newSpawn, flag, cause);
- Bukkit.getServer().getPluginManager().callEvent(event);
- if (event.isCancelled()) {
- return;
- }
- newSpawn = event.getNewSpawn();
- flag = event.isForced();
-
- if (newSpawn != null) {
- resourcekey = ((CraftWorld) newSpawn.getWorld()).getHandle().dimension();
- blockposition = BlockPos.containing(newSpawn.getX(), newSpawn.getY(), newSpawn.getZ());
- f = newSpawn.getYaw();
- } else {
- resourcekey = Level.OVERWORLD;
- blockposition = null;
- f = 0.0F;
+ // Paper start
+ this.setRespawnPosition(dimension, pos, angle, forced, sendMessage, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.UNKNOWN);
+ }
+ @Deprecated
+ public boolean setRespawnPosition(ResourceKey<Level> dimension, @Nullable BlockPos pos, float angle, boolean forced, boolean sendMessage, PlayerSpawnChangeEvent.Cause cause) {
+ return this.setRespawnPosition(dimension, pos, angle, forced, sendMessage, cause == PlayerSpawnChangeEvent.Cause.RESET ?
+ com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLAYER_RESPAWN : com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.valueOf(cause.name()));
+ }
+ public boolean setRespawnPosition(ResourceKey<Level> dimension, @Nullable BlockPos pos, float angle, boolean forced, boolean sendMessage, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause cause) {
+ Location spawnLoc = null;
+ boolean willNotify = false;
+ if (pos != null) {
+ boolean flag2 = pos.equals(this.respawnPosition) && dimension.equals(this.respawnDimension);
+ spawnLoc = io.papermc.paper.util.MCUtil.toLocation(this.getServer().getLevel(dimension), pos);
+ spawnLoc.setYaw(angle);
+ willNotify = sendMessage && !flag2;
+ }
+
+ PlayerSpawnChangeEvent dumbEvent = new PlayerSpawnChangeEvent(this.getBukkitEntity(), spawnLoc, forced,
+ cause == com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLAYER_RESPAWN ? PlayerSpawnChangeEvent.Cause.RESET : PlayerSpawnChangeEvent.Cause.valueOf(cause.name()));
+ dumbEvent.callEvent();
+
+ com.destroystokyo.paper.event.player.PlayerSetSpawnEvent event = new com.destroystokyo.paper.event.player.PlayerSetSpawnEvent(this.getBukkitEntity(), cause, dumbEvent.getNewSpawn(), dumbEvent.isForced(), willNotify, willNotify ? net.kyori.adventure.text.Component.translatable("block.minecraft.set_spawn") : null);
+ event.setCancelled(dumbEvent.isCancelled());
+ if (!event.callEvent()) {
+ return false;
}
- // CraftBukkit end
- if (blockposition != null) {
- boolean flag2 = blockposition.equals(this.respawnPosition) && resourcekey.equals(this.respawnDimension);
+ if (event.getLocation() != null) {
+ dimension = event.getLocation().getWorld() != null ? ((CraftWorld) event.getLocation().getWorld()).getHandle().dimension() : dimension;
+ pos = io.papermc.paper.util.MCUtil.toBlockPosition(event.getLocation());
+ angle = event.getLocation().getYaw();
+ forced = event.isForced();
+ // Paper end
- if (flag1 && !flag2) {
- this.sendSystemMessage(Component.translatable("block.minecraft.set_spawn"));
+ if (event.willNotifyPlayer() && event.getNotification() != null) { // Paper
+ this.sendSystemMessage(PaperAdventure.asVanilla(event.getNotification())); // Paper
}
- this.respawnPosition = blockposition;
- this.respawnDimension = resourcekey;
- this.respawnAngle = f;
- this.respawnForced = flag;
+ this.respawnPosition = pos;
+ this.respawnDimension = dimension;
+ this.respawnAngle = angle;
+ this.respawnForced = forced;
} else {
this.respawnPosition = null;
this.respawnDimension = Level.OVERWORLD;
@@ -2241,6 +2247,7 @@ public class ServerPlayer extends Player {
this.respawnForced = false;
}
+ return true; // Paper
}
public void trackChunk(ChunkPos chunkPos, Packet<?> chunkDataPacket) {
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index c8e38ed5e14fb137a7d169c57bc58bdf4421e0cc..c3fb1bbc0e2a44160e86b5bb4ab88f78991fa9e6 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -860,7 +860,7 @@ public abstract class PlayerList {
location = CraftLocation.toBukkit(vec3d, worldserver1.getWorld(), f1, 0.0F);
} else if (blockposition != null) {
entityplayer1.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.NO_RESPAWN_BLOCK_AVAILABLE, 0.0F));
- entityplayer1.setRespawnPosition(null, null, 0f, false, false, PlayerSpawnChangeEvent.Cause.RESET); // CraftBukkit - SPIGOT-5988: Clear respawn location when obstructed
+ entityplayer1.setRespawnPosition(null, null, 0f, false, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLAYER_RESPAWN); // CraftBukkit - SPIGOT-5988: Clear respawn location when obstructed // Paper - PlayerSetSpawnEvent
}
}
diff --git a/src/main/java/net/minecraft/world/level/block/RespawnAnchorBlock.java b/src/main/java/net/minecraft/world/level/block/RespawnAnchorBlock.java
index 1a27b7faa22e6b3dc5fce329ed06425de56c4315..b9903c29bdea8d1e3b6fce0e97be6bd9493cfdf4 100644
--- a/src/main/java/net/minecraft/world/level/block/RespawnAnchorBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/RespawnAnchorBlock.java
@@ -80,9 +80,14 @@ public class RespawnAnchorBlock extends Block {
ServerPlayer entityplayer = (ServerPlayer) player;
if (entityplayer.getRespawnDimension() != world.dimension() || !pos.equals(entityplayer.getRespawnPosition())) {
- entityplayer.setRespawnPosition(world.dimension(), pos, 0.0F, false, true, org.bukkit.event.player.PlayerSpawnChangeEvent.Cause.RESPAWN_ANCHOR); // CraftBukkit
+ if (entityplayer.setRespawnPosition(world.dimension(), pos, 0.0F, false, true, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.RESPAWN_ANCHOR)) { // Paper - PlayerSetSpawnEvent
world.playSound((Player) null, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, SoundEvents.RESPAWN_ANCHOR_SET_SPAWN, SoundSource.BLOCKS, 1.0F, 1.0F);
return InteractionResult.SUCCESS;
+ // Paper start - handle failed set spawn
+ } else {
+ return InteractionResult.FAIL;
+ }
+ // Paper end
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 0177065e849d25954737712cb0156fdb79a89156..a8da7ff2f8e2cf0195731f5034afa94fcd248340 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -1322,9 +1322,9 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@Override
public void setBedSpawnLocation(Location location, boolean override) {
if (location == null) {
- this.getHandle().setRespawnPosition(null, null, 0.0F, override, false, PlayerSpawnChangeEvent.Cause.PLUGIN);
+ this.getHandle().setRespawnPosition(null, null, 0.0F, override, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLUGIN); // Paper - PlayerSetSpawnEvent
} else {
- this.getHandle().setRespawnPosition(((CraftWorld) location.getWorld()).getHandle().dimension(), CraftLocation.toBlockPosition(location), location.getYaw(), override, false, PlayerSpawnChangeEvent.Cause.PLUGIN);
+ this.getHandle().setRespawnPosition(((CraftWorld) location.getWorld()).getHandle().dimension(), CraftLocation.toBlockPosition(location), location.getYaw(), override, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLUGIN); // Paper - PlayerSetSpawnEvent
}
}