The Bug:
You can ignite TNT in adventure mode.
This behavior is inconsistent as the player cannot ignite candles or campfires in adventure mode, so one would expect the same behavior for TNT.
Steps to Reproduce:
Obtain some flint and steel, place down some TNT, and switch into adventure mode.
Attempt to ignite the TNT using the flint and steel.
Take note as to whether or not you can ignite TNT in adventure mode.
Observed Behavior:
TNT can be ignited.
Expected Behavior:
TNT would not be able to be ignited.
Code Analysis:
Code analysis by @unknown can be found below.
The following is based on a decompiled version of Minecraft 1.19.2 using MCP-Reborn.
net.minecraft.world.level.block.TntBlock.java
public class TntBlock extends Block {
...
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) {
ItemStack itemstack = player.getItemInHand(interactionHand);
if (!itemstack.is(Items.FLINT_AND_STEEL) && !itemstack.is(Items.FIRE_CHARGE)) {
return super.use(blockState, level, blockPos, player, interactionHand, blockHitResult);
} else {
explode(level, blockPos, player);
level.setBlock(blockPos, Blocks.AIR.defaultBlockState(), 11);
Item item = itemstack.getItem();
if (!player.isCreative()) {
if (itemstack.is(Items.FLINT_AND_STEEL)) {
itemstack.hurtAndBreak(1, player, (player1) -> {
player1.broadcastBreakEvent(interactionHand);
});
} else {
itemstack.shrink(1);
}
}
player.awardStat(Stats.ITEM_USED.get(item));
return InteractionResult.sidedSuccess(level.isClientSide);
}
}
...
If we look at the above class, we can see that there is only one necessary check that's carried out before allowing TNT to be ignited. This check is to quite simply see if the player is holding either flint and steel or a fire charge at the time of the interaction. If they are, the TNT will be ignited. The game doesn't check to see what abilities the player possesses (what game mode they are in) before allowing them to ignite TNT, therefore resulting in this problem occurring.
Fix:
Simply altering the appropriate existing "if" statement by adding an "if else" statement to it to check what abilities the player possesses before allowing them to ignite TNT will resolve this problem.
Current "if" statement:
if (!itemstack.is(Items.FLINT_AND_STEEL) && !itemstack.is(Items.FIRE_CHARGE)) {
...
} else {
...
Fixed "if" statement:
if (!itemstack.is(Items.FLINT_AND_STEEL) && !itemstack.is(Items.FIRE_CHARGE)) {
...
} else if (player.getAbilities().mayBuild) {
...
}
return InteractionResult.PASS;
...
Linked issues
Attachments
Comments 5
Following on from my code analysis, I've double-checked my proposed fix and I can confidently confirm that it's fully functioning and works as expected, so I've attached two screenshots to this report, one of which shows the current code and the other that shows the fixed code. I feel this information may be quite insightful hence my reasoning for providing it. 🙂
[media][media]
Can confirm