Solved InventoryOpenEvent

Discussion in 'Plugin Development' started by BrickBoy55, Apr 17, 2015.

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

    BrickBoy55

    I'm having trouble with the inventory open event. I've tried debugging, but the event isn't even registering.

    Code:
        @EventHandler
        public void onInventoryOpen(InventoryOpenEvent event) {
           
            Bukkit.broadcastMessage("1");
            if (event.getInventory().equals(InventoryType.CHEST) ||
                event.getInventory().equals(InventoryType.FURNACE) ||
                event.getInventory().equals(InventoryType.CRAFTING) ||
                event.getInventory().equals(InventoryType.ENCHANTING) ||
                event.getInventory().equals(InventoryType.BREWING) ||
                event.getInventory().equals(InventoryType.ENDER_CHEST) ||
                event.getInventory().equals(InventoryType.HOPPER)) {
               
                Bukkit.broadcastMessage("2");
               
                HumanEntity player = event.getPlayer();
                int radius = 25;
                Location loc = player.getLocation();
    
                Bukkit.broadcastMessage("3");
                for (int x = (int) (loc.getX() - radius); x < loc.getX() + radius; x++) {
                    for (int y = (int) (loc.getY() - radius); y < loc.getY() + radius; y++) {
                        for (int z = (int) (loc.getZ() - radius); z < loc.getZ() + radius; z++) {
                           
                            Bukkit.broadcastMessage("4");
    
                            Block block = player.getWorld().getBlockAt(x, y, z);
    
                            if (block.getState() instanceof Sign) {
                                Sign sign = (Sign) block.getState();
                                    Bukkit.broadcastMessage("5");
                                if (sign.getLine(1).equals("§8[§9Protected§8]")) {
                                    if (!player.getName().equals(ChatColor.stripColor(sign.getLine(2)))) {
                                       
                                        Bukkit.broadcastMessage("6");
                                        event.setCancelled(true);
                                        player.sendMessage(prefix + "You cannot use that in " + ChatColor.stripColor(sign.getLine(2)) + "'s land!");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    
    Before you ask me:

    Yes, my events are registered.

    Help is appreciated!
     
  2. Offline

    Freelix2000

    @BrickBoy55
    If you're trying to detect when a player opens their inventory, I don't think that event will do it. One way that you could do it though would be to remove all "Taking inventory" achievements from players, listening for the achievement event, cancelling it, and then adding all the inventory open code in that event. The one downside is that players will not be able to have the "Taking inventory" achievement with that work-around.
     
  3. Offline

    BrickBoy55

    @Freelix2000

    Theres got to be an easier way though.

    EDIT: The debugging now gets to #1

    @Freelix2000

    Oh yeah, I found a major fail in my code (I was getting the inventory name, not the type), also the debug four message spammed my server into crash, so I have no status whether it works right now or not.

    Issue resolved itself.

    Solution: Needed to get the inventory type, not the name.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page