Skip to content

Commit

Permalink
Touch ups
Browse files Browse the repository at this point in the history
  • Loading branch information
TBG1000 committed Jul 21, 2024
1 parent 9b1b1e5 commit c6dea76
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
32 changes: 32 additions & 0 deletions src/main/java/me/tbg/match/bot/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class DiscordBot {
private Logger logger;

private Map<Long, Long> matchMessageMap = new HashMap<>();
private Map<Long, Long> matchStartTimestamps = new HashMap<>();
private Map<Long, Integer> matchStartPlayers = new HashMap<>();

public DiscordBot(BotConfig config, Logger logger) {
this.config = config;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/me/tbg/match/bot/MatchFinishListener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -68,7 +67,11 @@ private EmbedBuilder createMatchFinishEmbed(
EmbedBuilder embed = new EmbedBuilder()
.setColor(winnerColor)
.setTitle("Match #" + match.getId() + " has finished!")
.setDescription("Finished at <t:" + Instant.now().getEpochSecond() + ":f> with **"
.setDescription("Started at <t:" + bot.getMatchStartTimestamp(Long.parseLong(match.getId()))
+ ":f> with **"
+ bot.getMatchStartPlayers(Long.parseLong(match.getId()))
+ (bot.getMatchStartPlayers(Long.parseLong(match.getId())) == 1 ? " player" : " players")
+ "** and finished at <t:" + Instant.now().getEpochSecond() + ":f> with **"
+ match.getPlayers().size() + (match.getPlayers().size() == 1 ? " player" : " players")
+ "** online.")
.addInlineField("Winner", winner)
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/me/tbg/match/bot/MatchStartListener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit c6dea76

Please sign in to comment.