From f0a1ad472c476132c41245c9b0a73ba8f53d969b Mon Sep 17 00:00:00 2001 From: TBG <46306028+TBG1000@users.noreply.github.com> Date: Sat, 20 Jul 2024 19:49:40 -0600 Subject: [PATCH] More cleanup --- .github/workflows/main.yml | 8 +- pom.xml | 288 +++++++++--------- src/main/java/me/tbg/match/bot/BotConfig.java | 76 +++-- .../java/me/tbg/match/bot/DiscordBot.java | 259 ++++++++-------- src/main/java/me/tbg/match/bot/MatchBot.java | 38 +-- .../me/tbg/match/bot/MatchFinishListener.java | 188 ++++++------ .../me/tbg/match/bot/MatchStartListener.java | 76 ++--- 7 files changed, 492 insertions(+), 441 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c086403..98b9d84 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,14 +6,14 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-java@v1 + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 with: java-version: '8' - name: Build jar run: mvn package - name: artifact - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4 with: name: MatchBot-1.0.0-SNAPSHOT.jar path: target/MatchBot-1.0.0-SNAPSHOT.jar @@ -33,7 +33,7 @@ jobs: - name: Upload Release Asset id: upload-release-asset if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main') - uses: actions/upload-release-asset@v1 + uses: actions/upload-release-asset@v4 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: diff --git a/pom.xml b/pom.xml index 08d334a..ec3cc17 100644 --- a/pom.xml +++ b/pom.xml @@ -1,148 +1,156 @@ - 4.0.0 - me.tbg.match - MatchBot - 1.0.0-SNAPSHOT - MatchBot - A minecraft to discord java plugin + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + me.tbg.match + MatchBot + 1.0.0-SNAPSHOT + MatchBot + A minecraft to discord java plugin - - - - pgm-repo - https://repo.pgm.fyi/snapshots - - - snapshots-repo - https://oss.sonatype.org/content/repositories/snapshots/ - - + + + + pgm-repo + https://repo.pgm.fyi/snapshots + + + snapshots-repo + https://oss.sonatype.org/content/repositories/snapshots/ + + - - - - app.ashcon - sportpaper - 1.8.8-R0.1-SNAPSHOT - provided - - - tc.oc.pgm - core - 0.16-SNAPSHOT - provided - - - tc.oc.pgm - util - 0.16-SNAPSHOT - provided - - - org.javacord - javacord - 3.8.0 - pom - - - - - - - ${basedir}/src/main/resources - true - - + + + + app.ashcon + sportpaper + 1.8.8-R0.1-SNAPSHOT + provided + + + tc.oc.pgm + core + 0.16-SNAPSHOT + provided + + + tc.oc.pgm + util + 0.16-SNAPSHOT + provided + + + org.javacord + javacord + 3.8.0 + pom + + + + + + + ${basedir}/src/main/resources + true + + - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 - - 1.8 - 1.8 - - -parameters - - - - - org.apache.maven.plugins - maven-shade-plugin - 3.2.3 - - false - true - - - *:* - - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - org.apache.logging.log4j - tc.oc.occ.bolt.lib.logger - - - okhttp3 - tc.oc.occ.bolt.lib.okhttp3 - - - okio - tc.oc.occ.bolt.lib.okio - - - com.fasterxml - tc.oc.occ.bolt.lib.jackson - - - - - - package - - shade - - - - + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + -parameters + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.3 + + false + true + + + *:* + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + org.apache.logging.log4j + tc.oc.occ.bolt.lib.logger + + + okhttp3 + tc.oc.occ.bolt.lib.okhttp3 + + + okio + tc.oc.occ.bolt.lib.okio + + + com.fasterxml + tc.oc.occ.bolt.lib.jackson + + + + + + package + + shade + + + + - - - com.coveo - fmt-maven-plugin - 2.9 - - - - - - - check - - - - - - + + + com.diffplug.spotless + spotless-maven-plugin + 2.43.0 + + origin/dev + + + + 2.47.0 + + true + + + + + + + check + + + + + + - - - UTF-8 - true - + + + UTF-8 + true + \ No newline at end of file diff --git a/src/main/java/me/tbg/match/bot/BotConfig.java b/src/main/java/me/tbg/match/bot/BotConfig.java index 2e10c49..10e02db 100644 --- a/src/main/java/me/tbg/match/bot/BotConfig.java +++ b/src/main/java/me/tbg/match/bot/BotConfig.java @@ -4,36 +4,48 @@ public class BotConfig { - private boolean enabled; - - private String token; - private String serverId; - private String matchChannel; - - public BotConfig(Configuration config) { - reload(config); - } - - public void reload(Configuration config) { - this.enabled = config.getBoolean("enabled"); - this.token = config.getString("token"); - this.serverId = config.getString("server"); - this.matchChannel = config.getString("match-channel"); - } - - public boolean isEnabled() { - return enabled; - } - - public String getToken() { - return token; - } - - public String getServerId() { - return serverId; - } - - public String getMatchChannel() { - return matchChannel; - } + private boolean enabled; + + private String token; + private String serverId; + private String matchChannel; + private String fallbackMapImages; + private String mapImageNotFound; + + public BotConfig(Configuration config) { + reload(config); + } + + public void reload(Configuration config) { + this.enabled = config.getBoolean("enabled"); + this.token = config.getString("token"); + this.serverId = config.getString("server"); + this.matchChannel = config.getString("match-channel"); + this.fallbackMapImages = config.getString("fallback-map-images"); + this.mapImageNotFound = config.getString("map-image-not-found"); + } + + public boolean isEnabled() { + return enabled; + } + + public String getToken() { + return token; + } + + public String getServerId() { + return serverId; + } + + public String getMatchChannel() { + return matchChannel; + } + + public String getFallbackMapImages() { + return fallbackMapImages; + } + + public String getMapImageNotFound() { + return mapImageNotFound; + } } diff --git a/src/main/java/me/tbg/match/bot/DiscordBot.java b/src/main/java/me/tbg/match/bot/DiscordBot.java index 0242318..63c73a6 100644 --- a/src/main/java/me/tbg/match/bot/DiscordBot.java +++ b/src/main/java/me/tbg/match/bot/DiscordBot.java @@ -6,6 +6,8 @@ import java.nio.file.Path; import java.time.Duration; import java.time.Instant; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Logger; import java.util.stream.Collectors; import org.javacord.api.DiscordApi; @@ -28,140 +30,153 @@ public class DiscordBot { - private DiscordApi api; - private BotConfig config; - private Logger logger; - - public DiscordBot(BotConfig config, Logger logger) { - this.config = config; - this.logger = logger; - reload(); - } - - public BotConfig getConfig() { - return config; - } - - public void enable() { - if (config.isEnabled()) { - logger.info("Enabling DiscordBot..."); - new DiscordApiBuilder() - .setToken(config.getToken()) - .setWaitForServersOnStartup(false) - .setWaitForUsersOnStartup(false) - .login() - .thenAcceptAsync( - api -> { - setAPI(api); - api.setMessageCacheSize(1, 60 * 60); - api.addServerBecomesAvailableListener( - listener -> logger.info(listener.getServer().getName() + " is now available")); - logger.info("Discord Bot (MatchBot) is now active!"); - }); + private DiscordApi api; + private BotConfig config; + private Logger logger; + + private Map matchMessageMap = new HashMap<>(); + + public DiscordBot(BotConfig config, Logger logger) { + this.config = config; + this.logger = logger; + reload(); + } + + public BotConfig getConfig() { + return config; + } + + public void enable() { + if (config.isEnabled()) { + logger.info("Enabling DiscordBot..."); + new DiscordApiBuilder() + .setToken(config.getToken()) + .setWaitForServersOnStartup(false) + .setWaitForUsersOnStartup(false) + .login() + .thenAcceptAsync(api -> { + setAPI(api); + api.setMessageCacheSize(1, 60 * 60); + api.addServerBecomesAvailableListener( + listener -> logger.info(listener.getServer().getName() + " is now available")); + logger.info("Discord Bot (MatchBot) is now active!"); + }) + .exceptionally(throwable -> { + logger.info("Failed to login to Discord: " + throwable.getMessage()); + return null; + }); + } + } + + private void setAPI(DiscordApi api) { + this.api = api; + } + + public void disable() { + if (this.api != null) { + this.api.disconnect(); + } + this.api = null; } - } - private void setAPI(DiscordApi api) { - this.api = api; - } + public void sendMatchEmbed(EmbedBuilder embed, Match match) { + if (api != null) { + api.updateActivity(ActivityType.PLAYING, match.getMap().getName()); + api.getServerById(config.getServerId()) + .flatMap(server -> + server.getChannelById(config.getMatchChannel()).flatMap(Channel::asTextChannel)) + .ifPresent(textChannel -> textChannel + .sendMessage(embed) + .thenAccept(message -> matchMessageMap.put(Long.valueOf(match.getId()), message.getId())) + .exceptionally(ExceptionLogger.get())); + } + } - public void disable() { - if (this.api != null) { - this.api.disconnect(); + public void editMatchEmbed(long matchId, EmbedBuilder newEmbed) { + if (api != null && matchMessageMap.containsKey(matchId)) { + long messageId = matchMessageMap.get(matchId); + api.getServerById(config.getServerId()) + .flatMap(server -> + server.getChannelById(config.getMatchChannel()).flatMap(Channel::asTextChannel)) + .ifPresent(textChannel -> textChannel + .getMessageById(messageId) + .thenAccept(message -> message.edit(newEmbed)) + .exceptionally(ExceptionLogger.get())); + } } - this.api = null; - } - - public void sendMatchEmbed(EmbedBuilder embed, Match match) { - if (api != null) { - api.updateActivity(ActivityType.PLAYING, match.getMap().getName()); - api.getServerById(config.getServerId()) - .flatMap( - server -> - server.getChannelById(config.getMatchChannel()).flatMap(Channel::asTextChannel)) - .ifPresent(text -> text.sendMessage(embed).exceptionally(ExceptionLogger.get())); + + public String parseDuration(Duration duration) { + long hours = duration.toHours(); + long minutes = duration.toMinutes(); + long seconds = duration.getSeconds(); + + StringBuilder result = new StringBuilder(); + + if (hours > 0) { + result.append(hours).append(hours == 1 ? " hour " : " hours "); + } + if (minutes > 0) { + result.append(minutes).append(minutes == 1 ? " minute " : " minutes "); + } + if (seconds > 0 || result.length() == 0) { + result.append(seconds).append(seconds == 1 ? " second" : " seconds"); + } + + return result.length() > 0 ? result.toString().trim() : "_Unavailable_"; } - } - public String parseDuration(Duration duration) { - long hours = duration.toHours(); - long minutes = duration.toMinutes(); - long seconds = duration.getSeconds(); + public String getMapPools(Match match) { + // Extracted from + // https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/command/MapCommand.java + if (PGM.get().getMapOrder() instanceof MapPoolManager) { + String mapPools = ((MapPoolManager) PGM.get().getMapOrder()) + .getMapPools().stream() + .filter(pool -> pool.getMaps().contains(match.getMap())) + .map(MapPool::getName) + .collect(Collectors.joining(", ")); + if (!mapPools.isEmpty()) { + return mapPools; + } + } + return "_No pools present_"; + } - StringBuilder result = new StringBuilder(); + public String getMapAuthors(Match match) { + return match.getMap().getAuthors().stream() + .map(Contributor::getNameLegacy) + .collect(Collectors.joining(", ")); + } - if (hours > 0) { - result.append(hours) - .append(hours == 1 ? " hour " : " hours "); + public String getMapGamemodes(Match match) { + return match.getMap().getGamemodes().stream().map(Gamemode::getId).collect(Collectors.joining(", ")); } - if (minutes > 0) { - result.append(minutes) - .append(minutes == 1 ? " minute " : " minutes "); + + public String getMatchDescription(Match match) { + int playerCount = match.getPlayers().size(); + return "Started at with **" + playerCount + + (playerCount == 1 ? " player" : " players") + "** online."; } - if (seconds > 0 || result.length() == 0) { - result.append(seconds) - .append(seconds == 1 ? " second" : " seconds"); + + public BufferedImage getMapImage(MapInfo map) throws IOException { + Path sourceDir = map.getSource().getAbsoluteDir(); + File pngFile = new File(sourceDir.toFile(), "map.png"); + return ImageIO.read(pngFile); } - return result.length() > 0 ? result.toString().trim() : "_Unavailable_"; - } - - public String getMapPools(Match match) { - // Extracted from - // https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/command/MapCommand.java - if (PGM.get().getMapOrder() instanceof MapPoolManager) { - String mapPools = - ((MapPoolManager) PGM.get().getMapOrder()) - .getMapPools().stream() - .filter(pool -> pool.getMaps().contains(match.getMap())) - .map(MapPool::getName) - .collect(Collectors.joining(", ")); - if (!mapPools.isEmpty()) { - return mapPools; - } + public long getOnlineStaffCount(Match match) { + // Adapted from + // https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/command/MapCommand.java + return match.getPlayers().stream() + .filter(player -> (player.getBukkit().hasPermission(Permissions.STAFF) + && !Integration.isVanished(player.getBukkit()))) + .count(); } - return "_No pools present_"; - } - - public String getMapAuthors(Match match) { - return match.getMap().getAuthors().stream() - .map(Contributor::getNameLegacy) - .collect(Collectors.joining(", ")); - } - - public String getMapGamemodes(Match match) { - return match.getMap().getGamemodes().stream() - .map(Gamemode::getId) - .collect(Collectors.joining(", ")); - } - - public String getMatchDescription(Match match) { - int playerCount = match.getPlayers().size(); - return "Started at with **" - + playerCount + (playerCount == 1 ? " player" : " players") + "** online."; - } - - public BufferedImage getMapImage(MapInfo map) throws IOException { - Path sourceDir = map.getSource().getAbsoluteDir(); - File pngFile = new File(sourceDir.toFile(), "map.png"); - return ImageIO.read(pngFile); - } - - public long getOnlineStaffCount(Match match) { - // Adapted from - // https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/command/MapCommand.java - // Line 88 - return match.getPlayers().stream() - .filter( - player -> (player.getBukkit().hasPermission(Permissions.STAFF) && !Integration.isVanished(player.getBukkit()))) - .count(); - } - - public void reload() { - if (this.api != null && !config.isEnabled()) { - disable(); - } else if (this.api == null && config.isEnabled()) { - enable(); + + public void reload() { + if (this.api != null && !config.isEnabled()) { + disable(); + } else if (this.api == null && config.isEnabled()) { + enable(); + } } - } } diff --git a/src/main/java/me/tbg/match/bot/MatchBot.java b/src/main/java/me/tbg/match/bot/MatchBot.java index 056324f..99eb067 100644 --- a/src/main/java/me/tbg/match/bot/MatchBot.java +++ b/src/main/java/me/tbg/match/bot/MatchBot.java @@ -4,26 +4,26 @@ public class MatchBot extends JavaPlugin { - private DiscordBot bot; - private BotConfig config; + private DiscordBot bot; + private BotConfig config; - @Override - public void onEnable() { - this.saveDefaultConfig(); - this.reloadConfig(); - this.config = new BotConfig(getConfig()); - this.bot = new DiscordBot(config, getLogger()); - this.registerListeners(); - } + @Override + public void onEnable() { + this.saveDefaultConfig(); + this.reloadConfig(); + this.config = new BotConfig(getConfig()); + this.bot = new DiscordBot(config, getLogger()); + this.registerListeners(); + } - private void registerListeners() { - this.getServer().getPluginManager().registerEvents(new MatchStartListener(bot), this); - this.getServer().getPluginManager().registerEvents(new MatchFinishListener(bot), this); - } + private void registerListeners() { + this.getServer().getPluginManager().registerEvents(new MatchStartListener(bot), this); + this.getServer().getPluginManager().registerEvents(new MatchFinishListener(bot), this); + } - public void reloadBotConfig() { - this.reloadConfig(); - config.reload(getConfig()); - bot.reload(); - } + public void reloadBotConfig() { + this.reloadConfig(); + config.reload(getConfig()); + bot.reload(); + } } diff --git a/src/main/java/me/tbg/match/bot/MatchFinishListener.java b/src/main/java/me/tbg/match/bot/MatchFinishListener.java index 4e065f4..83b2c04 100644 --- a/src/main/java/me/tbg/match/bot/MatchFinishListener.java +++ b/src/main/java/me/tbg/match/bot/MatchFinishListener.java @@ -1,9 +1,10 @@ package me.tbg.match.bot; -import java.awt.*; +import java.awt.Color; import java.io.IOException; import java.time.Instant; -import java.util.*; +import java.util.Comparator; +import java.util.Map; import java.util.stream.Collectors; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -17,103 +18,112 @@ public class MatchFinishListener implements Listener { - private final DiscordBot bot; + private final DiscordBot bot; - public MatchFinishListener(DiscordBot bot) { - this.bot = bot; - } - - @EventHandler - public void onMatchFinish(MatchFinishEvent event) { - Match match = event.getMatch(); - MapInfo map = match.getMap(); - ScoreMatchModule scoreModule = match.getModule(ScoreMatchModule.class); - TeamMatchModule teamModule = match.getModule(TeamMatchModule.class); + public MatchFinishListener(DiscordBot bot) { + this.bot = bot; + } - String winner = getWinner(event, match.getCompetitors()); - Color winnerColor = getWinnerColor(event, match.getCompetitors()); + @EventHandler + public void onMatchFinish(MatchFinishEvent event) { + Match match = event.getMatch(); + MapInfo map = match.getMap(); + ScoreMatchModule scoreModule = match.getModule(ScoreMatchModule.class); + TeamMatchModule teamModule = match.getModule(TeamMatchModule.class); - EmbedBuilder matchInfo = createMatchInfoEmbed(match, map, winner, winnerColor); + String winner = getWinner(event); + Color winnerColor = getWinnerColor(event); - addScoresToEmbed(matchInfo, match, scoreModule, teamModule); + EmbedBuilder matchFinishEmbed = + createMatchFinishEmbed(match, map, winner, winnerColor, scoreModule, teamModule); - addAdditionalInfoToEmbed(matchInfo, match, map); + bot.editMatchEmbed(Long.parseLong(match.getId()), matchFinishEmbed); + } - try { - matchInfo.setThumbnail(bot.getMapImage(map)); - } catch (IOException e) { - System.out.println("Unable to get map image for " + map.getName()); + private String getWinner(MatchFinishEvent event) { + if (event.getWinners().size() == 1) { + return event.getWinners().iterator().next().getNameLegacy(); + } else if (event.getWinners().isEmpty()) { + return "_No winner_"; + } else { + return "Tie"; + } } - bot.sendMatchEmbed(matchInfo, match); - } + private Color getWinnerColor(MatchFinishEvent event) { + if (event.getWinners().size() == 1) { + return new Color(event.getWinners().iterator().next().getFullColor().asRGB()); + } else { + return Color.RED; + } + } - private String getWinner(MatchFinishEvent event, Collection teams) { - if (event.getWinners().size() == 1) { - return event.getWinners().iterator().next().getNameLegacy(); - } else if (event.getWinners().isEmpty()) { - return "_No winner_"; - } else { - return "Tie"; + private EmbedBuilder createMatchFinishEmbed( + Match match, + MapInfo map, + String winner, + Color winnerColor, + ScoreMatchModule scoreModule, + TeamMatchModule teamModule) { + EmbedBuilder embed = new EmbedBuilder() + .setColor(winnerColor) + .setTitle("Match #" + match.getId() + " has finished!") + .setDescription("Finished at with **" + + match.getPlayers().size() + (match.getPlayers().size() == 1 ? " player" : " players") + + "** online.") + .addInlineField("Winner", winner) + .addInlineField("Time", bot.parseDuration(match.getDuration())); + + if (scoreModule != null) { + if (teamModule != null) { + Map teamScores = match.getCompetitors().stream() + .collect(Collectors.toMap(Competitor::getNameLegacy, team -> (int) scoreModule.getScore(team))); + embed.addInlineField("Scores", formatScores(teamScores)); + } else { + Map playerScores = match.getCompetitors().stream() + .collect(Collectors.toMap( + Competitor::getNameLegacy, player -> (int) scoreModule.getScore(player))); + embed.addInlineField("Podium", formatPodium(playerScores)); + } + } else { + embed.addInlineField("\u200E", "\u200E"); + } + + embed.addInlineField("Map", map.getName()) + .addInlineField("Version", map.getVersion().toString()) + .addInlineField("Gamemodes", bot.getMapGamemodes(match).toUpperCase()) + .addInlineField( + "Participants", String.valueOf(match.getParticipants().size())) + .addInlineField( + "Observers", + String.valueOf(match.getDefaultParty().getPlayers().size())) + .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()); + } + } + + return embed; } - } - private Color getWinnerColor(MatchFinishEvent event, Collection teams) { - if (event.getWinners().size() == 1) { - return new Color(event.getWinners().iterator().next().getFullColor().asRGB()); - } else { - return Color.RED; + private String formatScores(Map scores) { + return scores.entrySet().stream() + .map(e -> e.getKey() + ": " + e.getValue() + " points") + .collect(Collectors.joining("\n")); } - } - - private EmbedBuilder createMatchInfoEmbed(Match match, MapInfo map, String winner, Color winnerColor) { - return new EmbedBuilder() - .setColor(winnerColor) - .setTitle("Match #" + match.getId() + " has finished!") - .setDescription( - "Finished at with **" - + match.getPlayers().size() + (match.getPlayers().size() == 1 ? " player" : " players") + "** online.") - .addInlineField("Winner", winner) - .addInlineField("Time", bot.parseDuration(match.getDuration())); - } - - private void addScoresToEmbed(EmbedBuilder embed, Match match, ScoreMatchModule scoreModule, TeamMatchModule teamModule) { - if (scoreModule != null) { - if (teamModule != null) { - Map teamScores = match.getCompetitors().stream() - .collect(Collectors.toMap(Competitor::getNameLegacy, team -> (int) scoreModule.getScore(team))); - embed.addInlineField("Scores", formatScores(teamScores)); - } else { - Map playerScores = match.getCompetitors().stream() - .collect(Collectors.toMap(Competitor::getNameLegacy, player -> (int) scoreModule.getScore(player))); - embed.addInlineField("Podium", formatPodium(playerScores)); - } - } else { - embed.addInlineField("\u200E", "\u200E"); + + private String formatPodium(Map scores) { + return scores.entrySet().stream() + .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) + .limit(3) + .map(e -> e.getKey() + ": " + e.getValue() + " points") + .collect(Collectors.joining("\n")); } - } - - private String formatScores(Map scores) { - return scores.entrySet().stream() - .map(e -> e.getKey() + ": " + e.getValue() + " points") - .collect(Collectors.joining("\n")); - } - - private String formatPodium(Map scores) { - return scores.entrySet().stream() - .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) - .limit(3) - .map(e -> e.getKey() + ": " + e.getValue() + " points") - .collect(Collectors.joining("\n")); - } - - private void addAdditionalInfoToEmbed(EmbedBuilder embed, Match match, MapInfo map) { - embed.addInlineField("Map", map.getName()) - .addInlineField("Version", map.getVersion().toString()) - .addInlineField("Gamemodes", bot.getMapGamemodes(match).toUpperCase()) - .addInlineField("Participants", String.valueOf(match.getParticipants().size())) - .addInlineField("Observers", String.valueOf(match.getDefaultParty().getPlayers().size())) - .addInlineField("Staff", String.valueOf(bot.getOnlineStaffCount(match))) - .setFooter("Map tags: " + map.getTags().toString()); - } -} +} \ No newline at end of file diff --git a/src/main/java/me/tbg/match/bot/MatchStartListener.java b/src/main/java/me/tbg/match/bot/MatchStartListener.java index 4663083..5ac8d34 100644 --- a/src/main/java/me/tbg/match/bot/MatchStartListener.java +++ b/src/main/java/me/tbg/match/bot/MatchStartListener.java @@ -1,8 +1,7 @@ package me.tbg.match.bot; -import java.awt.*; +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; @@ -12,41 +11,48 @@ public class MatchStartListener implements Listener { - private final DiscordBot bot; + private final DiscordBot bot; - public MatchStartListener(DiscordBot bot) { - this.bot = bot; - } - - @EventHandler - public void onMatchStart(MatchStartEvent event) { - Match match = event.getMatch(); - MapInfo map = match.getMap(); - EmbedBuilder matchStartEmbed = createMatchStartEmbed(match, map); + public MatchStartListener(DiscordBot bot) { + this.bot = bot; + } - try { - matchStartEmbed.setThumbnail(bot.getMapImage(map)); - } catch (IOException e) { - System.out.println("Unable to get map image for " + map.getName()); + @EventHandler + public void onMatchStart(MatchStartEvent event) { + Match match = event.getMatch(); + 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.sendMatchEmbed(matchStartEmbed, match); } - bot.sendMatchEmbed(matchStartEmbed, match); - } - - private EmbedBuilder createMatchStartEmbed(Match match, MapInfo map) { - return new EmbedBuilder() - .setColor(Color.WHITE) - .setTitle("Match #" + match.getId() + " has started!") - .setDescription(bot.getMatchDescription(match)) - .addInlineField("Map", map.getName()) - .addInlineField("Version", map.getVersion().toString()) - .addInlineField("Gamemodes", bot.getMapGamemodes(match).toUpperCase()) - .addInlineField("Created by", bot.getMapAuthors(match)) - .addInlineField("Pools", bot.getMapPools(match)) - .addField("Objective", map.getDescription()) - .addInlineField("Participants", String.valueOf(match.getParticipants().size())) - .addInlineField("Observers", String.valueOf(match.getDefaultParty().getPlayers().size())) - .addInlineField("Staff", String.valueOf(bot.getOnlineStaffCount(match))) - .setFooter("Map tags: " + map.getTags().toString()); - } + private EmbedBuilder createMatchStartEmbed(Match match, MapInfo map) { + return new EmbedBuilder() + .setColor(Color.WHITE) + .setTitle("Match #" + match.getId() + " has started!") + .setDescription(bot.getMatchDescription(match)) + .addInlineField("Map", map.getName()) + .addInlineField("Version", map.getVersion().toString()) + .addInlineField("Gamemodes", bot.getMapGamemodes(match).toUpperCase()) + .addInlineField("Created by", bot.getMapAuthors(match)) + .addInlineField("Pools", bot.getMapPools(match)) + .addField("Objective", map.getDescription()) + .addInlineField( + "Participants", String.valueOf(match.getParticipants().size())) + .addInlineField( + "Observers", + String.valueOf(match.getDefaultParty().getPlayers().size())) + .addInlineField("Staff", String.valueOf(bot.getOnlineStaffCount(match))) + .setFooter("Map tags: " + map.getTags().toString()); + } }