Check if event.getClickedBlock() has an inventory/opens?

Discussion in 'Plugin Development' started by MiningDead, Apr 23, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    MiningDead

    Basically I want the player to consume a Mushroom Stew instantly for health and satiety kinda like this thread https://bukkit.org/threads/instant-soup.152408/ but they didn't cover everything with Action.RIGHT_CLICK_BLOCK and Action.RIGHT_CLICK_AIR. They had maybe somehow thought chests were not normal right clicked blocks? I've also seen this thread: https://bukkit.org/threads/how-do-i...-inventory-and-then-get-that-inventory.67641/

    The issue: right clicking on a chest still passes through RIGHT_CLICK_BLOCK and consumes the stew if their hunger/health is below max. I needed to make sure they don't consume stew when they open a container aka an InventoryHolder, so I made a Block block equal to the event.getClickedBlock(); and made an if statement that did nothing when block.getState() is an instanceof InventoryHolder. This got rid of chests being an issue, but freakin' anvils aren't instanceof InventoryHolder, but they do open, so how do I test properly if a block has an inventory/GUI/temporaryopenedthing? I don't want the player to consume the stew if they're meaning to click on a chest or something opened. Also don't want to go in and add more conditions each time they add a new ender chest or smoker/grindstone. But I need it to run with every other block, for example, right clicking looking at grass or air.
    Code:
    //EventHandler
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            Player player = (Player) event.getPlayer();
            ItemStack itemInHand = player.getItemInHand();
            Material theMaterial = itemInHand.getType();
            Block block = player.getTargetBlock(null, 100);
            if (!(block.getState() instanceof InventoryHolder) && theMaterial == Material.MUSHROOM_STEW) {
                    //Add health, remove  the stew instantly and give them hunger if they need it instead of health, hunger first, health second
                    int hungrys = player.getFoodLevel();
                    Double healths = player.getHealth();
                    if (hungrys <= 14) {
                        player.setFoodLevel(hungrys + 6);
                        player.getInventory().setItemInHand(new ItemStack(Material.BOWL, 1));
                        } else if (healths <= 14 && hungrys == 20) {
                            player.setHealth(healths + 6);
                            player.getInventory().setItemInHand(new ItemStack(Material.BOWL, 1));
                        } else if (hungrys>14 && hungrys != 20) {
                            int saturation = (player.getFoodLevel() - 14);
                            player.setFoodLevel(20);
                            player.setSaturation(saturation);
                            player.getInventory().setItemInHand(new ItemStack(Material.BOWL, 1));
                        } else if (healths>14 && healths != 20 && hungrys == 20) {
                                player.setHealth(20);
                                player.getInventory().setItemInHand(new ItemStack(Material.BOWL, 1));
                    } else {
                        //Do nothing
                    }
                }
            }
    
     
    Last edited: Apr 23, 2019
  2. Online

    timtower Administrator Administrator Moderator

    @MiningDead Check the click then, you can check which type of click it is.
     
  3. Offline

    MiningDead

    Like checking if it's Action.RIGHT_CLICK_BLOCK? Two right clicks are the same chest vs regular block. Is there a way to see if a right click was just starting to consume the object and not opening something? Does it have to do with the target block I select, the first argument there that I have null?
     
    Last edited: Apr 23, 2019
  4. Offline

    KarimAKL

    @MiningDead If you can't find any good way to do this then you could probably make a list of materials you don't want it to work with, maybe something like this?
    Code:Java
    1. // Create list of materials you don't want the soup to active when clicking
    2. List<Material> list = new ArrayList<>(Arrays.asList(Material.CHEST, Material.ANVIL/*, etc*/));
    3.  
    4. // Check if clicked block's material type is any of the materials in the list
    5. if (list.contains(block.getType())) {
    6. // Do nothing
    7. } else {
    8. // Activate soup
    9. }
     
  5. Offline

    MiningDead

    @KarimAKL I think I will just go ahead and do that, it seems like I'd have to do it anyways each time MC adds some new block because they might open but not be a part of those InventoryHolder (or Containers). Happy Village and Pillage ya'll!
     
Thread Status:
Not open for further replies.

Share This Page