diff --git a/src/main/java/me/tbg/match/bot/DiscordBot.java b/src/main/java/me/tbg/match/bot/DiscordBot.java index 63c73a6..209a980 100644 --- a/src/main/java/me/tbg/match/bot/DiscordBot.java +++ b/src/main/java/me/tbg/match/bot/DiscordBot.java @@ -35,6 +35,8 @@ public class DiscordBot { private Logger logger; private Map matchMessageMap = new HashMap<>(); + private Map matchStartTimestamps = new HashMap<>(); + private Map matchStartPlayers = new HashMap<>(); public DiscordBot(BotConfig config, Logger logger) { this.config = config; @@ -105,6 +107,23 @@ public void editMatchEmbed(long matchId, EmbedBuilder newEmbed) { } } + public EmbedBuilder setEmbedThumbnail(MapInfo map, EmbedBuilder embed, DiscordBot bot) { + try { + embed.setThumbnail(bot.getMapImage(map)); + return embed; + } catch (IOException e) { + if (!bot.getConfig().getFallbackMapImages().isEmpty()) { + String mapName = map.getName().replace(" ", "%20"); + embed.setThumbnail(bot.getConfig().getFallbackMapImages() + mapName + "/map.png"); + return embed; + } else if (!bot.getConfig().getMapImageNotFound().isEmpty()) { + embed.setThumbnail(bot.getConfig().getMapImageNotFound()); + return embed; + } + } + return embed; + } + public String parseDuration(Duration duration) { long hours = duration.toHours(); long minutes = duration.toMinutes(); @@ -172,6 +191,19 @@ public long getOnlineStaffCount(Match match) { .count(); } + public void storeMatchStartData(long matchId, Long startTimestamp, Integer players) { + matchStartTimestamps.put(matchId, startTimestamp); + matchStartPlayers.put(matchId, players); + } + + public Long getMatchStartTimestamp(long matchId) { + return matchStartTimestamps.get(matchId); + } + + public Integer getMatchStartPlayers(long matchId) { + return matchStartPlayers.get(matchId); + } + public void reload() { if (this.api != null && !config.isEnabled()) { disable(); diff --git a/src/main/java/me/tbg/match/bot/MatchFinishListener.java b/src/main/java/me/tbg/match/bot/MatchFinishListener.java index 83b2c04..452e250 100644 --- a/src/main/java/me/tbg/match/bot/MatchFinishListener.java +++ b/src/main/java/me/tbg/match/bot/MatchFinishListener.java @@ -1,7 +1,6 @@ package me.tbg.match.bot; import java.awt.Color; -import java.io.IOException; import java.time.Instant; import java.util.Comparator; import java.util.Map; @@ -68,7 +67,11 @@ private EmbedBuilder createMatchFinishEmbed( EmbedBuilder embed = new EmbedBuilder() .setColor(winnerColor) .setTitle("Match #" + match.getId() + " has finished!") - .setDescription("Finished at with **" + .setDescription("Started at with **" + + bot.getMatchStartPlayers(Long.parseLong(match.getId())) + + (bot.getMatchStartPlayers(Long.parseLong(match.getId())) == 1 ? " player" : " players") + + "** and finished at with **" + match.getPlayers().size() + (match.getPlayers().size() == 1 ? " player" : " players") + "** online.") .addInlineField("Winner", winner) @@ -100,15 +103,7 @@ private EmbedBuilder createMatchFinishEmbed( .addInlineField("Staff", String.valueOf(bot.getOnlineStaffCount(match))) .setFooter("Map tags: " + map.getTags().toString()); - try { - embed.setThumbnail(bot.getMapImage(map)); - } catch (IOException e) { - if (!bot.getConfig().getFallbackMapImages().isEmpty()) { - embed.setThumbnail(bot.getConfig().getFallbackMapImages() + map.getName()); - } else if (!bot.getConfig().getMapImageNotFound().isEmpty()) { - embed.setThumbnail(bot.getConfig().getMapImageNotFound()); - } - } + bot.setEmbedThumbnail(map, embed, bot); return embed; } diff --git a/src/main/java/me/tbg/match/bot/MatchStartListener.java b/src/main/java/me/tbg/match/bot/MatchStartListener.java index 5ac8d34..8e0a5e9 100644 --- a/src/main/java/me/tbg/match/bot/MatchStartListener.java +++ b/src/main/java/me/tbg/match/bot/MatchStartListener.java @@ -1,7 +1,8 @@ package me.tbg.match.bot; import java.awt.Color; -import java.io.IOException; +import java.time.Instant; + import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.javacord.api.entity.message.embed.EmbedBuilder; @@ -23,17 +24,12 @@ public void onMatchStart(MatchStartEvent event) { MapInfo map = match.getMap(); EmbedBuilder matchStartEmbed = createMatchStartEmbed(match, map); - try { - matchStartEmbed.setThumbnail(bot.getMapImage(map)); - } catch (IOException e) { - if (!bot.getConfig().getFallbackMapImages().isEmpty()) { - matchStartEmbed.setThumbnail(bot.getConfig().getFallbackMapImages() + map.getName()); - } else if (!bot.getConfig().getMapImageNotFound().isEmpty()) { - matchStartEmbed.setThumbnail(bot.getConfig().getMapImageNotFound()); - } - } - + bot.setEmbedThumbnail(map, matchStartEmbed, bot); bot.sendMatchEmbed(matchStartEmbed, match); + bot.storeMatchStartData( + Long.parseLong(match.getId()), + Instant.now().getEpochSecond(), + match.getPlayers().size()); } private EmbedBuilder createMatchStartEmbed(Match match, MapInfo map) {