Search in sources :

Example 1 with Rod

use of com.sx4.bot.entities.economy.item.Rod in project Sx4 by sx4-discord-bot.

the class FishCommand method onCommand.

public void onCommand(Sx4CommandEvent event) {
    EmbedBuilder embed = new EmbedBuilder();
    event.getMongo().withTransaction(session -> {
        Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.ROD.getId()));
        Document data = event.getMongo().getItems().find(session, filter).first();
        if (data == null) {
            event.replyFailure("You do not have a fishing rod").queue();
            session.abortTransaction();
            return;
        }
        CooldownItemStack<Rod> rodStack = new CooldownItemStack<>(event.getBot().getEconomyManager(), data);
        long usableAmount = rodStack.getUsableAmount();
        if (usableAmount == 0) {
            event.reply("Slow down! You can go fishing again in " + TimeUtility.LONG_TIME_FORMATTER.parse(rodStack.getTimeRemaining()) + " :stopwatch:").queue();
            session.abortTransaction();
            return;
        }
        Rod rod = rodStack.getItem();
        long yield = rod.getYield(event.getBot().getEconomyManager());
        embed.setAuthor(event.getAuthor().getName(), null, event.getAuthor().getEffectiveAvatarUrl()).setColor(event.getMember().getColorRaw()).setDescription(String.format("You fish for 5 minutes and sell your fish! (**$%,d**) :fish:", yield));
        if (rod.getDurability() == 2) {
            embed.appendDescription("\n\nYour fishing rod will break the next time you use it :warning:");
        } else if (rod.getDurability() == 1) {
            embed.appendDescription("\n\nYour fishing rod broke in the process");
        }
        Bson itemFilter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.id", rod.getId()));
        if (rod.getDurability() == 1) {
            event.getMongo().getItems().deleteOne(session, itemFilter);
        } else {
            List<Bson> update = List.of(EconomyUtility.getResetsUpdate(usableAmount, FishCommand.COOLDOWN), Operators.set("item.durability", Operators.subtract("$item.durability", 1)));
            event.getMongo().getItems().updateOne(session, itemFilter, update);
        }
        event.getMongo().getUsers().updateOne(session, Filters.eq("_id", event.getAuthor().getIdLong()), Updates.inc("economy.balance", yield), new UpdateOptions().upsert(true));
    }).whenComplete((updated, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (updated) {
            event.reply(embed.build()).queue();
        }
    });
}
Also used : CooldownItemStack(com.sx4.bot.entities.economy.item.CooldownItemStack) Document(org.bson.Document) Operators(com.sx4.bot.database.mongo.model.Operators) ItemType(com.sx4.bot.entities.economy.item.ItemType) EconomyUtility(com.sx4.bot.utility.EconomyUtility) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) Updates(com.mongodb.client.model.Updates) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) TimeUtility(com.sx4.bot.utility.TimeUtility) Rod(com.sx4.bot.entities.economy.item.Rod) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) UpdateOptions(com.mongodb.client.model.UpdateOptions) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Rod(com.sx4.bot.entities.economy.item.Rod) List(java.util.List) Document(org.bson.Document) CooldownItemStack(com.sx4.bot.entities.economy.item.CooldownItemStack) UpdateOptions(com.mongodb.client.model.UpdateOptions) Bson(org.bson.conversions.Bson)

Example 2 with Rod

use of com.sx4.bot.entities.economy.item.Rod in project Sx4 by sx4-discord-bot.

the class FishingRodCommand method info.

@Command(value = "info", aliases = { "information" }, description = "View information on a users fishing rod")
@CommandId(382)
@Examples({ "fishing rod info", "fishing rod info @Shea#6653", "fishing rod info Shea" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void info(Sx4CommandEvent event, @Argument(value = "user", endless = true, nullDefault = true) Member member) {
    Member effectiveMember = member == null ? event.getMember() : member;
    User user = member == null ? event.getAuthor() : effectiveMember.getUser();
    Bson filter = Filters.and(Filters.eq("userId", effectiveMember.getIdLong()), Filters.eq("item.type", ItemType.ROD.getId()));
    Document data = event.getMongo().getItem(filter, Projections.include("item"));
    if (data == null) {
        event.replyFailure("That user does not have a fishing rod").queue();
        return;
    }
    Rod rod = Rod.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
    EmbedBuilder embed = new EmbedBuilder().setAuthor(user.getName() + "'s " + rod.getName(), null, user.getEffectiveAvatarUrl()).setColor(effectiveMember.getColorRaw()).setThumbnail("https://emojipedia-us.s3.amazonaws.com/thumbs/120/twitter/147/fishing-pole-and-fish_1f3a3.png").addField("Durability", rod.getDurability() + "/" + rod.getMaxDurability(), false).addField("Current Price", String.format("$%,d", rod.getCurrentPrice()), false).addField("Price", String.format("$%,d", rod.getPrice()), false).addField("Yield", String.format("$%,d to $%,d", rod.getMinYield(), rod.getMaxYield()), false);
    event.reply(embed.build()).queue();
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) User(net.dv8tion.jda.api.entities.User) Rod(com.sx4.bot.entities.economy.item.Rod) Document(org.bson.Document) Member(net.dv8tion.jda.api.entities.Member) Bson(org.bson.conversions.Bson) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 3 with Rod

use of com.sx4.bot.entities.economy.item.Rod in project Sx4 by sx4-discord-bot.

the class FishingRodCommand method shop.

@Command(value = "shop", description = "View all the fishing rods you are able to buy or craft")
@CommandId(379)
@Examples({ "fishing rod shop" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void shop(Sx4CommandEvent event) {
    List<Rod> rods = event.getBot().getEconomyManager().getItems(Rod.class);
    PagedResult<Rod> paged = new PagedResult<>(event.getBot(), rods).setPerPage(12).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder().setAuthor("Fishing Rod Shop", null, event.getSelfUser().getEffectiveAvatarUrl()).setTitle("Page " + page.getPage() + "/" + page.getMaxPage()).setDescription("Fishing rods are a good way to gain some money").setFooter(PagedResult.DEFAULT_FOOTER_TEXT);
        page.forEach((rod, index) -> {
            List<ItemStack<CraftItem>> items = rod.getCraft();
            String craft = items.isEmpty() ? "None" : items.stream().map(ItemStack::toString).collect(Collectors.joining("\n"));
            embed.addField(rod.getName(), String.format("Price: $%,d\nCraft: %s\nDurability: %,d", rod.getPrice(), craft, rod.getMaxDurability()), true);
        });
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Rod(com.sx4.bot.entities.economy.item.Rod) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) PagedResult(com.sx4.bot.paged.PagedResult) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 4 with Rod

use of com.sx4.bot.entities.economy.item.Rod in project Sx4 by sx4-discord-bot.

the class FishingRodCommand method upgrade.

@Command(value = "upgrade", description = "Upgrade your fishing rod by a certain attribute")
@CommandId(431)
@Examples({ "fishing rod upgrade money", "fishing rod upgrade durability 5" })
public void upgrade(Sx4CommandEvent event, @Argument(value = "upgrade") Upgrade upgrade, @Argument(value = "upgrades") @DefaultNumber(1) @Limit(min = 1, max = 100) int upgrades) {
    if (!upgrade.containsType(ItemType.ROD)) {
        event.replyFailure("You can not use that upgrade on a fishing rod").queue();
        return;
    }
    event.getMongo().withTransaction(session -> {
        Document data = event.getMongo().getItems().find(session, Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.ROD.getId()))).first();
        if (data == null) {
            event.replyFailure("You do not have a fishing rod").queue();
            session.abortTransaction();
            return null;
        }
        Document item = data.get("item", Document.class);
        Rod defaultRod = event.getBot().getEconomyManager().getItemById(item.getInteger("id"), Rod.class);
        Rod rod = new Rod(item, defaultRod);
        int currentUpgrades = rod.getUpgrades();
        long price = 0;
        for (int i = 0; i < upgrades; i++) {
            price += Math.round(0.015D * defaultRod.getPrice() * currentUpgrades++ + 0.025D * defaultRod.getPrice());
        }
        UpdateResult result = event.getMongo().getUsers().updateOne(session, Filters.eq("_id", event.getAuthor().getIdLong()), List.of(EconomyUtility.decreaseBalanceUpdate(price)));
        if (result.getModifiedCount() == 0) {
            event.replyFormat("You do not have **$%,d** %s", price, event.getConfig().getFailureEmote()).queue();
            session.abortTransaction();
            return null;
        }
        List<Bson> update = new ArrayList<>();
        update.add(Operators.set("item.upgrades", Operators.add(Operators.ifNull("$item.upgrades", 0), upgrades)));
        update.add(Operators.set("item.price", Operators.add("$item.price", Math.round(defaultRod.getPrice() * 0.015D) * upgrades)));
        if (upgrade == Upgrade.MONEY) {
            int increase = (int) Math.round(defaultRod.getMinYield() * upgrade.getValue()) * upgrades;
            update.add(Operators.set("item.minYield", Operators.add("$item.minYield", increase)));
            update.add(Operators.set("item.maxYield", Operators.add("$item.maxYield", increase)));
        } else if (upgrade == Upgrade.DURABILITY) {
            int increase = (int) upgrade.getValue() * upgrades;
            update.add(Operators.set("item.durability", Operators.add("$item.durability", increase)));
            update.add(Operators.set("item.maxDurability", Operators.add("$item.maxDurability", increase)));
        }
        event.getMongo().getItems().updateOne(session, Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.id", rod.getId())), update);
        return String.format("You just upgraded %s %d time%s for your `%s` for **$%,d**", upgrade.getName().toLowerCase(), upgrades, (upgrades == 1 ? "" : "s"), rod.getName(), price);
    }).whenComplete((message, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception) || message == null) {
            return;
        }
        event.replySuccess(message).queue();
    });
}
Also used : Document(org.bson.Document) EconomyUtility(com.sx4.bot.utility.EconomyUtility) Arrays(java.util.Arrays) CancelException(com.sx4.bot.waiter.exception.CancelException) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Permission(net.dv8tion.jda.api.Permission) CraftItem(com.sx4.bot.entities.economy.item.CraftItem) CommandId(com.sx4.bot.annotations.command.CommandId) CompletableFuture(java.util.concurrent.CompletableFuture) Member(net.dv8tion.jda.api.entities.Member) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) ItemStack(com.sx4.bot.entities.economy.item.ItemStack) ButtonUtility(com.sx4.bot.utility.ButtonUtility) UpdateResult(com.mongodb.client.result.UpdateResult) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) com.mongodb.client.model(com.mongodb.client.model) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) EnumSet(java.util.EnumSet) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Upgrade(com.sx4.bot.entities.economy.upgrade.Upgrade) Operators(com.sx4.bot.database.mongo.model.Operators) ItemType(com.sx4.bot.entities.economy.item.ItemType) Sx4Command(com.sx4.bot.core.Sx4Command) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) DefaultNumber(com.sx4.bot.annotations.argument.DefaultNumber) Examples(com.sx4.bot.annotations.command.Examples) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Rod(com.sx4.bot.entities.economy.item.Rod) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Rod(com.sx4.bot.entities.economy.item.Rod) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 5 with Rod

use of com.sx4.bot.entities.economy.item.Rod in project Sx4 by sx4-discord-bot.

the class FishingRodCommand method upgrades.

@Command(value = "upgrades", description = "View all the upgrades you can use on a fishing rod")
@CommandId(432)
@Examples({ "pickaxe upgrades" })
public void upgrades(Sx4CommandEvent event) {
    EnumSet<Upgrade> upgrades = Upgrade.getUpgrades(ItemType.ROD);
    PagedResult<Upgrade> paged = new PagedResult<>(event.getBot(), Arrays.asList(upgrades.toArray(Upgrade[]::new))).setPerPage(3).setCustomFunction(page -> {
        EmbedBuilder embed = new EmbedBuilder().setTitle("Fishing Rod Upgrades");
        page.forEach((upgrade, index) -> embed.addField(upgrade.getName(), upgrade.getDescription(), false));
        return new MessageBuilder().setEmbeds(embed.build());
    });
    paged.execute(event);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Upgrade(com.sx4.bot.entities.economy.upgrade.Upgrade) PagedResult(com.sx4.bot.paged.PagedResult) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)6 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)6 Command (com.jockie.bot.core.command.Command)5 CommandId (com.sx4.bot.annotations.command.CommandId)5 Examples (com.sx4.bot.annotations.command.Examples)5 Rod (com.sx4.bot.entities.economy.item.Rod)5 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)4 PagedResult (com.sx4.bot.paged.PagedResult)4 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)4 ModuleCategory (com.sx4.bot.category.ModuleCategory)3 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)3 Operators (com.sx4.bot.database.mongo.model.Operators)3 ItemStack (com.sx4.bot.entities.economy.item.ItemStack)3 ItemType (com.sx4.bot.entities.economy.item.ItemType)3 Upgrade (com.sx4.bot.entities.economy.upgrade.Upgrade)3 EconomyUtility (com.sx4.bot.utility.EconomyUtility)3 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)3 List (java.util.List)3 Permission (net.dv8tion.jda.api.Permission)3 Member (net.dv8tion.jda.api.entities.Member)3