2021-06-15 04:59:31 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 15 May 2021 10:04:43 -0700
2024-01-19 17:54:05 +01:00
Subject: [PATCH] Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
diff --git a/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java b/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
2024-04-12 21:14:06 +02:00
index b95a979f8f58f43e0becedcd843ff8bb992eb455..a046a0b1519806ff3d987e6402f152b60a3a6f4c 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
+++ b/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
2024-04-12 21:14:06 +02:00
@@ -28,9 +28,13 @@ public class DefaultGameModeCommands {
2021-06-15 04:59:31 +02:00
GameType gameType = minecraftServer.getForcedGameType();
if (gameType != null) {
2024-04-12 21:14:06 +02:00
for (ServerPlayer serverPlayer : minecraftServer.getPlayerList().getPlayers()) {
2021-06-15 04:59:31 +02:00
- if (serverPlayer.setGameMode(gameType)) {
2024-04-12 21:14:06 +02:00
- i++;
2024-01-19 17:54:05 +01:00
+ // Paper start - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
+ org.bukkit.event.player.PlayerGameModeChangeEvent event = serverPlayer.setGameMode(gameType, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.DEFAULT_GAMEMODE, net.kyori.adventure.text.Component.empty());
+ if (event != null && event.isCancelled()) {
2023-06-08 22:56:13 +02:00
+ source.sendSuccess(() -> io.papermc.paper.adventure.PaperAdventure.asVanilla(event.cancelMessage()), false);
2021-06-15 04:59:31 +02:00
}
2024-01-19 17:54:05 +01:00
+ // Paper end - Expand PlayerGameModeChangeEvent
2024-04-12 21:14:06 +02:00
+ i++;
2021-06-15 04:59:31 +02:00
}
}
diff --git a/src/main/java/net/minecraft/server/commands/GameModeCommand.java b/src/main/java/net/minecraft/server/commands/GameModeCommand.java
2024-04-12 21:14:06 +02:00
index 7f09119bc7d661e08a960dd2bd46006efe752d3e..d1da3600dc07107309b20ebe6e7c0c4da0e8de76 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/server/commands/GameModeCommand.java
+++ b/src/main/java/net/minecraft/server/commands/GameModeCommand.java
2024-04-12 21:14:06 +02:00
@@ -60,9 +60,14 @@ public class GameModeCommand {
2021-06-15 04:59:31 +02:00
int i = 0;
2024-04-12 21:14:06 +02:00
for (ServerPlayer serverPlayer : targets) {
2021-06-15 04:59:31 +02:00
- if (serverPlayer.setGameMode(gameMode)) {
2024-01-19 17:54:05 +01:00
+ // Paper start - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
+ org.bukkit.event.player.PlayerGameModeChangeEvent event = serverPlayer.setGameMode(gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.COMMAND, net.kyori.adventure.text.Component.empty());
+ if (event != null && !event.isCancelled()) {
logGamemodeChange(context.getSource(), serverPlayer, gameMode);
2024-04-12 21:14:06 +02:00
i++;
2021-06-15 04:59:31 +02:00
+ } else if (event != null && event.cancelMessage() != null) {
2023-06-08 22:56:13 +02:00
+ context.getSource().sendSuccess(() -> io.papermc.paper.adventure.PaperAdventure.asVanilla(event.cancelMessage()), true);
2024-01-19 17:54:05 +01:00
+ // Paper end - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
}
}
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-04-24 15:46:45 +02:00
index 2db78d991ef0595a0d8806414a959479e289c0fc..50c6b668a8e915081007dfd9c9fc1aae179e16c9 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-04-24 15:46:45 +02:00
@@ -2024,8 +2024,16 @@ public class ServerPlayer extends Player {
2021-06-15 04:59:31 +02:00
}
public boolean setGameMode(GameType gameMode) {
- if (!this.gameMode.changeGameModeForPlayer(gameMode)) {
- return false;
2024-01-19 17:54:05 +01:00
+ // Paper start - Expand PlayerGameModeChangeEvent
2021-11-24 22:30:53 +01:00
+ org.bukkit.event.player.PlayerGameModeChangeEvent event = this.setGameMode(gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.UNKNOWN, null);
2021-06-15 04:59:31 +02:00
+ return event == null ? false : event.isCancelled();
+ }
2023-06-08 03:13:54 +02:00
+ @Nullable
+ public org.bukkit.event.player.PlayerGameModeChangeEvent setGameMode(GameType gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause cause, @Nullable net.kyori.adventure.text.Component message) {
2021-11-24 22:30:53 +01:00
+ org.bukkit.event.player.PlayerGameModeChangeEvent event = this.gameMode.changeGameModeForPlayer(gameMode, cause, message);
2021-06-15 04:59:31 +02:00
+ if (event == null || event.isCancelled()) {
+ return null;
2024-01-19 17:54:05 +01:00
+ // Paper end - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
} else {
this.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, (float) gameMode.getId()));
if (gameMode == GameType.SPECTATOR) {
2024-04-24 15:46:45 +02:00
@@ -2037,7 +2045,7 @@ public class ServerPlayer extends Player {
2021-06-15 04:59:31 +02:00
this.onUpdateAbilities();
this.updateEffectVisibility();
- return true;
2024-01-19 17:54:05 +01:00
+ return event; // Paper - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
}
}
2024-04-24 15:46:45 +02:00
@@ -2447,6 +2455,16 @@ public class ServerPlayer extends Player {
2021-06-15 04:59:31 +02:00
}
public void loadGameTypes(@Nullable CompoundTag nbt) {
2024-01-19 17:54:05 +01:00
+ // Paper start - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
+ if (this.server.getForcedGameType() != null && this.server.getForcedGameType() != ServerPlayer.readPlayerMode(nbt, "playerGameType")) {
2021-11-24 22:30:53 +01:00
+ if (new org.bukkit.event.player.PlayerGameModeChangeEvent(this.getBukkitEntity(), org.bukkit.GameMode.getByValue(this.server.getDefaultGameType().getId()), org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.DEFAULT_GAMEMODE, null).callEvent()) {
2021-06-15 04:59:31 +02:00
+ this.gameMode.setGameModeForPlayer(this.server.getForcedGameType(), GameType.DEFAULT_MODE);
+ } else {
+ this.gameMode.setGameModeForPlayer(ServerPlayer.readPlayerMode(nbt,"playerGameType"), ServerPlayer.readPlayerMode(nbt, "previousPlayerGameType"));
+ }
+ return;
+ }
2024-01-19 17:54:05 +01:00
+ // Paper end - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
this.gameMode.setGameModeForPlayer(this.calculateGameModeForNewPlayer(ServerPlayer.readPlayerMode(nbt, "playerGameType")), ServerPlayer.readPlayerMode(nbt, "previousPlayerGameType"));
}
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
2024-04-24 15:46:45 +02:00
index 486d9c4dbe3ee23198eba7f34ea8b5f4e9048c0b..f080322bc766361b9d2d8b1214c3dff1c3df9ae8 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
2024-04-24 15:46:45 +02:00
@@ -72,14 +72,21 @@ public class ServerPlayerGameMode {
2021-06-15 04:59:31 +02:00
}
public boolean changeGameModeForPlayer(GameType gameMode) {
2024-01-20 23:13:41 +01:00
+ // Paper start - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
+ PlayerGameModeChangeEvent event = this.changeGameModeForPlayer(gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.UNKNOWN, null);
2023-06-08 03:13:54 +02:00
+ return event != null && event.isCancelled();
2021-06-15 04:59:31 +02:00
+ }
2023-06-08 03:13:54 +02:00
+ @Nullable
+ public PlayerGameModeChangeEvent changeGameModeForPlayer(GameType gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause cause, @Nullable net.kyori.adventure.text.Component cancelMessage) {
2024-01-19 17:54:05 +01:00
+ // Paper end - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
if (gameMode == this.gameModeForPlayer) {
- return false;
2024-01-19 17:54:05 +01:00
+ return null; // Paper - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
} else {
// CraftBukkit start
2021-07-15 23:44:59 +02:00
- PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(this.player.getBukkitEntity(), GameMode.getByValue(gameMode.getId()));
+ PlayerGameModeChangeEvent event = new PlayerGameModeChangeEvent(this.player.getBukkitEntity(), GameMode.getByValue(gameMode.getId()), cause, cancelMessage); // Paper
2021-06-15 04:59:31 +02:00
this.level.getCraftServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
- return false;
2024-01-19 17:54:05 +01:00
+ return event; // Paper - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
}
// CraftBukkit end
2022-12-07 21:16:54 +01:00
this.setGameModeForPlayer(gameMode, this.previousGameModeForPlayer);
2024-04-24 15:46:45 +02:00
@@ -90,7 +97,7 @@ public class ServerPlayerGameMode {
this.player.resetCurrentImpulseContext();
}
2021-06-15 04:59:31 +02:00
- return true;
2024-01-19 17:54:05 +01:00
+ return event; // Paper - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
}
}
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-04-25 23:21:18 +02:00
index f84a6f1ec9d1081b05ab44dd99659918aa0f1874..806f16a183ff1abae4cc21409bf8d26e0803483d 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-04-25 23:21:18 +02:00
@@ -2719,7 +2719,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2021-06-15 04:59:31 +02:00
2023-04-10 10:03:36 +02:00
this.player = this.server.getPlayerList().respawn(this.player, false, RespawnReason.DEATH);
2021-06-15 04:59:31 +02:00
if (this.server.isHardcore()) {
- this.player.setGameMode(GameType.SPECTATOR);
2024-01-19 17:54:05 +01:00
+ this.player.setGameMode(GameType.SPECTATOR, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.HARDCORE_DEATH, null); // Paper - Expand PlayerGameModeChangeEvent
Updated Upstream (Bukkit/CraftBukkit) (#10242)
* Updated Upstream (Bukkit/CraftBukkit)
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:
a6a9d2a4 Remove some old ApiStatus.Experimental annotations
be72314c SPIGOT-7300, PR-829: Add new DamageSource API providing enhanced information about entity damage
b252cf05 SPIGOT-7576, PR-970: Add methods in MushroomCow to change stew effects
b1c689bd PR-902: Add Server#isLoggingIPs to get log-ips configuration
08f86d1c PR-971: Add Player methods for client-side potion effects
2e3024a9 PR-963: Add API for in-world structures
a23292a7 SPIGOT-7530, PR-948: Improve Resource Pack API with new 1.20.3 functionality
1851857b SPIGOT-3071, PR-969: Add entity spawn method with spawn reason
cde4c52a SPIGOT-5553, PR-964: Add EntityKnockbackEvent
CraftBukkit Changes:
38fd4bd50 Fix accidentally renamed internal damage method
80f0ce4be SPIGOT-7300, PR-1180: Add new DamageSource API providing enhanced information about entity damage
7e43f3b16 SPIGOT-7581: Fix typo in BlockMushroom
ea14b7d90 SPIGOT-7576, PR-1347: Add methods in MushroomCow to change stew effects
4c687f243 PR-1259: Add Server#isLoggingIPs to get log-ips configuration
22a541a29 Improve support for per-world game rules
cb7dccce2 PR-1348: Add Player methods for client-side potion effects
b8d6109f0 PR-1335: Add API for in-world structures
4398a1b5b SPIGOT-7577: Make CraftWindCharge#explode discard the entity
e74107678 Fix Crafter maximum stack size
0bb0f4f6a SPIGOT-7530, PR-1314: Improve Resource Pack API with new 1.20.3 functionality
4949f556d SPIGOT-3071, PR-1345: Add entity spawn method with spawn reason
20ac73ca2 PR-1353: Fix Structure#place not working as documented with 0 palette
3c1b77871 SPIGOT-6911, PR-1349: Change max book length in CraftMetaBook
333701839 SPIGOT-7572: Bee nests generated without bees
f48f4174c SPIGOT-5553, PR-1336: Add EntityKnockbackEvent
2024-02-11 22:28:00 +01:00
((GameRules.BooleanValue) this.player.level().getGameRules().getRule(GameRules.RULE_SPECTATORSGENERATECHUNKS)).set(false, this.player.serverLevel()); // CraftBukkit - per-world
2021-06-15 04:59:31 +02:00
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2024-04-26 03:51:28 +02:00
index 36aaf37154c01c7a8f8543149e6bedec1386f5e7..6100e2313a86f9baca136a178cc30d4adc5cae53 100644
2021-06-15 04:59:31 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2024-04-24 15:46:45 +02:00
@@ -1640,7 +1640,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2023-06-13 01:51:45 +02:00
Preconditions.checkArgument(mode != null, "GameMode cannot be null");
if (this.getHandle().connection == null) return;
2021-06-15 04:59:31 +02:00
- this.getHandle().setGameMode(GameType.byId(mode.getValue()));
2024-01-19 17:54:05 +01:00
+ this.getHandle().setGameMode(GameType.byId(mode.getValue()), org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.PLUGIN, null); // Paper - Expand PlayerGameModeChangeEvent
2021-06-15 04:59:31 +02:00
}
@Override