-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 #11810
base: main
Are you sure you want to change the base?
Extend HumanEntity#dropItem API #11810
Conversation
All of the default methods that are single-line redirects probably make it harder to parse than they help, I'd reduce some or even all of them, not sure. If you do want to keep default helpers, they should be moved as proper |
Oh that's smart, I hadn't though about that. Thanks for that suggestion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you can remove the Paper comments and add imports instead of fqn
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
Are this many overloads really needed? |
I think removing the thrower parameter would already decrease the method amount by a fair bit |
I feel like that is too complicated and would kinda defeat its purpose.
That feels weird, as dropping an item sounds like you actually don't have it in your inventory anymore. Giving the developer the task of removing it themselves feels like a fair bit of unneeded code in this context. |
I mean, it would not remove a single method, as there is also That sounds like a good middle-ground solution to me. If the developer wants to set the thrower they can do so. That means it would turn to only these overloads:
|
It's similar to already existing methods (e.g. World#dropItem), entity spawning methods have the save overloads.
No, I mean to literally not link the dropped itemstack to anything, i.e. the drop would simply drop the cloned item no matter its source, not remove it from inventory or anything else. Then you just get the item in that slot and decrease its amount. |
It's not entirely the same though. @Override
public org.bukkit.entity.Item dropItem(Location loc, ItemStack item, Consumer<? super org.bukkit.entity.Item> function) {
Preconditions.checkArgument(loc != null, "Location cannot be null");
Preconditions.checkArgument(item != null, "ItemStack cannot be null");
ItemEntity entity = new ItemEntity(this.world, loc.getX(), loc.getY(), loc.getZ(), CraftItemStack.asNMSCopy(item));
org.bukkit.entity.Item itemEntity = (org.bukkit.entity.Item) entity.getBukkitEntity();
entity.pickupDelay = 10;
if (function != null) {
function.accept(itemEntity);
}
this.world.addFreshEntity(entity, SpawnReason.CUSTOM);
return itemEntity;
} Notice the In my implementation, I directly call |
You can add overload to Edit: by the way, since it calls Player#drop, might be useful to mention in javadoc that it'll cause PlayerDropItemEvent? |
It'd honestly rather not change NMS code without a valid reason. Could add the JavaDocs information though |
Should be fine, it's done all the time and is a small change. I'd argue the ability to drop any item, not linked to inventory would be more useful and cover more cases. You can already get an item by int/equipment slot and decrease its amount alongside if you need that. |
I suggest that it is not yet merged. After some discussion, I'd like to change it one last time |
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
Alright, I've done the following changes now:
|
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java
Outdated
Show resolved
Hide resolved
I genuinely do not see the benefit of doing this, as opposed to: Code sample// Get any item you want
ItemStack item = player.getInventory().getItem(EquipmentSlot.HAND);
ItemStack item = ItemStack.of(Material.GOWSTONE);
ItemStack item = // some other source
// Drop it, provide any amount with `item.asQuantity(amount)`
// Additionally with optional overload to apply data before drop event & item spawn occurred
player.dropItem(item, item -> {
item.setGlowing(true);
});
// Subtract its amount if needed (this will decrease item's amount in inventory)
item.subtract(item.getAmount()); The difference would literally be a one-liner (subtract is done behind the scenes instead of manually at the cost of additional clutter), but with more control and wouldn't need deprecating |
Closes #11656
Post-softspoon version of #11689
Motive
Developers currently have to either use NMS or copy-paste the already existing NMS implementation. This PR aims to fix that by exposing ways to call that NMS implementation more easily by extending onto the (Human)Player#dropItem method to provide more extensive ways of making the player drop items from their inventory.
What does it add?
The following overloads to the
HumanPlayer#dropItem(...)
method have been added:Item dropItem(int slot, int amount)
Item dropItem(EquipmentSlot slot, int amount)
Additionally, the following methods were introduced:
Item dropItemRandomly(int slot, int amount)
Item dropItemRandomly(EquipmentSlot slot, int amount)
Furthermore,
booleam dropItem(boolean)
has been deprecated in favor ofItem dropItem(EquipmentSlot.HAND, amount)
.Implementation details
Internally, the implementation calls
net.minecraft.world.entity.player.Player#dropItem(...)
, making it a fairly maintainable API.JavaDocs status
JavaDocs have been added.
Usage example