From ddd3e7600a11d9d5a219224b2c22bc1e49340004 Mon Sep 17 00:00:00 2001 From: masmc05 Date: Tue, 21 Jan 2025 12:17:00 +0100 Subject: [PATCH] Add Player#give --- .../main/java/org/bukkit/entity/Player.java | 35 +++++++++++++++++++ .../craftbukkit/entity/CraftPlayer.java | 33 +++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/paper-api/src/main/java/org/bukkit/entity/Player.java b/paper-api/src/main/java/org/bukkit/entity/Player.java index 7d21ee64c9b9..ef2eefcc5c7f 100644 --- a/paper-api/src/main/java/org/bukkit/entity/Player.java +++ b/paper-api/src/main/java/org/bukkit/entity/Player.java @@ -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; @@ -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); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 98fc89cc7a71..db232e07b17e 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -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; @@ -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; @@ -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 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(); + } }