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 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 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 getAwards(Member member) { Map 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; } }