use of slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage in project TinkersConstruct by SlimeKnights.
the class ModifierRemovalRecipe method getValidatedResult.
@Override
public ValidatedResult getValidatedResult(ITinkerStationContainer inv) {
ItemStack toolStack = inv.getTinkerableStack();
ToolStack tool = ToolStack.from(toolStack);
List<ModifierEntry> modifiers = tool.getUpgrades().getModifiers();
if (modifiers.isEmpty()) {
return NO_MODIFIERS;
}
// find the modifier to remove
ModifierEntry toRemove = getModifierToRemove(inv, modifiers);
if (toRemove == null) {
return ValidatedResult.PASS;
}
// salvage
tool = tool.copy();
Modifier modifier = toRemove.getModifier();
AbstractModifierSalvage salvage = ModifierRecipeLookup.getSalvage(toolStack, tool, modifier, toRemove.getLevel());
// restore the slots
if (salvage != null) {
salvage.updateTool(tool);
}
// first remove hook, primarily for removing raw NBT which is highly discouraged using
int newLevel = tool.getModifierLevel(modifier) - 1;
if (newLevel <= 0) {
modifier.beforeRemoved(tool, tool.getRestrictedNBT());
}
// remove the actual modifier
tool.removeModifier(modifier, 1);
// second remove hook, useful for removing modifier specific state data
if (newLevel <= 0) {
modifier.onRemoved(tool);
}
// ensure the tool is still valid
ValidatedResult validated = tool.validate();
if (validated.hasError()) {
return validated;
}
// if this was the last level, validate the tool is still valid without it
if (newLevel <= 0) {
validated = modifier.validate(tool, 0);
if (validated.hasError()) {
return validated;
}
}
// check the modifier requirements
// creating a stack to make it as accurate as possible, though the old stack should be sufficient
ItemStack resultStack = tool.createStack(Math.min(toolStack.getCount(), shrinkToolSlotBy()));
validated = ModifierRecipeLookup.checkRequirements(resultStack, tool);
if (validated.hasError()) {
return validated;
}
// successfully removed
return ValidatedResult.success(resultStack);
}
use of slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage in project TinkersConstruct by SlimeKnights.
the class ModifierRemovalRecipe method updateInputs.
@Override
public void updateInputs(ItemStack result, IMutableTinkerStationContainer inv, boolean isServer) {
// return salvage items for modifier, using the original tool as that still has the modifier
if (isServer) {
ItemStack toolStack = inv.getTinkerableStack();
ToolStack tool = ToolStack.from(toolStack);
ModifierEntry toRemove = getModifierToRemove(inv, tool.getUpgrades().getModifiers());
if (toRemove != null) {
AbstractModifierSalvage salvage = ModifierRecipeLookup.getSalvage(toolStack, tool, toRemove.getModifier(), toRemove.getLevel());
if (salvage != null) {
int salvageMax = Math.min(toolStack.getMaxStackSize(), salvage.getMaxToolSize());
int currentSize = result.getCount();
Consumer<ItemStack> consumer;
// if the size is smaller than 16, shrink all salvage stack sizes to prevent a salvage dupe
if (currentSize < salvageMax) {
consumer = stack -> {
int newSize = stack.getCount() * currentSize / salvageMax;
if (newSize > 0) {
stack.setCount(newSize);
inv.giveItem(stack);
}
};
// if larger, grow salvage
} else if (currentSize > salvageMax) {
consumer = stack -> {
int newSize = stack.getCount() * currentSize / salvageMax;
int maxStackSize = stack.getMaxStackSize();
while (newSize > maxStackSize) {
inv.giveItem(ItemHandlerHelper.copyStackWithSize(stack, maxStackSize));
newSize -= maxStackSize;
}
if (newSize > 0) {
stack.setCount(newSize);
inv.giveItem(stack);
}
};
} else {
consumer = inv::giveItem;
}
salvage.acceptItems(tool, consumer, TConstruct.RANDOM);
}
}
}
// remove the input item, done second as we need its location for salvage
for (int i = 0; i < inv.getInputCount(); i++) {
ItemStack stack = inv.getInput(i);
if (!stack.isEmpty() && ingredient.test(stack)) {
inv.shrinkInput(i, 1, container.copy());
break;
}
}
}
Aggregations