Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion Essentials/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.earth2me.essentialss</groupId>
<artifactId>essentials-parent</artifactId>
<version>2.6.9</version>
<version>2.7.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down 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.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();
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 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<String> 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<String> 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);
}
}
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
Loading
Loading