summaryrefslogtreecommitdiff
path: root/src/main/java/net/uomc/mineshaft/FishCommand.java
blob: 3df799484c3e860e1a33ab7516349b6d86576260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package net.uomc.mineshaft;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.json.JSONObject;

import com.mouldycheerio.dbot.CustomBot;
import com.mouldycheerio.dbot.commands.CommandDetails;
import com.mouldycheerio.dbot.commands.cooldowns.CooldownCommand;
import com.mouldycheerio.dbot.util.EventWaiter;
import com.vdurmont.emoji.EmojiParser;

import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.uomc.mineshaft.resources.ResourceManager;

public class FishCommand extends CooldownCommand {
    private static final double XP_SPAWN_AMOUNT = 2000;
    private static final double BOOK_SPAWN_CHANCE = 0.05;
    private static long MIN_WAIT_MS = 1500;
    private static long MAX_WAIT_MS = 10000;

    private static long MIN_REEL_MS = 1000;
    private static long MAX_REEL_MS = 4000;

    private static final String REACTION_CLICK = ":fishing_pole_and_fish:";
    private static final String REACTION_FISH = ":fish:";
    private static final String TITLE = "Fishing";
    private static final String IMAGE_ROD = "https://minecraft.wiki/images/Fishing_Rod_JE2_BE2.png";

    private Mineshaft bot;

    protected FishCommand(Mineshaft bot) {
        super(bot);
        setCooldown(20l * 1000l);
        this.bot = bot;
        setDetails(CommandDetails.from("fish", "go fishing"));
    }

    @Override
    public boolean trigger(MessageReceivedEvent e) {

        e.getMessage().addReaction(Emoji.fromUnicode(EmojiParser.parseToUnicode(REACTION_FISH))).queue();
        double random = Math.random();

        long waitTime = MIN_WAIT_MS + (long) (Math.random() * (MAX_WAIT_MS - MIN_WAIT_MS));
        long reelTime = MIN_REEL_MS + (long) (Math.random() * (MAX_REEL_MS - MIN_REEL_MS));

        Map<MineshaftItem, Long> awards = getAwards(e.getMember());

        MessageEmbed messageEmbed = bot.createMessageEmbed(e.getMessage(), TITLE, "You sit and wait for a fish...",
                IMAGE_ROD, Color.GRAY);

        MessageEmbed messageEmbedReel = bot.createMessageEmbed(e.getMessage(), TITLE,
                "**You feel something bite your bobber!**\n\nHit the reaction to reel in", IMAGE_ROD, bot.getColor());

        MessageEmbed messageEmbedFail = bot.createMessageEmbed(e.getMessage(), TITLE, ":x: The fish managed to get away...",
                "", Color.GRAY);

        MessageEmbed messageEmbedSuccess = bot.createMessageEmbed(e.getMessage(), TITLE,
                String.format("CATCH!\n%s", bot.createItemList(awards, "+%s")), "", bot.getColor());

        Consumer<Message> cleanup = m -> {
            m.clearReactions().queue();
            m.delete().queueAfter(5, TimeUnit.SECONDS);
        };

        e.getMessage().replyEmbeds(messageEmbed).queue(message -> {
            message.addReaction(Emoji.fromUnicode(EmojiParser.parseToUnicode(REACTION_CLICK)))
                    .queueAfter(waitTime, TimeUnit.MILLISECONDS, r -> {
                        EventWaiter eventWaiter = new EventWaiter();
                        eventWaiter.waitForEvent(MessageReactionAddEvent.class,
                                e1 -> e1.getMessageId().equals(message.getId())
                                        && e1.getUser().getId().equals(e.getAuthor().getId()),
                                e1 -> {
                                    bot.addItems(e.getMember(), awards);
                                    message.editMessageEmbeds(messageEmbedSuccess).queue();
                                }, reelTime, TimeUnit.MILLISECONDS, () -> {
                                    message.editMessageEmbeds(messageEmbedFail).queue();
                                    ;
                                    cleanup.accept(message);
                                });
                        message.editMessageEmbeds(messageEmbedReel).queue(x -> {
                            e.getJDA().addEventListener(eventWaiter);
                        });
                    });
        });

        return true;
    }

    public Map<MineshaftItem, Long> getAwards(Member member) {
        Map<MineshaftItem, Long> award = new HashMap<>();
        if (Math.random() < BOOK_SPAWN_CHANCE) {
            award.put(MineshaftItem.BOOK, 1l);
        } else {
            award.put(MineshaftItem.FISH, Math.random() > 0.5 ? 1l : 2l);
            award.put(MineshaftItem.XP, (long) Math.ceil(Math.random() * XP_SPAWN_AMOUNT));
        }
        return award;
    }
}