diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java index 9c17c8655..b821bd7c0 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java @@ -58,9 +58,11 @@ public final class MessageType { public final static MessageType CHAT = new MessageType("chat", true); public final static MessageType DEATH = new MessageType("death", true); public final static MessageType AFK = new MessageType("afk", true); + public final static MessageType SERVER_START = new MessageType("server-start", false); + public final static MessageType SERVER_STOP = new MessageType("server-stop", false); public final static MessageType KICK = new MessageType("kick", false); public final static MessageType MUTE = new MessageType("mute", false); - private final static MessageType[] VALUES = new MessageType[]{JOIN, LEAVE, CHAT, DEATH, AFK, KICK, MUTE}; + private final static MessageType[] VALUES = new MessageType[]{JOIN, LEAVE, CHAT, DEATH, AFK, SERVER_START, SERVER_STOP, KICK, MUTE}; /** * Gets an array of all the default {@link MessageType MessageTypes}. diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java index 7e3192f5f..4d9bc25ee 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java @@ -263,6 +263,14 @@ public class DiscordSettings implements IConf { "username", "displayname"); } + public String getStartMessage() { + return config.getString("messages.server-start", ":white_check_mark: The server has started!"); + } + + public String getStopMessage() { + return config.getString("messages.server-stop", ":octagonal_sign: The server has stopped!"); + } + public MessageFormat getKickFormat() { return kickFormat; } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java index 97e0d3f8b..1d4777640 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java @@ -172,6 +172,7 @@ public class JDADiscordService implements DiscordService { updateTypesRelay(); Bukkit.getPluginManager().registerEvents(new BukkitListener(this), plugin); + getPlugin().getEss().scheduleSyncDelayedTask(() -> DiscordUtil.dispatchDiscordMessage(JDADiscordService.this, MessageType.DefaultTypes.SERVER_START, getSettings().getStartMessage(), true, null, null, null)); Bukkit.getServicesManager().register(DiscordService.class, this, plugin, ServicePriority.Normal); } @@ -347,6 +348,9 @@ public class JDADiscordService implements DiscordService { } if (jda != null) { + sendMessage(MessageType.DefaultTypes.SERVER_STOP, getSettings().getStopMessage(), true); + DiscordUtil.dispatchDiscordMessage(JDADiscordService.this, MessageType.DefaultTypes.SERVER_STOP, getSettings().getStopMessage(), true, null, null, null); + shutdownConsoleRelay(true); // Unregister leftover jda listeners diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java index 5e6c5f66b..9bef98cee 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java @@ -10,6 +10,7 @@ import net.essentialsx.api.v2.events.discord.DiscordChatMessageEvent; import net.essentialsx.api.v2.events.discord.DiscordMessageEvent; import net.essentialsx.api.v2.services.discord.MessageType; import net.essentialsx.discord.JDADiscordService; +import net.essentialsx.discord.util.DiscordUtil; import net.essentialsx.discord.util.MessageUtil; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -177,15 +178,6 @@ public class BukkitListener implements Listener { } private void sendDiscordMessage(final MessageType messageType, final String message, final boolean allowPing, final String avatarUrl, final String name, final UUID uuid) { - if (jda.getPlugin().getSettings().getMessageChannel(messageType.getKey()).equalsIgnoreCase("none")) { - return; - } - - final DiscordMessageEvent event = new DiscordMessageEvent(messageType, FormatUtil.stripFormat(message), allowPing, avatarUrl, name, uuid); - if (Bukkit.getServer().isPrimaryThread()) { - Bukkit.getPluginManager().callEvent(event); - } else { - Bukkit.getScheduler().runTask(jda.getPlugin(), () -> Bukkit.getPluginManager().callEvent(event)); - } + DiscordUtil.dispatchDiscordMessage(jda, messageType, message, allowPing, avatarUrl, name, uuid); } } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java index 0daa82c3e..45ccd551a 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/DiscordUtil.java @@ -13,10 +13,15 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.Webhook; +import net.essentialsx.api.v2.events.discord.DiscordMessageEvent; +import net.essentialsx.api.v2.services.discord.MessageType; +import net.essentialsx.discord.JDADiscordService; import okhttp3.OkHttpClient; +import org.bukkit.Bukkit; import java.awt.Color; import java.util.List; +import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; @@ -165,4 +170,24 @@ public final class DiscordUtil { } return false; } + + public static void dispatchDiscordMessage(final JDADiscordService jda, final MessageType messageType, final String message, final boolean allowPing, final String avatarUrl, final String name, final UUID uuid) { + if (jda.getPlugin().getSettings().getMessageChannel(messageType.getKey()).equalsIgnoreCase("none")) { + return; + } + + final DiscordMessageEvent event = new DiscordMessageEvent(messageType, FormatUtil.stripFormat(message), allowPing, avatarUrl, name, uuid); + + // If the server is stopping, we cannot dispatch events. + if (messageType == MessageType.DefaultTypes.SERVER_STOP) { + jda.sendMessage(event, event.getMessage(), event.isAllowGroupMentions()); + return; + } + + if (Bukkit.getServer().isPrimaryThread()) { + Bukkit.getPluginManager().callEvent(event); + } else { + Bukkit.getScheduler().runTask(jda.getPlugin(), () -> Bukkit.getPluginManager().callEvent(event)); + } + } } diff --git a/EssentialsDiscord/src/main/resources/config.yml b/EssentialsDiscord/src/main/resources/config.yml index 78d0916fe..448cb7e01 100644 --- a/EssentialsDiscord/src/main/resources/config.yml +++ b/EssentialsDiscord/src/main/resources/config.yml @@ -110,6 +110,10 @@ message-types: death: primary # AFK status change messages sent when a player's AFK status changes. afk: primary + # Message sent when the server starts up. + server-start: primary + # Message sent when the server shuts down. + server-stop: primary # Message sent when a player is kicked from the Minecraft server. kick: staff # Message sent when a player's mute state is changed on the Minecraft server. @@ -279,6 +283,10 @@ messages: # - {deathmessage}: The full default death message used in game # ... PlaceholderAPI placeholders are also supported here too! death: ":skull: {deathmessage}" + # This is the message sent to discord when the server starts. + server-start: ":white_check_mark: The server has started!" + # This is the message sent to discord when the server stops. + server-stop: ":octagonal_sign: The server has stopped!" # This is the message sent to discord when a player becomes afk. # The following placeholders can be used here: # - {username}: The name of the user who became afk