mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-27 06:50:12 +01:00
Refactor methods
This commit is contained in:
parent
2d9e357a45
commit
02afac7fea
2 changed files with 58 additions and 51 deletions
|
@ -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.
|
||||
* <br>
|
||||
* 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.
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue