Minecraft Spigot命令后从库存中删除项目(对不起,我很新)

发布于 2025-02-10 23:17:33 字数 1490 浏览 1 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2025-02-17 23:17:33

您的内部静态方法 remove intemfromineventory 在那里无法使用;您不能将方法直接嵌套在Java中。但是,无论如何不是必需的。

查看,在实体的库存上有一个删除方法,因此我们可以删除这样的项目:

player.getInventory().remove(new ItemStack(Material.DIAMOND, 1));

但是,如文档中所述:

这仅在堆栈匹配的类型和数量

的类型和数量时才匹配插槽

因此,只有在玩家只有一个钻石的插槽时,才能去除一颗钻石;例如,如果他们有两个,它将不会尝试删除。

因此,我们需要做一些腿部工作:

// Iterate over the player's inventory
for (ItemStack itemStack : player.getInventory()) {
    // Check that the ItemStack isn't null (empty/air slots) or if it's not a slot containing diamonds
    if(itemStack == null || !itemStack.getType().equals(Material.DIAMOND)) {
        // if so, skip running further logic on this stack
        continue;
    }
    // Remove one from the amount, the ItemStack will be removed if it has 0 amount
    itemStack.setAmount(itemStack.getAmount() - 1);
    // Break out of the parent for loop to avoid removing more than we want
    break;
}

您可以将此逻辑提取到具有签名的方法中,例如 delectItemstackbyamount(玩家播放器,材料,材料,int量);但是,当金额大于堆栈(64)时,您将需要添加额外的逻辑,并说明堆栈尺寸小于您要删除的数量。作为学习练习,我将把这个实施留给您。

PS For future StackOverflow questions, take the tour and read

Your inner static method removeItemFromInventory won't work there; you can't directly nest methods in Java. However, it's not necessary anyway.

Looking at the Spigot documentation, there's a remove method on the entity's Inventory, so we could remove an item like so:

player.getInventory().remove(new ItemStack(Material.DIAMOND, 1));

However, as noted in the documentation:

This will only match a slot if both the type and the amount of the stack match

So this will only remove one diamond if the player has a slot containing only one diamond; it will not attempt to remove if they had two, for example.

Therefore, we need to do some leg work:

// Iterate over the player's inventory
for (ItemStack itemStack : player.getInventory()) {
    // Check that the ItemStack isn't null (empty/air slots) or if it's not a slot containing diamonds
    if(itemStack == null || !itemStack.getType().equals(Material.DIAMOND)) {
        // if so, skip running further logic on this stack
        continue;
    }
    // Remove one from the amount, the ItemStack will be removed if it has 0 amount
    itemStack.setAmount(itemStack.getAmount() - 1);
    // Break out of the parent for loop to avoid removing more than we want
    break;
}

You could extract this logic into a method with a signature like reduceItemStackByAmount(Player player, Material material, int amount); however, you will need to add extra logic for when the amount is greater than a stack (64) and account for the stack size being smaller than the amount you're trying to remove. I'll leave this implementation up to you as a learning exercise.

P.S. For future StackOverflow questions, take the tour and read How do I ask a good question? Also why you shouldn't post images of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文