Minecraft how to detect when a player accesses an anvil

minecraft-commandsminecraft-java-edition

How would I detect when a player interacts with an anvil? And I don't mean by repairing, I mean opening the anvil interface. How would I also detect when a player interacts with an enchantment table? The only way I can think of is wait till Mojang adds those statistics. Also, is there anyway to stop the interface coming up for crafting tables? the only way I can think to do it is to test for if it has been interacted with, and then immediately replace it with another crafting table. Any help would be appreciated.

Best Answer

First two questions aren't possible as far as I know. There's not really a good way to detect if a player right clicks in general; whether it's a specific block would be even harder to detect.


The third question is possible though, as there's a stat.craftingTableInteraction scoreboard objective type.

First, create the objective to track people opening a crafting table:

/scoreboard objectives add openedTable stat.craftingTableInteraction

Delete the crafting table:

/setblock X Y Z air

Then wait a tick for the interface to close, and replace the crafting table:

/setblock X Y Z crafting_table

This should work, but keep in mind that if the player is lagging or has a modded client, they may still be able to craft; don't rely on it completely to stop people from crafting.