Search in sources :

Example 1 with ItemStack

use of net.kodehawa.mantarobot.commands.currency.item.ItemStack in project MantaroBot by Mantaro.

the class CurrencyCmds method transferItems.

@Command
public static void transferItems(CommandRegistry cr) {
    cr.register("itemtransfer", new SimpleCommand(Category.CURRENCY) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (args.length < 2) {
                onError(event);
                return;
            }
            List<User> mentionedUsers = event.getMessage().getMentionedUsers();
            if (mentionedUsers.size() == 0)
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention a user").queue();
            else {
                User giveTo = mentionedUsers.get(0);
                if (event.getAuthor().getId().equals(giveTo.getId())) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot transfer an item to yourself!").queue();
                    return;
                }
                Item item = Items.fromAny(args[1]).orElse(null);
                if (item == null) {
                    event.getChannel().sendMessage("There isn't an item associated with this emoji.").queue();
                } else {
                    Player player = MantaroData.db().getPlayer(event.getAuthor());
                    Player giveToPlayer = MantaroData.db().getPlayer(giveTo);
                    if (args.length == 2) {
                        if (player.getInventory().containsItem(item)) {
                            if (item.isHidden()) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot transfer this item!").queue();
                                return;
                            }
                            if (giveToPlayer.getInventory().asMap().getOrDefault(item, new ItemStack(item, 0)).getAmount() >= 5000) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "Don't do that").queue();
                                return;
                            }
                            player.getInventory().process(new ItemStack(item, -1));
                            giveToPlayer.getInventory().process(new ItemStack(item, 1));
                            event.getChannel().sendMessage(EmoteReference.OK + event.getAuthor().getAsMention() + " gave 1 " + item.getName() + " to " + giveTo.getAsMention()).queue();
                        } else {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You don't have any of these items in your inventory").queue();
                        }
                        player.saveAsync();
                        giveToPlayer.saveAsync();
                        return;
                    }
                    try {
                        int amount = Math.abs(Integer.parseInt(args[2]));
                        if (player.getInventory().containsItem(item) && player.getInventory().getAmount(item) >= amount) {
                            if (item.isHidden()) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot transfer this item!").queue();
                                return;
                            }
                            if (giveToPlayer.getInventory().asMap().getOrDefault(item, new ItemStack(item, 0)).getAmount() + amount >= 5000) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "Don't do that").queue();
                                return;
                            }
                            player.getInventory().process(new ItemStack(item, amount * -1));
                            giveToPlayer.getInventory().process(new ItemStack(item, amount));
                            event.getChannel().sendMessage(EmoteReference.OK + event.getAuthor().getAsMention() + " gave " + amount + " " + item.getName() + " to " + giveTo.getAsMention()).queue();
                        } else
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You don't have enough of this item " + "to do that").queue();
                    } catch (NumberFormatException nfe) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid number provided").queue();
                    }
                    player.saveAsync();
                    giveToPlayer.saveAsync();
                }
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Transfer Items command").setDescription("**Transfers items from you to another player.**").addField("Usage", "`~>itemtransfer <@user> <item emoji> <amount (optional)>` - **Transfers the item to player x**", false).addField("Parameters", "`@user` - user to send the item to\n" + "`item emoji` - write out the emoji of the item you want to send\n" + "`amount` - optional, send a specific amount of an item to someone.", false).addField("Important", "You cannot send more items than what you already have", false).build();
        }
    });
    cr.registerAlias("itemtransfer", "transferitems");
}
Also used : Item(net.kodehawa.mantarobot.commands.currency.item.Item) Player(net.kodehawa.mantarobot.data.entities.Player) DBUser(net.kodehawa.mantarobot.data.entities.DBUser) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) ItemStack(net.kodehawa.mantarobot.commands.currency.item.ItemStack) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 2 with ItemStack

use of net.kodehawa.mantarobot.commands.currency.item.ItemStack in project MantaroBot by Mantaro.

the class CurrencyCmds method market.

@Command
public static void market(CommandRegistry cr) {
    cr.register("market", new SimpleCommand(Category.CURRENCY) {

        RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 5);

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (!rateLimiter.process(event.getAuthor().getId())) {
                event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Wait! You're calling me so fast that I can't get enough items!").queue();
                return;
            }
            Player player = MantaroData.db().getPlayer(event.getMember());
            if (args.length > 0) {
                int itemNumber = 1;
                String itemName = content.replace(args[0] + " ", "");
                boolean isMassive = !itemName.isEmpty() && itemName.split(" ")[0].matches("^[0-9]*$");
                if (isMassive) {
                    try {
                        itemNumber = Math.abs(Integer.valueOf(itemName.split(" ")[0]));
                        itemName = itemName.replace(args[1] + " ", "");
                    } catch (Exception e) {
                        if (e instanceof NumberFormatException) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "Not a valid number of items to buy.").queue();
                        } else {
                            onHelp(event);
                            return;
                        }
                    }
                }
                if (itemNumber > 5000) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "You can't buy more than 5000 items").queue();
                    return;
                }
                if (args[0].equals("sell")) {
                    try {
                        if (args[1].equals("all")) {
                            long all = player.getInventory().asList().stream().filter(item -> item.getItem().isSellable()).mapToLong(value -> (long) (value.getItem().getValue() * value.getAmount() * 0.9d)).sum();
                            if (args.length > 2 && args[2].equals("calculate")) {
                                event.getChannel().sendMessage(EmoteReference.THINKING + "You'll get **" + all + "** credits if you " + "sell all of your items").queue();
                                return;
                            }
                            player.getInventory().clearOnlySellables();
                            if (player.addMoney(all)) {
                                event.getChannel().sendMessage(EmoteReference.MONEY + "You sold all your inventory items and gained " + all + " credits!").queue();
                            } else {
                                event.getChannel().sendMessage(EmoteReference.MONEY + "You sold all your inventory items and gained " + all + " credits. But you already had too many credits. Your bag overflowed" + ".\nCongratulations, you exploded a Java long (how??). Here's a buggy money bag for you.").queue();
                            }
                            player.saveAsync();
                            return;
                        }
                        Item toSell = Items.fromAny(itemName).orElse(null);
                        if (!toSell.isSellable()) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell an item that cannot be sold.").queue();
                            return;
                        }
                        if (player.getInventory().asMap().getOrDefault(toSell, null) == null) {
                            event.getChannel().sendMessage(EmoteReference.STOP + "You cannot sell an item you don't have.").queue();
                            return;
                        }
                        if (player.getInventory().getAmount(toSell) < itemNumber) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot sell more items than what you have.").queue();
                            return;
                        }
                        int many = itemNumber * -1;
                        long amount = Math.round((toSell.getValue() * 0.9)) * Math.abs(many);
                        player.getInventory().process(new ItemStack(toSell, many));
                        if (player.addMoney(amount)) {
                            event.getChannel().sendMessage(EmoteReference.CORRECT + "You sold " + Math.abs(many) + " **" + toSell.getName() + "** and gained " + amount + " credits!").queue();
                        } else {
                            event.getChannel().sendMessage(EmoteReference.CORRECT + "You sold **" + toSell.getName() + "** and gained" + amount + " credits. But you already had too many credits. Your bag overflowed" + ".\nCongratulations, you exploded a Java long (how??). Here's a buggy money bag for you.").queue();
                        }
                        player.save();
                        return;
                    } catch (Exception e) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Item doesn't exist or invalid syntax").queue();
                        e.printStackTrace();
                    }
                    return;
                }
                if (args[0].equals("buy")) {
                    Item itemToBuy = Items.fromAny(itemName).orElse(null);
                    if (itemToBuy == null) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy an unexistant item.").queue();
                        return;
                    }
                    try {
                        if (!itemToBuy.isBuyable()) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy an item that cannot be bought.").queue();
                            return;
                        }
                        ItemStack stack = player.getInventory().getStackOf(itemToBuy);
                        if (stack != null && !stack.canJoin(new ItemStack(itemToBuy, itemNumber))) {
                            //assume overflow
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot buy more of that object!").queue();
                            return;
                        }
                        if (player.removeMoney(itemToBuy.getValue() * itemNumber)) {
                            player.getInventory().process(new ItemStack(itemToBuy, itemNumber));
                            player.save();
                            event.getChannel().sendMessage(EmoteReference.OK + "Bought " + itemNumber + " " + itemToBuy.getEmoji() + " successfully. You now have " + player.getMoney() + " credits.").queue();
                        } else {
                            event.getChannel().sendMessage(EmoteReference.STOP + "You don't have enough money to buy this item.").queue();
                        }
                        return;
                    } catch (Exception e) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Item doesn't exist or invalid syntax.").queue();
                    }
                    return;
                }
            }
            EmbedBuilder embed = baseEmbed(event, EmoteReference.MARKET + "Mantaro Market");
            Stream.of(Items.ALL).forEach(item -> {
                if (!item.isHidden()) {
                    String buyValue = item.isBuyable() ? EmoteReference.BUY + "$" + String.valueOf((int) Math.floor(item.getValue() * 1.1)) + " " : "";
                    String sellValue = item.isSellable() ? EmoteReference.SELL + "$" + String.valueOf((int) Math.floor(item.getValue() * 0.9)) : "";
                    embed.addField(item.getEmoji() + " " + item.getName(), buyValue + sellValue, true);
                }
            });
            event.getChannel().sendMessage(embed.setThumbnail("https://i.imgur.com/OA7QCaM.png").build()).queue();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Mantaro's market").setDescription("**List current items for buying and selling.**").addField("Buying and selling", "To buy do ~>market buy <item emoji>. It will substract the value from your money" + " and give you the item.\n" + "To sell do `~>market sell all` to sell all your items or `~>market sell <item emoji>` to sell the " + "specified item. " + "**You'll get the sell value of the item on coins to spend.**", false).addField("To know", "If you don't have enough money you cannot buy the items.", false).addField("Information", "To buy and sell multiple items you need to do `~>market <buy/sell> <amount> <item>`", false).build();
        }
    });
}
Also used : Items(net.kodehawa.mantarobot.commands.currency.item.Items) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) UserData(net.kodehawa.mantarobot.data.entities.helpers.UserData) java.util(java.util) Member(net.dv8tion.jda.core.entities.Member) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) MantaroShard(net.kodehawa.mantarobot.MantaroShard) MantaroBot(net.kodehawa.mantarobot.MantaroBot) RethinkDB.r(com.rethinkdb.RethinkDB.r) Inventory(net.kodehawa.mantarobot.data.entities.helpers.Inventory) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Cursor(com.rethinkdb.net.Cursor) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OptArgs(com.rethinkdb.model.OptArgs) StringUtils(br.com.brjdevs.java.utils.texts.StringUtils) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) Player(net.kodehawa.mantarobot.data.entities.Player) ItemStack(net.kodehawa.mantarobot.commands.currency.item.ItemStack) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) DBUser(net.kodehawa.mantarobot.data.entities.DBUser) SPLIT_PATTERN(net.kodehawa.mantarobot.utils.StringUtils.SPLIT_PATTERN) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) Category(net.kodehawa.mantarobot.modules.commands.base.Category) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) Guild(net.dv8tion.jda.core.entities.Guild) Stream(java.util.stream.Stream) User(net.dv8tion.jda.core.entities.User) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) OrderBy(com.rethinkdb.gen.ast.OrderBy) Item(net.kodehawa.mantarobot.commands.currency.item.Item) Player(net.kodehawa.mantarobot.data.entities.Player) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) Item(net.kodehawa.mantarobot.commands.currency.item.Item) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) ItemStack(net.kodehawa.mantarobot.commands.currency.item.ItemStack) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 3 with ItemStack

use of net.kodehawa.mantarobot.commands.currency.item.ItemStack in project MantaroBot by Mantaro.

the class CurrencyCmds method openLootBox.

public static void openLootBox(GuildMessageReceivedEvent event, boolean special) {
    List<Item> toAdd = new ArrayList<>();
    int amtItems = random.nextInt(3) + 3;
    List<Item> items = new ArrayList<>();
    items.addAll(Arrays.asList(Items.ALL));
    items.removeIf(item -> item.isHidden() || !item.isBuyable() || !item.isSellable());
    items.sort((o1, o2) -> {
        if (o1.getValue() > o2.getValue())
            return 1;
        if (o1.getValue() == o2.getValue())
            return 0;
        return -1;
    });
    if (!special) {
        for (Item i : Items.ALL) if (i.isHidden() || !i.isBuyable() || i.isSellable())
            items.add(i);
    }
    for (int i = 0; i < amtItems; i++) toAdd.add(selectReverseWeighted(items));
    Player player = MantaroData.db().getPlayer(event.getMember());
    ArrayList<ItemStack> ita = new ArrayList<>();
    toAdd.forEach(item -> ita.add(new ItemStack(item, 1)));
    boolean overflow = player.getInventory().merge(ita);
    player.save();
    event.getChannel().sendMessage(EmoteReference.LOOT_CRATE.getDiscordNotation() + "**You won:** " + toAdd.stream().map(Item::toString).collect(Collectors.joining(", ")) + (overflow ? ". But you already had too much, so you decided to throw away the excess" : "")).queue();
}
Also used : Item(net.kodehawa.mantarobot.commands.currency.item.Item) Player(net.kodehawa.mantarobot.data.entities.Player) ItemStack(net.kodehawa.mantarobot.commands.currency.item.ItemStack)

Aggregations

Item (net.kodehawa.mantarobot.commands.currency.item.Item)3 ItemStack (net.kodehawa.mantarobot.commands.currency.item.ItemStack)3 Player (net.kodehawa.mantarobot.data.entities.Player)3 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)2 User (net.dv8tion.jda.core.entities.User)2 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)2 DBUser (net.kodehawa.mantarobot.data.entities.DBUser)2 Command (net.kodehawa.mantarobot.modules.Command)2 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)2 StringUtils (br.com.brjdevs.java.utils.texts.StringUtils)1 RethinkDB.r (com.rethinkdb.RethinkDB.r)1 OrderBy (com.rethinkdb.gen.ast.OrderBy)1 OptArgs (com.rethinkdb.model.OptArgs)1 Cursor (com.rethinkdb.net.Cursor)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1