mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-23 21:49:46 +00:00
1.20.6 Initial Update
New Decompiler Runtime Official mappings ItemStacks arnt Capability providers anymore, use Vanilla's system. Co-authored-by: RealMangoRage <[email protected]> Co-authored-by: Paint_Ninja <[email protected]>
This commit is contained in:
1114 files changed
+24245
-28536
No files matched your search
@@ -16,19 +16,15 @@ import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ItemHandlerHelper
|
||||
{
|
||||
public class ItemHandlerHelper {
|
||||
@NotNull
|
||||
public static ItemStack insertItem(IItemHandler dest, @NotNull ItemStack stack, boolean simulate)
|
||||
{
|
||||
public static ItemStack insertItem(IItemHandler dest, @NotNull ItemStack stack, boolean simulate) {
|
||||
if (dest == null || stack.isEmpty())
|
||||
return stack;
|
||||
|
||||
for (int i = 0; i < dest.getSlots(); i++)
|
||||
{
|
||||
for (int i = 0; i < dest.getSlots(); i++) {
|
||||
stack = dest.insertItem(i, stack, simulate);
|
||||
if (stack.isEmpty())
|
||||
{
|
||||
if (stack.isEmpty()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
@@ -36,42 +32,12 @@ public class ItemHandlerHelper
|
||||
return stack;
|
||||
}
|
||||
|
||||
public static boolean canItemStacksStack(@NotNull ItemStack a, @NotNull ItemStack b)
|
||||
{
|
||||
if (a.isEmpty() || !ItemStack.isSameItem(a, b) || a.hasTag() != b.hasTag())
|
||||
return false;
|
||||
|
||||
return (!a.hasTag() || a.getTag().equals(b.getTag())) && a.areCapsCompatible(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* A relaxed version of canItemStacksStack that stacks itemstacks with different metadata if they don't have subtypes.
|
||||
* This usually only applies when players pick up items.
|
||||
*/
|
||||
public static boolean canItemStacksStackRelaxed(@NotNull ItemStack a, @NotNull ItemStack b)
|
||||
{
|
||||
if (a.isEmpty() || b.isEmpty() || a.getItem() != b.getItem())
|
||||
return false;
|
||||
|
||||
if (!a.isStackable())
|
||||
return false;
|
||||
|
||||
// Metadata value only matters when the item has subtypes
|
||||
// Vanilla stacks non-subtype items with different metadata together
|
||||
// TODO Item subtypes, is this still necessary?
|
||||
/* e.g. a stick with metadata 0 and a stick with metadata 1 stack
|
||||
if (a.getHasSubtypes() && a.getMetadata() != b.getMetadata())
|
||||
return false;
|
||||
*/
|
||||
if (a.hasTag() != b.hasTag())
|
||||
return false;
|
||||
|
||||
return (!a.hasTag() || a.getTag().equals(b.getTag())) && a.areCapsCompatible(b);
|
||||
public static boolean canItemStacksStack(@NotNull ItemStack a, @NotNull ItemStack b) {
|
||||
return ItemStack.isSameItemSameComponents(a, b);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ItemStack copyStackWithSize(@NotNull ItemStack itemStack, int size)
|
||||
{
|
||||
public static ItemStack copyStackWithSize(@NotNull ItemStack itemStack, int size) {
|
||||
if (size == 0)
|
||||
return ItemStack.EMPTY;
|
||||
ItemStack copy = itemStack.copy();
|
||||
@@ -85,45 +51,36 @@ public class ItemHandlerHelper
|
||||
* Note: This function stacks items without subtypes with different metadata together.
|
||||
*/
|
||||
@NotNull
|
||||
public static ItemStack insertItemStacked(IItemHandler inventory, @NotNull ItemStack stack, boolean simulate)
|
||||
{
|
||||
public static ItemStack insertItemStacked(IItemHandler inventory, @NotNull ItemStack stack, boolean simulate) {
|
||||
if (inventory == null || stack.isEmpty())
|
||||
return stack;
|
||||
|
||||
// not stackable -> just insert into a new slot
|
||||
if (!stack.isStackable())
|
||||
{
|
||||
if (!stack.isStackable()) {
|
||||
return insertItem(inventory, stack, simulate);
|
||||
}
|
||||
|
||||
int sizeInventory = inventory.getSlots();
|
||||
|
||||
// go through the inventory and try to fill up already existing items
|
||||
for (int i = 0; i < sizeInventory; i++)
|
||||
{
|
||||
for (int i = 0; i < sizeInventory; i++) {
|
||||
ItemStack slot = inventory.getStackInSlot(i);
|
||||
if (canItemStacksStackRelaxed(slot, stack))
|
||||
{
|
||||
if (canItemStacksStack(slot, stack)) {
|
||||
stack = inventory.insertItem(i, stack, simulate);
|
||||
|
||||
if (stack.isEmpty())
|
||||
{
|
||||
if (stack.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// insert remainder into empty slots
|
||||
if (!stack.isEmpty())
|
||||
{
|
||||
if (!stack.isEmpty()) {
|
||||
// find empty slot
|
||||
for (int i = 0; i < sizeInventory; i++)
|
||||
{
|
||||
if (inventory.getStackInSlot(i).isEmpty())
|
||||
{
|
||||
for (int i = 0; i < sizeInventory; i++) {
|
||||
if (inventory.getStackInSlot(i).isEmpty()) {
|
||||
stack = inventory.insertItem(i, stack, simulate);
|
||||
if (stack.isEmpty())
|
||||
{
|
||||
if (stack.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -145,8 +102,7 @@ public class ItemHandlerHelper
|
||||
* @param player The player to give the item to
|
||||
* @param stack The itemstack to insert
|
||||
*/
|
||||
public static void giveItemToPlayer(Player player, @NotNull ItemStack stack, int preferredSlot)
|
||||
{
|
||||
public static void giveItemToPlayer(Player player, @NotNull ItemStack stack, int preferredSlot) {
|
||||
if (stack.isEmpty()) return;
|
||||
|
||||
IItemHandler inventory = new PlayerMainInvWrapper(player.getInventory());
|
||||
@@ -155,26 +111,23 @@ public class ItemHandlerHelper
|
||||
// try adding it into the inventory
|
||||
ItemStack remainder = stack;
|
||||
// insert into preferred slot first
|
||||
if (preferredSlot >= 0 && preferredSlot < inventory.getSlots())
|
||||
{
|
||||
if (preferredSlot >= 0 && preferredSlot < inventory.getSlots()) {
|
||||
remainder = inventory.insertItem(preferredSlot, stack, false);
|
||||
}
|
||||
|
||||
// then into the inventory in general
|
||||
if (!remainder.isEmpty())
|
||||
{
|
||||
if (!remainder.isEmpty()) {
|
||||
remainder = insertItemStacked(inventory, remainder, false);
|
||||
}
|
||||
|
||||
// play sound if something got picked up
|
||||
if (remainder.isEmpty() || remainder.getCount() != stack.getCount())
|
||||
{
|
||||
if (remainder.isEmpty() || remainder.getCount() != stack.getCount()) {
|
||||
level.playSound(null, player.getX(), player.getY() + 0.5, player.getZ(),
|
||||
SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, ((level.random.nextFloat() - level.random.nextFloat()) * 0.7F + 1.0F) * 2.0F);
|
||||
}
|
||||
|
||||
// drop remaining itemstack into the level
|
||||
if (!remainder.isEmpty() && !level.isClientSide)
|
||||
{
|
||||
if (!remainder.isEmpty() && !level.isClientSide) {
|
||||
ItemEntity entityitem = new ItemEntity(level, player.getX(), player.getY() + 0.5, player.getZ(), remainder);
|
||||
entityitem.setPickUpDelay(40);
|
||||
entityitem.setDeltaMovement(entityitem.getDeltaMovement().multiply(0, 1, 0));
|
||||
@@ -189,23 +142,17 @@ public class ItemHandlerHelper
|
||||
* @param inv The inventory handler to test.
|
||||
* @return A redstone value in the range [0,15] representing how "full" this inventory is.
|
||||
*/
|
||||
public static int calcRedstoneFromInventory(@Nullable IItemHandler inv)
|
||||
{
|
||||
if (inv == null)
|
||||
{
|
||||
public static int calcRedstoneFromInventory(@Nullable IItemHandler inv) {
|
||||
if (inv == null) {
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
int itemsFound = 0;
|
||||
float proportion = 0.0F;
|
||||
|
||||
for (int j = 0; j < inv.getSlots(); ++j)
|
||||
{
|
||||
for (int j = 0; j < inv.getSlots(); ++j) {
|
||||
ItemStack itemstack = inv.getStackInSlot(j);
|
||||
|
||||
if (!itemstack.isEmpty())
|
||||
{
|
||||
if (!itemstack.isEmpty()) {
|
||||
proportion += (float)itemstack.getCount() / (float)Math.min(inv.getSlotLimit(j), itemstack.getMaxStackSize());
|
||||
++itemsFound;
|
||||
}
|
||||
|
||||
@@ -5,64 +5,55 @@
|
||||
|
||||
package net.minecraftforge.items;
|
||||
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.world.ContainerHelper;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraftforge.common.util.INBTSerializable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, INBTSerializable<CompoundTag>
|
||||
{
|
||||
public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, INBTSerializable<CompoundTag> {
|
||||
protected NonNullList<ItemStack> stacks;
|
||||
|
||||
public ItemStackHandler()
|
||||
{
|
||||
public ItemStackHandler() {
|
||||
this(1);
|
||||
}
|
||||
|
||||
public ItemStackHandler(int size)
|
||||
{
|
||||
public ItemStackHandler(int size) {
|
||||
stacks = NonNullList.withSize(size, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
public ItemStackHandler(NonNullList<ItemStack> stacks)
|
||||
{
|
||||
public ItemStackHandler(NonNullList<ItemStack> stacks) {
|
||||
this.stacks = stacks;
|
||||
}
|
||||
|
||||
public void setSize(int size)
|
||||
{
|
||||
public void setSize(int size) {
|
||||
stacks = NonNullList.withSize(size, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(int slot, @NotNull ItemStack stack)
|
||||
{
|
||||
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
|
||||
validateSlotIndex(slot);
|
||||
this.stacks.set(slot, stack);
|
||||
onContentsChanged(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots()
|
||||
{
|
||||
public int getSlots() {
|
||||
return stacks.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack getStackInSlot(int slot)
|
||||
{
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
validateSlotIndex(slot);
|
||||
return this.stacks.get(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate)
|
||||
{
|
||||
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
|
||||
if (stack.isEmpty())
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
@@ -75,8 +66,7 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
|
||||
|
||||
int limit = getStackLimit(slot, stack);
|
||||
|
||||
if (!existing.isEmpty())
|
||||
{
|
||||
if (!existing.isEmpty()) {
|
||||
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
|
||||
return stack;
|
||||
|
||||
@@ -88,14 +78,10 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
|
||||
|
||||
boolean reachedLimit = stack.getCount() > limit;
|
||||
|
||||
if (!simulate)
|
||||
{
|
||||
if (existing.isEmpty())
|
||||
{
|
||||
if (!simulate) {
|
||||
if (existing.isEmpty()) {
|
||||
this.stacks.set(slot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
existing.grow(reachedLimit ? limit : stack.getCount());
|
||||
}
|
||||
onContentsChanged(slot);
|
||||
@@ -106,8 +92,7 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
||||
{
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
if (amount == 0)
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
@@ -120,23 +105,16 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
|
||||
|
||||
int toExtract = Math.min(amount, existing.getMaxStackSize());
|
||||
|
||||
if (existing.getCount() <= toExtract)
|
||||
{
|
||||
if (!simulate)
|
||||
{
|
||||
if (existing.getCount() <= toExtract) {
|
||||
if (!simulate) {
|
||||
this.stacks.set(slot, ItemStack.EMPTY);
|
||||
onContentsChanged(slot);
|
||||
return existing;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return existing.copy();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!simulate)
|
||||
{
|
||||
} else {
|
||||
if (!simulate) {
|
||||
this.stacks.set(slot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
|
||||
onContentsChanged(slot);
|
||||
}
|
||||
@@ -146,73 +124,40 @@ public class ItemStackHandler implements IItemHandler, IItemHandlerModifiable, I
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(int slot)
|
||||
{
|
||||
public int getSlotLimit(int slot) {
|
||||
return 64;
|
||||
}
|
||||
|
||||
protected int getStackLimit(int slot, @NotNull ItemStack stack)
|
||||
{
|
||||
protected int getStackLimit(int slot, @NotNull ItemStack stack) {
|
||||
return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(int slot, @NotNull ItemStack stack)
|
||||
{
|
||||
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag serializeNBT()
|
||||
{
|
||||
ListTag nbtTagList = new ListTag();
|
||||
for (int i = 0; i < stacks.size(); i++)
|
||||
{
|
||||
if (!stacks.get(i).isEmpty())
|
||||
{
|
||||
CompoundTag itemTag = new CompoundTag();
|
||||
itemTag.putInt("Slot", i);
|
||||
stacks.get(i).save(itemTag);
|
||||
nbtTagList.add(itemTag);
|
||||
}
|
||||
}
|
||||
CompoundTag nbt = new CompoundTag();
|
||||
nbt.put("Items", nbtTagList);
|
||||
nbt.putInt("Size", stacks.size());
|
||||
return nbt;
|
||||
public CompoundTag serializeNBT(HolderLookup.Provider lookup) {
|
||||
var tag = new CompoundTag();
|
||||
ContainerHelper.saveAllItems(tag, this.stacks, lookup);
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserializeNBT(CompoundTag nbt)
|
||||
{
|
||||
setSize(nbt.contains("Size", Tag.TAG_INT) ? nbt.getInt("Size") : stacks.size());
|
||||
ListTag tagList = nbt.getList("Items", Tag.TAG_COMPOUND);
|
||||
for (int i = 0; i < tagList.size(); i++)
|
||||
{
|
||||
CompoundTag itemTags = tagList.getCompound(i);
|
||||
int slot = itemTags.getInt("Slot");
|
||||
|
||||
if (slot >= 0 && slot < stacks.size())
|
||||
{
|
||||
stacks.set(slot, ItemStack.of(itemTags));
|
||||
}
|
||||
}
|
||||
public void deserializeNBT(HolderLookup.Provider lookup, CompoundTag nbt) {
|
||||
ContainerHelper.loadAllItems(nbt, stacks, lookup);
|
||||
onLoad();
|
||||
}
|
||||
|
||||
protected void validateSlotIndex(int slot)
|
||||
{
|
||||
protected void validateSlotIndex(int slot) {
|
||||
if (slot < 0 || slot >= stacks.size())
|
||||
throw new RuntimeException("Slot " + slot + " not in valid range - [0," + stacks.size() + ")");
|
||||
}
|
||||
|
||||
protected void onLoad()
|
||||
{
|
||||
|
||||
protected void onLoad() {
|
||||
}
|
||||
|
||||
protected void onContentsChanged(int slot)
|
||||
{
|
||||
|
||||
protected void onContentsChanged(int slot) {
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,12 @@
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
/*
|
||||
package net.minecraftforge.items.wrapper;
|
||||
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.core.component.DataComponents;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.ContainerHelper;
|
||||
@@ -14,6 +16,7 @@ import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.item.component.CustomData;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
||||
@@ -30,12 +33,10 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapabilityProvider
|
||||
{
|
||||
public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapabilityProvider {
|
||||
@ApiStatus.Internal
|
||||
@Nullable
|
||||
public static ICapabilityProvider createDefaultProvider(ItemStack itemStack)
|
||||
{
|
||||
public static ICapabilityProvider createDefaultProvider(ItemStack itemStack) {
|
||||
var item = itemStack.getItem();
|
||||
if (item == Items.SHULKER_BOX ||
|
||||
item == Items.BLACK_SHULKER_BOX ||
|
||||
@@ -62,32 +63,28 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
private final ItemStack stack;
|
||||
private final LazyOptional<IItemHandler> holder = LazyOptional.of(() -> this);
|
||||
|
||||
private CompoundTag cachedTag;
|
||||
private CustomData cachedData;
|
||||
private NonNullList<ItemStack> itemStacksCache;
|
||||
|
||||
private ShulkerItemStackInvWrapper(ItemStack stack)
|
||||
{
|
||||
private ShulkerItemStackInvWrapper(ItemStack stack) {
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots()
|
||||
{
|
||||
public int getSlots() {
|
||||
return 27;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack getStackInSlot(int slot)
|
||||
{
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
validateSlotIndex(slot);
|
||||
return getItemList().get(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate)
|
||||
{
|
||||
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
|
||||
if (stack.isEmpty())
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
@@ -102,8 +99,7 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
|
||||
int limit = Math.min(getSlotLimit(slot), stack.getMaxStackSize());
|
||||
|
||||
if (!existing.isEmpty())
|
||||
{
|
||||
if (!existing.isEmpty()) {
|
||||
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
|
||||
return stack;
|
||||
|
||||
@@ -115,14 +111,10 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
|
||||
boolean reachedLimit = stack.getCount() > limit;
|
||||
|
||||
if (!simulate)
|
||||
{
|
||||
if (existing.isEmpty())
|
||||
{
|
||||
if (!simulate) {
|
||||
if (existing.isEmpty()) {
|
||||
itemStacks.set(slot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
existing.grow(reachedLimit ? limit : stack.getCount());
|
||||
}
|
||||
setItemList(itemStacks);
|
||||
@@ -133,8 +125,7 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate)
|
||||
{
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
NonNullList<ItemStack> itemStacks = getItemList();
|
||||
if (amount == 0)
|
||||
return ItemStack.EMPTY;
|
||||
@@ -148,23 +139,16 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
|
||||
int toExtract = Math.min(amount, existing.getMaxStackSize());
|
||||
|
||||
if (existing.getCount() <= toExtract)
|
||||
{
|
||||
if (!simulate)
|
||||
{
|
||||
if (existing.getCount() <= toExtract) {
|
||||
if (!simulate) {
|
||||
itemStacks.set(slot, ItemStack.EMPTY);
|
||||
setItemList(itemStacks);
|
||||
return existing;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return existing.copy();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!simulate)
|
||||
{
|
||||
} else {
|
||||
if (!simulate) {
|
||||
itemStacks.set(slot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
|
||||
setItemList(itemStacks);
|
||||
}
|
||||
@@ -173,27 +157,23 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
}
|
||||
}
|
||||
|
||||
private void validateSlotIndex(int slot)
|
||||
{
|
||||
private void validateSlotIndex(int slot) {
|
||||
if (slot < 0 || slot >= getSlots())
|
||||
throw new RuntimeException("Slot " + slot + " not in valid range - [0," + getSlots() + ")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(int slot)
|
||||
{
|
||||
public int getSlotLimit(int slot) {
|
||||
return 64;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(int slot, @NotNull ItemStack stack)
|
||||
{
|
||||
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
|
||||
return stack.getItem().canFitInsideContainerItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(int slot, @NotNull ItemStack stack)
|
||||
{
|
||||
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
|
||||
validateSlotIndex(slot);
|
||||
if (!isItemValid(slot, stack)) throw new RuntimeException("Invalid stack " + stack + " for slot " + slot + ")");
|
||||
NonNullList<ItemStack> itemStacks = getItemList();
|
||||
@@ -201,37 +181,34 @@ public class ShulkerItemStackInvWrapper implements IItemHandlerModifiable, ICapa
|
||||
setItemList(itemStacks);
|
||||
}
|
||||
|
||||
private NonNullList<ItemStack> getItemList()
|
||||
{
|
||||
CompoundTag rootTag = BlockItem.getBlockEntityData(this.stack);
|
||||
if (cachedTag == null || !cachedTag.equals(rootTag))
|
||||
private NonNullList<ItemStack> getItemList() {
|
||||
var rootTag = this.stack.get(DataComponents.BLOCK_ENTITY_DATA);
|
||||
if (cachedData == null || !cachedData.equals(rootTag))
|
||||
itemStacksCache = refreshItemList(rootTag);
|
||||
return itemStacksCache;
|
||||
}
|
||||
|
||||
private NonNullList<ItemStack> refreshItemList(CompoundTag rootTag)
|
||||
{
|
||||
NonNullList<ItemStack> itemStacks = NonNullList.withSize(getSlots(), ItemStack.EMPTY);
|
||||
if (rootTag != null && rootTag.contains("Items", CompoundTag.TAG_LIST))
|
||||
{
|
||||
private NonNullList<ItemStack> refreshItemList(CustomData data) {
|
||||
var itemStacks = NonNullList.withSize(getSlots(), ItemStack.EMPTY);
|
||||
var rootTag = data.copyTag();
|
||||
if (rootTag != null && rootTag.contains("Items", CompoundTag.TAG_LIST)) {
|
||||
ContainerHelper.loadAllItems(rootTag, itemStacks);
|
||||
}
|
||||
cachedTag = rootTag;
|
||||
cachedData = data;
|
||||
return itemStacks;
|
||||
}
|
||||
|
||||
private void setItemList(NonNullList<ItemStack> itemStacks)
|
||||
{
|
||||
CompoundTag existing = BlockItem.getBlockEntityData(this.stack);
|
||||
private void setItemList(NonNullList<ItemStack> itemStacks) {
|
||||
var exisiting = this.stack.get(DataComponents.BLOCK_ENTITY_DATA);
|
||||
CompoundTag rootTag = ContainerHelper.saveAllItems(existing == null ? new CompoundTag() : existing, itemStacks);
|
||||
BlockItem.setBlockEntityData(this.stack, BlockEntityType.SHULKER_BOX, rootTag);
|
||||
cachedTag = rootTag;
|
||||
cachedData = rootTag;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side)
|
||||
{
|
||||
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
|
||||
return ForgeCapabilities.ITEM_HANDLER.orEmpty(cap, this.holder);
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in new issue
Block a user