#PlayerInteractEvent fires EquipmentSlot.HAND twice when RIGHT_CLICK_BLOCK

1 messages · Page 1 of 1 (latest)

zealous tulip
#
@EventHandler
    public void onUse(PlayerInteractEvent e) {
        if (e.getHand() == EquipmentSlot.HAND) {
            Player p = e.getPlayer();
            Material materialInHand = p.getInventory().getItemInMainHand().getType();
            String itemDisplayName = p.getInventory().getItemInMainHand().getItemMeta().getDisplayName();
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                if (!materialInHand.equals(Material.AIR)) {
                    if (materialInHand.equals(Material.BLAZE_ROD) ||
                            materialInHand.equals(Material.STICK)) {
                        if (itemDisplayName.equals("§eSpieler verstecken")) {
                            hidePlayers(p);
                        } else if (itemDisplayName.equals("§eSpieler anzeigen")) {
                            showPlayers(p);
                        }
                    }
                }
            }
        }

    }

    private void hidePlayers(Player player)
    {
        Lino.getPlayerhider().add(player.getName());
        for (Player current : Bukkit.getOnlinePlayers()) {
            player.hidePlayer(Lino.getInstance(), current);
        }
        Inventory inventory = player.getInventory();
        inventory.setItem(8, createItemStack(Material.STICK, 1, "§eSpieler anzeigen"));
        SoundFeedback.positiveSound(player);
        player.sendMessage(Lino.getSystemPrefix() + "§7Die Spieler sind nun §6unsichtbar§7!");
    }

    private void showPlayers(Player player)
    {
        Lino.getPlayerhider().remove(player.getName());
        for (Player current : Bukkit.getOnlinePlayers()) {
            player.showPlayer(Lino.getInstance(), current);
        }
        Inventory inventory = player.getInventory();
        inventory.setItem(8, createItemStack(Material.BLAZE_ROD, 1, "§eSpieler verstecken"));
        SoundFeedback.positiveSound(player);
        player.sendMessage(Lino.getSystemPrefix() + "§7Die Spieler sind nun §6sichtbar§7!");
    }

The event is only fired when right click block and not when right click air. does anyone know how i can fix this?

full citrus
#

if (!materialInHand.equals(Material.AIR)) {

#

Isnt that causing the issue?

#

Seems like you're returning if you right click air

zealous tulip
full citrus
#

Then idk

zealous tulip
#

I added p.sendMessage("Equipmentslot: " + e.getHand().toString());
When i rightclick a block it fireds the hand equipmenslot twice, so its not the offhand

full citrus
#

It's the only thing I can think of that's preventing the event from being called when rightclicking air

zealous tulip
# full citrus Then idk
    public void onUse(PlayerInteractEvent e) {
        if (e.getHand() == EquipmentSlot.HAND) {
            Player p = e.getPlayer();
            p.sendMessage("Equipmentslot: " + e.getHand().toString());
            Material materialInHand = p.getInventory().getItemInMainHand().getType();
            String itemDisplayName = p.getInventory().getItemInMainHand().getItemMeta().getDisplayName();
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                if (materialInHand.equals(Material.BLAZE_ROD) || materialInHand.equals(Material.STICK)) {
                    if (itemDisplayName.equals("§eSpieler verstecken")) {
                        hidePlayers(p);
                    } else if (itemDisplayName.equals("§eSpieler anzeigen")) {
                        showPlayers(p);
                    }
                }
            }
        }

    }
#

so the hole event is fired twice, but its registered only once

full citrus
#

Can you show your onEnable method

zealous tulip
#
@Override
    public void onEnable() {
        registerEventsAndCommands();
        registerYaml();
        setPrefixes();
        setBroadcasts();
        setVariables();
    }
private void registerEventsAndCommands() {
        getServer().getPluginManager().registerEvents(new BlockListener(), this);
        getServer().getPluginManager().registerEvents(new ConnectionListener(this), this);
        getServer().getPluginManager().registerEvents(new ChatListener(), this);
        getServer().getPluginManager().registerEvents(new SecretListener(), this);
        getServer().getPluginManager().registerEvents(new ServerListener(), this);
        getServer().getPluginManager().registerEvents(new HotbarListener(), this);

        getCommand("build").setExecutor(new Build());
        getCommand("spawn").setExecutor(new Spawn());
        getCommand("setspawn").setExecutor(new SetSpawn());
        getCommand("msg").setExecutor(new Msg());
        getCommand("announce").setExecutor(new Announce());
        getCommand("rank").setExecutor(new RankCommand(this));
    }
#

The PlayerInteractEvent is in HotbarListener

sweet marlin
#

interact event runs once for each hand

full citrus
#

Wait really? Good to know lol