Solved No way to cancel milking goats?

Discussion in 'Spigot Plugin Development' started by YHDiamond, Jun 21, 2021.

  1. Hi, if I want to disable players milking goats, there doesn't seem to be a way to do that.

    public void onMilk(PlayerInteractAtEntityEvent e) {
    if (e.getRightClicked() instanceof Goat) {

    This code will still have the player milk the goat. I know I registered the listener properly because debug messages work. Any way to fix this? Please don't suggest to set their main hand back to bucket in a tick using BukkitRunnables.
  2. I would assume this is because the event is fired after the goat has already been milked. So maybe set the event priority to lowest to make sure it fires first.

    Code (Text):
    @EventHandler(priority = EventPriority.LOWEST)
  3. #3 EvanTheSurfer, Jun 21, 2021
    Last edited: Jun 21, 2021
    • Agree Agree x 1
  4. Yea, I'm not sure. The code looks like it should work. Probably just an event's thing.

    They could also instead check the type of entity that is being clicked is EntityType.Goat rather than using instanceof. I don't see it making a difference, but it's worth a shot maybe. Maybe you could utilize the PlayerBucketFillEvent to your advantage somehow. I don't think there is any such thing as a milk block, and the only 2 sources of getting milk would be from cows or goats right? But there wouldn't be a way of distinguishing if you got the milk from a cow or a goat without looping nearby entities or something similar (which is not ideal).
    #5 EvanTheSurfer, Jun 21, 2021
    Last edited: Jun 21, 2021
    • Winner Winner x 1
  5. I didn't even know that PlayerBucketFillEvent was a thing, that may just be the answer. Big pog
  6. The priority won't change anything if the event is called after the interaction you're trying to prevent. Event priority is solely to do with the order that plugin event listeners are called in.
    That looks like it would only work with water and lava, since it uses Block and BlockFace.

    I'd stick with switching to PlayerInteractEntityEvent.
  7. I aint that experienced of a dev, but since the event fires, cant you check if their ItemInMainHand and ItemInOffHand is a bucket with milk and it is set it to a normal bucket
  8. I've just looked at the Minecraft code and the PlayerBucketFillEvent is indeed called when milking a goat.
    • Winner Winner x 2
    • Informative Informative x 1
    • Useful Useful x 1
  9. A player bucket fill event would be extremely useful, thanks I did not see that. BTW, setting priority and changing to the PlayerInteractEntityEvent event did work. Thanks so much!