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;
}
Aggregations