Skip to content

Commit

Permalink
Add Player#give
Browse files Browse the repository at this point in the history
  • Loading branch information
masmc05 committed Jan 21, 2025
1 parent d69981b commit ddd3e76
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
35 changes: 35 additions & 0 deletions paper-api/src/main/java/org/bukkit/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.Instant;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -3892,4 +3893,38 @@ default boolean isChunkSent(@NotNull org.bukkit.Chunk chunk) {
*/
void sendEntityEffect(org.bukkit.@NotNull EntityEffect effect, @NotNull Entity target);
// Paper end - entity effect API

/**
* Gives the player the items following full vanilla logic,
* making the player drop the items that did not fit into
* the inventory. This method will update the count of items
* to the amount that did not fit in the inventory.
*
* @param items the items to give, their count will be mutated
* @return spawned item entities
*/
@NotNull List<@NotNull Item> give(@NotNull ItemStack @NotNull... items);

/**
* Gives the player those items following full vanilla logic,
* making the player drop the items that did not fit into
* the inventory. This method will update the count of items
* to the amount that did not fit in the inventory.
*
* @param items the items to give, their count will be mutated
* @return spawned item entities
*/
@NotNull List<@NotNull Item> give(@NotNull Collection<@NotNull ItemStack> items);

/**
* Gives the player those items following full vanilla logic.
* This method will update the count of items to the amount that
* did not fit in the inventory.
*
* @param items the items to give, their count will be mutated
* @param dropIfFull whether the player should drop items that
* did not fit the inventory
* @return spawned item entities, always empty if dropIfFull is false
*/
@NotNull List<@NotNull Item> give(@NotNull Collection<@NotNull ItemStack> items, boolean dropIfFull);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.entity;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.BaseEncoding;
import com.mojang.authlib.GameProfile;
Expand Down Expand Up @@ -175,6 +176,7 @@
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.EnderPearl;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent;
Expand Down Expand Up @@ -3541,4 +3543,35 @@ public void sendEntityEffect(final org.bukkit.EntityEffect effect, final org.buk
this.getHandle().connection.send(new net.minecraft.network.protocol.game.ClientboundEntityEventPacket(((CraftEntity) target).getHandle(), effect.getData()));
}
// Paper end - entity effect API


@Override
public @NotNull List<@NotNull Item> give(final @NotNull ItemStack @NotNull ... items) {
return this.give(List.of(items), true);
}

@Override
public @NotNull List<@NotNull Item> give(@NotNull final Collection<@NotNull ItemStack> items) {
return this.give(items, true);
}

@Override
public @NotNull List<@NotNull Item> give(@NotNull final Collection<@NotNull ItemStack> items, final boolean dropIfFull) {
Preconditions.checkNotNull(items, "items cannot be null");
ImmutableList.Builder<Item> droppedItems = ImmutableList.builder();
for (ItemStack item : items) {
Preconditions.checkNotNull(item, "ItemStack cannot be null");
Preconditions.checkArgument(!item.isEmpty(), "ItemStack cannot be empty");
Preconditions.checkArgument(item.getAmount() <= item.getMaxStackSize(), "ItemStack amount cannot be greater than its max stack size");
boolean added = this.getHandle().getInventory().add(CraftItemStack.unwrap(item));
if (!dropIfFull || (added && item.isEmpty())) {
continue;
}
Item entity = this.dropItem(item);
if (entity != null) {
droppedItems.add(entity);
}
}
return droppedItems.build();
}
}

0 comments on commit ddd3e76

Please sign in to comment.