diff --git a/Essentials/pom.xml b/Essentials/pom.xml index 5786062b7..a1eab4994 100644 --- a/Essentials/pom.xml +++ b/Essentials/pom.xml @@ -8,7 +8,7 @@ com.earth2me.essentialss essentials-parent - 2.6.9 + 2.7.0 ../pom.xml @@ -76,6 +76,12 @@ 1.1.3 + + com.johnymuffin.beta + discordauth + 1.1.3 + + net.dv8tion JDA diff --git a/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java index 4ddfade21..a2d1ffc30 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java @@ -208,6 +208,8 @@ public void onPlayerJoin(final PlayerJoinEvent event) { ess.getBackup().onPlayerJoin(); final User user = ess.getUser(event.getPlayer()); + user.setUUID(event.getPlayer().getUniqueId()); // Set the UUID of the player + //we do not know the ip address on playerlogin so we need to do this here. if (user.isIpBanned()) { final String banReason = user.getBanReason(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/UserData.java b/Essentials/src/main/java/com/earth2me/essentials/UserData.java index f60febb5d..665f22fad 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/UserData.java +++ b/Essentials/src/main/java/com/earth2me/essentials/UserData.java @@ -5,11 +5,9 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import javax.annotation.Nullable; import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.logging.Logger; @@ -41,6 +39,9 @@ public abstract class UserData extends PlayerExtension implements IConf { private boolean isSocialSpyEnabled; private boolean isNPC; + private UUID uuid; + private boolean receiveMailOnDiscord; + protected UserData(Player base, IEssentials ess) { super(base, ess); File folder = new File(ess.getDataFolder(), "userdata"); @@ -77,6 +78,8 @@ public final void reloadConfig() { geolocation = _getGeoLocation(); isSocialSpyEnabled = _isSocialSpyEnabled(); isNPC = _isNPC(); + uuid = _getUUID(); + receiveMailOnDiscord = _getReceiveMailOnDiscord(); } private double _getMoney() { @@ -617,4 +620,48 @@ public void setNPC(boolean set) { config.setProperty("npc", set); config.save(); } + + public void setUUID(UUID uuid) { + this.uuid = uuid; + config.setProperty("uuid", uuid.toString()); + config.save(); + } + + @Nullable + private UUID _getUUID() { + String uuid = config.getString("uuid"); + if (uuid == null) { + return null; + } + + try { + return UUID.fromString(uuid); + } catch (IllegalArgumentException e) { + logger.warning("Invalid UUID in user data for " + getName() + ": " + uuid + ". UUID will be removed."); + config.removeProperty("uuid"); + config.save(); + } + + return null; + } + + @Nullable + public UUID getUUID() { + return uuid; + } + + private boolean _getReceiveMailOnDiscord() { + return config.getBoolean("receiveMailOnDiscord", true); // Default to true + } + + public boolean getReceiveMailOnDiscord() { + return receiveMailOnDiscord; + } + + public void setReceiveMailOnDiscord(boolean receiveMailOnDiscord) { + this.receiveMailOnDiscord = receiveMailOnDiscord; + config.setProperty("receiveMailOnDiscord", receiveMailOnDiscord); + config.save(); + } + } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java index 027c82ece..ad6cbd037 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java @@ -2,12 +2,20 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.Util; +import com.johnymuffin.beta.discordauth.DiscordAuthentication; +import com.johnymuffin.discordcore.DiscordCore; +import com.johnymuffin.essentials.ESSAdvConfig; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.JDA; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.List; +import java.util.UUID; +import java.util.logging.Level; public class Commandmail extends EssentialsCommand { private final String CAN_SEND_MAIL_PERMISSION_NODE = "essentials.mail.send"; @@ -19,122 +27,133 @@ public Commandmail() { } @Override - public void run( - Server server, - CommandSender sender, - String commandLabel, - String[] args - ) throws NotEnoughArgumentsException { + public void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception { this.server = server; - User user = ess.getUser(sender); - int argCount = args.length; - - boolean success = false; - if(argCount == 1) - success = oneParameter(args[0], user); - else if(argCount >= 3) - success = threeParameter(args, user); - - if(!success) - throw new NotEnoughArgumentsException(); - } - private boolean oneParameter(String arg, User user) { - switch(arg) { - case "read": - return read(user); - case "clear": - return clear(user); + if (args.length >= 1 && "read".equalsIgnoreCase(args[0])) { + List mail = user.getMails(); + if (mail.isEmpty()) { + user.sendMessage(Util.i18n("noMail")); + return; + } + for (String s : mail) { + user.sendMessage(s); + } + user.sendMessage(Util.i18n("mailClear")); + return; } - - return false; - } - - private boolean threeParameter(String[] args, User user) { - String subCommand = args[0]; - String to = args[1]; - String message = getFinalArg(args, 2); - - // yes this is stupid, but its done for consistancy with the oneParameter() function - switch(subCommand) { - case "send": - return send(to, message, user); + if (args.length >= 3 && "send".equalsIgnoreCase(args[0])) { + if (!user.isAuthorized(CAN_SEND_MAIL_PERMISSION_NODE)) { + throw new Exception(Util.i18n("noMailSendPerm")); + } + + Player player = server.getPlayer(args[1]); + User u; + if (player != null) { + u = ess.getUser(player); + } else { + u = ess.getOfflineUser(args[1]); + } + if (u == null) { + throw new Exception(Util.format("playerNeverOnServer", args[1])); + } + + if (!u.isIgnoredPlayer(user.getName())) { + u.addMail(ChatColor.stripColor(user.getDisplayName()) + ": " + getFinalArg(args, 2)); + + boolean forwardToDiscord = ESSAdvConfig.getInstance().getConfigBoolean("settings.mail.send-to-discord.enabled"); + try { + forwardMailToDiscord(user, u, args); + } catch (Exception e) { + System.out.println("Failed to forward mail to Discord for " + u.getName() + "."); + e.printStackTrace(); + } + + } + user.sendMessage(Util.i18n("mailSent")); + return; } - - return false; - } - - private boolean read(User user) { - List mails = user.getMails(); - - if (mails.isEmpty()) { - String noMailMessage = Util.i18n("noMail"); - user.sendMessage(noMailMessage); - - return true; + if (args.length >= 1 && "clear".equalsIgnoreCase(args[0])) { + user.setMails(null); + user.sendMessage(Util.i18n("mailCleared")); + return; } - - for (String mail : mails) { - user.sendMessage(mail); + if(args.length == 1 && "discord".equalsIgnoreCase(args[0])) { + if (!user.isAuthorized("essentials.mail.discord")) { + throw new Exception(Util.i18n("noMailDiscordPerm")); + } + user.setReceiveMailOnDiscord(!user.getReceiveMailOnDiscord()); + boolean receiveMailOnDiscord = user.getReceiveMailOnDiscord(); + user.sendMessage(Util.i18n(receiveMailOnDiscord ? "mailDiscordEnabled" : "mailDiscordDisabled")); + return; } - - String clearMessage = Util.i18n("mailClear"); - user.sendMessage(clearMessage); - - return true; + throw new NotEnoughArgumentsException(); } - private boolean clear(User user) { - user.setMails(null); + private void forwardMailToDiscord(User sender, User recipient, String[] args) { - String clearedMailMessage = Util.i18n("mailCleared"); - user.sendMessage(clearedMailMessage); - - return true; - } - - private boolean send(String to, String message, User user) { - if (!canSendMail(user)) { - String noPermissionMessage = Util.i18n("noMailSendPerm"); - user.sendMessage(noPermissionMessage); - - return true; + // Check if both DiscordAuthentication and DiscordCore plugins are enabled + if (!(Bukkit.getPluginManager().isPluginEnabled("DiscordAuthentication") && Bukkit.getPluginManager().isPluginEnabled("DiscordCore"))) { + return; } - User toUser = findUser(to); - if (toUser == null) { - String neverSeenMessage = Util.format("playerNeverOnServer", to); - user.sendMessage(neverSeenMessage); - - return true; + // Check if recipient is online or has disabled mail forwarding + if (recipient.isOnline()) { + return; } - String mailSentMessage = Util.i18n("mailSent"); - user.sendMessage(mailSentMessage); - - if (hasIgnoredPlayer(toUser, user)) - return true; - - String mailMessage = ChatColor.stripColor(user.getDisplayName()) + ": " + message; - toUser.addMail(mailMessage); - - return true; - } - - private User findUser(String playerName) { - Player player = server.getPlayer(playerName); - if (player == null) - return ess.getOfflineUser(playerName); + // Check if recipient has disabled mail forwarding + if (!recipient.getReceiveMailOnDiscord()) { + return; + } - return ess.getUser(player); - } - private boolean hasIgnoredPlayer(User recipient, User sender) { - return recipient.isIgnoredPlayer(sender.getName()); + DiscordAuthentication plugin = (DiscordAuthentication) Bukkit.getPluginManager().getPlugin("DiscordAuthentication"); + String targetUsername = recipient.getName(); + UUID targetUUID = recipient.getUUID(); + + if (targetUUID != null) { + String discordID = plugin.getData().getDiscordIDFromUUID(String.valueOf(targetUUID)); + + if (discordID != null) { + log(Level.INFO, "Mail sent from " + sender.getName() + " to " + targetUsername + " forwarding to Discord ID: " + discordID); + DiscordCore discordCore = (DiscordCore) Bukkit.getPluginManager().getPlugin("DiscordCore"); + JDA jda = discordCore.getDiscordBot().getJda(); + + jda.retrieveUserById(discordID).queue(discordUser -> { + if (discordUser != null) { + discordUser.openPrivateChannel().queue(privateChannel -> { + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setAuthor(ChatColor.stripColor(sender.getDisplayName()), null, "https://skins.legacyminecraft.com/avatars/" + sender.getBase().getUniqueId()); + embedBuilder.setTitle("Forwarded Mail from Minecraft Server"); + embedBuilder.setDescription("You have received a new mail message on the Minecraft server from " + sender.getName() + ".\n\n**Message:**\n" + getFinalArg(args, 2) + "\n\n*Note: You can use `/mail discord` in-game to toggle receiving mail via Discord.*"); + embedBuilder.setFooter("Essentials (RetroMC fork) - Mail Command", "https://wiki.retromc.org/images/1/1a/Retromcnew.png"); + embedBuilder.setColor(0x00FF00); + privateChannel.sendMessage(embedBuilder.build()).queue(); + + Bukkit.getScheduler().callSyncMethod(ess, () -> { + sender.sendMessage(Util.format("mailSentDiscord", discordUser.getAsTag())); + return null; + }); + }, throwable -> { + log(Level.WARNING, "Failed to send a message to user with Discord ID: " + discordID + ". Reason: " + throwable.getMessage()); + }); + } else { + log(Level.WARNING, "User with Discord ID " + discordID + " could not be found in any mutual guild."); + } + }, throwable -> { + log(Level.WARNING, "Failed to retrieve user with Discord ID: " + discordID + ". Reason: " + throwable.getMessage()); + }); + } else { + log(Level.INFO, "No Discord ID found for UUID: " + targetUUID + " when attempting to forward mail to Discord."); + } + } else { + log(Level.WARNING, "Failed to send mail to " + targetUsername + " on Discord: No UUID found in Essentials data."); + } } - private boolean canSendMail(User user) { - return user.isAuthorized(CAN_SEND_MAIL_PERMISSION_NODE); + private void log(Level level, String message) { + ess.getServer().getLogger().log(level, "[" + ess.getDescription().getName() + "] " + message); } } diff --git a/Essentials/src/main/java/com/johnymuffin/essentials/ESSAdvConfig.java b/Essentials/src/main/java/com/johnymuffin/essentials/ESSAdvConfig.java index fc64054d0..2f0cf2fb8 100644 --- a/Essentials/src/main/java/com/johnymuffin/essentials/ESSAdvConfig.java +++ b/Essentials/src/main/java/com/johnymuffin/essentials/ESSAdvConfig.java @@ -38,6 +38,9 @@ private void write() { generateConfigOption("discord.channel-id", "0"); + generateConfigOption("settings.mail.send-to-discord.enabled", false); + generateConfigOption("settings.mail.send-to-discord.info", "This will send mail that a player receives to them via Discord if they have linked their account with Discord Authentication Core."); + } private void generateConfigOption(String key, Object defaultValue) { diff --git a/Essentials/src/main/resources/examples/bpermissions.yml b/Essentials/src/main/resources/examples/bpermissions.yml index 9ba493fae..7253febaf 100644 --- a/Essentials/src/main/resources/examples/bpermissions.yml +++ b/Essentials/src/main/resources/examples/bpermissions.yml @@ -26,6 +26,7 @@ groups: - essentials.kit.tools - essentials.mail - essentials.mail.send + - essentials.mail.discord - essentials.me - essentials.msg - essentials.nick diff --git a/Essentials/src/main/resources/examples/permissionsbukkit.yml b/Essentials/src/main/resources/examples/permissionsbukkit.yml index 243752232..5b2ed5c2b 100644 --- a/Essentials/src/main/resources/examples/permissionsbukkit.yml +++ b/Essentials/src/main/resources/examples/permissionsbukkit.yml @@ -34,6 +34,7 @@ groups: essentials.kit.tools: true essentials.mail: true essentials.mail.send: true + essentials.mail.discord: true essentials.me: true essentials.msg: true essentials.nick: true diff --git a/Essentials/src/main/resources/examples/permissionsex.yml b/Essentials/src/main/resources/examples/permissionsex.yml index 47407cd62..6e9bf2b5c 100644 --- a/Essentials/src/main/resources/examples/permissionsex.yml +++ b/Essentials/src/main/resources/examples/permissionsex.yml @@ -32,6 +32,7 @@ groups: - essentials.kit.tools - essentials.mail - essentials.mail.send + - essentials.mail.discord - essentials.me - essentials.msg - essentials.nick diff --git a/Essentials/src/main/resources/messages.properties b/Essentials/src/main/resources/messages.properties index 09771525a..4ee7a2853 100644 --- a/Essentials/src/main/resources/messages.properties +++ b/Essentials/src/main/resources/messages.properties @@ -370,3 +370,7 @@ year = year years = years youAreHealed = \u00a77You have been healed. youHaveNewMail = \u00a7cYou have {0} messages!\u00a7f Type \u00a77/mail read\u00a7f to view your mail. +mailSentDiscord = \u00a77Mail sent to {0} via Discord. +mailDiscordEnabled = \u00a77You will now receive mail on Discord. +mailDiscordDisabled = \u00a77You will no longer receive mail on Discord. +noMailDiscordPerm = \u00a7cYou do not have permission to alter Discord mail settings. \ No newline at end of file diff --git a/Essentials/src/main/resources/messages_da.properties b/Essentials/src/main/resources/messages_da.properties index e5f306466..a4fedde0d 100644 --- a/Essentials/src/main/resources/messages_da.properties +++ b/Essentials/src/main/resources/messages_da.properties @@ -358,3 +358,4 @@ year = \u00e5r years = \u00e5r youAreHealed = \u00a77Du er blevet helbredt. youHaveNewMail = \u00a7cDu har {0} beskeder!\u00a7f Type \u00a77/post l\u00e6s\u00a7f for at se din post. +mailSentDiscord = \u00a77Mail sent to {0} via Discord. \ No newline at end of file diff --git a/Essentials/src/main/resources/messages_de.properties b/Essentials/src/main/resources/messages_de.properties index 489d734e6..90d2d9828 100644 --- a/Essentials/src/main/resources/messages_de.properties +++ b/Essentials/src/main/resources/messages_de.properties @@ -358,3 +358,4 @@ year = Jahr years = Jahre youAreHealed = \u00a77Du wurdest geheilt. youHaveNewMail = \u00a7cDu hast {0} Nachrichten!\u00a7f Schreibe \u00a77/mail read\u00a7f um deine Nachrichten anzuzeigen. +mailSentDiscord = \u00a77Mail sent to {0} via Discord. \ No newline at end of file diff --git a/Essentials/src/main/resources/messages_en.properties b/Essentials/src/main/resources/messages_en.properties index a83cca2f2..c0c18d51f 100644 --- a/Essentials/src/main/resources/messages_en.properties +++ b/Essentials/src/main/resources/messages_en.properties @@ -360,3 +360,4 @@ year = year years = years youAreHealed = \u00a77You have been healed. youHaveNewMail = \u00a7cYou have {0} messages!\u00a7f Type \u00a77/mail read\u00a7f to view your mail. +mailSentDiscord = \u00a77Mail sent to {0} via Discord. \ No newline at end of file diff --git a/Essentials/src/main/resources/messages_fr.properties b/Essentials/src/main/resources/messages_fr.properties index 61854255d..ec9bf52a1 100644 --- a/Essentials/src/main/resources/messages_fr.properties +++ b/Essentials/src/main/resources/messages_fr.properties @@ -358,3 +358,4 @@ year = ann\u00e9e years = ann\u00e9es youAreHealed = \u00a77Vous avez \u00e9t\u00e9 soign\u00e9. youHaveNewMail = \u00a7cVous avez {0} messages! \u00a7fEntrez \u00a77/mail read\u00a7f pour voir votre courrier. +mailSentDiscord = \u00a77Mail sent to {0} via Discord. diff --git a/Essentials/src/main/resources/messages_nl.properties b/Essentials/src/main/resources/messages_nl.properties index acabd87f8..3e5b60c84 100644 --- a/Essentials/src/main/resources/messages_nl.properties +++ b/Essentials/src/main/resources/messages_nl.properties @@ -358,3 +358,4 @@ year = jaar years = jaren youAreHealed = \u00a77Je bent genezen. youHaveNewMail = \u00a7cJe hebt {0} berichten!\u00a7f Type \u00a77/mail read\u00a7f om je berichten te bekijken. +mailSentDiscord = \u00a77Mail sent to {0} via Discord. \ No newline at end of file diff --git a/Essentials/src/main/resources/plugin.yml b/Essentials/src/main/resources/plugin.yml index a6e067acd..ef72402a6 100644 --- a/Essentials/src/main/resources/plugin.yml +++ b/Essentials/src/main/resources/plugin.yml @@ -183,7 +183,7 @@ commands: usage: / [warp] mail: description: Manages inter-player, intra-server mail. - usage: / [read|clear|send [to] [message]] + usage: / [read|clear|send|discord] [to] [message] aliases: [email] me: description: Describes an action in the context of the player. diff --git a/EssentialsChat/pom.xml b/EssentialsChat/pom.xml index 6d7a29570..cbd3866c8 100644 --- a/EssentialsChat/pom.xml +++ b/EssentialsChat/pom.xml @@ -8,7 +8,7 @@ com.earth2me.essentialss essentials-parent - 2.6.9 + 2.7.0 ../pom.xml diff --git a/EssentialsGeoIP/pom.xml b/EssentialsGeoIP/pom.xml index 4abfa6810..8de8d406b 100644 --- a/EssentialsGeoIP/pom.xml +++ b/EssentialsGeoIP/pom.xml @@ -8,7 +8,7 @@ com.earth2me.essentialss essentials-parent - 2.6.9 + 2.7.0 ../pom.xml diff --git a/EssentialsProtect/pom.xml b/EssentialsProtect/pom.xml index 67d9f0820..066386499 100644 --- a/EssentialsProtect/pom.xml +++ b/EssentialsProtect/pom.xml @@ -8,7 +8,7 @@ com.earth2me.essentialss essentials-parent - 2.6.9 + 2.7.0 ../pom.xml @@ -57,18 +57,6 @@ ${project.version} - - com.mchange - c3p0 - 0.9.5.5 - - - - com.mchange - mchange-commons-java - 0.2.20 - - \ No newline at end of file diff --git a/EssentialsSpawn/pom.xml b/EssentialsSpawn/pom.xml index 4b3a3a721..1452904b2 100644 --- a/EssentialsSpawn/pom.xml +++ b/EssentialsSpawn/pom.xml @@ -8,7 +8,7 @@ com.earth2me.essentialss essentials-parent - 2.6.9 + 2.7.0 ../pom.xml diff --git a/pom.xml b/pom.xml index c5b0c3c0a..800880972 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.earth2me.essentialss essentials-parent - 2.6.9 + 2.7.0 pom