Skip to content

Commit

Permalink
shulker fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
MC-XiaoHei committed May 8, 2024
1 parent fb26f3a commit 4587f00
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/main/java/top/leavesmc/leaves/util/ShulkerBoxUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package top.leavesmc.leaves.util;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.ShulkerBoxBlock;
import org.jetbrains.annotations.NotNull;
import top.leavesmc.leaves.LeavesConfig;

// Powered by fabric-carpet/src/main/java/carpet/helpers/InventoryHelper.java
public class ShulkerBoxUtils {
// From nbt/NbtElement.java createTag()
public static final int TAG_END = 0;
public static final int TAG_BYTE = 1;
public static final int TAG_SHORT = 2;
public static final int TAG_INT = 3;
public static final int TAG_LONG = 4;
public static final int TAG_FLOAT = 5;
public static final int TAG_DOUBLE = 6;
public static final int TAG_BYTEARRAY = 7;
public static final int TAG_STRING = 8;
public static final int TAG_LIST = 9;
public static final int TAG_COMPOUND = 10;
public static final int TAG_INTARRAY = 11;
public static final int TAG_LONGARRAY = 12;

public static boolean cleanUpShulkerBoxTag(@NotNull ItemStack stack) {
boolean changed = false;
CompoundTag tag = stack.getTag();

if (tag == null || !tag.contains("BlockEntityTag", TAG_COMPOUND)) return false;

CompoundTag bet = tag.getCompound("BlockEntityTag");
if (bet.contains("Items", TAG_LIST) && bet.getList("Items", TAG_COMPOUND).isEmpty()) {
bet.remove("Items");
changed = true;
}

if (bet.isEmpty() || (bet.size() == 1 && bet.getString("id").equals("minecraft:shulker_box"))) {
tag.remove("BlockEntityTag");
changed = true;
}
if (tag.isEmpty()) {
stack.setTag(null);
changed = true;
}
return changed;
}

public static boolean shulkerBoxHasItems(@NotNull ItemStack stack) {
CompoundTag tag = stack.getTag();

if (tag == null || !tag.contains("BlockEntityTag", TAG_COMPOUND)) return false;

CompoundTag bet = tag.getCompound("BlockEntityTag");
return bet.contains("Items", TAG_LIST) && !bet.getList("Items", TAG_COMPOUND).isEmpty();
}

public static int getItemStackMaxCount(ItemStack stack) {
if (LeavesConfig.shulkerBoxStackSize > 1 && stack.getItem() instanceof BlockItem bi &&
bi.getBlock() instanceof ShulkerBoxBlock && !top.leavesmc.leaves.util.ShulkerBoxUtils.shulkerBoxHasItems(stack)) {
return top.leavesmc.leaves.LeavesConfig.shulkerBoxStackSize;
}
return stack.getMaxStackSize();
}

public static boolean isStackable(ItemStack itemStack) {
return getItemStackMaxCount(itemStack) > 1 && (!itemStack.isDamageableItem() || !itemStack.isDamaged());
}
}

0 comments on commit 4587f00

Please sign in to comment.