Skip to content

Commit

Permalink
Attempt 2 at allowing "overloaded" slots in tool belts to migrate nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Sep 30, 2022
1 parent 4c85775 commit f62f000
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@ public void addVolatileData(ToolRebuildContext context, int level, ModDataNBT vo
ToolInventoryCapability.addSlots(volatileData, getSlots(context, level));
}

@Override
public ValidatedResult validate(IToolStackView tool, int level) {
/**
* Same as {@link #validate(IToolStackView, int)} but allows passing in a max slots count.
* Allows the subclass to validate on a different max slots if needed
* @param tool Tool to check
* @param maxSlots Max slots to use in the check
* @return True if the number of slots is valid
*/
protected ValidatedResult validateForMaxSlots(IToolStackView tool, int maxSlots) {
IModDataView persistentData = tool.getPersistentData();
ResourceLocation key = getInventoryKey();
if (persistentData.contains(key, Tag.TAG_LIST)) {
ListTag listNBT = persistentData.get(key, GET_COMPOUND_LIST);
if (!listNBT.isEmpty()) {
if (level == 0) {
if (maxSlots == 0) {
return HAS_ITEMS;
}
// 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++) {
Expand All @@ -85,6 +90,11 @@ public ValidatedResult validate(IToolStackView tool, int level) {
return ValidatedResult.PASS;
}

@Override
public ValidatedResult validate(IToolStackView tool, int level) {
return validateForMaxSlots(tool, level == 0 ? 0 : getSlots(tool, level));
}

@Override
public void onRemoved(IToolStackView tool) {
tool.getPersistentData().remove(getInventoryKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import slimeknights.tconstruct.library.modifiers.util.ModifierLevelDisplay;
import slimeknights.tconstruct.library.recipe.partbuilder.Pattern;
import slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult;
import slimeknights.tconstruct.library.tools.capability.ToolInventoryCapability;
import slimeknights.tconstruct.library.tools.context.ToolRebuildContext;
import slimeknights.tconstruct.library.tools.nbt.IModDataView;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import slimeknights.tconstruct.library.utils.RestrictedCompoundTag;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -105,13 +107,15 @@ private int getProperSlots(int level) {
}

@Override
public void addRawData(IToolStackView tool, int level, RestrictedCompoundTag tag) {
ModDataNBT modData = tool.getPersistentData();
public void addVolatileData(ToolRebuildContext context, int level, ModDataNBT volatileData) {
int properSlots = getProperSlots(level);
int slots;
// find the largest slot index and either add or update the override as needed
// TODO: can probably remove this code for 1.19
if (properSlots < 9) {
slots = properSlots;
ResourceLocation key = getInventoryKey();
IModDataView modData = context.getPersistentData();
if (modData.contains(key, Tag.TAG_LIST)) {
ListTag list = modData.get(key, GET_COMPOUND_LIST);
int maxSlot = 0;
Expand All @@ -121,13 +125,16 @@ public void addRawData(IToolStackView tool, int level, RestrictedCompoundTag tag
maxSlot = newSlot;
}
}
if (maxSlot >= properSlots) {
modData.putInt(SLOT_OVERRIDE, maxSlot + 1);
return;
maxSlot = Math.min(maxSlot + 1, 9);
if (maxSlot > properSlots) {
volatileData.putInt(SLOT_OVERRIDE, maxSlot);
slots = maxSlot;
}
}
} else {
slots = 9;
}
modData.remove(SLOT_OVERRIDE);
ToolInventoryCapability.addSlots(volatileData, slots);
}

@Override
Expand All @@ -136,15 +143,12 @@ public int getSlots(IToolContext tool, int level) {
if (properSlots >= 9) {
return 9;
}
return Mth.clamp(properSlots, tool.getPersistentData().getInt(SLOT_OVERRIDE), 9);
return Mth.clamp(tool.getVolatileData().getInt(SLOT_OVERRIDE), properSlots, 9);
}

@Override
public ValidatedResult validate(IToolStackView tool, int level) {
// remove the slot override so you may not modify the tool to remove a higher level
// means you are not allowed to modify a tool with an invalid tool belt slot count until you remove the items
tool.getPersistentData().remove(SLOT_OVERRIDE);
return super.validate(tool, level);
return validateForMaxSlots(tool, getProperSlots(level));
}

@Override
Expand Down

0 comments on commit f62f000

Please sign in to comment.