use of slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult 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.tinkerstation.ValidatedResult in project TinkersConstruct by SlimeKnights.
the class ModifierRecipe method getValidatedResult.
/**
* Gets the recipe result, or an object containing an error message if the recipe matches but cannot be applied.
* @return Validated result
*/
@Override
public ValidatedResult getValidatedResult(ITinkerStationContainer inv) {
ItemStack tinkerable = inv.getTinkerableStack();
ToolStack tool = ToolStack.from(tinkerable);
// common errors
ValidatedResult commonError = validatePrerequisites(tool);
if (commonError.hasError()) {
return commonError;
}
// consume slots
tool = tool.copy();
ModDataNBT persistentData = tool.getPersistentData();
SlotCount slots = getSlots();
if (slots != null) {
persistentData.addSlots(slots.getType(), -slots.getCount());
}
// add modifier
tool.addModifier(result.getModifier(), result.getLevel());
// ensure no modifier problems
ValidatedResult toolValidation = tool.validate();
if (toolValidation.hasError()) {
return toolValidation;
}
return ValidatedResult.success(tool.createStack(Math.min(tinkerable.getCount(), shrinkToolSlotBy())));
}
use of slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult in project TinkersConstruct by SlimeKnights.
the class ModifierRecipeLookup method addRequirements.
/**
* Adds a modifier requirement, typically called by the recipe
* @param ingredient Ingredient that must match the tool for this to be attempted
* @param entry Modifier to check, level determines amount added per level
* @param requirements Actual requirements to attempt
* @param errorMessage Error to display if the requirements fail
*/
public static void addRequirements(Ingredient ingredient, ModifierEntry entry, ModifierMatch requirements, String errorMessage) {
if (requirements != ModifierMatch.ALWAYS) {
// if the key is empty, use the default
ValidatedResult error;
if (errorMessage.isEmpty()) {
error = DEFAULT_ERROR;
} else {
error = ValidatedResult.failure(errorMessage);
}
Modifier modifier = entry.getModifier();
addRequirements(new ModifierRequirements(ingredient, modifier, requirements.getMinLevel(modifier) + entry.getLevel(), requirements, error));
}
}
use of slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult in project Materialis by RCXcrafter.
the class SensorModifierRecipe method getValidatedResult.
@Override
public ValidatedResult getValidatedResult(ITinkerStationInventory inv) {
ToolStack tool = ToolStack.from(inv.getTinkerableStack());
// if the tool has the modifier already, can skip most requirements
Modifier modifier = result.getModifier();
ValidatedResult commonError;
boolean needsModifier;
if (tool.getUpgrades().getLevel(modifier) == 0) {
needsModifier = true;
commonError = validatePrerequisites(tool);
} else {
needsModifier = false;
commonError = validateRequirements(tool);
}
if (commonError.hasError()) {
return commonError;
}
// consume slots
tool = tool.copy();
ModDataNBT persistentData = tool.getPersistentData();
if (needsModifier) {
SlotCount slots = getSlots();
if (slots != null) {
persistentData.addSlots(slots.getType(), -slots.getCount());
}
}
// set the new value to the modifier
persistentData.putString(modifier.getId(), value);
// add modifier if needed
if (needsModifier) {
tool.addModifier(result.getModifier(), 1);
} else {
tool.rebuildStats();
}
// ensure no modifier problems
ValidatedResult toolValidation = tool.validate();
if (toolValidation.hasError()) {
return toolValidation;
}
// add sensor information
if (enabled) {
for (int i = 0; i < inv.getInputCount(); i++) {
Item item = inv.getInput(i).getItem();
if (item instanceof IExosuitSensor) {
persistentData.put(PsionizingRadiationModifierSensor.SENSOR, inv.getInput(i).serializeNBT());
break;
}
}
}
return ValidatedResult.success(tool.createStack());
}
use of slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult in project TinkersConstruct by SlimeKnights.
the class ModifiersCommand method remove.
/**
* Runs the command
*/
private static int remove(CommandContext<CommandSourceStack> context, int level) throws CommandSyntaxException {
Modifier modifier = ModifierArgument.getModifier(context, "modifier");
MutableInt maxRemove = new MutableInt(1);
List<LivingEntity> successes = HeldModifiableItemIterator.apply(context, (living, stack) -> {
// add modifier
ToolStack tool = ToolStack.from(stack);
// first, see if the modifier exists
int currentLevel = tool.getUpgrades().getLevel(modifier);
if (currentLevel == 0) {
throw CANNOT_REMOVE.create(modifier.getDisplayName(level), living.getName());
}
int removeLevel = level == -1 ? currentLevel : level;
if (removeLevel > maxRemove.intValue()) {
maxRemove.setValue(removeLevel);
}
tool = tool.copy();
// first remove hook, primarily for removing raw NBT which is highly discouraged using
int newLevel = currentLevel - removeLevel;
if (newLevel <= 0) {
modifier.beforeRemoved(tool, tool.getRestrictedNBT());
}
// remove the actual modifier
tool.removeModifier(modifier, removeLevel);
// ensure the tool is still valid
ValidatedResult validated = tool.validate();
if (validated.hasError()) {
throw MODIFIER_ERROR.create(validated.getMessage());
}
// second remove hook, useful for removing modifier specific state data
if (newLevel <= 0) {
modifier.onRemoved(tool);
}
// 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()) {
throw MODIFIER_ERROR.create(validated.getMessage());
}
}
// 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(stack.getCount());
validated = ModifierRecipeLookup.checkRequirements(resultStack, tool);
if (validated.hasError()) {
throw MODIFIER_ERROR.create(validated.getMessage());
}
// if successful, update held item
living.setItemInHand(InteractionHand.MAIN_HAND, tool.createStack(stack.getCount()));
return true;
});
// success message
CommandSourceStack source = context.getSource();
int size = successes.size();
if (size == 1) {
source.sendSuccess(new TranslatableComponent(REMOVE_SUCCESS, modifier.getDisplayName(maxRemove.intValue()), successes.get(0).getDisplayName()), true);
} else {
source.sendSuccess(new TranslatableComponent(REMOVE_SUCCESS_MULTIPLE, modifier.getDisplayName(maxRemove.intValue()), size), true);
}
return size;
}
Aggregations