finish mails?

This commit is contained in:
destro174 2022-01-29 23:21:35 +01:00
parent 26b044b2e7
commit 57a43e9c1c
8 changed files with 111 additions and 44 deletions

View File

@ -296,4 +296,16 @@ public final class Config {
NOTIFICATIONFORMAT = getString("settings.blockedmessage-notification", NOTIFICATIONFORMAT);
}
public static String mailHeader = "===== List Mails ====='";
public static String mailBody = "<white>From:</white> <staffprefix><sender> <white>on:<date></white>\n<message>";
public static String mailFooter = "======================";
public static String mailNoUser = "<red>A player with this name hasn't logged in recently.";
public static List<String> mailCommandAlias = new ArrayList<>();
private static void mailSettings() {
mailHeader = getString("settings.mail.header", mailHeader);
mailBody = getString("settings.mail.message", mailBody);
mailFooter = getString("settings.mail.footer", mailFooter);
mailCommandAlias = getList("settings.mail.command-aliases", Lists.newArrayList("gmail"));
}
}

View File

@ -9,10 +9,7 @@ import com.alttd.chat.objects.PartyUser;
import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.util.ALogger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.*;
import java.util.List;
@ -25,7 +22,7 @@ public class Queries {
tables.add("CREATE TABLE IF NOT EXISTS ignored_users (`uuid` VARCHAR(36) NOT NULL, `ignored_uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`uuid`, `ignored_uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS parties (`id` INT NOT NULL AUTO_INCREMENT, `owner_uuid` VARCHAR(36) NOT NULL, `party_name` VARCHAR(36) NOT NULL, `password` VARCHAR(36), PRIMARY KEY (`id`))");
tables.add("CREATE TABLE IF NOT EXISTS chat_users (`uuid` VARCHAR(36) NOT NULL, `party_id` INT NOT NULL, `toggled_channel` VARCHAR(36) NULL DEFAULT NULL, PRIMARY KEY (`uuid`))");
tables.add("CREATE TABLE IF NOT EXISTS mails (`id` INT NOT NULL AUTO_INCREMENT, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(36) NOT NULL, `message` VARCHAR(256) NOT NULL, `sendtime` BIGINT default 0, `readtime` BIGINT default 0, PRIMARY KEY (`id`))");
tables.add("CREATE TABLE IF NOT EXISTS mails (`id` INT NOT NULL AUTO_INCREMENT, `uuid` VARCHAR(36) NOT NULL, `sender` VARCHAR(36) NOT NULL, `message` VARCHAR(256) NOT NULL, `sendtime` BIGINT default 0, `readtime` BIGINT default 0, PRIMARY KEY (`id`))");
try {
Connection connection = DatabaseConnection.getConnection();
@ -412,11 +409,12 @@ public class Queries {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
UUID fromUUID = UUID.fromString(resultSet.getString("from"));
int id = resultSet.getInt("id");
UUID fromUUID = UUID.fromString(resultSet.getString("sender"));
String message = resultSet.getString("message");
long sendTime = resultSet.getLong("sendtime");
long readTime = resultSet.getLong("readtime");
mails.add(new Mail(uuid, fromUUID, sendTime, readTime, message));
mails.add(new Mail(id, uuid, fromUUID, sendTime, readTime, message));
}
} catch (SQLException e) {
e.printStackTrace();
@ -504,4 +502,47 @@ public class Queries {
}
return uuid;
}
public static int insertMail(Mail mail) {
String query = "INSERT INTO mails (uuid , sender, message, sendtime, readtime) VALUES (?, ?, ?, ?, ?)";
int id = 0;
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, mail.getUuid().toString());
statement.setString(2, mail.getSender().toString());
statement.setString(3, mail.getMessage());
statement.setLong(4, mail.getSendTime());
statement.setLong(5, mail.getReadTime());
statement.execute();
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
id = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return id;
}
public static void markMailRead(Mail mail) {
String query = "INSERT INTO mails (Id, uuid , sender, message, sendtime, readtime) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE readtime = ?";
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, mail.getId());
statement.setString(2, mail.getUuid().toString());
statement.setString(3, mail.getSender().toString());
statement.setString(4, mail.getMessage());
statement.setLong(5, mail.getSendTime());
statement.setLong(6, mail.getReadTime());
statement.setLong(7, mail.getReadTime());
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -27,10 +27,4 @@ public final class ChatUserManager {
return chatUsers.computeIfAbsent(uuid, k -> Queries.loadChatUser(uuid));
}
public List<Mail> getUnReadMail(ChatUser user) {
return user.getMails().stream()
.filter(Mail::isUnRead)
.collect(Collectors.toList());
}
}

View File

@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class ChatUser {
private final UUID uuid; // player uuid
@ -108,6 +109,12 @@ public class ChatUser {
return mails;
}
public List<Mail> getUnReadMail() {
return getMails().stream()
.filter(Mail::isUnRead)
.collect(Collectors.toList());
}
public void addMail(Mail mail) {
mails.add(mail);
}

View File

@ -1,16 +1,20 @@
package com.alttd.chat.objects;
import com.alttd.chat.database.Queries;
import java.util.UUID;
public class Mail {
private final int id;
private final UUID uuid;
private final UUID sender;
private final long sendTime;
private long readTime;
private final String message;
public Mail(UUID player, UUID sender, long sendTime, long readTime, String message) {
public Mail(int id, UUID player, UUID sender, long sendTime, long readTime, String message) {
this.id = id;
this.uuid = player;
this.sender = sender;
this.sendTime = sendTime;
@ -21,9 +25,15 @@ public class Mail {
public Mail(UUID player, UUID sender, String message) {
this.uuid = player;
this.sender = sender;
this.sendTime = System.currentTimeMillis();
this.readTime = System.currentTimeMillis();
long time = System.currentTimeMillis();
this.sendTime = time;
this.readTime = time;
this.message = message;
this.id = Queries.insertMail(this);
}
public int getId() {
return id;
}
public UUID getUuid() {
@ -35,7 +45,7 @@ public class Mail {
}
public boolean isUnRead() {
return getSendTime() != getReadTime();
return getSendTime() == getReadTime();
}
public long getSendTime() {

View File

@ -2,7 +2,11 @@ package com.alttd.velocitychat;
import com.alttd.chat.ChatAPI;
import com.alttd.chat.ChatImplementation;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.commands.GlobalAdminChat;
import com.alttd.velocitychat.commands.MailCommand;
import com.alttd.velocitychat.commands.Reload;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.DatabaseConnection;
@ -71,8 +75,11 @@ public class VelocityChat {
channelIdentifier = MinecraftChannelIdentifier.create(channels[0], channels[1]);
server.getChannelRegistrar().register(channelIdentifier);
server.getEventManager().register(this, new PluginMessageListener(channelIdentifier));
loadCommands();
// setup console chatuser
ChatUser console = new ChatUser(Config.CONSOLEUUID, -1, null);
console.setDisplayName(Config.CONSOLENAME);
ChatUserManager.addUser(console);
}
public void ReloadConfig() {
@ -104,6 +111,7 @@ public class VelocityChat {
public void loadCommands() {
new GlobalAdminChat(server);
new Reload(server);
new MailCommand(server);
// all (proxy)commands go here
}

View File

@ -1,5 +1,6 @@
package com.alttd.velocitychat.commands;
import com.alttd.chat.config.Config;
import com.alttd.velocitychat.VelocityChat;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
@ -77,20 +78,9 @@ public class MailCommand {
})
)
.then(playerNode
.then(LiteralArgumentBuilder.<CommandSource>literal("unread")
.executes(context -> {
VelocityChat.getPlugin().getChatHandler().readMail(context.getSource(), context.getArgument("player", String.class), true);
return 1;
})
)
.then(LiteralArgumentBuilder.<CommandSource>literal("all")
.executes(context -> {
VelocityChat.getPlugin().getChatHandler().readMail(context.getSource(), context.getArgument("player", String.class), false);
return 1;
})
)
.requires(ctx -> ctx.hasPermission("command.chat.mail.list.other"))// TODO permission
.executes(context -> {
sendHelpMessage(context.getSource());
VelocityChat.getPlugin().getChatHandler().readMail(context.getSource(), context.getArgument("player", String.class));
return 1;
})
)
@ -113,9 +103,9 @@ public class MailCommand {
CommandMeta.Builder metaBuilder = proxyServer.getCommandManager().metaBuilder(brigadierCommand);
/*for (String alias : Config.MAILCOMMANDALIASES) {
for (String alias : Config.mailCommandAlias) {
metaBuilder.aliases(alias);
}*/
}
CommandMeta meta = metaBuilder.build();

View File

@ -1,6 +1,7 @@
package com.alttd.velocitychat.handlers;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser;
@ -139,37 +140,41 @@ public class ChatHandler {
optionalPlayer.ifPresent(player -> player.sendMessage(Utility.parseMiniMessage("<yellow>New mail from " + finalSenderName)));
}
public void readMail(CommandSource commandSource, String targetPlayer, boolean unread) {
public void readMail(CommandSource commandSource, String targetPlayer) {
UUID uuid = ServerHandler.getPlayerUUID(targetPlayer);
if (uuid == null) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>A player with this name hasn't logged in recently.")); // TOOD load from config
commandSource.sendMessage(Utility.parseMiniMessage(Config.mailNoUser));
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(uuid);
commandSource.sendMessage(getMails(chatUser.getMails(), false));
commandSource.sendMessage(parseMails(chatUser.getMails(), false));
}
public void readMail(CommandSource commandSource, boolean unread) {
if (commandSource instanceof Player player) {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
commandSource.sendMessage(getMails(chatUser.getMails(), unread));
commandSource.sendMessage(parseMails(unread ? chatUser.getUnReadMail() : chatUser.getMails(), unread));
}
}
private Component getMails(List<Mail> mails, boolean mark) {
Component component = Component.empty();
private Component parseMails(List<Mail> mails, boolean mark) {
Component component = Utility.parseMiniMessage(Config.mailHeader);
for (Mail mail : mails) {
if (mail.isUnRead() && mark) mail.setReadTime(System.currentTimeMillis());
if (mail.isUnRead() && mark) {
mail.setReadTime(System.currentTimeMillis());
Queries.markMailRead(mail);
}
ChatUser chatUser = ChatUserManager.getChatUser(mail.getSender());
List<Template> templates = new ArrayList<>(List.of(
Template.template("staffprefix", chatUser.getStaffPrefix()),
Template.template("name", chatUser.getDisplayName()),
Template.template("message", "<pre>" + mail.getMessage() + "<pre>"),
Template.template("sendtime", new Date(mail.getSendTime()).toString())
Template.template("sender", chatUser.getDisplayName()),
Template.template("message", mail.getMessage()),
Template.template("date", new Date(mail.getSendTime()).toString())
));
Component mailMessage = Utility.parseMiniMessage("", templates);
Component mailMessage = Utility.parseMiniMessage(Config.mailBody, templates);
component = component.append(Component.newline()).append(mailMessage);
}
component = component.append(Component.newline()).append(Utility.parseMiniMessage(Config.mailFooter));
return component;
}