在Minecraft SpigotAPI库中检查玩家的特定物品在库存中。

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

Checking a Player for a Specific Item in Minecraft SpigotAPI Inventory

问题

我需要查找玩家的背包中是否有特定物品。我试图在文档中找到它,但对我来说它很糟糕,以至于我找不到它。
到目前为止,有这样一个代码,它可以工作,但只有当玩家放置或破坏带有我需要的物品的方块时才能工作,而我需要它在不断跟踪。例如,我写了一个命令,结果发现是否有这样一个物体或者没有。

@EventHandler
public  void yes(PlayerInteractEvent e){
    Player player = e.getPlayer();
    ItemStack item = e.getItem();
    if(item.getType() == Material.DIAMOND_ORE){
        player.sendMessage("Yes");
    }else{
        player.sendMessage("No");
    }
}
英文:

I need to find out if a player has a certain item in their inventory. I tried to find it in the documentation, but it is so terrible (as for me) that I could not find it.
So far, there is such a code that works, but it works when a player places or breaks a block with the item I need, and I need it to be constantly tracked. For example, I wrote a command and it turned out that there is such an object, or not.

@EventHandler
public  void yes(PlayerInteractEvent e){
    Player player = e.getPlayer();
    ItemStack item = e.getItem();
    if(item.getType() == Material.DIAMOND_ORE){
        player.sendMessage("Yes");
    }else{
        player.sendMessage("No");
    }
}

答案1

得分: 2

<h3> Minecraft Java库存任务 </h3>

我有一个解决方案。第一个代码片段用于检查库存中是否有物品但不考虑数量。

**代码:**

```java
@EventHandler
public void checkInventory(InventoryClickEvent e) {
    if (e.getWhoClicked() instanceof Player) {
        Player player = (Player) e.getWhoClicked();
        ItemStack item = new ItemStack(Material.DIAMOND_ORE);

        if (player.getInventory().contains(item)) {
            player.sendMessage("是");
        } else {
            player.sendMessage("否");
        }
    }
}

在这里,您还可以使用数量来检查的方法。

代码:

@EventHandler
public void checkInventory(InventoryClickEvent e) {
    if (e.getWhoClicked() instanceof Player) {
        Player player = (Player) e.getWhoClicked();
        ItemStack item = new ItemStack(Material.DIAMOND_ORE);
        int desiredQuantity = 2;
        int itemCount = 0;

        for (ItemStack stack : player.getInventory().getContents()) {
            if (stack != null && stack.isSimilar(item)) {
                itemCount += stack.getAmount();
            }
        }

        if (itemCount >= desiredQuantity) {
            player.sendMessage("是");
        } else {
            player.sendMessage("否");
        }
    }
}

<details>
<summary>英文:</summary>

&lt;h3&gt; Minecraft Java Inventory Task &lt;/h3&gt;

There I have a solution. The first code snippet is for checking if an item is in inventory without quantity.

**The Code:**

    @EventHandler
    public void checkInventory(InventoryClickEvent e) {
        if (e.getWhoClicked() instanceof Player) {
            Player player = (Player) e.getWhoClicked();
            ItemStack item = new ItemStack(Material.DIAMOND_ORE);
    
            if (player.getInventory().contains(item)) {
                player.sendMessage(&quot;Yes&quot;);
            } else {
                player.sendMessage(&quot;No&quot;);
            }
        }
    }


Here you also have a method where you can check with quantity.

**The Code:**
```java
@EventHandler
public void checkInventory(InventoryClickEvent e) {
    if (e.getWhoClicked() instanceof Player) {
        Player player = (Player) e.getWhoClicked();
        ItemStack item = new ItemStack(Material.DIAMOND_ORE);
        int desiredQuantity = 2;
        int itemCount = 0;

        for (ItemStack stack : player.getInventory().getContents()) {
            if (stack != null &amp;&amp; stack.isSimilar(item)) {
                itemCount += stack.getAmount();
            }
        }

        if (itemCount &gt;= desiredQuantity) {
            player.sendMessage(&quot;Yes&quot;);
        } else {
            player.sendMessage(&quot;No&quot;);
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月26日 20:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556503.html
匿名

发表评论

匿名网友

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

确定