Skip to content

Commit

Permalink
Make inventory modifiers move items if there is space available befor…
Browse files Browse the repository at this point in the history
…e giving up

This means if the size does decrease, you can just remove items then modify it to retrive old ones
  • Loading branch information
KnightMiner committed Sep 29, 2022
1 parent ce1b6ae commit a8369ef
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;

import javax.annotation.Nullable;
import java.util.BitSet;
import java.util.function.BiFunction;

/** Modifier that has an inventory */
Expand Down Expand Up @@ -60,12 +61,23 @@ public ValidatedResult validate(IToolStackView tool, int level) {
if (level == 0) {
return HAS_ITEMS;
}
// determine the largest index we are using
// first, see whether we have any available slots
int maxSlots = getSlots(tool, level);
BitSet freeSlots = new BitSet(maxSlots);
freeSlots.set(0, maxSlots-1, true);
for (int i = 0; i < listNBT.size(); i++) {
freeSlots.set(listNBT.getCompound(i).getInt(TAG_SLOT), false);
}
for (int i = 0; i < listNBT.size(); i++) {
CompoundTag compoundNBT = listNBT.getCompound(i);
if (compoundNBT.getInt(TAG_SLOT) >= maxSlots) {
return HAS_ITEMS;
int free = freeSlots.stream().findFirst().orElse(-1);
if (free == -1) {
return HAS_ITEMS;
} else {
freeSlots.set(free, false);
compoundNBT.putInt(TAG_SLOT, free);
}
}
}
}
Expand Down

0 comments on commit a8369ef

Please sign in to comment.