Search in sources :

Example 1 with ModifierRequirements

use of slimeknights.tconstruct.library.recipe.modifiers.ModifierRequirements in project TinkersConstruct by SlimeKnights.

the class ModifiersCommand method add.

/**
 * Runs the command
 */
private static int add(CommandContext<CommandSourceStack> context, int level) throws CommandSyntaxException {
    Modifier modifier = ModifierArgument.getModifier(context, "modifier");
    List<LivingEntity> successes = HeldModifiableItemIterator.apply(context, (living, stack) -> {
        // add modifier
        ToolStack tool = ToolStack.from(stack);
        // first, see if we can add the modifier
        int currentLevel = tool.getModifierLevel(modifier);
        List<ModifierEntry> modifiers = tool.getModifierList();
        for (ModifierRequirements requirements : ModifierRecipeLookup.getRequirements(modifier)) {
            ValidatedResult result = requirements.check(stack, level + currentLevel, modifiers);
            if (result.hasError()) {
                throw MODIFIER_ERROR.create(result.getMessage());
            }
        }
        tool = tool.copy();
        tool.addModifier(modifier, level);
        // ensure no modifier problems after adding
        ValidatedResult toolValidation = tool.validate();
        if (toolValidation.hasError()) {
            throw MODIFIER_ERROR.create(toolValidation.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(ADD_SUCCESS, modifier.getDisplayName(level), successes.get(0).getDisplayName()), true);
    } else {
        source.sendSuccess(new TranslatableComponent(ADD_SUCCESS_MULTIPLE, modifier.getDisplayName(level), size), true);
    }
    return size;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) ValidatedResult(slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) ModifierRequirements(slimeknights.tconstruct.library.recipe.modifiers.ModifierRequirements) CommandSourceStack(net.minecraft.commands.CommandSourceStack)

Aggregations

CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 Modifier (slimeknights.tconstruct.library.modifiers.Modifier)1 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)1 ModifierRequirements (slimeknights.tconstruct.library.recipe.modifiers.ModifierRequirements)1 ValidatedResult (slimeknights.tconstruct.library.recipe.tinkerstation.ValidatedResult)1 ToolStack (slimeknights.tconstruct.library.tools.nbt.ToolStack)1