diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java index 8ba81cfb16c..fb2819b4ed1 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Commands.java @@ -166,6 +166,21 @@ public static Command either(Command onTrue, Command onFalse, BooleanSupplier se return new ConditionalCommand(onTrue, onFalse, selector); } + /** + * Runs a command if the boolean condition function is true. Otherwise it does nothing and exits + * (but still has the same requirements as the command, interrupting any commands with shared + * requirements). + * + * @param condition the condition function + * @param onTrue the command to run if the condition function returns true + * @return the command + * @see ConditionalCommand + * @see Commands#none() + */ + public static Command runIf(BooleanSupplier condition, Command onTrue) { + return onTrue.onlyIf(condition); + } + /** * Runs one of several commands, based on the selector function. *