英文:
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>
<h3> Minecraft Java Inventory Task </h3>
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("Yes");
} else {
player.sendMessage("No");
}
}
}
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 && stack.isSimilar(item)) {
itemCount += stack.getAmount();
}
}
if (itemCount >= desiredQuantity) {
player.sendMessage("Yes");
} else {
player.sendMessage("No");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论