Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Essentials/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
<version>1.1.3</version>
</dependency>

<dependency>
<groupId>com.johnymuffin.beta</groupId>
<artifactId>discordauth</artifactId>
<version>1.1.3</version>
</dependency>

<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public void onPlayerJoin(final PlayerJoinEvent event) {
ess.getBackup().onPlayerJoin();
final User user = ess.getUser(event.getPlayer());

user.setUUUID(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();
Expand Down
55 changes: 51 additions & 4 deletions Essentials/src/main/java/com/earth2me/essentials/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -77,6 +78,8 @@ public final void reloadConfig() {
geolocation = _getGeoLocation();
isSocialSpyEnabled = _isSocialSpyEnabled();
isNPC = _isNPC();
uuid = _getUUID();
receiveMailOnDiscord = _getReceiveMailOnDiscord();
}

private double _getMoney() {
Expand Down Expand Up @@ -617,4 +620,48 @@ public void setNPC(boolean set) {
config.setProperty("npc", set);
config.save();
}

public void setUUUID(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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

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.entity.Player;

import java.util.List;
import java.util.UUID;


public class Commandmail extends EssentialsCommand {
Expand Down Expand Up @@ -41,8 +48,21 @@ public void run(Server server, User user, String commandLabel, String[] args) th
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));

// Send to user on Discord if they are linked
boolean forwardToDiscord = ESSAdvConfig.getInstance().getConfigBoolean("settings.mail.send-to-discord.enabled");
if (forwardToDiscord && Bukkit.getPluginManager().isPluginEnabled("DiscordAuthentication") && Bukkit.getPluginManager().isPluginEnabled("DiscordCore")) {
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;
Expand All @@ -51,6 +71,73 @@ public void run(Server server, User user, String commandLabel, String[] args) th
user.setMails(null);
throw new Exception(Util.i18n("mailCleared"));
}

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;
}

throw new NotEnoughArgumentsException();
}

private void forwardMailToDiscord(User sender, User recipient, String[] args) {
if (recipient.isOnline()) {
System.out.println("Skipping mail forwarding to Discord for " + recipient.getName() + ": Recipient is online.");
return;
}
// Check recipient has received mail before forwarding
if (!recipient.getReceiveMailOnDiscord()) {
System.out.println("Skipping mail forwarding to Discord for " + recipient.getName() + ": Recipient has disabled mail forwarding.");
return;
}

System.out.println("Forwarding mail to Discord for " + recipient.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) {
System.out.println("Sending mail to " + targetUsername + " on Discord with 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(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.getDisplayName() + ".\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 -> {
System.out.println("Failed to send a message to user with Discord ID: " + discordID + ". Reason: " + throwable.getMessage());
});
} else {
System.out.println("User with Discord ID " + discordID + " could not be found in any mutual guild.");
}
}, throwable -> {
System.out.println("Failed to retrieve user with Discord ID: " + discordID + ". Reason: " + throwable.getMessage());
});
} else {
System.out.println("Failed to send mail to " + targetUsername + " on Discord: No Discord ID found.");
}
} else {
System.out.println("Failed to send mail to " + targetUsername + " on Discord: No UUID found in Essentials data.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Essentials/src/main/resources/examples/bpermissions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ groups:
- essentials.kit.tools
- essentials.mail
- essentials.mail.send
- essentials.mail.discord
- essentials.me
- essentials.msg
- essentials.nick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Essentials/src/main/resources/examples/permissionsex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ groups:
- essentials.kit.tools
- essentials.mail
- essentials.mail.send
- essentials.mail.discord
- essentials.me
- essentials.msg
- essentials.nick
Expand Down
4 changes: 4 additions & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,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.
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion Essentials/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ commands:
usage: /<command> [warp]
mail:
description: Manages inter-player, intra-server mail.
usage: /<command> [read|clear|send [to] [message]]
usage: /<command> [read|clear|send|discord] [to] [message]
aliases: [email]
me:
description: Describes an action in the context of the player.
Expand Down