Solved Player interact event

Discussion in 'Plugin Development' started by gabsloco, Jan 20, 2015.

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

    gabsloco

    Hi, i'm a java begginer and i'm trying to do a plugin, in this part of the plugin, I want open a inventory when a player has a book on his hand and clicks with right or left click in somewhere.
    I tryed this:
    Code:
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getAction() == Action.RIGHT_CLICK_AIR
                    && (event.getAction() == Action.RIGHT_CLICK_BLOCK
                            && (event.getAction() == Action.LEFT_CLICK_AIR) && (event
                            .getAction() == Action.LEFT_CLICK_BLOCK))) {
                Bukkit.broadcastMessage("1");
                if (p.getItemInHand().getType() == Material.BOOK) {
                    p.openInventory(inv);
                    Bukkit.broadcastMessage("2");
                }
            }
        }
    But is not working, someone can help me?
     
  2. Offline

    DaanSander

    @gabsloco have you tried this?:
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().equals(Your inventory)) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Item name")) {
                e.setCancelled(true);
                //some code
     
  3. Offline

    Skionz

    @gabsloco Is your Listener registered?
    EDIT: @Creeoer Is correct. The action can't be right click block if it is already right click air.
     
  4. Offline

    Creeoer

    @gabsloco

    I don't understand why you are doing && Action equals whatever , just use the || (or) statement to let different conditions give true on the if statement.
     
    gabsloco likes this.
  5. Offline

    gabsloco

    @Creeoer OH, I forgot that && is "and". I don't know what was I thinking.
    Worked, but now i have another problem, I have put different display names and lores in the itemStacks the inventory have, but the items in the inventory are the same as the originals, i don't know why, take a look:
    Code:
        public static Inventory inv = Bukkit.createInventory(null, 9,
                ChatColor.GOLD + "Loja");
        String nome = ChatColor.GOLD + "Loja";
        private Player player;
        static ItemStack ep = new ItemStack(Material.ENDER_PEARL, 1);
        ItemMeta perlmeta = ep.getItemMeta();
        List<String> perlmetal = Arrays.asList(ChatColor.WHITE
                + "Valor: 5 Skycents");
        static ItemStack cb = new ItemStack(Material.COBBLESTONE, 32);
        ItemMeta cbm = cb.getItemMeta();
        List<String> cbml = Arrays.asList(ChatColor.WHITE + "Valor: 1 Skycent");
        static ItemStack d = new ItemStack(Material.DIAMOND, 1);
        ItemMeta dm = d.getItemMeta();
        List<String> dml = Arrays.asList(ChatColor.WHITE + "Valor: 5 Skycents");
        static ItemStack fe = new ItemStack(Material.IRON_INGOT, 1);
        ItemMeta fem = fe.getItemMeta();
        List<String> feml = Arrays.asList(ChatColor.WHITE + "Valor: 5 Skycents");
        static ItemStack b = new ItemStack(Material.BOW, 1);
        ItemMeta bm = b.getItemMeta();
        List<String> bml = Arrays.asList(ChatColor.WHITE + "Valor: 3 Skycents");
        static ItemStack f = new ItemStack(Material.ARROW, 5);
        ItemMeta fm = f.getItemMeta();
        List<String> fml = Arrays.asList(ChatColor.WHITE + "Valor: 1 Skycents");
        static ItemStack e = new ItemStack(Material.FLINT_AND_STEEL, 1);
        ItemMeta em = e.getItemMeta();
        List<String> eml = Arrays.asList(ChatColor.WHITE + "Valor: 1 Skycents");
        static ItemStack a = new ItemStack(Material.GOLDEN_APPLE, 1);
        ItemMeta am = a.getItemMeta();
        List<String> aml = Arrays.asList(ChatColor.WHITE + "Valor: 2 Skycents");
        static ItemStack vip = new ItemStack(Material.GOLD_CHESTPLATE, 1);
        ItemMeta vipm = vip.getItemMeta();
        List<String> vipml = Arrays.asList(ChatColor.WHITE + "Valor: 2 Skycents");
        ItemStack loja = new ItemStack(Material.BOOK, 1);
        ItemMeta lojam = loja.getItemMeta();
        List<String> lojaml = Arrays.asList(ChatColor.GRAY
                + "Compre itens para ajudar na batalha!");
    
        public void onEnable() {
    
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            // item stacks
            loja.setItemMeta(lojam);
            lojam.setLore(lojaml);
            lojam.setDisplayName(ChatColor.GOLD + "Loja " + ChatColor.GRAY
                    + "(Click Direito)");
            ep.setItemMeta(perlmeta);
            perlmeta.setLore(perlmetal);
            perlmeta.setDisplayName(ChatColor.BLUE + "EnderPearl");
            cb.setItemMeta(cbm);
            cbm.setLore(cbml);
            cbm.setDisplayName(ChatColor.BLUE + "Pedregulho");
            d.setItemMeta(dm);
            dm.setLore(dml);
            dm.setDisplayName(ChatColor.BLUE + "Diamante");
            fe.setItemMeta(fem);
            fem.setLore(feml);
            fem.setDisplayName(ChatColor.BLUE + "Ferro");
            b.setItemMeta(bm);
            bm.setLore(bml);
            bm.setDisplayName(ChatColor.BLUE + "Arco");
            f.setItemMeta(fm);
            fm.setLore(fml);
            fm.setDisplayName(ChatColor.BLUE + "Flecha");
            e.setItemMeta(em);
            em.setLore(eml);
            em.setDisplayName(ChatColor.BLUE + "Isqueiro");
            a.setItemMeta(am);
            am.setLore(aml);
            am.setDisplayName(ChatColor.BLUE + "GoldenApple");
            vip.setItemMeta(vipm);
            vipm.setLore(vipml);
            vipm.setDisplayName(ChatColor.GOLD + "Kit-Vip");
    
            // fim dos itemstacks
        }
    
        static {
            inv.setItem(0, fe);
            inv.setItem(1, cb);
            inv.setItem(2, e);
            inv.setItem(3, d);
            inv.setItem(4, ep);
            inv.setItem(5, b);
            inv.setItem(6, f);
            inv.setItem(7, a);
            inv.setItem(8, vip);
        }
    
     
Thread Status:
Not open for further replies.

Share This Page