Add recursive item stack NBT walker

This commit is contained in:
Ruling-0 committed 2026-08-27 19:45:59 -05:00
1 parent 490eefc6b7
commit 99907e564c
2 files changed
+141

No files matched your search

@@ -0,0 +1,43 @@
package com.gtnewhorizons.postea.utility;
import static net.minecraftforge.common.util.Constants.NBT.TAG_ANY_NUMERIC;
import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND;
import static net.minecraftforge.common.util.Constants.NBT.TAG_INT;
import static net.minecraftforge.common.util.Constants.NBT.TAG_SHORT;
import java.util.ArrayList;
import java.util.function.Consumer;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
/**
* Visits every serialized item stack in an NBT tree. A compound is a stack when it has a numeric {@code id} or an
* EndlessIDs {@code idExt} together with a {@code Count}. The walk is pre-order and descends into every compound
* and every list of compounds, so stacks nested inside another stack's {@code tag} (backpacks, storage cells) are
* visited too. Lists of lists are not descended.
*/
public final class NbtItemStacks {
private NbtItemStacks() {}
public static void forEach(NBTBase root, Consumer<NBTTagCompound> visitor) {
if (root instanceof NBTTagCompound compound) {
if (isStack(compound)) visitor.accept(compound);
for (Object key : new ArrayList<>(compound.func_150296_c())) {
forEach(compound.getTag((String) key), visitor);
}
} else if (root instanceof NBTTagList list && list.func_150303_d() == TAG_COMPOUND) {
for (int i = 0; i < list.tagCount(); i++) {
forEach(list.getCompoundTagAt(i), visitor);
}
}
}
public static boolean isStack(NBTTagCompound compound) {
return compound.hasKey("Count", TAG_ANY_NUMERIC)
&& (compound.hasKey("id", TAG_SHORT) || compound.hasKey("id", TAG_INT)
|| compound.hasKey("idExt", TAG_INT));
}
}