Search in sources :

Example 1 with ValidatedResult

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);
}
Also used : ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) ItemStack(net.minecraft.world.item.ItemStack) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) AbstractModifierSalvage(slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage)

Example 2 with ValidatedResult

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())));
}
Also used : ModDataNBT(slimeknights.tconstruct.library.tools.nbt.ModDataNBT) SlotCount(slimeknights.tconstruct.library.tools.SlotType.SlotCount) ItemStack(net.minecraft.world.item.ItemStack) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack)

Example 3 with ValidatedResult

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));
    }
}
Also used : ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) Modifier(slimeknights.tconstruct.library.modifiers.Modifier)

Example 4 with ValidatedResult

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());
}
Also used : Item(net.minecraft.item.Item) ModDataNBT(slimeknights.tconstruct.library.tools.nbt.ModDataNBT) SlotCount(slimeknights.tconstruct.library.tools.SlotType.SlotCount) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) IExosuitSensor(vazkii.psi.api.exosuit.IExosuitSensor) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) Modifier(slimeknights.tconstruct.library.modifiers.Modifier)

Example 5 with ValidatedResult

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;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) ItemStack(net.minecraft.world.item.ItemStack) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

Aggregations

ValidatedResult (slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult)11 ToolStack (slimeknights.tconstruct.library.tools.nbt.ToolStack)10 Modifier (slimeknights.tconstruct.library.modifiers.Modifier)9 ModDataNBT (slimeknights.tconstruct.library.tools.nbt.ModDataNBT)7 SlotCount (slimeknights.tconstruct.library.tools.SlotType.SlotCount)6 ItemStack (net.minecraft.world.item.ItemStack)5 CommandSourceStack (net.minecraft.commands.CommandSourceStack)3 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)3 LivingEntity (net.minecraft.world.entity.LivingEntity)3 Item (net.minecraft.item.Item)2 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)2 ColorizedModifier (com.rcx.materialis.modifiers.ColorizedModifier)1 RunedModifier (com.rcx.materialis.modifiers.RunedModifier)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1 IncrementalModifier (slimeknights.tconstruct.library.modifiers.impl.IncrementalModifier)1 ModifierRequirements (slimeknights.tconstruct.library.recipe.modifiers.ModifierRequirements)1 AbstractModifierSalvage (slimeknights.tconstruct.library.recipe.modifiers.salvage.AbstractModifierSalvage)1 SlotType (slimeknights.tconstruct.library.tools.SlotType)1 ICADColorizer (vazkii.psi.api.cad.ICADColorizer)1 IExosuitSensor (vazkii.psi.api.exosuit.IExosuitSensor)1