Home > Net >  InventoryClickEvent: Right-Click in a custom Inventory is not working
InventoryClickEvent: Right-Click in a custom Inventory is not working

Time:11-21

I have another problem and hope you may help me. I try to teleport a player when he clicks an item in a custom made inventory (in this case it is a Acacia door). The inventory itself works just fine, but when I try to click the item, nothing happens. Here's my code so far: Inventory Class

package de.Daniel260.startup.items;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;


public class HerrDesWindesTalisman implements Listener{
    
    @EventHandler
    public void onPlayerInteract (PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Action a = e.getAction();
        if(a.equals(Action.RIGHT_CLICK_AIR)  || a.equals(Action.RIGHT_CLICK_BLOCK)) {
            
            if(e.getItem() != null && e.getItem().getType().equals(Material.TOTEM_OF_UNDYING)) {
                if(p.hasPermission("quests.herrderwinde")) {
                    p.sendMessage("Warpmenü");
                    Inventory inv = Bukkit.createInventory(p, 27, "Ziele");
                    ItemStack item1 = new ItemStack(Material.ACACIA_DOOR);
                    ItemMeta imeta1 = item1.getItemMeta();
                    imeta1.setDisplayName("Reise nach Irgendwo");
                    item1.setItemMeta(imeta1);
                    inv.setItem(10, item1);
                    p.openInventory(inv);
                } 
                else {
                    p.sendMessage("§4Für dieses Item brauchst du folgende Quest: §6Herr des Windes");
                }
                
                
            }
            
        }
    }
    @SuppressWarnings("unused")
    public void onInventoryClick (InventoryClickEvent e) {
        Player p = (Player) e.getWhoClicked();
        InventoryAction a = e.getAction();
        
        
        if(p.getItemOnCursor().getItemMeta().getDisplayName().equals("Reise nach Irgendwo")) {
            Location loc = new Location(p.getWorld(), 0, 6, 0);
            p.teleport(loc);
            
        }
    }

}

CodePudding user response:

Firstly, you forge the @EventHandler tag on the method.

Also, p.getItemOnCursor() is on player and not of event, so it's not the good way to get the item.

You should use event.getCurrentItem(). Warn: this method can return null.

This is the final method onInventoryClick :

@EventHandler
public void onInventoryClick (InventoryClickEvent e) {
    Player p = (Player) e.getWhoClicked();
    if(e.getCurrentItem() != null && e.getCurrentItem().getItemMeta().getDisplayName().equals("Reise nach Irgendwo")) {
        p.teleport(new Location(p.getWorld(), 0, 6, 0));
    }
}
  • Related