From cba01a39b6d922685b7ae762e642ddb846a62071 Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Thu, 31 Mar 2022 20:28:46 +0800 Subject: [PATCH] feat(utils): new PlayerUtils.giveItem() method can control fakeItem and playSound --- .../arnicalib/utils/game/PlayerUtils.java | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/auioc/mods/arnicalib/utils/game/PlayerUtils.java b/src/main/java/org/auioc/mods/arnicalib/utils/game/PlayerUtils.java index 54aee899..b3df2b4d 100644 --- a/src/main/java/org/auioc/mods/arnicalib/utils/game/PlayerUtils.java +++ b/src/main/java/org/auioc/mods/arnicalib/utils/game/PlayerUtils.java @@ -3,6 +3,8 @@ import javax.annotation.Nullable; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; @@ -11,35 +13,55 @@ public interface PlayerUtils { - static void giveItem(ServerPlayer player, Item item, int count, @Nullable CompoundTag nbt) { - int i = count; - while (i > 0) { - int j = Math.min(ItemUtils.getMaxStackSize(item), i); - i -= j; - ItemStack itemStack = ItemUtils.createItemStack(item, j, nbt); - boolean flag = player.getInventory().add(itemStack); - ItemEntity itementity = player.drop(itemStack, false); - if (flag && itemStack.isEmpty()) { - itemStack.setCount(1); - if (itementity != null) { - itementity.makeFakeItem(); + + /** + * Gives the specified item to the specified player. + * If the player's inventory can not hold it, the itemstack(s) will be dropped in the world at the player's position. + * + * @param player the player to give the item to + * @param item the item to give + * @param count the count of the item + * @param nbt the nbt of the item + * @param fakeItem if {@code true}, will generate a fake {@link ItemEntity} at the player's position on success + * @param playSound if {@code true}, will play the {@link SoundEvents#ITEM_PICKUP} at the player's position on success + * @since 5.1.4 + */ + static void giveItem(ServerPlayer player, Item item, int count, @Nullable CompoundTag nbt, boolean fakeItem, boolean playSound) { + int maxStackSize = ItemUtils.getMaxStackSize(item); + int remainingCount = count; + while (remainingCount > 0) { + int stackSize = Math.min(maxStackSize, remainingCount); + remainingCount -= stackSize; + var itemStack = ItemUtils.createItemStack(item, stackSize, nbt); + if (player.getInventory().add(itemStack) && itemStack.isEmpty()) { + if (fakeItem) { + itemStack.setCount(1); + var itemEntity = player.drop(itemStack, false); + if (itemEntity != null) itemEntity.makeFakeItem(); + } + if (playSound) { + player.level.playSound( + (Player) null, player.getX(), player.getY(), player.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, + ((player.getRandom().nextFloat() - player.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F + ); } player.inventoryMenu.broadcastChanges(); } else { - if (itementity != null) { - itementity.setNoPickUpDelay(); - itementity.setOwner(player.getUUID()); + var itemEntity = player.drop(itemStack, false); + if (itemEntity != null) { + itemEntity.setNoPickUpDelay(); + itemEntity.setOwner(player.getUUID()); } } } } static void giveItem(ServerPlayer player, Item item) { - giveItem(player, item, 1, null); + giveItem(player, item, 1, null, true, true); } static void giveItem(ServerPlayer player, ItemStack itemStack) { - giveItem(player, itemStack.getItem(), itemStack.getCount(), itemStack.getTag()); + giveItem(player, itemStack.getItem(), itemStack.getCount(), itemStack.getTag(), true, true); } static String toString(Player player) {