Search in sources :

Example 1 with Player

use of net.kodehawa.mantarobot.data.entities.Player 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 Player

use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.

the class MoneyCmds method loot.

@Command
public static void loot(CommandRegistry cr) {
    RateLimiter rateLimiter = new RateLimiter(TimeUnit.MINUTES, 5);
    Random r = new Random();
    cr.register("loot", new SimpleCommand(Category.CURRENCY) {

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            String id = event.getAuthor().getId();
            Player player = MantaroData.db().getPlayer(event.getMember());
            if (!rateLimiter.process(id)) {
                event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Cooldown a lil bit, you can only do this once every 5 minutes.\n **You'll be able to use this command again " + "in " + Utils.getVerboseTime(rateLimiter.tryAgainIn(event.getAuthor())) + ".**").queue();
                return;
            }
            TextChannelGround ground = TextChannelGround.of(event);
            if (r.nextInt(450) == 0) {
                //1 in 450 chance of it dropping a loot crate.
                ground.dropItem(Items.LOOT_CRATE);
            }
            List<ItemStack> loot = ground.collectItems();
            int moneyFound = ground.collectMoney() + Math.max(0, r.nextInt(50) - 10);
            if (MantaroData.db().getUser(event.getMember()).isPremium() && moneyFound > 0) {
                moneyFound = moneyFound + random.nextInt(moneyFound);
            }
            if (!loot.isEmpty()) {
                String s = ItemStack.toString(ItemStack.reduce(loot));
                String overflow;
                if (player.getInventory().merge(loot)) {
                    overflow = "But you already had too many items, so you decided to throw away the excess. ";
                } else {
                    overflow = "";
                }
                if (moneyFound != 0) {
                    if (player.addMoney(moneyFound)) {
                        event.getChannel().sendMessage(EmoteReference.POPPER + "Digging through messages, you found " + s + ", along " + "with $" + moneyFound + " credits!" + overflow).queue();
                    } else {
                        event.getChannel().sendMessage(EmoteReference.POPPER + "Digging through messages, you found " + s + ", along " + "with $" + moneyFound + " credits. " + overflow + "But you already had too many credits. Your bag overflowed" + ".\nCongratulations, you exploded a Java long. Here's a buggy money bag for you.").queue();
                    }
                } else {
                    event.getChannel().sendMessage(EmoteReference.MEGA + "Digging through messages, you found " + s + ". " + overflow).queue();
                }
            } else {
                if (moneyFound != 0) {
                    if (player.addMoney(moneyFound)) {
                        event.getChannel().sendMessage(EmoteReference.POPPER + "Digging through messages, you found **$" + moneyFound + " credits!**").queue();
                    } else {
                        event.getChannel().sendMessage(EmoteReference.POPPER + "Digging through messages, you found $" + moneyFound + " credits. But you already had too many credits. Your bag overflowed.\nCongratulations, you exploded " + "a Java long. Here's a buggy money bag for you.").queue();
                    }
                } else {
                    event.getChannel().sendMessage(EmoteReference.SAD + "Digging through messages, you found nothing but dust").queue();
                }
            }
            player.save();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Loot command").setDescription("**Loot the current chat for items, for usage in Mantaro's currency system.**\n" + "Currently, there are ``" + Items.ALL.length + "`` items available in chance," + "in which you have a `random chance` of getting one or more.").addField("Usage", "~>loot", false).build();
        }
    });
}
Also used : Player(net.kodehawa.mantarobot.data.entities.Player) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) RateLimiter(net.kodehawa.mantarobot.commands.currency.RateLimiter) 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 Player

use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.

the class Game method callDefault.

protected boolean callDefault(GuildMessageReceivedEvent e, GameLobby lobby, HashMap<Member, Player> players, List<String> expectedAnswer, int attempts, int maxAttempts) {
    if (!e.getChannel().getId().equals(lobby.getChannel().getId())) {
        return false;
    }
    if (e.getMessage().getContent().startsWith(MantaroData.config().get().getPrefix())) {
        return false;
    }
    if (MantaroData.db().getGuild(lobby.getChannel().getGuild()).getData().getGuildCustomPrefix() != null && e.getMessage().getContent().startsWith(MantaroData.db().getGuild(lobby.getChannel().getGuild()).getData().getGuildCustomPrefix())) {
        return false;
    }
    if (players.keySet().contains(e.getMember())) {
        if (e.getMessage().getContent().equalsIgnoreCase("end")) {
            lobby.getChannel().sendMessage(EmoteReference.CORRECT + "Ended game. Possible answers were: " + expectedAnswer.stream().collect(Collectors.joining(" ,"))).queue();
            lobby.startNextGame();
            GameLobby.LOBBYS.remove(lobby.getChannel());
            return true;
        }
        if (attempts > maxAttempts) {
            lobby.getChannel().sendMessage(EmoteReference.ERROR + "Already used all attempts, ending game. Possible answers were: " + expectedAnswer.stream().collect(Collectors.joining(" ,"))).queue();
            //This should take care of removing the lobby, actually.
            lobby.startNextGame();
            return true;
        }
        if (expectedAnswer.stream().anyMatch(e.getMessage().getRawContent()::equalsIgnoreCase)) {
            Player player = players.get(e.getMember());
            player.addMoney(45);
            player.save();
            TextChannelGround.of(e).dropItemWithChance(Items.FLOPPY_DISK, 3);
            lobby.getChannel().sendMessage(EmoteReference.MEGA + "**" + e.getMember().getEffectiveName() + "**" + " Just won $45 credits by answering correctly!").queue();
            lobby.startNextGame();
            return true;
        }
        lobby.getChannel().sendMessage(EmoteReference.ERROR + "That's not it, you have " + (maxAttempts - attempts) + " attempts remaning.").queue();
        setAttempts(getAttempts() + 1);
        return false;
    }
    return false;
}
Also used : Player(net.kodehawa.mantarobot.data.entities.Player)

Example 4 with Player

use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.

the class CommandListener method onEvent.

@Override
public void onEvent(Event event) {
    if (event instanceof ShardMonitorEvent) {
        if (MantaroBot.getInstance().getShards()[shardId].getEventManager().getLastJDAEventTimeDiff() > 120000)
            return;
        ((ShardMonitorEvent) event).alive(shardId, ShardMonitorEvent.COMMAND_LISTENER);
        return;
    }
    if (event instanceof GuildMessageReceivedEvent) {
        GuildMessageReceivedEvent msg = (GuildMessageReceivedEvent) event;
        messageCache.put(msg.getMessage().getId(), Optional.of(msg.getMessage()));
        if (msg.getAuthor().isBot() || msg.getAuthor().equals(msg.getJDA().getSelfUser()))
            return;
        Async.thread("Cmd:" + msg.getAuthor().getName() + "#" + msg.getAuthor().getDiscriminator() + ":" + msg.getMessage().getRawContent(), () -> onCommand(msg));
        if (random.nextInt(15) > 10) {
            if (((GuildMessageReceivedEvent) event).getMember() == null)
                return;
            Player player = MantaroData.db().getPlayer(((GuildMessageReceivedEvent) event).getMember());
            if (player != null) {
                //kill me
                if (player.getLevel() == 0)
                    player.setLevel(1);
                player.getData().setExperience(player.getData().getExperience() + Math.round(random.nextInt(6)));
                if (player.getData().getExperience() > (player.getLevel() * Math.log10(player.getLevel()) * 1000)) {
                    player.setLevel(player.getLevel() + 1);
                }
                player.saveAsync();
            }
        }
    }
}
Also used : Player(net.kodehawa.mantarobot.data.entities.Player) ShardMonitorEvent(net.kodehawa.mantarobot.core.ShardMonitorEvent) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)

Example 5 with Player

use of net.kodehawa.mantarobot.data.entities.Player in project MantaroBot by Mantaro.

the class CurrencyCmds method inventory.

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

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            Player user = MantaroData.db().getPlayer(event.getMember());
            EmbedBuilder builder = baseEmbed(event, event.getMember().getEffectiveName() + "'s Inventory", event.getAuthor().getEffectiveAvatarUrl());
            List<ItemStack> list = user.getInventory().asList();
            if (list.isEmpty())
                builder.setDescription("There is only dust.");
            else
                user.getInventory().asList().forEach(stack -> {
                    long buyValue = stack.getItem().isBuyable() ? (long) (stack.getItem().getValue() * 1.1) : 0;
                    long sellValue = stack.getItem().isSellable() ? (long) (stack.getItem().getValue() * 0.9) : 0;
                    builder.addField(stack.getItem().getEmoji() + " " + stack.getItem().getName() + " x " + stack.getAmount(), String.format("**Price**: 📥 %d 📤 %d\n%s", buyValue, sellValue, stack.getItem().getDesc()), false);
                });
            event.getChannel().sendMessage(builder.build()).queue();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Inventory command").setDescription("**Shows your current inventory.**").build();
        }
    });
}
Also used : Player(net.kodehawa.mantarobot.data.entities.Player) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Aggregations

Player (net.kodehawa.mantarobot.data.entities.Player)11 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)9 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)8 Command (net.kodehawa.mantarobot.modules.Command)8 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)8 User (net.dv8tion.jda.core.entities.User)5 RateLimiter (net.kodehawa.mantarobot.commands.currency.RateLimiter)5 Item (net.kodehawa.mantarobot.commands.currency.item.Item)3 ItemStack (net.kodehawa.mantarobot.commands.currency.item.ItemStack)3 DBUser (net.kodehawa.mantarobot.data.entities.DBUser)3 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 Member (net.dv8tion.jda.core.entities.Member)2 TextChannelGround (net.kodehawa.mantarobot.commands.currency.TextChannelGround)2 MantaroData (net.kodehawa.mantarobot.data.MantaroData)2 CommandRegistry (net.kodehawa.mantarobot.modules.CommandRegistry)2 Module (net.kodehawa.mantarobot.modules.Module)2 Category (net.kodehawa.mantarobot.modules.commands.base.Category)2 PostLoadEvent (net.kodehawa.mantarobot.modules.events.PostLoadEvent)2 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)2 StringUtils (br.com.brjdevs.java.utils.texts.StringUtils)1