我正在尝试查看玩家是否在其广告资源中有以下任何项目,如果他们这样做,我想删除这些项目并向他们发送消息说该项目已被删除但我无法弄清楚如何要做到这一点,这就是我到目前为止所做的:
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))行上收到错误。
感谢您的帮助!
答案 0 :(得分:2)
对于数组中的每个元素,您必须检查它,而不仅仅是比较数组。这是一个概念证明:
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循环,它将bannedItems
中的当前元素存储在m
变量中,然后我们使用m
进行比较,这是一种材料。
我还会使用不同的事件,例如当他们拿起物品然后检查时,您可以立即将其删除。