Skip to content

Commit

Permalink
feat(utils): new PlayerUtils.giveItem() method can control fakeItem a…
Browse files Browse the repository at this point in the history
…nd playSound
  • Loading branch information
WakelessSloth56 committed Mar 31, 2022
1 parent d50b272 commit cba01a3
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions src/main/java/org/auioc/mods/arnicalib/utils/game/PlayerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit cba01a3

Please sign in to comment.