From 4acc8341d0bfb41c165aafb6ca83f2a2f8321c09 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Mon, 25 Jan 2021 17:32:56 +0800 Subject: [PATCH] Add ItemStackHooks --- .../architectury/hooks/ItemStackHooks.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 common/src/main/java/me/shedaniel/architectury/hooks/ItemStackHooks.java diff --git a/common/src/main/java/me/shedaniel/architectury/hooks/ItemStackHooks.java b/common/src/main/java/me/shedaniel/architectury/hooks/ItemStackHooks.java new file mode 100644 index 00000000..c6755140 --- /dev/null +++ b/common/src/main/java/me/shedaniel/architectury/hooks/ItemStackHooks.java @@ -0,0 +1,56 @@ +/* + * This file is part of architectury. + * Copyright (C) 2020, 2021 shedaniel + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package me.shedaniel.architectury.hooks; + +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.item.ItemStack; + +public final class ItemStackHooks { + private ItemStackHooks() {} + + public static ItemStack copyWithCount(ItemStack stack, int count) { + ItemStack copy = stack.copy(); + copy.setCount(count); + return copy; + } + + public static void giveItem(ServerPlayer player, ItemStack stack) { + boolean bl = player.inventory.add(stack); + if (bl && stack.isEmpty()) { + stack.setCount(1); + ItemEntity entity = player.drop(stack, false); + if (entity != null) { + entity.makeFakeItem(); + } + + player.level.playSound(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 { + ItemEntity entity = player.drop(stack, false); + if (entity != null) { + entity.setNoPickUpDelay(); + entity.setOwner(player.getUUID()); + } + } + } +}