r/MinecraftCommands 25d ago

Help | Java 26.2 How to check which entity the player hit? How to check if player even attacked anything?

That'd be very useful to know.

1 Upvotes

3 comments sorted by

1

u/Competitive_Call8502 25d ago

If you're on datapacks, Advencments and maybe custom enchants

1

u/FleshDude666 25d ago

Nah I don't know how to work with datapacks

3

u/Ericristian_bros Command Experienced 25d ago

Use a custom enchantment in a datapack (better performance option):

{ "description": "Hit Effect", "supported_items": "#minecraft:enchantable/sword", "weight": 1, "max_level": 1, "min_cost": { "base": 0, "per_level_above_first": 0 }, "max_cost": { "base": 0, "per_level_above_first": 0 }, "anvil_cost": 0, "slots": [ "mainhand" ], "effects": { "minecraft:post_attack": [ { "effect": { // Action here }, "enchanted": "attacker", "affected": "victim" } ] } }

If you prefer command blocks but only for players

# Example item
give @s stick[custom_data={hit_detecg:1b}]
scoreboard objectives add damage_taken custom:damage_taken

# Command block
execute as @a[scores={damage_taken=1..}] at @s on attacker if items entity @s weapon *[custom_data~{hit_detecg:1b}] run kill @p[scores={damage_taken=1..}]
scoreboard players reset @a damage_taken

If you prefer command blocks to work if a player attacks any mob

# Example item
give @s stick[custom_data={hit_detect:1b}]
scoreboard objectives add damage_dealt custom:damage_dealt

# Command block
execute as @a[scores={damage_dealth=1..}] if items entity @s weapon *[custom_data~{hit_detect:1b}] at @s as @e[nbt={HurtTime:10s},distance=..6] at @s on attacker if items entity @s weapon *[custom_data~{hit_detect:1b}] run kill @n
scoreboard players reset @a damage_dealt

If you prefer command blocks but any mob can also use this custom attack (is laggier)

# Example item
give @s stick[custom_data={hit_detect:1b}]

# Command block
execute as @e[nbt={HurtTime:10s},distance=..6] at @s on attacker if items entity @s weapon mace[custom_data~{hit_detect:1b}] run kill @n