gpt4 book ai didi

java - 无法从 1 个单项中减去?

转载 作者:行者123 更新时间:2023-11-29 03:17:38 25 4
gpt4 key购买 nike

我已经设置了这种方法,玩家可以在右键单击隐身几秒钟时使用火药,并且将从他们的库存中减去 1 火药。问题是,当他们只剩下 1 个火药时,它不会被减去,因此他们将拥有无限的斗篷。这是我的代码:

if (e.getAction().equals(Action.RIGHT_CLICK_AIR)
|| e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
if (e.getPlayer().getInventory().getItemInHand().getType()
.equals(Material.SULPHUR)) {
Player player = e.getPlayer();

Location location = e.getPlayer().getLocation().add(new Vector(0, 2, 0));

Bukkit.getWorld(e.getPlayer().getWorld().getName())
.createExplosion(location, 0);

player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 80, 0));

player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
}
}

最佳答案

我觉得问题就出在这里

player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);

ItemStack.setAmount(0) 将失败。


检查您的整个算法。

ItemStack hand = player.getItemInHand();
int amount = hand.getAmount();
if (amount > 1) {
hand.setAmount(amount - 1);
player.setItemInHand(hand);
} else {
player.setItemInHand(new ItemStack(Material.AIR));
}

关于java - 无法从 1 个单项中减去?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25590202/

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