From c056e2319d9656a681c19ae411f0c426fcf2fc72 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 21 Sep 2024 22:01:52 +0200 Subject: [PATCH] Fix PlayerCommandPreprocessEvent on signed commands --- .../ServerGamePacketListenerImpl.java.patch | 126 ++++++++++-------- 1 file changed, 68 insertions(+), 58 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch b/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch index 1073050489..fe96cfa36c 100644 --- a/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch +++ b/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch @@ -1184,10 +1184,13 @@ return; default: throw new IllegalArgumentException("Invalid player action"); -@@ -1218,9 +1885,31 @@ - } - } +@@ -1215,12 +1882,34 @@ + Item item = stack.getItem(); + return (item instanceof BlockItem || item instanceof BucketItem) && !player.getCooldowns().isOnCooldown(stack); ++ } ++ } ++ + // Spigot start - limit place/interactions + private int limitedPackets; + private long lastLimitedPacket = -1; @@ -1202,12 +1205,12 @@ + this.lastLimitedPacket = timestamp; + this.limitedPackets = 0; + return true; -+ } + } + + return true; -+ } + } + // Spigot end -+ + @Override public void handleUseItemOn(ServerboundUseItemOnPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1466,7 +1469,7 @@ ParseResults parseresults = this.parseCommand(command); if (this.server.enforceSecureProfile() && SignableCommand.hasSignableArguments(parseresults)) { -@@ -1431,19 +2235,39 @@ +@@ -1431,30 +2235,58 @@ if (!optional.isEmpty()) { this.tryHandleChat(packet.command(), () -> { @@ -1491,27 +1494,35 @@ + ServerGamePacketListenerImpl.LOGGER.info(this.player.getScoreboardName() + " issued server command: " + command); + } // Paper - Add missing SpigotConfig logCommands check +- Map map; + PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(this.getCraftPlayer(), command, new LazyPlayerSet(this.server)); + this.cserver.getPluginManager().callEvent(event); -+ -+ if (event.isCancelled()) { -+ return; -+ } + command = event.getMessage().substring(1); -+ -+ ParseResults parseresults = this.parseCommand(command); -+ // CraftBukkit end -+ - Map map; ++ // Paper start - Fix cancellation and message changing ++ ParseResults parseresults = this.parseCommand(packet.command()); ++ ++ Map map; try { -- map = this.collectSignedArguments(packet, SignableCommand.of(parseresults), lastSeenMessages); -+ map = (packet.command().equals(command)) ? this.collectSignedArguments(packet, SignableCommand.of(parseresults), lastSeenMessages) : Collections.emptyMap(); // CraftBukkit ++ // Always parse the original command to add to the chat chain + map = this.collectSignedArguments(packet, SignableCommand.of(parseresults), lastSeenMessages); } catch (SignedMessageChain.DecodeException signedmessagechain_a) { this.handleMessageDecodeFailure(signedmessagechain_a); return; -@@ -1451,10 +2275,10 @@ + } ++ if (event.isCancelled()) { ++ // Only now are we actually good to return ++ return; ++ } ++ ++ // Remove signed parts if the command was changed ++ if (!command.equals(packet.command())) { ++ parseresults = this.parseCommand(command); ++ map = Collections.emptyMap(); ++ } ++ // Paper end - Fix cancellation and message changing ++ CommandSigningContext.SignedArguments commandsigningcontext_a = new CommandSigningContext.SignedArguments(map); - parseresults = Commands.mapSource(parseresults, (commandlistenerwrapper) -> { @@ -1523,7 +1534,7 @@ } private void handleMessageDecodeFailure(SignedMessageChain.DecodeException exception) { -@@ -1530,14 +2354,20 @@ +@@ -1530,14 +2362,20 @@ return com_mojang_brigadier_commanddispatcher.parse(command, this.player.createCommandSourceStack()); } @@ -1549,7 +1560,7 @@ } } -@@ -1549,7 +2379,7 @@ +@@ -1549,7 +2387,7 @@ if (optional.isEmpty()) { ServerGamePacketListenerImpl.LOGGER.warn("Failed to validate message acknowledgements from {}", this.player.getName().getString()); @@ -1558,12 +1569,10 @@ } return optional; -@@ -1564,8 +2394,119 @@ - } - +@@ -1566,6 +2404,117 @@ return false; -+ } -+ + } + + // CraftBukkit start - add method + public void chat(String s, PlayerChatMessage original, boolean async) { + if (s.isEmpty() || this.player.getChatVisibility() == ChatVisiblity.HIDDEN) { @@ -1656,8 +1665,8 @@ + this.server.console.sendMessage(s); + } + } - } - ++ } ++ + @Deprecated // Paper + public void handleCommand(String s) { // Paper - private -> public + // Paper start - Remove all this old duplicated logic @@ -1678,7 +1687,7 @@ private PlayerChatMessage getSignedMessage(ServerboundChatPacket packet, LastSeenMessages lastSeenMessages) throws SignedMessageChain.DecodeException { SignedMessageBody signedmessagebody = new SignedMessageBody(packet.message(), packet.timeStamp(), packet.salt(), lastSeenMessages); -@@ -1573,15 +2514,44 @@ +@@ -1573,15 +2522,44 @@ } private void broadcastChatMessage(PlayerChatMessage message) { @@ -1729,7 +1738,7 @@ } -@@ -1592,7 +2562,7 @@ +@@ -1592,7 +2570,7 @@ synchronized (this.lastSeenMessages) { if (!this.lastSeenMessages.applyOffset(packet.offset())) { ServerGamePacketListenerImpl.LOGGER.warn("Failed to validate message acknowledgements from {}", this.player.getName().getString()); @@ -1738,7 +1747,7 @@ } } -@@ -1601,7 +2571,40 @@ +@@ -1601,7 +2579,40 @@ @Override public void handleAnimate(ServerboundSwingPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1779,7 +1788,7 @@ this.player.swing(packet.getHand()); } -@@ -1609,6 +2612,29 @@ +@@ -1609,6 +2620,29 @@ public void handlePlayerCommand(ServerboundPlayerCommandPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); if (this.player.hasClientLoaded()) { @@ -1809,7 +1818,7 @@ this.player.resetLastActionTime(); Entity entity; PlayerRideableJumping ijumpable; -@@ -1616,6 +2642,11 @@ +@@ -1616,6 +2650,11 @@ switch (packet.getAction()) { case PRESS_SHIFT_KEY: this.player.setShiftKeyDown(true); @@ -1821,7 +1830,7 @@ break; case RELEASE_SHIFT_KEY: this.player.setShiftKeyDown(false); -@@ -1684,15 +2715,25 @@ +@@ -1684,15 +2723,25 @@ } if (i > 4096) { @@ -1848,7 +1857,7 @@ } public void sendDisguisedChatMessage(Component message, ChatType.Bound params) { -@@ -1703,6 +2744,18 @@ +@@ -1703,6 +2752,18 @@ return this.connection.getRemoteAddress(); } @@ -1867,7 +1876,7 @@ public void switchToConfig() { this.waitingForSwitchToConfig = true; this.removePlayerFromWorld(); -@@ -1718,9 +2771,17 @@ +@@ -1718,9 +2779,17 @@ @Override public void handleInteract(ServerboundInteractPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1885,7 +1894,7 @@ this.player.resetLastActionTime(); this.player.setShiftKeyDown(packet.isUsingSecondaryAction()); -@@ -1731,22 +2792,61 @@ +@@ -1731,22 +2800,61 @@ AABB axisalignedbb = entity.getBoundingBox(); @@ -1952,7 +1961,7 @@ } } -@@ -1755,19 +2855,20 @@ +@@ -1755,19 +2863,20 @@ @Override public void onInteraction(InteractionHand hand) { @@ -1976,7 +1985,7 @@ label23: { if (entity instanceof AbstractArrow) { -@@ -1785,17 +2886,41 @@ +@@ -1785,17 +2894,41 @@ } ServerGamePacketListenerImpl.this.player.attack(entity); @@ -2019,7 +2028,7 @@ } } -@@ -1809,7 +2934,7 @@ +@@ -1809,7 +2942,7 @@ case PERFORM_RESPAWN: if (this.player.wonGame) { this.player.wonGame = false; @@ -2028,7 +2037,7 @@ this.resetPosition(); CriteriaTriggers.CHANGED_DIMENSION.trigger(this.player, Level.END, Level.OVERWORLD); } else { -@@ -1817,11 +2942,11 @@ +@@ -1817,11 +2950,11 @@ return; } @@ -2043,7 +2052,7 @@ } } break; -@@ -1833,16 +2958,27 @@ +@@ -1833,16 +2966,27 @@ @Override public void handleContainerClose(ServerboundContainerClosePacket packet) { @@ -2073,7 +2082,7 @@ this.player.containerMenu.sendAllDataToRemote(); } else if (!this.player.containerMenu.stillValid(this.player)) { ServerGamePacketListenerImpl.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, this.player.containerMenu); -@@ -1855,7 +2991,315 @@ +@@ -1855,7 +2999,315 @@ boolean flag = packet.getStateId() != this.player.containerMenu.getStateId(); this.player.containerMenu.suppressRemoteUpdates(); @@ -2390,7 +2399,7 @@ ObjectIterator objectiterator = Int2ObjectMaps.fastIterable(packet.getChangedSlots()).iterator(); while (objectiterator.hasNext()) { -@@ -1879,6 +3323,14 @@ +@@ -1879,6 +3331,14 @@ @Override public void handlePlaceRecipe(ServerboundPlaceRecipePacket packet) { @@ -2405,7 +2414,7 @@ PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); this.player.resetLastActionTime(); if (!this.player.isSpectator() && this.player.containerMenu.containerId == packet.containerId()) { -@@ -1900,8 +3352,42 @@ +@@ -1900,9 +3360,43 @@ ServerGamePacketListenerImpl.LOGGER.debug("Player {} tried to place impossible recipe {}", this.player, recipeholder.id().location()); return; } @@ -2438,7 +2447,7 @@ + return; + } + // Paper end - Add PlayerRecipeBookClickEvent - forward to legacy event -+ + + // Cast to keyed should be safe as the recipe will never be a MerchantRecipe. + recipeholder = this.server.getRecipeManager().byKey(net.minecraft.resources.ResourceKey.create(net.minecraft.core.registries.Registries.RECIPE, org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(recipeName))).orElse(null); // Paper - Add PlayerRecipeBookClickEvent - forward to legacy event + if (recipeholder == null) { @@ -2446,10 +2455,11 @@ + } + RecipeBookMenu.PostPlaceAction containerrecipebook_a = containerrecipebook.handlePlacement(makeAll, this.player.isCreative(), recipeholder, this.player.serverLevel(), this.player.getInventory()); + // CraftBukkit end - ++ if (containerrecipebook_a == RecipeBookMenu.PostPlaceAction.PLACE_GHOST_RECIPE) { this.player.connection.send(new ClientboundPlaceGhostRecipePacket(this.player.containerMenu.containerId, craftingmanager_d.display().display())); -@@ -1917,6 +3403,7 @@ + } +@@ -1917,6 +3411,7 @@ @Override public void handleContainerButtonClick(ServerboundContainerButtonClickPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -2457,7 +2467,7 @@ this.player.resetLastActionTime(); if (this.player.containerMenu.containerId == packet.containerId() && !this.player.isSpectator()) { if (!this.player.containerMenu.stillValid(this.player)) { -@@ -1945,7 +3432,44 @@ +@@ -1945,7 +3440,44 @@ boolean flag1 = packet.slotNum() >= 1 && packet.slotNum() <= 45; boolean flag2 = itemstack.isEmpty() || itemstack.getCount() <= itemstack.getMaxStackSize(); @@ -2502,7 +2512,7 @@ if (flag1 && flag2) { this.player.inventoryMenu.getSlot(packet.slotNum()).setByPlayer(itemstack); this.player.inventoryMenu.setRemoteSlot(packet.slotNum(), itemstack); -@@ -1964,7 +3488,19 @@ +@@ -1964,7 +3496,19 @@ @Override public void handleSignUpdate(ServerboundSignUpdatePacket packet) { @@ -2523,7 +2533,7 @@ this.filterTextPacket(list).thenAcceptAsync((list1) -> { this.updateSignText(packet, list1); -@@ -1972,6 +3508,7 @@ +@@ -1972,6 +3516,7 @@ } private void updateSignText(ServerboundSignUpdatePacket packet, List signText) { @@ -2531,7 +2541,7 @@ this.player.resetLastActionTime(); ServerLevel worldserver = this.player.serverLevel(); BlockPos blockposition = packet.getPos(); -@@ -1993,15 +3530,33 @@ +@@ -1993,15 +3538,33 @@ @Override public void handlePlayerAbilities(ServerboundPlayerAbilitiesPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -2566,7 +2576,7 @@ if (this.player.isModelPartShown(PlayerModelPart.HAT) != flag) { this.server.getPlayerList().broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_HAT, this.player)); } -@@ -2012,7 +3567,7 @@ +@@ -2012,7 +3575,7 @@ public void handleChangeDifficulty(ServerboundChangeDifficultyPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); if (this.player.hasPermissions(2) || this.isSingleplayerOwner()) { @@ -2575,7 +2585,7 @@ } } -@@ -2033,7 +3588,7 @@ +@@ -2033,7 +3596,7 @@ if (!Objects.equals(profilepublickey_a, profilepublickey_a1)) { if (profilepublickey_a != null && profilepublickey_a1.expiresAt().isBefore(profilepublickey_a.expiresAt())) { @@ -2584,7 +2594,7 @@ } else { try { SignatureValidator signaturevalidator = this.server.getProfileKeySignatureValidator(); -@@ -2045,8 +3600,8 @@ +@@ -2045,8 +3608,8 @@ this.resetPlayerChatState(remotechatsession_a.validate(this.player.getGameProfile(), signaturevalidator)); } catch (ProfilePublicKey.ValidationException profilepublickey_b) { @@ -2595,7 +2605,7 @@ } } -@@ -2058,7 +3613,7 @@ +@@ -2058,7 +3621,7 @@ if (!this.waitingForSwitchToConfig) { throw new IllegalStateException("Client acknowledged config, but none was requested"); } else { @@ -2604,7 +2614,7 @@ } } -@@ -2076,15 +3631,18 @@ +@@ -2076,15 +3639,18 @@ private void resetPlayerChatState(RemoteChatSession session) { this.chatSession = session; @@ -2626,7 +2636,7 @@ @Override public void handleClientTickEnd(ServerboundClientTickEndPacket packet) { -@@ -2115,4 +3673,17 @@ +@@ -2115,4 +3681,17 @@ InteractionResult run(ServerPlayer player, Entity entity, InteractionHand hand); }