Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend HumanEntity#dropItem API #11689

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions patches/api/0501-Extend-HumanEntity-dropItem-API.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Strokkur24 <strokkur24@gmail.com>
Date: Sat, 30 Nov 2024 11:54:38 +0100
Subject: [PATCH] Extend HumanEntity#dropItem API


diff --git a/src/main/java/org/bukkit/entity/HumanEntity.java b/src/main/java/org/bukkit/entity/HumanEntity.java
index 488604ba1a516b477693877c74712e4a45624a8b..4b931431695c8258ed2c8b7e783e243241160442 100644
--- a/src/main/java/org/bukkit/entity/HumanEntity.java
+++ b/src/main/java/org/bukkit/entity/HumanEntity.java
@@ -706,6 +706,97 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, InventoryHolder
*/
public boolean dropItem(boolean dropAll);

+ // Paper start - Extend HumanEntity#dropItem API
+ /**
+ * Makes the entity drop an item from their inventory.
+ * <br>
+ * This method calls {@link HumanEntity#dropItem(int slot)}
+ * with the first {@link ItemStack} occurrence in the inventory
+ *
+ * @param itemStack The ItemStack to drop
+ * @return The dropped item, or null if the action was unsuccessful
+ */
+ public Item dropItem(final ItemStack itemStack);
+
+ /**
+ * Makes the entity drop an item from their inventory.
+ * <br>
+ * This method calls {@link HumanEntity#dropItem(int slot, boolean makeThrower)}
+ * with the first {@link ItemStack} occurrence in the inventory
+ *
+ * @param itemStack The ItemStack to drop from their inventory
+ * @param makeThrower Whether the resulting {@link Item} should have its thrower set to this {@link HumanEntity}
Strokkur424 marked this conversation as resolved.
Show resolved Hide resolved
+ * @return The dropped item, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final ItemStack itemStack, final boolean makeThrower);
+
+ /**
+ * Makes the entity drop an item from their inventory based on the specified ItemStack.
+ * <br>
+ * This method calls {@link HumanEntity#dropItem(int slot, boolean makeThrower, boolean throwRandomly)}
+ * with the first {@link ItemStack} occurrence in the inventory
+ *
+ * @param itemStack The ItemStack to drop
+ * @param makeThrower Whether the resulting {@link Item} should have its thrower set to this {@link HumanEntity}
+ * @param throwRandomly Whether the item should disperse randomly
+ * @return The dropped item, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final ItemStack itemStack, final boolean makeThrower, final boolean throwRandomly);
+
+ /**
+ * Makes the entity drop an item from their inventory based on the slot.
+ *
+ * @param slot The slot to drop
+ * @return The dropped item, or null if the action was unsuccessful
+ */
+ public Item dropItem(final int slot);
+
+ /**
+ * Makes the entity drop an item from their inventory based on the slot.
+ *
+ * @param slot The slot to drop
+ * @param makeThrower Whether the resulting {@link Item} should have its thrower set to this {@link HumanEntity}
+ * @return The dropped item, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final int slot, final boolean makeThrower);
+
+ /**
+ * Makes the entity drop an item from their inventory based on the slot.
+ *
+ * @param slot The slot to drop
+ * @param makeThrower Whether the resulting {@link Item} should have its thrower set to this {@link HumanEntity}
+ * @param throwRandomly Whether the item should disperse randomly
+ * @return The dropped item entity, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final int slot, final boolean makeThrower, final boolean throwRandomly);
+
+ /**
+ * Makes the entity drop an item from their inventory based on the {@link org.bukkit.inventory.EquipmentSlot}
+ *
+ * @param slot The equipment slot to drop
+ * @return The dropped item entity, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final org.bukkit.inventory.EquipmentSlot slot);
+ /**
+ * Makes the entity drop an item from their inventory based on the {@link org.bukkit.inventory.EquipmentSlot}
+ *
+ * @param slot The equipment slot to drop
+ * @param makeThrower Whether the resulting {@link Item} should have its thrower set to this {@link HumanEntity}
+ * @return The dropped item entity, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final org.bukkit.inventory.EquipmentSlot slot, final boolean makeThrower);
+
+ /**
+ * Makes the entity drop an item from their inventory based on the equipment slot.
+ *
+ * @param slot The equipment slot to drop
+ * @param makeThrower Whether the resulting {@link Item} should have its thrower set to this {@link HumanEntity}
+ * @param throwRandomly Whether the item should disperse randomly
+ * @return The dropped item entity, or null if the action was unsuccessful
+ */
+ public @Nullable Item dropItem(final org.bukkit.inventory.EquipmentSlot slot, final boolean makeThrower, final boolean throwRandomly);
+ // Paper end
+
/**
* Gets the players current exhaustion level.
* <p>
94 changes: 94 additions & 0 deletions patches/server/1073-Extend-HumanEntity-dropItem-API.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Strokkur24 <strokkur24@gmail.com>
Date: Sat, 30 Nov 2024 11:54:38 +0100
Subject: [PATCH] Extend HumanEntity#dropItem API


diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
index e345cdbfab44a0f5da80d738798dbb4424b7ab5c..e23ed42fc81a27eb619f4ee8bae704d8bf6a5115 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
@@ -801,6 +801,84 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
// Paper end - Fix HumanEntity#drop not updating the client inv
}

+ // Paper start - Extend HumanEntity#dropItem API
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final ItemStack itemStack) {
+ return this.dropItem(itemStack, false, false);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final ItemStack itemStack, final boolean makeThrower) {
+ return this.dropItem(itemStack, makeThrower, false);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final ItemStack itemStack, final boolean makeThrower, final boolean throwRandomly) {
+ return this.dropItem(this.inventory.first(itemStack), makeThrower, throwRandomly);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final int slot) {
+ return this.dropItem(slot, false, false);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final int slot, final boolean makeThrower) {
+ return this.dropItem(slot, makeThrower, false);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final int slot, final boolean makeThrower, final boolean throwRandomly) {
+ // Make sure the slot is in bounds
+ if (slot < 0 || this.inventory.getSize() <= slot) {
Strokkur424 marked this conversation as resolved.
Show resolved Hide resolved
+ return null;
+ }
+
+ final ItemStack stack = this.inventory.getItem(slot);
+ final org.bukkit.entity.Item itemEntity = dropItemRaw(stack, makeThrower, throwRandomly);
+
+ this.inventory.setItem(slot, null);
+ return itemEntity;
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final org.bukkit.inventory.EquipmentSlot slot) {
+ return dropItem(slot, false, false);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final org.bukkit.inventory.EquipmentSlot slot, final boolean makeThrower) {
+ return dropItem(slot, makeThrower, false);
+ }
+
+ @Override
+ public @org.jetbrains.annotations.Nullable org.bukkit.entity.Item dropItem(final org.bukkit.inventory.EquipmentSlot slot, final boolean makeThrower, final boolean throwRandomly) {
+ final ItemStack stack = this.inventory.getItem(slot);
+ final org.bukkit.entity.Item itemEntity = dropItemRaw(stack, makeThrower, throwRandomly);
+
+ this.inventory.setItem(slot, null);
+ return itemEntity;
+ }
+
+ private org.bukkit.entity.Item dropItemRaw(final ItemStack is, final boolean makeThrower, final boolean throwRandomly) {
+ if (!(this.getHandle() instanceof ServerPlayer player)) {
Strokkur424 marked this conversation as resolved.
Show resolved Hide resolved
+ return null;
+ }
+
+ if (is == null || is.getType() == Material.AIR) {
Strokkur424 marked this conversation as resolved.
Show resolved Hide resolved
+ return null;
+ }
+
+ final net.minecraft.world.entity.item.ItemEntity droppedEntity = player.drop(CraftItemStack.asNMSCopy(is), makeThrower, throwRandomly);
+ if (droppedEntity == null) {
+ return null;
+ }
+
+ return (org.bukkit.entity.Item) droppedEntity.getBukkitEntity();
+ }
+ // Paper end
+
@Override
public float getExhaustion() {
return this.getHandle().getFoodData().exhaustionLevel;