gpt4 book ai didi

java - Bukkit 检查玩家的库存

转载 作者:行者123 更新时间:2023-12-02 03:27:09 26 4
gpt4 key购买 nike

我正在尝试查看玩家的库存中是否有以下任何元素,如果有,我想删除这些元素并向他们发送一条消息,说明该元素已被删除,但我不知道如何为此,这就是我到目前为止所拥有的:

    Material[] bannedItems = { Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE, Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS, Material.GOLD_HELMET, Material.GOLD_CHESTPLATE, Material.GOLD_LEGGINGS, Material.GOLD_BOOTS };

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if(player.getInventory().contains(bannedItems, 1)){
player.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + ChatColor.BOLD + "RPG" + ChatColor.GRAY + "] " + ChatColor.RED + "A has been removed from your possession.");
}
}

我在 .contains(bannedItems, 1)) 行上收到错误。

感谢您的帮助!

最佳答案

对于数组中的每个元素,都必须检查它,而不仅仅是比较数组。这是概念证明:

double[] check = {1.3, 4.2, 4.3}; //A sample double array

for(double d : check) { //Loop thru all elements
if(4.2 == d) { //If current element is equal to 4.2
System.out.println("EQUAL!"); //Print "EQUAL!"
}
}

这会逐一循环检查 check 数组,并将每个元素与 4.2 进行比较。当他们平等时,他们就会报告。然后您可以将其应用于这种情况:

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Material[] bannedItems = { Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE, Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS, Material.GOLD_HELMET, Material.GOLD_CHESTPLATE, Material.GOLD_LEGGINGS, Material.GOLD_BOOTS };
Player player = (Player) event.getWhoClicked();

for(Material m : bannedItems) { //Loop thru all elements
if(player.getInventory().contains(m, 1)){ //Check if in inventory
player.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + ChatColor.BOLD + "RPG" + ChatColor.GRAY + "] " + ChatColor.RED + "A has been removed from your possession.");
//Remove stuff
}
}
}

现在,该事件将循环访问 bannedItems 的每个元素,并检查用户的库存中是否有该元素。

for(Material m : bannedItems) 

是一个增强的 for 循环,它将当前元素存储在 m 变量中的 bannedItems 中,然后我们使用单个 Material m 进行比较.

我还会使用不同的事件,例如当他们拿起一个元素时,然后进行检查,以便您可以立即将其删除。

关于java - Bukkit 检查玩家的库存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38687583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com