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
Subject: [PATCH] additions to PlayerGameModeChangeEvent
diff --git a/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java b/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
2023-09-22 19:59:56 +02:00
index 50e581613156cca765dfd8e3596dcb3b58520cec..12d4c141a1bc72e53e18f73d7ee4d5a2467e08e1 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
2022-12-07 21:16:54 +01:00
@@ -25,9 +25,13 @@ public class DefaultGameModeCommands {
2021-06-15 04:59:31 +02:00
GameType gameType = minecraftServer.getForcedGameType();
if (gameType != null) {
for(ServerPlayer serverPlayer : minecraftServer.getPlayerList().getPlayers()) {
- if (serverPlayer.setGameMode(gameType)) {
- ++i;
+ // Paper start - extend PlayerGameModeChangeEvent
+ 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
}
+ // Paper end
+ ++i;
}
}
diff --git a/src/main/java/net/minecraft/server/commands/GameModeCommand.java b/src/main/java/net/minecraft/server/commands/GameModeCommand.java
2023-09-22 19:59:56 +02:00
index aee8618e27b893b72931e925724dd683d2e6d2aa..5cb15e2209d7b315904a1fc6d650ce1e75584271 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
2023-06-08 03:13:54 +02:00
@@ -48,9 +48,14 @@ public class GameModeCommand {
2021-06-15 04:59:31 +02:00
int i = 0;
for(ServerPlayer serverPlayer : targets) {
- if (serverPlayer.setGameMode(gameMode)) {
+ // Paper start - extend PlayerGameModeChangeEvent
+ 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);
++i;
+ } 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);
2021-06-15 04:59:31 +02:00
+ // Paper end
}
}
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2023-09-23 01:42:59 +02:00
index cb2f86e0ad0edc54711826215df4406c5725f29c..0abc9dfd73b3cb16b29bdbcc40f0d0ea1c022f34 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
2023-09-22 22:13:57 +02:00
@@ -1888,8 +1888,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;
+ // Paper start - Add cause and nullable message to event
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()) {
+ // Paper end
+ return null;
} else {
this.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, (float) gameMode.getId()));
if (gameMode == GameType.SPECTATOR) {
2023-09-22 22:13:57 +02:00
@@ -1901,7 +1909,7 @@ public class ServerPlayer extends Player {
2021-06-15 04:59:31 +02:00
this.onUpdateAbilities();
this.updateEffectVisibility();
- return true;
+ return event; // Paper
}
}
2023-09-23 01:42:59 +02:00
@@ -2305,6 +2313,16 @@ public class ServerPlayer extends Player {
2021-06-15 04:59:31 +02:00
}
public void loadGameTypes(@Nullable CompoundTag nbt) {
2021-08-22 17:10:33 +02:00
+ // Paper start
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;
+ }
2021-08-22 17:10:33 +02:00
+ // Paper end
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
2023-09-22 07:41:27 +02:00
index 89f0c3d6e4dc9701f02fa7ed5331c3969de81ffc..0a15cda0919c0adf5009a023c17b3e25cc365623 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
2023-06-08 03:13:54 +02:00
@@ -73,21 +73,28 @@ public class ServerPlayerGameMode {
2021-06-15 04:59:31 +02:00
}
public boolean changeGameModeForPlayer(GameType gameMode) {
+ // Paper end
+ 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) {
2021-06-15 04:59:31 +02:00
+ // Paper end
if (gameMode == this.gameModeForPlayer) {
- return false;
+ return null; // Paper
} 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;
+ return event; // Paper
}
// CraftBukkit end
2022-12-07 21:16:54 +01:00
this.setGameModeForPlayer(gameMode, this.previousGameModeForPlayer);
this.player.onUpdateAbilities();
this.player.server.getPlayerList().broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE, this.player), this.player); // CraftBukkit
this.level.updateSleepingPlayerList();
2021-06-15 04:59:31 +02:00
- return true;
+ return event; // Paper
}
}
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2023-09-23 01:42:59 +02:00
index d8dcb3c7f81b946a866c43f808b2588a849e2d83..d8a616fdf70179816b17d46ddeedeaadf353f065 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
2023-09-22 22:13:57 +02:00
@@ -2637,7 +2637,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);
+ this.player.setGameMode(GameType.SPECTATOR, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.HARDCORE_DEATH, null); // Paper
2023-06-08 03:13:54 +02:00
((GameRules.BooleanValue) this.player.level().getGameRules().getRule(GameRules.RULE_SPECTATORSGENERATECHUNKS)).set(false, this.server);
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
2023-09-22 22:13:57 +02:00
index ad04638955bebdf1392f9dc58be1de699858f5b3..18a446978fdf956e935e6a6c66413b7f528ea671 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
2023-09-22 22:13:57 +02:00
@@ -1564,7 +1564,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()));
+ this.getHandle().setGameMode(GameType.byId(mode.getValue()), org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.PLUGIN, null); // Paper
}
@Override