简体   繁体   English

单击两次而不是一次

[英]Clicking on item two times instead on one

I got a gui and when I click on material coal I need to click on it again to get it to activate the code. 我得到了一个gui,当我单击Material Coal时,我需要再次单击它以激活代码。 Here is my inventoryclickevent 这是我的清单点击事件

    String stripname = ChatColor.stripColor(e.getClickedInventory().getName());
    if(stripname.equals("Upgrade Spawner".toUpperCase())) 
    {
        if(e.getCursor()!=null) 
        {
            if(e.getCursor().getType()!=null) 
            {
                Material item = e.getCursor().getType();
                if(item.equals(Material.STAINED_GLASS_PANE)) e.setCursor(null);
                else if(item.equals(Material.COAL))
                {
                    if(checkBalance(e.getWhoClicked(), config.getInt("Coal Cost"))) 
                    {
                        takeAwayMoney(e.getWhoClicked(), config.getInt("Coal Cost"));
                        e.getWhoClicked().closeInventory();
                        e.getWhoClicked().sendMessage(ChatColor.GREEN + "Iron Golems now drop coal!");
                    } else 
                    {
                        e.getWhoClicked().closeInventory();
                        e.getWhoClicked().sendMessage(ChatColor.RED + "You do not have the requirment amount of money! You need " + getMoneyNeeded(e.getWhoClicked(), config.getInt("Coal Cost")) + " more!");
                    }
                }
            }
        }
    }

Because getCursor returns item that player is holding in the cursor and you are checking if it is not null - so first click will move that item to cursor, and then on next click cursor will not be null and code will execute. 因为getCursor返回了玩家在光标中保留的项目,并且您正在检查它是否不为null-因此,首先单击将该项移至光标,然后在下次单击时,光标将不为null并执行代码。
Use getCurrentItem instead. 请改用getCurrentItem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM