Minecraft – Inflict damage when player is hit by minecart

minecraft-commandsminecraft-java-edition

Is it possible to test when a player gets hit by a minecart, then inflict damage on the person hit by the minecart based on the speed of the minecart? If testing for the speed of the minecart isn't possible, that's okay. This is as far as I got with this:

/testfor @e[type=MinecartRideable]

Obviously, this command block would be running on a constant redstone loop, and would feed into the command block testing for the player, then into the command block that does damage to the player, unfortunately, I just can't figure out how to test for the player in the same location as the minecart. Does anyone know how to do this? Is it possible? As always, any help is appreciated, thanks!

Best Answer

First, the name of a normal minecart entity is actually "MinecartRideable".

Rather than just testing for a minecart being existent, we should make the minecart do something. The /execute command allows another command to be run as a specific entity. What we want the minecart to do is /execute an /effect command that damages nearby players:

/execute @e[type=MinecartRideable] ~ ~ ~ /effect @p[r=0] 7 10 0 true

Hook that up to a clock and all minecarts will constantly be giving damage to players that they touch. It doesn't need to check if it is actually touching anyone first, because if it isn't then nobody (@p[r=0]) will be hurt so it doesn't matter.

There's no real way to test for how fast a minecart is going, so be warned that it will always give the same amount of damage, even if you are the one pushing it away from you.

(We can technically test the speed of a minecart, but only in single values rather than a range, and its momentum tag can be decimal values.)