Search in sources :

Example 11 with I18nContext

use of net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext in project MantaroBot by Mantaro.

the class ItemHelper method openLootBox.

private static void openLootBox(Context ctx, Player player, SeasonPlayer seasonPlayer, ItemType.LootboxType type, Item crate, EmoteReference typeEmote, int bound, boolean seasonal) {
    List<Item> toAdd = selectItems(random.nextInt(bound) + bound, type);
    ArrayList<ItemStack> ita = new ArrayList<>();
    toAdd.forEach(item -> ita.add(new ItemStack(item, 1)));
    PlayerData data = player.getData();
    if ((type == ItemType.LootboxType.MINE || type == ItemType.LootboxType.MINE_PREMIUM) && toAdd.contains(ItemReference.SPARKLE_PICKAXE)) {
        data.addBadgeIfAbsent(Badge.DESTINY_REACHES);
    }
    if ((type == ItemType.LootboxType.FISH || type == ItemType.LootboxType.FISH_PREMIUM) && toAdd.contains(ItemReference.SHARK)) {
        data.addBadgeIfAbsent(Badge.TOO_BIG);
    }
    var toShow = ItemStack.reduce(ita);
    // Tools must only drop one, if any.
    toShow = toShow.stream().map(stack -> {
        var item = stack.getItem();
        if (stack.getAmount() > 1 && ((item instanceof Pickaxe) || (item instanceof FishRod) || (item instanceof Axe))) {
            return new ItemStack(item, 1);
        }
        return stack;
    }).collect(Collectors.toList());
    boolean overflow = seasonal ? seasonPlayer.getInventory().merge(toShow) : player.getInventory().merge(toShow);
    if (seasonal) {
        seasonPlayer.getInventory().process(new ItemStack(ItemReference.LOOT_CRATE_KEY, -1));
        seasonPlayer.getInventory().process(new ItemStack(crate, -1));
    } else {
        player.getInventory().process(new ItemStack(ItemReference.LOOT_CRATE_KEY, -1));
        player.getInventory().process(new ItemStack(crate, -1));
    }
    data.setCratesOpened(data.getCratesOpened() + 1);
    player.save();
    if (seasonal) {
        seasonPlayer.save();
    }
    I18nContext lang = ctx.getLanguageContext();
    var show = toShow.stream().map(itemStack -> "x%,d \u2009%s".formatted(itemStack.getAmount(), itemStack.getItem().toDisplayString())).collect(Collectors.joining(", "));
    var extra = "";
    if (overflow) {
        extra = ". " + lang.get("general.misc_item_usage.crate.overflow");
    }
    var high = toShow.stream().filter(stack -> stack.getItem() instanceof Tiered).filter(stack -> ((Tiered) stack.getItem()).getTier() >= 4).map(stack -> "%s \u2009(%d \u2b50)".formatted(stack.getItem().getEmoji(), ((Tiered) stack.getItem()).getTier())).collect(Collectors.joining(", "));
    if (high.length() >= 1) {
        extra = ".\n\n" + lang.get("general.misc_item_usage.crate.success_high").formatted(EmoteReference.POPPER, high);
    }
    ctx.sendFormat(lang.get("general.misc_item_usage.crate.success"), typeEmote.getDiscordNotation() + " ", show, extra);
}
Also used : Badge(net.kodehawa.mantarobot.commands.currency.profile.Badge) java.util(java.util) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData) LoggerFactory(org.slf4j.LoggerFactory) SeasonPlayer(net.kodehawa.mantarobot.commands.currency.seasons.SeasonPlayer) SecureRandom(java.security.SecureRandom) Pair(org.apache.commons.lang3.tuple.Pair) FishRod(net.kodehawa.mantarobot.commands.currency.item.special.tools.FishRod) DBUser(net.kodehawa.mantarobot.db.entities.DBUser) Inventory(net.kodehawa.mantarobot.db.entities.helpers.Inventory) Context(net.kodehawa.mantarobot.core.modules.commands.base.Context) Tiered(net.kodehawa.mantarobot.commands.currency.item.special.helpers.attributes.Tiered) Broken(net.kodehawa.mantarobot.commands.currency.item.special.Broken) Player(net.kodehawa.mantarobot.db.entities.Player) UserData(net.kodehawa.mantarobot.db.entities.helpers.UserData) Logger(org.slf4j.Logger) Potion(net.kodehawa.mantarobot.commands.currency.item.special.Potion) Predicate(java.util.function.Predicate) IncreasingRateLimiter(net.kodehawa.mantarobot.utils.commands.ratelimit.IncreasingRateLimiter) Collectors(java.util.stream.Collectors) Axe(net.kodehawa.mantarobot.commands.currency.item.special.tools.Axe) TimeUnit(java.util.concurrent.TimeUnit) Breakable(net.kodehawa.mantarobot.commands.currency.item.special.helpers.Breakable) RatelimitUtils(net.kodehawa.mantarobot.utils.commands.ratelimit.RatelimitUtils) Stream(java.util.stream.Stream) I18nContext(net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) RandomCollection(net.kodehawa.mantarobot.utils.RandomCollection) Pickaxe(net.kodehawa.mantarobot.commands.currency.item.special.tools.Pickaxe) Pickaxe(net.kodehawa.mantarobot.commands.currency.item.special.tools.Pickaxe) FishRod(net.kodehawa.mantarobot.commands.currency.item.special.tools.FishRod) Axe(net.kodehawa.mantarobot.commands.currency.item.special.tools.Axe) Tiered(net.kodehawa.mantarobot.commands.currency.item.special.helpers.attributes.Tiered) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData) I18nContext(net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext)

Example 12 with I18nContext

use of net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext in project MantaroBot by Mantaro.

the class Poll method createPoll.

private void createPoll(Context ctx, Message message, I18nContext languageContext) {
    runningPoll = ReactionOperations.create(message, TimeUnit.MILLISECONDS.toSeconds(timeout), new ReactionOperation() {

        @Override
        public int add(MessageReactionAddEvent e) {
            // always return false anyway lul
            return Operation.IGNORED;
        }

        @Override
        public void onExpire() {
            if (getChannel() == null)
                return;
            var user = ctx.getAuthor();
            var embedBuilder = new EmbedBuilder().setTitle(languageContext.get("commands.poll.result_header")).setDescription(String.format(languageContext.get("commands.poll.result_screen"), user.getName(), name)).setFooter(languageContext.get("commands.poll.thank_note"), null);
            var react = new AtomicInteger(0);
            var counter = new AtomicInteger(0);
            getChannel().retrieveMessageById(message.getIdLong()).queue(message -> {
                var votes = message.getReactions().stream().filter(r -> react.getAndIncrement() <= options.length).map(r -> String.format(languageContext.get("commands.poll.vote_results"), r.getCount() - 1, options[counter.getAndIncrement()])).collect(Collectors.joining("\n"));
                embedBuilder.addField(languageContext.get("commands.poll.results"), "```diff\n" + votes + "```", false);
                getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
            });
            getRunningPolls().remove(getChannel().getId());
        }

        @Override
        public void onCancel() {
            getChannel().sendMessageFormat(languageContext.get("commands.poll.cancelled"), EmoteReference.CORRECT).queue();
            onExpire();
        }
    }, reactions(options.length));
}
Also used : ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) Message(net.dv8tion.jda.api.entities.Message) Color(java.awt.Color) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) Permission(net.dv8tion.jda.api.Permission) Utils(net.kodehawa.mantarobot.utils.Utils) HashMap(java.util.HashMap) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) Collectors(java.util.stream.Collectors) ReactionOperations(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations) Lobby(net.kodehawa.mantarobot.commands.interaction.Lobby) TimeUnit(java.util.concurrent.TimeUnit) Future(java.util.concurrent.Future) Stream(java.util.stream.Stream) I18nContext(net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) Map(java.util.Map) MantaroData(net.kodehawa.mantarobot.data.MantaroData) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) Context(net.kodehawa.mantarobot.core.modules.commands.base.Context) Operation(net.kodehawa.mantarobot.core.listeners.operations.core.Operation) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 13 with I18nContext

use of net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext in project MantaroBot by Mantaro.

the class Character method onStart.

@Override
public boolean onStart(GameLobby lobby) {
    final I18nContext languageContext = lobby.getLanguageContext();
    try {
        var data = JsonDataManager.fromJson(APIUtils.getFrom("/mantaroapi/bot/character"), AnimeGameData.class);
        characterNameL = new ArrayList<>();
        characterName = data.getName();
        var imageUrl = data.getImage();
        // Allow for replying with only the first name of the character.
        if (characterName.contains(" ")) {
            characterNameL.add(characterName.split(" ")[0]);
        }
        characterNameL.add(characterName);
        sendEmbedImage(lobby.getChannel(), imageUrl, eb -> eb.setAuthor(languageContext.get("commands.game.character_start"), null, lobby.getEvent().getAuthor().getEffectiveAvatarUrl()).setFooter(languageContext.get("commands.game.end_footer"), null)).queue(success -> lobby.setGameLoaded(true));
        return true;
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
        lobby.getChannel().sendMessageFormat(languageContext.get("commands.game.character_load_error"), EmoteReference.WARNING, characterName).queue();
        return false;
    } catch (InsufficientPermissionException ex) {
        lobby.getChannel().sendMessageFormat(languageContext.get("commands.game.error_missing_permissions"), EmoteReference.ERROR).queue();
        return false;
    } catch (Exception e) {
        lobby.getChannel().sendMessageFormat(languageContext.get("commands.game.error"), EmoteReference.ERROR).queue();
        log.warn("Exception while setting up a game", e);
        return false;
    }
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) ImageGame(net.kodehawa.mantarobot.commands.game.core.ImageGame) Logger(org.slf4j.Logger) GameLobby(net.kodehawa.mantarobot.commands.game.core.GameLobby) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) LoggerFactory(org.slf4j.LoggerFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InteractiveOperation(net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation) ArrayList(java.util.ArrayList) APIUtils(net.kodehawa.mantarobot.utils.APIUtils) List(java.util.List) I18nContext(net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext) JsonDataManager(net.kodehawa.mantarobot.utils.data.JsonDataManager) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) AnimeGameData(net.kodehawa.mantarobot.commands.game.core.AnimeGameData) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) I18nContext(net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext)

Aggregations

I18nContext (net.kodehawa.mantarobot.core.modules.commands.i18n.I18nContext)13 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)11 Context (net.kodehawa.mantarobot.core.modules.commands.base.Context)10 MantaroData (net.kodehawa.mantarobot.data.MantaroData)9 Subscribe (com.google.common.eventbus.Subscribe)8 TimeUnit (java.util.concurrent.TimeUnit)8 CommandRegistry (net.kodehawa.mantarobot.core.CommandRegistry)8 Module (net.kodehawa.mantarobot.core.modules.Module)8 CommandCategory (net.kodehawa.mantarobot.core.modules.commands.base.CommandCategory)8 HelpContent (net.kodehawa.mantarobot.core.modules.commands.help.HelpContent)8 Utils (net.kodehawa.mantarobot.utils.Utils)8 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 IncreasingRateLimiter (net.kodehawa.mantarobot.utils.commands.ratelimit.IncreasingRateLimiter)7 RatelimitUtils (net.kodehawa.mantarobot.utils.commands.ratelimit.RatelimitUtils)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)6 Permission (net.dv8tion.jda.api.Permission)6 Badge (net.kodehawa.mantarobot.commands.currency.profile.Badge)6 SubCommand (net.kodehawa.mantarobot.core.modules.commands.SubCommand)6 Command (net.kodehawa.mantarobot.core.modules.commands.base.Command)6