mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-26 12:36:07 +01:00
159 lines
10 KiB
Diff
159 lines
10 KiB
Diff
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] Expand PlayerGameModeChangeEvent
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java b/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
|
|
index 50e581613156cca765dfd8e3596dcb3b58520cec..21d6a2a5d1722d44146384c28a3cba2df9b42771 100644
|
|
--- a/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
|
|
+++ b/src/main/java/net/minecraft/server/commands/DefaultGameModeCommands.java
|
|
@@ -25,9 +25,13 @@ public class DefaultGameModeCommands {
|
|
GameType gameType = minecraftServer.getForcedGameType();
|
|
if (gameType != null) {
|
|
for(ServerPlayer serverPlayer : minecraftServer.getPlayerList().getPlayers()) {
|
|
- if (serverPlayer.setGameMode(gameType)) {
|
|
- ++i;
|
|
+ // Paper start - Expand 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()) {
|
|
+ source.sendSuccess(() -> io.papermc.paper.adventure.PaperAdventure.asVanilla(event.cancelMessage()), false);
|
|
}
|
|
+ // Paper end - Expand PlayerGameModeChangeEvent
|
|
+ ++i;
|
|
}
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/commands/GameModeCommand.java b/src/main/java/net/minecraft/server/commands/GameModeCommand.java
|
|
index aee8618e27b893b72931e925724dd683d2e6d2aa..f7c9127346261d83413ca03a1cdaa84975ae17d6 100644
|
|
--- a/src/main/java/net/minecraft/server/commands/GameModeCommand.java
|
|
+++ b/src/main/java/net/minecraft/server/commands/GameModeCommand.java
|
|
@@ -48,9 +48,14 @@ public class GameModeCommand {
|
|
int i = 0;
|
|
|
|
for(ServerPlayer serverPlayer : targets) {
|
|
- if (serverPlayer.setGameMode(gameMode)) {
|
|
+ // Paper start - Expand 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) {
|
|
+ context.getSource().sendSuccess(() -> io.papermc.paper.adventure.PaperAdventure.asVanilla(event.cancelMessage()), true);
|
|
+ // Paper end - Expand PlayerGameModeChangeEvent
|
|
}
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
index 8184aaff18d2372ae1de0fe1c53e63e1e6e3b2b0..68f6bb415d4b9624f147dc641543bce56bbbca09 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
|
|
@@ -1989,8 +1989,16 @@ public class ServerPlayer extends Player {
|
|
}
|
|
|
|
public boolean setGameMode(GameType gameMode) {
|
|
- if (!this.gameMode.changeGameModeForPlayer(gameMode)) {
|
|
- return false;
|
|
+ // Paper start - Expand PlayerGameModeChangeEvent
|
|
+ org.bukkit.event.player.PlayerGameModeChangeEvent event = this.setGameMode(gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.UNKNOWN, null);
|
|
+ return event == null ? false : event.isCancelled();
|
|
+ }
|
|
+ @Nullable
|
|
+ public org.bukkit.event.player.PlayerGameModeChangeEvent setGameMode(GameType gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause cause, @Nullable net.kyori.adventure.text.Component message) {
|
|
+ org.bukkit.event.player.PlayerGameModeChangeEvent event = this.gameMode.changeGameModeForPlayer(gameMode, cause, message);
|
|
+ if (event == null || event.isCancelled()) {
|
|
+ return null;
|
|
+ // Paper end - Expand PlayerGameModeChangeEvent
|
|
} else {
|
|
this.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, (float) gameMode.getId()));
|
|
if (gameMode == GameType.SPECTATOR) {
|
|
@@ -2002,7 +2010,7 @@ public class ServerPlayer extends Player {
|
|
|
|
this.onUpdateAbilities();
|
|
this.updateEffectVisibility();
|
|
- return true;
|
|
+ return event; // Paper - Expand PlayerGameModeChangeEvent
|
|
}
|
|
}
|
|
|
|
@@ -2414,6 +2422,16 @@ public class ServerPlayer extends Player {
|
|
}
|
|
|
|
public void loadGameTypes(@Nullable CompoundTag nbt) {
|
|
+ // Paper start - Expand PlayerGameModeChangeEvent
|
|
+ if (this.server.getForcedGameType() != null && this.server.getForcedGameType() != ServerPlayer.readPlayerMode(nbt, "playerGameType")) {
|
|
+ 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()) {
|
|
+ this.gameMode.setGameModeForPlayer(this.server.getForcedGameType(), GameType.DEFAULT_MODE);
|
|
+ } else {
|
|
+ this.gameMode.setGameModeForPlayer(ServerPlayer.readPlayerMode(nbt,"playerGameType"), ServerPlayer.readPlayerMode(nbt, "previousPlayerGameType"));
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
+ // Paper end - Expand PlayerGameModeChangeEvent
|
|
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
|
|
index 49d29e4b3395d2ebea1ed13338f26c7cfbaa5f72..d04669c1d2434c1dbc617777b769e9004359f208 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
|
|
@@ -73,21 +73,28 @@ public class ServerPlayerGameMode {
|
|
}
|
|
|
|
public boolean changeGameModeForPlayer(GameType gameMode) {
|
|
+ // Paper start - Expand PlayerGameModeChangeEvent
|
|
+ PlayerGameModeChangeEvent event = this.changeGameModeForPlayer(gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.UNKNOWN, null);
|
|
+ return event != null && event.isCancelled();
|
|
+ }
|
|
+ @Nullable
|
|
+ public PlayerGameModeChangeEvent changeGameModeForPlayer(GameType gameMode, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause cause, @Nullable net.kyori.adventure.text.Component cancelMessage) {
|
|
+ // Paper end - Expand PlayerGameModeChangeEvent
|
|
if (gameMode == this.gameModeForPlayer) {
|
|
- return false;
|
|
+ return null; // Paper - Expand PlayerGameModeChangeEvent
|
|
} else {
|
|
// CraftBukkit start
|
|
- 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
|
|
this.level.getCraftServer().getPluginManager().callEvent(event);
|
|
if (event.isCancelled()) {
|
|
- return false;
|
|
+ return event; // Paper - Expand PlayerGameModeChangeEvent
|
|
}
|
|
// CraftBukkit end
|
|
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();
|
|
- return true;
|
|
+ return event; // Paper - Expand PlayerGameModeChangeEvent
|
|
}
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
index 5c54f56a350def1c4c7180c05e853fe10d687f74..f1a684e8af0b05c68ccb16cdb9c0e236d1a5834a 100644
|
|
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
@@ -2648,7 +2648,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
|
|
|
|
this.player = this.server.getPlayerList().respawn(this.player, false, RespawnReason.DEATH);
|
|
if (this.server.isHardcore()) {
|
|
- this.player.setGameMode(GameType.SPECTATOR);
|
|
+ this.player.setGameMode(GameType.SPECTATOR, org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.HARDCORE_DEATH, null); // Paper - Expand PlayerGameModeChangeEvent
|
|
((GameRules.BooleanValue) this.player.level().getGameRules().getRule(GameRules.RULE_SPECTATORSGENERATECHUNKS)).set(false, this.server);
|
|
}
|
|
}
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
index 298380e217f41f01d0818767a6d8f1da5a004037..b602c6dfece70580971baedd638e465e8e25bd02 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
|
@@ -1559,7 +1559,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
|
Preconditions.checkArgument(mode != null, "GameMode cannot be null");
|
|
if (this.getHandle().connection == null) return;
|
|
|
|
- this.getHandle().setGameMode(GameType.byId(mode.getValue()));
|
|
+ this.getHandle().setGameMode(GameType.byId(mode.getValue()), org.bukkit.event.player.PlayerGameModeChangeEvent.Cause.PLUGIN, null); // Paper - Expand PlayerGameModeChangeEvent
|
|
}
|
|
|
|
@Override
|