1.18.2 setCanceled doesnt seem to cancel the PlayerInteract Event

Discussion in 'Spigot Plugin Development' started by Snailtan, Jan 5, 2022.

  1. I have a system where you need to right click specific blocks with specific items so stuff happens.

    I tried canceling the player Interact event but it isnt working, the block GUI is still opening. In this case its a smithing table.


    Code (Text):

    public void onRightClickItem2(PlayerInteractEvent e) {
            ItemStack offHand = e.getPlayer().getInventory().getItemInOffHand();
            ItemStack mainHand = e.getPlayer().getInventory().getItemInMainHand();
            //Check for various cases
            if(e.getHand() == EquipmentSlot.OFF_HAND) {
                if(!offHand.hasItemMeta()) {
                    return;
                }
                ItemMeta meta = offHand.getItemMeta();
                if (!meta.hasLore()) {
                    return;
                }
                if(meta.getLore().get(0).equalsIgnoreCase("Work Item")) {
                    e.setCancelled(true);
                    String ToolID = CraftingItem.getNeededToolFromMeta(meta, plugin);
                    String StationID = CraftingItem.getWorkStationFromMeta(meta, plugin);
                    boolean stationOptional = CraftingItem.getWorkStationOptionalFromMeta(meta, plugin);
                    Material StationMaterial = Recipe.getMaterialFromEnum(Recipe.WorkstationEnum.valueOf(StationID));
                    //Check if right tool is used
                    ItemStack Tool;
                    if(Material.matchMaterial(ToolID)==null) {
                        Tool = ItemStackSerialization.loadFromFile(ToolID, plugin);
                    }
                    else {
                        Tool = new ItemStack(Material.matchMaterial(ToolID));
                    }
                    if(Tool == null) {
                        e.getPlayer().sendMessage("This work Item has an incorrect tool ID. Please contact an admin or mod with the item in question!");
                        return;
                    }
                    if(!mainHand.equals(Tool)) {
                        e.getPlayer().sendMessage("You are using the wrong tool!");
                    }
                    else{
                        if(e.getAction() == Action.RIGHT_CLICK_AIR) {
                            if(stationOptional || StationMaterial == Material.AIR) {
                                meta = CraftingItem.reduceWorkAmount(meta, 1, plugin);
                            }
                            else {
                                e.getPlayer().sendMessage("You have to use the right Station to work on this item!");
                            }
                        }
                        else if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                            Material material = e.getClickedBlock().getType();
                            if(material == StationMaterial) {
                                e.setCancelled(true);
                                meta = CraftingItem.reduceWorkAmount(meta, 1*CraftingItem.getStationMultiplierFromMeta(meta, plugin), plugin);
                            }
                            else if (stationOptional){
                                meta = CraftingItem.reduceWorkAmount(meta, 1, plugin);
                            }
                            else {
                                e.getPlayer().sendMessage("You have to use the right Station to work on this item!");
                            }
                        }
                        offHand.setItemMeta(meta);
                    }
                    if(CraftingItem.getWorkAmountFromMeta(meta, plugin) < 0) {
                        offHand.setAmount(0);
                        e.getPlayer().getInventory().addItem(PersistentDataHandler.getItemFromID(CraftingItem.getGoalItemFromMeta(meta, plugin),plugin));
                    }
                    offHand.setItemMeta(meta);
                }
            }
        }
     
    To quickly explain my code, when you hold a "Work Item" in your off hand, it checks if you hold the right tool for that work item in your main hand. Then it checks if the work item needs a specific workstation (in my case, yes, a smithing table), and if you are right clicking this specific work station.

    When I register that the correct work station is clicked, I set the cancel of that event to true, which should cancel the smithing table GUI from opening, no? Also whenever the gui opens, the rest of the code doesnt fire. It produces no errors and gets simply ignored.
     
    #1 Snailtan, Jan 5, 2022
    Last edited: Jan 5, 2022
  2. When I shift click the Smithing table is works fine btw...
     
  3. Are you sure you registered the event and added @EventHandler?
     
  4. You should cancel the InventoryOpenEvent, not PlayerInteractEvent.
     
  5. I would suggest that you rewrite your code so that you logicaly exclude events which are not relative first.
    ie
    If Action is NOT Right_Click_Block return
    If clicked block is NOT a workstation return
    if heldMainHand item is NOT a Tool return
    If heldOffHand item is NOT a WorkItem return
    etc ...
    Do your other stuff
     
  6. This is most likely because PlayerInteractEvent is called twice when right clicking, once for each hand. That needs to be taken into account.
     
  7. Where is @EventHandler ?