Fabric API

Fabric API

106M Downloads

fabric-events-interaction-v0: UseEntityCallback cancelling not completely cancelling interaction

nickrobson opened this issue · 0 comments

commented

Hey there!

It looks like subscribing to events using the UseEntityCallback.EVENT callback and returning a ActionResult.FAIL doesn't actually prevent the entire entity interaction. The mixin seems to only be listening to/affecting the interact at interaction type and not the interact type, which the Minecraft client will fall back to if the former fails on the client side (see the ENTITY handling block in MinecraftClient#doItemUse (yarn mappings)) - and cancelling this fallback too works as expected.

Code to reproduce (just right-click a villager and you'll see the UI still opens)

UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
    return ActionResult.FAIL;
});

I'd love to see this fixed as I'm writing a mod that involves cancelling entity interactions, so I've had to do the double injection to work around this!


To fix this, I see two ways of doing it:

  1. Adding another injection point at the start of the interact method in the same class as the interactAt injection point. This has the positive of being simple, but the negative of meaning each handler is called twice, which may be a deal-breaker – or will at least need a way of differentiating the two interaction types.

  2. Lifting the injection point up one layer so that it covers both interact and interactAt in one go, so a FAIL result skips both interact at and interact attempts. I believe this would be the preferred option as each listener would be called only once and would be able to truly allow/deny the entire entity interaction.


Callback: https://github.com/FabricMC/fabric/blob/1.18/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/UseEntityCallback.java#L41

Mixin: https://github.com/FabricMC/fabric/blob/1.18/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/mixin/event/interaction/MixinServerPlayNetworkHandler.java#L45-L57