From 02afac7feaca6dc0faec52608f1ac81ca067e776 Mon Sep 17 00:00:00 2001 From: Strokkur24 <133226102+Strokkur424@users.noreply.github.com> Date: Wed, 25 Dec 2024 00:28:01 +0100 Subject: [PATCH] Refactor methods --- .../java/org/bukkit/entity/HumanEntity.java | 54 ++++++++++-------- .../craftbukkit/entity/CraftHumanEntity.java | 55 ++++++++++--------- 2 files changed, 58 insertions(+), 51 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/entity/HumanEntity.java b/paper-api/src/main/java/org/bukkit/entity/HumanEntity.java index c293d013c7..d0fcfbb09f 100644 --- a/paper-api/src/main/java/org/bukkit/entity/HumanEntity.java +++ b/paper-api/src/main/java/org/bukkit/entity/HumanEntity.java @@ -703,48 +703,54 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder * This will force the entity to drop the item they are holding with * an option to drop the entire {@link ItemStack} or just 1 of the items. * + * @deprecated You should instead use {@link #dropItem(EquipmentSlot, int)} with a {@link EquipmentSlot#HAND} parameter. * @param dropAll True to drop entire stack, false to drop 1 of the stack * @return True if item was dropped successfully */ + @Deprecated public boolean dropItem(boolean dropAll); - /** - * Makes the entity drop an item from their inventory based on the specified ItemStack. - *
- * This method calls {@link HumanEntity#dropItem(int slot, boolean throwRandomly)} - * with the first {@link ItemStack} occurrence in the inventory - * - * @param itemStack The ItemStack to drop - * @param throwRandomly Whether the item should disperse randomly. - * This means that instead of the item being dropped where the player is currently looking, - * it instead throws it in any direction, similar to how items drop after a player's death. - * @return The dropped item, or null if the action was unsuccessful - */ - public @Nullable Item dropItem(final @NotNull ItemStack itemStack, final boolean throwRandomly); - /** * Makes the entity drop an item from their inventory based on the slot. * - * @param slot The slot to drop - * @param throwRandomly Whether the item should disperse randomly. - * This means that instead of the item being dropped where the player is currently looking, - * it instead throws it in any direction, similar to how items drop after a player's death. + * @param slot The slot to drop + * @param amount The number of items to drop from this slot. Values below 1 don't drop an Item * @return The dropped item entity, or null if the action was unsuccessful * @throws IndexOutOfBoundsException If the slot is negative or bigger than the player's inventory */ - public @Nullable Item dropItem(final int slot, final boolean throwRandomly); + public @Nullable Item dropItem(int slot, int amount); /** * Makes the player drop an item from their inventory based on the equipment slot. * - * @param slot The equipment slot to drop - * @param throwRandomly Whether the item should disperse randomly. - * This means that instead of the item being dropped where the player is currently looking, - * it instead throws it in any direction, similar to how items drop after a player's death. + * @param slot The equipment slot to drop + * @param amount The amount of items to drop from this equipment slot. Values below 1 don't drop an Item * @return The dropped item entity, or null if the action was unsuccessful */ - public @Nullable Item dropItem(final @NotNull EquipmentSlot slot, final boolean throwRandomly); + public @Nullable Item dropItem(@NotNull EquipmentSlot slot, int amount); + /** + * Makes the entity drop an item from their inventory based on the slot. + * Instead of the item being dropped where the player is currently looking, this method makes it drop in + * a random direction, similar to how items are dropped after a player's death. + * + * @param slot The slot to drop + * @param amount The number of items to drop from this slot. Values below 1 don't drop an Item + * @return The dropped item entity, or null if the action was unsuccessful + * @throws IndexOutOfBoundsException If the slot is negative or bigger than the player's inventory + */ + public @Nullable Item dropItemRandomly(int slot, int amount); + + /** + * Makes the player drop an item from their inventory based on the equipment slot. + * Instead of the item being dropped where the player is currently looking, this method makes it drop in + * a random direction, similar to how items are dropped after a player's death. + * + * @param slot The equipment slot to drop + * @param amount The amount of items to drop from this equipment slot. Values below 1 don't drop an Item + * @return The dropped item entity, or null if the action was unsuccessful + */ + public @Nullable Item dropItemRandomly(@NotNull EquipmentSlot slot, int amount); /** * Gets the players current exhaustion level. diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index f954d1b907..1caa3ec08e 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -807,52 +807,53 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { } @Override - public @Nullable Item dropItem(final @NotNull ItemStack itemStack, final boolean throwRandomly) { - final int slot = this.inventory.first(itemStack); - if (slot == -1) { - return null; - } - - return this.dropItem(slot, throwRandomly); - } - - @Override - public @Nullable Item dropItem(final int slot, final boolean throwRandomly) { + public @Nullable Item dropItem(final int slot, final int amount) { // Make sure the slot is in bounds if (slot < 0 || slot >= this.inventory.getSize()) { throw new IndexOutOfBoundsException("Slot " + slot + " out of range for inventory of size " + this.inventory.getSize()); } - final ItemStack stack = this.inventory.getItem(slot); - final Item itemEntity = dropItemRaw(stack, throwRandomly); - - this.inventory.setItem(slot, null); - return itemEntity; + return dropItemRaw(this.inventory.getItem(slot), amount, false); } @Override - public @Nullable Item dropItem(final @NotNull EquipmentSlot slot, final boolean throwRandomly) { - final ItemStack stack = this.inventory.getItem(slot); - final Item itemEntity = dropItemRaw(stack, throwRandomly); - - this.inventory.setItem(slot, null); - return itemEntity; + public @Nullable Item dropItem(final @NotNull EquipmentSlot slot, final int amount) { + return dropItemRaw(this.inventory.getItem(slot), amount, false); } - private Item dropItemRaw(final ItemStack itemStack, final boolean throwRandomly) { - if (itemStack == null || itemStack.isEmpty()) { + @Override + public @Nullable Item dropItemRandomly(final int slot, final int amount) { + // Make sure the slot is in bounds + if (slot < 0 || slot >= this.inventory.getSize()) { + throw new IndexOutOfBoundsException("Slot " + slot + " out of range for inventory of size " + this.inventory.getSize()); + } + + return dropItemRaw(this.inventory.getItem(slot), amount, true); + } + + @Override + public @Nullable Item dropItemRandomly(final @NotNull EquipmentSlot slot, final int amount) { + return dropItemRaw(this.inventory.getItem(slot), amount, true); + } + + private @Nullable Item dropItemRaw(final ItemStack originalItemStack, final int amount, final boolean throwRandomly) { + if (originalItemStack == null || originalItemStack.isEmpty() || amount <= 0) { return null; } - final ItemEntity droppedEntity = this.getHandle().drop(CraftItemStack.asNMSCopy(itemStack), throwRandomly, true); + final ItemStack clonedItemStack = originalItemStack.clone(); + final int droppedAmount = Math.min(clonedItemStack.getAmount(), amount); + clonedItemStack.setAmount(droppedAmount); + + final ItemEntity droppedEntity = this.getHandle().drop(CraftItemStack.asNMSCopy(clonedItemStack), throwRandomly, true); if (droppedEntity == null) { return null; } - return (Item) droppedEntity.getBukkitEntity(); + originalItemStack.setAmount(originalItemStack.getAmount() - droppedAmount); + return new CraftItem(this.server, droppedEntity); } - @Override public float getExhaustion() { return this.getHandle().getFoodData().exhaustionLevel;