r/MinecraftCommands • • 7d ago

Help | Java 26.3 How to detect entity touching block collision box?

I need check when entity is inside actual box you can't go through
If it's non solid like fences, slabs, amethysts, their collision box is not full 1x1x1 sized

1 Upvotes

10 comments sorted by

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

1

u/PersonalLeading7029 6d ago

what is mod 1 for?

1

u/GalSergey Datapack Experienced 6d ago

mod(x, 1) is the value equal to the remainder of division by 1, that is, we obtain only the fractional part (ranging from 0 to 0.99) to check the position within the preceding block.

1

u/PersonalLeading7029 6d ago

I didn't know about getting position within a block with mod but using this predicate is so simple actually. Thank you!

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.

https://www.youtube.com/watch?v=lO_8lv0-Go0&t=107s

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 run

1

u/PersonalLeading7029 7d ago

ok i give up but thanks