无法解析 ‘Inventory’ 中的 ‘getTitle’ 方法,当点击事件时。

huangapple go评论168阅读模式
英文:

Cannot resolve method 'getTitle' in 'Inventory' when click event

问题

在点击套件GUI的事件中,我遇到了这个错误 "无法解决 'Inventory' 中的 'getTitle' 方法"。

// 点击套件GUI的事件
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
    Player player = (Player) e.getWhoClicked();
    Inventory inv = e.getInventory();
    if (inv.getTitle().equals("Kit Creator")) {
        e.setCancelled(true);
        if (e.getCurrentItem() == null) {
            return;
        }
        switch (e.getCurrentItem().getType()) {
            case GREEN_WOOL:
                createKit(player);
                break;
            case RED_WOOL:
                deleteKit(player);
                break;
            case BLUE_WOOL:
                useKit(player);
                break;
            default:
                addItem(player, e.getCurrentItem());
                break;
        }
    }
}

我非常感激任何愿意帮助我的人。

我找到了关于 .getView() 的信息,但我无法弄清如何在我的情况下使用它。

英文:

In my event for clicking on kit GUI I get this error "Cannot resolve method 'getTitle' in 'Inventory'".

 // Event for clicking on kit GUI
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
        Player player = (Player) e.getWhoClicked();
        Inventory inv = e.getInventory();
        if (inv.getTitle().equals("Kit Creator")) {
            e.setCancelled(true);
            if (e.getCurrentItem() == null) {
                return;
            }
            switch (e.getCurrentItem().getType()) {
                case GREEN_WOOL:
                    createKit(player);
                    break;
                case RED_WOOL:
                    deleteKit(player);
                    break;
                case BLUE_WOOL:
                    useKit(player);
                    break;
                default:
                    addItem(player, e.getCurrentItem());
                    break;
            }
        }
    }

I am very appreciative of anyone willing to help me.

I found information regarding .getView(), but I couldn't figure out how to use it in my case.

答案1

得分: 0

要获取标题,您应该使用 e.getView().getTitle()。视图只是已打开的库存。

我建议您使用 InventoryHolder 来检查您的库存。

首先,您应该创建一个实现 Inventory Holder 接口的类,像这样:

public class MyHolder implements InventoryHolder {
	
	@Override
	public Inventory getInventory() {
		return null;
	}
}

在创建库存时,不要将 "null" 或玩家作为第一个参数(即持有者,像 new MyHolder())。

现在,您可以使用 e.getInventory().getHolder(),然后检查持有者是否不为 null,且是否是 MyHolder 的实例。

最后,您可以将数据放入您的持有者中,因为您将检索到用于创建库存的确切对象。

英文:

To get the title, you should use e.getView().getTitle(). The view is just the inventory opened.

I suggest you to use InventoryHolder to check for your inventory.

Firstly, you should create a class that implement Inventory Holder like that:

public class MyHolder implements InventoryHolder {
	
	@Override
	public Inventory getInventory() {
		return null;
	}
}

When creating the inventory, instead of putting "null" or the player as first argument (the holder, like new MyHolder()).

Now, you can do e.getInventory().getHolder() then check if the holder is not null and if instanceof MyHolder.

Finally, You can put data into your holder as you will retrieve the exact same object used to create the inventory.

huangapple
  • 本文由 发表于 2023年2月8日 12:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定