diff --git a/build.gradle b/build.gradle index d216309..7f06563 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ apply plugin: 'java' apply plugin: 'maven-publish' group = 'main' -version = '8.0.3' +version = '8.0.6' description = "A bot for the Facets tabletop RPG" diff --git a/src/main/java/logic/RollLogic.java b/src/main/java/logic/RollLogic.java index 7bcfabb..140b7b8 100644 --- a/src/main/java/logic/RollLogic.java +++ b/src/main/java/logic/RollLogic.java @@ -30,7 +30,6 @@ import javax.annotation.Nullable; import java.text.MessageFormat; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -41,24 +40,29 @@ */ public class RollLogic implements VelenHybridHandler { + public static final SlashCommandOption DICE_POOL = SlashCommandOption.create(SlashCommandOptionType.STRING, "dicepool", "The dice pool to roll with.", true); + public static final SlashCommandOption DISCOUNT = SlashCommandOption.create(SlashCommandOptionType.LONG, "discount", "The number of plot points to discount (negative results in a plot point cost increase).", false); + public static final SlashCommandOption DICE_KEPT = SlashCommandOption.create(SlashCommandOptionType.LONG, "dicekept", "The number of dice kept. Keeps two dice by default.", false); + public static final SlashCommandOption ENHANCEABLE = SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "enhanceable", "Allows the roll to be enhanced after the roll.", false); + public static final SlashCommandOption OPPORTUNITY = SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "opportunity", "Allows for opportunities on the roll. Defaults to true.", false); + private final int diceKept; + + public RollLogic(int diceKept) { + this.diceKept = diceKept; + } + public static void setupRollCommand(Velen velen) { - RollLogic rollLogic = new RollLogic(); - final List rollCommandOptions = getRollCommandOptions(); - rollCommandOptions.add(SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "opportunity", "Allows for opportunities on the roll. Defaults to true.", false)); - VelenCommand.ofHybrid("roll", "Rolls some dice!", velen, rollLogic) - .addOptions(rollCommandOptions.toArray(new SlashCommandOption[0])) + RollLogic rollLogic = new RollLogic(2); + VelenCommand.ofHybrid("roll", "Roll some dice!", velen, rollLogic) + .addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE, DICE_KEPT, OPPORTUNITY) .addFormats("roll :[dicepool:of(string):hasMany()]") - .addShortcuts("r") + .addShortcuts("r", "r2") .attach(); - } - - static List getRollCommandOptions() { - List options = new ArrayList<>(); - options.add(SlashCommandOption.create(SlashCommandOptionType.STRING, "dicepool", "The dice pool to roll with.", true)); - options.add(SlashCommandOption.create(SlashCommandOptionType.LONG, "discount", "The number of plot points to discount (negative results in a plot point cost increase).", false)); - options.add(SlashCommandOption.create(SlashCommandOptionType.LONG, "dicekept", "The number of dice kept. Keeps two dice by default.", false)); - options.add(SlashCommandOption.create(SlashCommandOptionType.BOOLEAN, "enhanceable", "Allows the roll to be enhanced after the roll.", false)); - return options; + List.of(1, 3, 4, 5).forEach(integer -> VelenCommand.ofHybrid("roll%d".formatted(integer), "Roll some dice and keeps %s dice!".formatted(integer), velen, new RollLogic(integer)) + .addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE, OPPORTUNITY) + .addFormats("roll%d :[dicepool:of(string):hasMany()]".formatted(integer)) + .addShortcuts("r%d".formatted(integer)) + .attach()); } /** @@ -71,7 +75,7 @@ static List getRollCommandOptions() { * @param enhanceable If there is an enhancement override on the roll * @param opportunity If opportunities are enabled */ - public static void handleSlashCommandRoll(VelenGeneralEvent event, String dicePool, Integer discount, Integer diceKept, @Nullable Boolean enhanceable, Boolean opportunity) { + public static void handleRoll(VelenGeneralEvent event, String dicePool, Integer discount, Integer diceKept, @Nullable Boolean enhanceable, Boolean opportunity) { final User user = event.getUser(); final RollParameters rollParameters = new RollParameters(dicePool, discount, enhanceable, opportunity, diceKept); final VelenGeneralResponder updaterFuture = event.createResponder(); @@ -156,12 +160,12 @@ public static EmbedBuilder[] removeFirstEmbedFooter(Message interactionMessage) @Override public void onEvent(VelenGeneralEvent event, VelenGeneralResponder responder, User user, VelenHybridArguments args) { final String dicePool = args.getManyWithName("dicepool").orElse(""); - final Long diceKept = args.withName("dicekept").flatMap(VelenOption::asLong).orElse(2L); + final Integer kept = args.withName("dicekept").flatMap(VelenOption::asInteger).orElse(this.diceKept); final Boolean opportunity = args.withName("opportunity").flatMap(VelenOption::asBoolean).orElse(true); final Boolean enhanceable = args.withName("enhanceable").flatMap(VelenOption::asBoolean).orElse(null); final Long discount = args.withName("discount").flatMap(VelenOption::asLong).orElse(0L); - handleSlashCommandRoll(event, dicePool, Math.toIntExact(discount), Math.toIntExact(diceKept), enhanceable, opportunity); + handleRoll(event, dicePool, Math.toIntExact(discount), kept, enhanceable, opportunity); } } diff --git a/src/main/java/logic/RollPoolLogic.java b/src/main/java/logic/RollPoolLogic.java index ffd8027..be29ef5 100644 --- a/src/main/java/logic/RollPoolLogic.java +++ b/src/main/java/logic/RollPoolLogic.java @@ -81,14 +81,14 @@ else if (Stream.of("vitality", "initiative", "willpower").anyMatch(pool::equalsI SheetsHandler.getSavePool(StringUtils.capitalize(pool.toLowerCase()), user) .map(defaultPool -> MessageFormat.format("{0} {1}", defaultPool, bonuses)) .onFailure(throwable -> event.getChannel().sendMessage(throwable.getMessage())) - .onSuccess(dicePool -> RollLogic.handleSlashCommandRoll(event, dicePool, discount, diceKept, enhanceable, false)); + .onSuccess(dicePool -> RollLogic.handleRoll(event, dicePool, discount, diceKept, enhanceable, false)); } else { Boolean opportunity = arguments.withName("opportunity").flatMap(VelenOption::asBoolean).orElse(true); SheetsHandler.getSavedPool(pool.toLowerCase(), user) .map(defaultPool -> MessageFormat.format("{0} {1}", defaultPool, bonuses)) .onFailure(throwable -> event.getChannel().sendMessage(throwable.getMessage())) - .onSuccess(dicePool -> RollLogic.handleSlashCommandRoll(event, dicePool, discount, diceKept, enhanceable, opportunity)); + .onSuccess(dicePool -> RollLogic.handleRoll(event, dicePool, discount, diceKept, enhanceable, opportunity)); } } } diff --git a/src/main/java/logic/TestLogic.java b/src/main/java/logic/TestLogic.java index cde5212..40e8621 100644 --- a/src/main/java/logic/TestLogic.java +++ b/src/main/java/logic/TestLogic.java @@ -1,7 +1,6 @@ package logic; import org.javacord.api.entity.user.User; -import org.javacord.api.interaction.SlashCommandOption; import pw.mihou.velen.interfaces.Velen; import pw.mihou.velen.interfaces.VelenCommand; import pw.mihou.velen.interfaces.VelenHybridHandler; @@ -12,25 +11,37 @@ import java.util.List; +import static logic.RollLogic.*; + public class TestLogic implements VelenHybridHandler { + private final int diceKept; + + public TestLogic(int diceKept) { + this.diceKept = diceKept; + } + public static void setTestCommand(Velen velen) { - TestLogic testLogic = new TestLogic(); - List options = RollLogic.getRollCommandOptions(); + TestLogic testLogic = new TestLogic(2); VelenCommand.ofHybrid("test", "Rolls some dice with opportunities disabled!", velen, testLogic) - .addOptions(options.toArray(new SlashCommandOption[0])) + .addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE, DICE_KEPT) .addFormats("test :[dicepool:of(string):hasMany()]") - .addShortcuts("t") + .addShortcuts("t", "t2") .attach(); + List.of(1, 3, 4, 5).forEach(integer -> VelenCommand.ofHybrid("test%d".formatted(integer), "Roll some dice with opportuities disabled and keeps %s dice!".formatted(integer), velen, new TestLogic(integer)) + .addOptions(DICE_POOL, DISCOUNT, ENHANCEABLE) + .addFormats("test%d :[dicepool:of(string):hasMany()]".formatted(integer)) + .addShortcuts("t%d".formatted(integer)) + .attach()); + } @Override public void onEvent(VelenGeneralEvent event, VelenGeneralResponder responder, User user, VelenHybridArguments args) { final String dicePool = args.getManyWithName("dicepool").orElse(""); + final Integer kept = args.withName("dicekept").flatMap(VelenOption::asInteger).orElse(this.diceKept); final Integer discount = args.withName("discount").flatMap(VelenOption::asInteger).orElse(0); - final Integer diceKept = args.withName("dicekept").flatMap(VelenOption::asInteger).orElse(2); final Boolean enhanceable = args.withName("enhanceable").flatMap(VelenOption::asBoolean).orElse(null); - RollLogic.handleSlashCommandRoll(event, dicePool, discount, diceKept, enhanceable, false); - + RollLogic.handleRoll(event, dicePool, discount, kept, enhanceable, false); } }