Minecraft – Making a horse ride-able by only one player in vanilla Minecraft

minecraft-commandsminecraft-java-edition

I run a 100% Vanilla Minecraft server, and would like to have Undead Horses that only Moderators are able to use, but I'm not sure about how to do this. I've tried to use CustomName data tags when summoning the horse and then testing for players near the horse, but do not know how to kick the player off the horse if they do not match the correct UUID.

Best Answer

The final bit that you need is to just teleport the non-mod player, like so: tp @p ~ ~ ~ (of course, you're probably going to execute that command at the horse). This works because the teleport moves the target, and everything that is riding it, but not the things that the target is riding.

But that's just the bit you were missing, my full solution is below.


First things first, you're going to have to give your mod horses custom names. They can be unique, but it makes it easier if they're all the same. For my example, I'll just use "Mod Horse".

Next, you'll need two scoreboard objectives, isRiding and isMod. I have to admit that I don't really like having to use the isMod objective, but for my solution to work, it's unavoidable.

The final step is to have 4 command blocks triggered by a 20Hz fill clock. In order, those commands are:

scoreboard players add @a isMod 0
scoreboard players set @a isRiding 0
scoreboard players set @a isRiding 1 {Riding:{CustomName:"Mod Horse"}}
tp @a[score_isRiding_min=1,score_isMod=0] ~ ~ ~

The first command ensures that everyone always has a score in the isMod objective, the second and third commands reset the isRiding score, then set the score for anyone riding an entity named "Mod Horse" (this can be abused, by the way), and the fourth command teleports anyone riding a mod horse who isn't a mod to the same spot they were in, effectively knocking them off the horse.

Of course, you're also going to have to set the isMod score for all your mods, but that's easy with /scoreboard players set <mod name> isMod 1.