From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic 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 e95f2222814e104bf9194a96385737dffe2cb2b5..dd6b78d94dcd16c0a708dbc1764e24e9dbe4d3b1 100644 --- a/src/main/java/net/minecraft/server/commands/SetSpawnCommand.java +++ b/src/main/java/net/minecraft/server/commands/SetSpawnCommand.java @@ -32,9 +32,21 @@ public class SetSpawnCommand { private static int setSpawn(CommandSourceStack source, Collection targets, BlockPos pos, float angle) { ResourceKey resourceKey = source.getLevel().dimension(); + final Collection actualTargets = new java.util.ArrayList<>(); // Paper for(ServerPlayer serverPlayer : targets) { - serverPlayer.setRespawnPosition(resourceKey, pos, angle, true, false); + // Paper start - PlayerSetSpawnEvent + if (serverPlayer.setRespawnPosition(resourceKey, pos, angle, true, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.COMMAND)) { + actualTargets.add(serverPlayer); + } + // Paper end } + // Paper start + if (actualTargets.isEmpty()) { + return 0; + } else { + targets = actualTargets; + } + // Paper end String string = resourceKey.location().toString(); if (targets.size() == 1) { diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java index 3b1a7cfadd669afafe7e34a3c7c31fd913e3bbd6..58ece1e30d316a81e347f196d288292154d71bfe 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -1279,7 +1279,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); + 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 { @@ -2107,12 +2107,33 @@ public class ServerPlayer extends Player { return this.respawnForced; } + @Deprecated // Paper public void setRespawnPosition(ResourceKey dimension, @Nullable BlockPos pos, float angle, boolean forced, boolean sendMessage) { + // Paper start + this.setRespawnPosition(dimension, pos, angle, forced, sendMessage, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.UNKNOWN); + } + public boolean setRespawnPosition(ResourceKey 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 = net.minecraft.server.MCUtil.toLocation(this.getServer().getLevel(dimension), pos); + spawnLoc.setYaw(angle); + willNotify = sendMessage && !flag2; + } + com.destroystokyo.paper.event.player.PlayerSetSpawnEvent event = new com.destroystokyo.paper.event.player.PlayerSetSpawnEvent(this.getBukkitEntity(), cause, spawnLoc, forced, willNotify, willNotify ? net.kyori.adventure.text.Component.translatable("block.minecraft.set_spawn") : null); + if (!event.callEvent()) { + return false; + } + if (event.getLocation() != null) { + dimension = event.getLocation().getWorld() != null ? ((CraftWorld) event.getLocation().getWorld()).getHandle().dimension() : dimension; + pos = net.minecraft.server.MCUtil.toBlockPosition(event.getLocation()); + angle = (float) event.getLocation().getYaw(); + forced = event.isForced(); + // Paper end - if (sendMessage && !flag2) { - this.sendMessage(new TranslatableComponent("block.minecraft.set_spawn"), Util.NIL_UUID); + if (event.willNotifyPlayer() && event.getNotification() != null) { // Paper + this.sendMessage(PaperAdventure.asVanilla(event.getNotification()), Util.NIL_UUID); // Paper } this.respawnPosition = pos; @@ -2126,6 +2147,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 88820ba073e89638304b26e52a39a426f76fb2e9..1fad95b709bbc755e5906a93c8d2ed09b52154ce 100644 --- a/src/main/java/net/minecraft/server/players/PlayerList.java +++ b/src/main/java/net/minecraft/server/players/PlayerList.java @@ -895,13 +895,13 @@ public abstract class PlayerList { f1 = (float) Mth.wrapDegrees(Mth.atan2(vec3d1.z, vec3d1.x) * 57.2957763671875D - 90.0D); } - entityplayer1.setRespawnPosition(worldserver1.dimension(), blockposition, f, flag1, false); + entityplayer1.setRespawnPosition(worldserver1.dimension(), blockposition, f, flag1, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLAYER_RESPAWN); // Paper - PlayerSetSpawnEvent flag2 = !flag && flag3; isBedSpawn = true; location = new Location(worldserver1.getWorld(), vec3d.x, vec3d.y, vec3d.z, 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); // 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 d620f559cdd1bd0e161a99123ef6c6f64e3302df..3770313892b6e9b9ab6f9fcc200dfc0b4972e516 100644 --- a/src/main/java/net/minecraft/world/level/block/RespawnAnchorBlock.java +++ b/src/main/java/net/minecraft/world/level/block/RespawnAnchorBlock.java @@ -73,9 +73,14 @@ public class RespawnAnchorBlock extends Block { if (!world.isClientSide) { ServerPlayer serverPlayer = (ServerPlayer)player; if (serverPlayer.getRespawnDimension() != world.dimension() || !pos.equals(serverPlayer.getRespawnPosition())) { - serverPlayer.setRespawnPosition(world.dimension(), pos, 0.0F, false, true); + if (serverPlayer.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 56884ab0053051fca28c5ff58af55c027e296f39..f110643dd7147f0e95dd5fba8e8506407089ed19 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -1194,9 +1194,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); + 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(), new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), location.getYaw(), override, false); + this.getHandle().setRespawnPosition(((CraftWorld) location.getWorld()).getHandle().dimension(), new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), location.getYaw(), override, false, com.destroystokyo.paper.event.player.PlayerSetSpawnEvent.Cause.PLUGIN); // Paper - PlayerSetSpawnEvent } }