9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 19:39:11 +00:00

fix(bukkit): 修复冒险模式下使用不正确的可破坏方块谓词的物品破坏方块导致刷物品的问题

This commit is contained in:
jhqwqmc
2025-03-25 23:33:14 +08:00
parent 1c8a2ef6f5
commit 3670700003
2 changed files with 48 additions and 1 deletions

View File

@@ -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()) {

View File

@@ -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
);
}