I’m new to command block programming and curious if it’s possible to code a mob to prevent it from leaving a predetermined area. As in, they can wander around the room they spawn in, but can’t leave through any of the doors. Thanks in advance.
Do you want the mob confined in a rectangular area or in an irregular room?
Just a normal rectangular room with 4 doors in each direction. I already have the mobs tagged using /scoreboard with position coordinates, so I can track them easily. The simplest solution is to teleport them to the center of their last room every time they change their room coordinates, but that would create a jarring effect that I'm trying to avoid. I experimented with rotating the mobs when they approach a door, but that doesn't really affect their trajectory.
***Update
I developed a system with barrier blocks that are replaced by air when a tagged entity approaches. There is still a chance mobs can get out if they time it right, and entities like arrows might not make it through. Additional it requires hundreds of armor stands. If anyone has a more efficient method please let me know!
Bumping, as I'm still looking for a more reliable method of controlling mob behavior/movement. Specifically, I discovered that prior creators exploited snowball ownerName mechanics to control how a mob targets other entities. I'm hoping to use this to force mobs to return to a target if they stray too far away.
However, I can no longer get the previously documented commands to work. I'm summoning a target mob with a custom UUID, then hitting an "attacking" mob with a snowball "owned" by the target mob. Nothing seems to happen. Code is below.
/summon minecraft:pig ~ ~ ~ {UUID:[I;0,0,0,1]}
/execute as @e[type=minecraft:skeleton,sort=nearest,limit=1] at @s run summon snowball ~ ~3 ~ {ownerName:"0-0-0-0-1"}
I confirmed that the target mob has a UUID of 1 with dataget, so that's working and it seems to be an issue with the second line of code. FYI I've also tried ownerNames of "1", "I;0,0,0,1", and "00000000-0000-0000-0000-000000000001". Ideally, if anyone knows how to get this to work using the direct data commands (ie copying a target UUID to the snowball owner UUID) that would be even better, as I will have hundreds of targets for my project.
Well, I was able to figure out a few things through trial and error:
First, the OwnerName tag no longer is functional. It's been replaced by just Owner. As nbt tag pathways using UUIDs now store data in arrays containing four 32-bit integers, the syntax is:
Secondly, you can indeed modify the tag directly using the data command. The syntax is:
data modify entity @e[type=minecraft:snowball,limit=1] Owner[3] set from entity @e[type=minecraft:pig,limit=1] UUID[3]
The fourth number in the array (Owner[3] or UUID[3]) essentially represents the UUIDLeast value (if <10^8). For most projects (including mine) you should have plenty of custom UUIDs just by modifying that single 32-bit number. CAUTION: Be sure not to duplicate UUIDs, or you'll glitch the game.
Third, by hitting an aggressive mob with one of these custom snowballs, you can force it to attack any entity, apparently, not just mobs. This can indeed be used to force a mob to return to a specified area around an armor stand if it strays too far. Some downsides: although I can make the snowball invisible (by using a custom blank model), the snowball gives the mob both damage and knockback animations. Not sure if I can get around that. I might try using zombie pigman as invisible riders on mobs and manipulate their HurtBy tag instead of using snowballs.
I’m new to command block programming and curious if it’s possible to code a mob to prevent it from leaving a predetermined area. As in, they can wander around the room they spawn in, but can’t leave through any of the doors. Thanks in advance.
Yes, you can add a tag called NoAi somewhere, you can do some googleing with thing fo
Do you want the mob confined in a rectangular area or in an irregular room?
Perseverance always pays. Sooner or later.
Unfortunately this prevents my monsters from attacking/moving around the room they spawn in. I just need them to stay in their room.
Just a normal rectangular room with 4 doors in each direction. I already have the mobs tagged using /scoreboard with position coordinates, so I can track them easily. The simplest solution is to teleport them to the center of their last room every time they change their room coordinates, but that would create a jarring effect that I'm trying to avoid. I experimented with rotating the mobs when they approach a door, but that doesn't really affect their trajectory.
***Update
I developed a system with barrier blocks that are replaced by air when a tagged entity approaches. There is still a chance mobs can get out if they time it right, and entities like arrows might not make it through. Additional it requires hundreds of armor stands. If anyone has a more efficient method please let me know!
Bumping, as I'm still looking for a more reliable method of controlling mob behavior/movement. Specifically, I discovered that prior creators exploited snowball ownerName mechanics to control how a mob targets other entities. I'm hoping to use this to force mobs to return to a target if they stray too far away.
However, I can no longer get the previously documented commands to work. I'm summoning a target mob with a custom UUID, then hitting an "attacking" mob with a snowball "owned" by the target mob. Nothing seems to happen. Code is below.
I confirmed that the target mob has a UUID of 1 with dataget, so that's working and it seems to be an issue with the second line of code. FYI I've also tried ownerNames of "1", "I;0,0,0,1", and "00000000-0000-0000-0000-000000000001". Ideally, if anyone knows how to get this to work using the direct data commands (ie copying a target UUID to the snowball owner UUID) that would be even better, as I will have hundreds of targets for my project.
Well, I was able to figure out a few things through trial and error:
First, the OwnerName tag no longer is functional. It's been replaced by just Owner. As nbt tag pathways using UUIDs now store data in arrays containing four 32-bit integers, the syntax is:
Secondly, you can indeed modify the tag directly using the data command. The syntax is:
The fourth number in the array (Owner[3] or UUID[3]) essentially represents the UUIDLeast value (if <10^8). For most projects (including mine) you should have plenty of custom UUIDs just by modifying that single 32-bit number. CAUTION: Be sure not to duplicate UUIDs, or you'll glitch the game.
Third, by hitting an aggressive mob with one of these custom snowballs, you can force it to attack any entity, apparently, not just mobs. This can indeed be used to force a mob to return to a specified area around an armor stand if it strays too far. Some downsides: although I can make the snowball invisible (by using a custom blank model), the snowball gives the mob both damage and knockback animations. Not sure if I can get around that. I might try using zombie pigman as invisible riders on mobs and manipulate their HurtBy tag instead of using snowballs.
Hope this helps someone else.