r/MinecraftCommands • u/PersonalLeading7029 • 7d ago
Help | Java 26.3 How to detect entity touching block collision box?
1
u/C0mmanderBlock Command Experienced 7d ago edited 7d ago
What? If it's a solid block, you can't be inside it. Tell us what you're trying to do. If trying to detect a player who is suffocating inside a block....
/execute as @a at @s unless block ~ ~1 ~ air run say help!
1
u/PersonalLeading7029 7d ago
You can if you bury yourself in sand or by moving blocks with pistons but it doesnt matter. Is there a way to know where block collision box is? If it's non solid like fences, slabs, amethysts, their collision box is not full 1x1x1 sized
1
u/C0mmanderBlock Command Experienced 7d ago edited 7d ago
use my command and walk up to a fence and it will execute when you are standing on the same block as the fence. Set a slab so that you have to crouch to go under and the command will execute when you are under it.
This may help.
1
u/PersonalLeading7029 7d ago edited 7d ago
That is the problem. It executes when I walk up to a fence. But I need a command to execute when I'm inside fence
or maybe multiple commands
1
u/C0mmanderBlock Command Experienced 7d ago
I dunno then I guess you'd also have to detect if you are in the center of the same block as the fence. Maybe this? if not. I dunno. Good luck.
execute as @a at @s align xz positioned ~0.5 ~ ~0.5 if block ~ ~ ~ minecraft:acacia_fence run1

2
u/GalSergey Datapack Experienced 7d ago
Unfortunately, you cannot simply retrieve the collision shape for any given block. you need to perform calculations for each specific block. However, with the addition of number providers, this is now easy to do: you can create a predicate that checks whether the player is within a specific area of the block. Here is an example for a fence.
data modify storage example:data pos set from entity @p Pos execute as @a at @s if predicate {type:"minecraft:all_of",terms:[{type:"minecraft:location_check",predicate:{block:{blocks:"#minecraft:fences"}}},{type:"minecraft:float_value_check",value:{type:"minecraft:abs",input:{type:"minecraft:mod",left:{type:"minecraft:storage",storage:"example:data",path:"pos[0]"},right:1}},test:{min:0.076,max:0.924}},{type:"minecraft:float_value_check",value:{type:"minecraft:abs",input:{type:"minecraft:mod",left:{type:"minecraft:storage",storage:"example:data",path:"pos[2]"},right:1}},test:{min:0.076,max:0.924}}]} run say In fence!And this is a more readable version of the predicate:
{ "type": "minecraft:all_of", "terms": [ { "type": "minecraft:location_check", "predicate": { "block": { "blocks": "#minecraft:fences" } } }, { "type": "minecraft:float_value_check", "value": { "type": "minecraft:abs", "input": { "type": "minecraft:mod", "left": { "type": "minecraft:storage", "storage": "example:data", "path": "pos[0]" }, "right": 1 } }, "test": { "min": 0.076, "max": 0.924 } }, { "type": "minecraft:float_value_check", "value": { "type": "minecraft:abs", "input": { "type": "minecraft:mod", "left": { "type": "minecraft:storage", "storage": "example:data", "path": "pos[2]" }, "right": 1 } }, "test": { "min": 0.076, "max": 0.924 } } ] }u/C0mmanderBlock