How to make a Mule unrideable yet still have access to the chest

I'm working on a custom crafting system and I'm trying to use donkeys/mules as a way for players to put in crafting items. The mule's chest is used for inserting items and having an item output. This is how I spawn one such mule:

/summon minecraft:mule ~ ~ ~ {Tame:1b,Silent:1b,NoAI:1b,ChestedHorse:1b,ActiveEffects:[{Id:14b,Amplifier:1b,Duration:1000000,ShowParticles:0b}]}

Using this, and invisible mule, and players can access the chest, where the custom crafting would take place. The only problem with this is that the player is able to ride the mule, so I add a passenger to prevent this.

/summon minecraft:mule ~ ~ ~ {Tame:1b,Silent:1b,NoAI:1b,ChestedHorse:1b,Passengers:[{id:"minecraft:armor_stand",Marker:1b,Invisible:1b}],ActiveEffects:[{Id:14b,Amplifier:1b,Duration:1000000,ShowParticles:0b}]}

However, doing so makes the player unable to access the chest. Is there any way to make a mule unrideable, but also making that mule's chest accessable to players?


Solution 1:

I think it is probably impossible to prevent mounting a mule while still giving the player access to its chest.

But you can make a player automatically dismount a mule by checking the RootVehicle tag of the player (the NBT of the mule doesn't actually change when it's ridden) and then placing water at the player's head (for some reason not the mule's head), which will automatically dismount the player for being in water. Then you can clear the water again one tick later. An easy way to do this would be a repeating command chain with these commands in them:

execute at @e[tag=water] run fill ~ ~1.62 ~ ~ ~1.62 ~ air replace water
kill @e[tag=water]
execute as @p at @s if entity @s[nbt={RootVehicle:{}}] run summon armor_stand ~ ~ ~ {Tags:["water"],NoGravity:1,NoAI:1,Invisible:1,Marker:1}
execute at @e[tag=water] run fill ~ ~1.62 ~ ~ ~1.62 ~ water replace air

The last two commands actually execute before the first two, putting them in a chain in this order is just an easy way to delay the execution of the first two by one tick.

What these commands do:

#3: Whenever the nearest player (you'll want to adjust this to your needs) has the RootVehicle tag (meaning they ride on something, you'll want to adjust this to your needs as well, for example with an additional check for something inside the RootVehicle tag that verifies that it's a mule, or just simple proximity to a mule), you summon a marker armour stand at the player's location. This is needed because you won't be on top of the mule anymore after placing the water, so you couldn't use it for removing the water again.
#4: Put water at the player's head to dismount them from the mule. I'm using the exact eye position of a non-sneaking player here, because while you sneak, you always either access the chest or instantly dismount yourself. If the mule is currently in the air or not aligned some other way, these exact coordinates will still always put the water at the right block. Something to consider: If you mount the mule in a way that your head ends up inside a block, then the water will not be placed. You can of course simply remove "replace air", but then you can overwrite blocks. If both alternatives aren't acceptable, you have to use a different solution for dismounting, maybe teleporting the player could work.
#1: Wait one tick and then replace the water with air again. It shouldn't flow anywhere within that one tick.
#2: Kill the marker armour stand. You might also want to adjust this to your needs, in case you could have other things tagged "water".