From 3670700003a1440ae8d79e38a8401b4c03675565 Mon Sep 17 00:00:00 2001 From: jhqwqmc <2110242767@qq.com> Date: Tue, 25 Mar 2025 23:33:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(bukkit):=20=E4=BF=AE=E5=A4=8D=E5=86=92?= =?UTF-8?q?=E9=99=A9=E6=A8=A1=E5=BC=8F=E4=B8=8B=E4=BD=BF=E7=94=A8=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=9A=84=E5=8F=AF=E7=A0=B4=E5=9D=8F=E6=96=B9?= =?UTF-8?q?=E5=9D=97=E8=B0=93=E8=AF=8D=E7=9A=84=E7=89=A9=E5=93=81=E7=A0=B4?= =?UTF-8?q?=E5=9D=8F=E6=96=B9=E5=9D=97=E5=AF=BC=E8=87=B4=E5=88=B7=E7=89=A9?= =?UTF-8?q?=E5=93=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/network/PacketConsumers.java | 23 +++++++++++++++- .../craftengine/bukkit/util/Reflections.java | 26 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java index 8e315fc3a..d64710e47 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java @@ -286,7 +286,6 @@ public class PacketConsumers { }; private static void handlePlayerActionPacketOnMainThread(BukkitServerPlayer player, World world, BlockPos pos, Object packet) throws Exception { - Object action = Reflections.field$ServerboundPlayerActionPacket$action.get(packet); if (action == Reflections.instance$ServerboundPlayerActionPacket$Action$START_DESTROY_BLOCK) { Object serverLevel = Reflections.field$CraftWorld$ServerLevel.get(world); @@ -306,6 +305,28 @@ public class PacketConsumers { } return; } + if (player.isAdventureMode()) { + Object itemStack = Reflections.method$CraftItemStack$asNMSCopy.invoke(null, player.platformPlayer().getInventory().getItemInMainHand()); + Object blockPos = Reflections.constructor$BlockPos.newInstance(pos.x(), pos.y(), pos.z()); + Object blockInWorld = Reflections.constructor$BlockInWorld.newInstance(serverLevel, blockPos, false); + if (VersionHelper.isVersionNewerThan1_20_5()) { + if (Reflections.method$ItemStack$canBreakBlockInAdventureMode != null + && !(boolean) Reflections.method$ItemStack$canBreakBlockInAdventureMode.invoke( + itemStack, blockInWorld + )) { + player.stopMiningBlock(); + return; + } + } else { + if (Reflections.method$ItemStack$canDestroy != null + && !(boolean) Reflections.method$ItemStack$canDestroy.invoke( + itemStack, Reflections.instance$BuiltInRegistries$BLOCK, blockInWorld + )) { + player.stopMiningBlock(); + return; + } + } + } player.startMiningBlock(world, pos, blockState, true, BukkitBlockManager.instance().getImmutableBlockStateUnsafe(stateId)); } else if (action == Reflections.instance$ServerboundPlayerActionPacket$Action$ABORT_DESTROY_BLOCK) { if (player.isMiningBlock()) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java index 3083a651d..fa78c1e62 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/Reflections.java @@ -5502,4 +5502,30 @@ public class Reflections { clazz$ClientboundLevelChunkWithLightPacket, clazz$LevelChunk, clazz$LevelLightEngine, BitSet.class, BitSet.class ) ); + + public static final Class clazz$BlockInWorld = requireNonNull( + ReflectionUtils.getClazz( + BukkitReflectionUtils.assembleMCClass("world.level.block.state.pattern.BlockInWorld"), + BukkitReflectionUtils.assembleMCClass("world.level.block.state.pattern.ShapeDetectorBlock") + ) + ); + + public static final Constructor constructor$BlockInWorld = requireNonNull( + ReflectionUtils.getConstructor( + clazz$BlockInWorld, 0 + ) + ); + + // 1.20.5+ + public static final Method method$ItemStack$canBreakBlockInAdventureMode = + ReflectionUtils.getMethod( + clazz$ItemStack, new String[]{"canBreakBlockInAdventureMode"}, clazz$BlockInWorld + ); + + // 1.20 ~ 1.20.4 + // instance$BuiltInRegistries$BLOCK + public static final Method method$ItemStack$canDestroy = + ReflectionUtils.getMethod( + clazz$ItemStack,new String[]{"b"}, clazz$Registry, clazz$BlockInWorld + ); }