Need help disabling a particular Item

I’m facing an issue when disabling the gravity gun from the gravity gun mod. The issue is that even if I cancel the InteractItemEvent I can still use this item, which is bizarre. I’ve seen a plugin that does block the use of this item, is called MMCRestrict.
However, looking at the code I’ve seen how it actually does it: it removes the item from your inventory, making impossible for the gun event to go on and therefore limiting the use of it.
If you keep the item in the inventory than the problem occurs, you can use the gun even if the event was cancelled.
What I’ve done to test this out is setting up a simple plugin that register a listener, that all it does is cancelling the ItemInteract event

@Listener
public void onItemUse(InteractEvent event, @Root Player player) {
   event.setCancelled(true);
}

But if I do this

@Listener
public void onItemUse(InteractEvent event, @Root Player player) {
   removeFromInventory(player, event.getItemStack().createStack()),
   event.setCancelled(true);
}

private void removeFromInventory(Player player, ItemStack itemStack) {
        Inventory items = player.getInventory().query(QueryOperationTypes.ITEM_TYPE.of(itemStack.getType()));
        if (items.peek(itemStack.getQuantity()).isPresent()) {
            items.poll(itemStack.getQuantity());
        }
    }

The event is “cancelled” as well. But of course I want to keep the item in the player’s inventory, and I’ve already tried to remove the item, cancel the event, add the item back, but it doesn’t work. So what am I missing there?

Not exactly a perfect solution, but one you could try is use a scheduler

remove the item -> cancel the event -> schedule a task to add the item back with 0 ticks (so it runs at the end of the tick/start of the next tick … not sure the logic but oh well).

I’ll try that, but I was wondering if there is a way to handle this without removing the Item entirely (as when you readd the Item the inventory may be filled up). Also why just for this Item it looks like cancelling the event does nothing?

Sponge isnt perfect at interacting with mods. Your having to deal with other peoples logic which can be wildly different from mod to mod.

Im not familier with the gravity gun mod nor its logic. So I cannot say another way to handle the event. Sorry. However as long as you remove the item within the event and the tick time is 0 then I dont belive a player can gain another item unless another mod is playing musicial inventories (slots) with your mod.

edit:
if you are having to play musical inventories with another mod, you could take note of the hand that the item was removed from and then override the item (if there is one).

Yes, I had some problems with some mods during development, like the iron chest mod where the chests from that mod doesn’t have the typical chest properties from vanilla (therefore are seen like normal blocks). So I guess the only way to do this is using that task… Thank you for your precious help :smiley:

Iron chest is a bit different as it uses TileEntity which arnt standard (unlike custom blocks and custom items). So a mod (or another mod acting as a bridge) needs to register the conversation, but most dont as mod developers dont understand why developers dont just jump into there code (or just arnt aware of what sponge can do).