Skip to content

Commit

Permalink
fix: correct the empty check for shulker boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
voidpointer0x00 committed Jun 2, 2024
1 parent 77565f5 commit d913084
Showing 1 changed file with 5 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/main/java/carpetextra/utils/InventoryUtils.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
package carpetextra.utils;

import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.component.type.ContainerComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;

public class InventoryUtils
{
public class InventoryUtils {
/**
* Checks if the given Shulker Box (or other storage item with the
* same NBT data structure) currently contains any items.
* @return true if the item's stored inventory has any items
*/
public static boolean shulkerBoxHasItems(ItemStack stackShulkerBox)
{
NbtComponent nbtComponent = stackShulkerBox.get(DataComponentTypes.BLOCK_ENTITY_DATA);
if (nbtComponent == null)
return false;
/* FIXME find a new way of doing this */
NbtCompound nbt = nbtComponent.getNbt();

if (nbt != null && nbt.contains("BlockEntityTag", NbtElement.COMPOUND_TYPE))
{
NbtCompound tag = nbt.getCompound("BlockEntityTag");

if (tag.contains("Items", NbtElement.LIST_TYPE))
{
NbtList tagList = tag.getList("Items", NbtElement.COMPOUND_TYPE);
return !tagList.isEmpty();
}
}

return false;
public static boolean shulkerBoxHasItems(ItemStack stackShulkerBox) {
ContainerComponent containerComponent = stackShulkerBox.get(DataComponentTypes.CONTAINER);
return containerComponent != null && !containerComponent.iterateNonEmpty().iterator().hasNext();
}
}

0 comments on commit d913084

Please sign in to comment.