merge basic mailing

This commit is contained in:
destro174 2022-01-29 14:53:47 +01:00
parent 99babd2120
commit 26b044b2e7
7 changed files with 107 additions and 30 deletions

View File

@ -482,4 +482,26 @@ public class Queries {
return userNames;
}
public static UUID getPlayerUUID(String playerName) {
String query = "SELECT UUID FROM utility_users WHERE Username = ?";
UUID uuid = null;
try {
Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, playerName);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
uuid = UUID.fromString(resultSet.getString("UUID"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return uuid;
}
}

View File

@ -4,7 +4,6 @@ import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.Component;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;

View File

@ -4,11 +4,11 @@ import java.util.UUID;
public class Mail {
private final UUID uuid; // the player
private final UUID sender; // the sender
private final long sendTime; // any other option for this? does the db store recordcreation and edit time?
private long readTime; // any other option for this?
private final String message; // do we want staff to edit this after being send but being unread?
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) {
this.uuid = player;
@ -21,8 +21,8 @@ public class Mail {
public Mail(UUID player, UUID sender, String message) {
this.uuid = player;
this.sender = sender;
this.sendTime = System.nanoTime();
this.readTime = System.nanoTime();
this.sendTime = System.currentTimeMillis();
this.readTime = System.currentTimeMillis();
this.message = message;
}

View File

@ -15,9 +15,9 @@ import com.velocitypowered.api.proxy.ProxyServer;
import java.util.ArrayList;
import java.util.Collection;
public class SendMail {
public class MailCommand {
public SendMail(ProxyServer proxyServer) {
public MailCommand(ProxyServer proxyServer) {
RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder
.<CommandSource, String>argument("player", StringArgumentType.string())

View File

@ -1,20 +1,20 @@
package com.alttd.velocitychat.handlers;
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.VelocityChat;
import com.alttd.chat.config.Config;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail;
import com.alttd.chat.objects.Party;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.VelocityChat;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
@ -113,29 +113,64 @@ public class ChatHandler {
});
}
/**
* constructs a mail object and notifies all involved players about it
* / mail send playerA,playerB,playerC message
*/
public void sendMail(CommandSource commandSource, String recipient, String message) {
UUID uuid;
if (commandSource instanceof Player) {
uuid = ((Player) commandSource).getUniqueId();
} else {
uuid = Config.CONSOLEUUID;
UUID uuid = Config.CONSOLEUUID;;
String senderName = Config.CONSOLENAME;
UUID targetUUID;
if (commandSource instanceof Player player) {
uuid = player.getUniqueId();
senderName = player.getUsername();
}
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(recipient);
//Mail mail = new Mail()
// todo construct the mail and notify the player if online?
if (optionalPlayer.isEmpty()) {
targetUUID = ServerHandler.getPlayerUUID(recipient);
if (targetUUID == null) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>A player with this name hasn't logged in recently.")); // TOOD load from config
return;
}
} else {
targetUUID = optionalPlayer.get().getUniqueId();
}
Mail mail = new Mail(uuid, targetUUID, message);
ChatUser chatUser = ChatUserManager.getChatUser(targetUUID);
chatUser.addMail(mail);
// TODO load from config
String finalSenderName = senderName;
optionalPlayer.ifPresent(player -> player.sendMessage(Utility.parseMiniMessage("<yellow>New mail from " + finalSenderName)));
}
public void readMail(CommandSource commandSource, String targetPlayer, boolean unread) {
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
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(uuid);
commandSource.sendMessage(getMails(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));
}
}
private Component getMails(List<Mail> mails, boolean mark) {
Component component = Component.empty();
for (Mail mail : mails) {
if (mail.isUnRead() && mark) mail.setReadTime(System.currentTimeMillis());
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())
));
Component mailMessage = Utility.parseMiniMessage("", templates);
component = component.append(Component.newline()).append(mailMessage);
}
return component;
}
public void partyChat(String partyId, UUID uuid, Component message) {

View File

@ -1,24 +1,29 @@
package com.alttd.velocitychat.handlers;
import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.ChatUser;
import com.alttd.velocitychat.VelocityChat;
import com.alttd.chat.config.ServerConfig;
import com.alttd.velocitychat.data.ServerWrapper;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.scheduler.ScheduledTask;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class ServerHandler {
private VelocityChat plugin;
private static List<ServerWrapper> servers;
private static Map<String, UUID> serverPlayers;
public ScheduledTask cleanupTask; // add a better way to catch NULL uuid? early return? get from another source? ...
public ServerHandler() {
plugin = VelocityChat.getPlugin();
serverPlayers = new TreeMap<>();
initialize();
}
@ -33,6 +38,11 @@ public class ServerHandler {
for (RegisteredServer registeredServer : plugin.getProxy().getAllServers()) {
servers.add(new ServerWrapper(registeredServer, new ServerConfig(registeredServer.getServerInfo().getName())));
}
cleanupTask = plugin.getProxy().getScheduler().buildTask(plugin, () -> {
serverPlayers.values().removeIf(Objects::isNull);
}).repeat(60, TimeUnit.SECONDS).schedule();
}
public void sendGlobalChat(String uuid, String message) {
@ -65,4 +75,12 @@ public class ServerHandler {
}
return null;
}
public static UUID getPlayerUUID(String playerName) {
return serverPlayers.computeIfAbsent(playerName, k -> Queries.getPlayerUUID(playerName));
}
public static void addPlayerUUID(String playerName, UUID uuid) {
serverPlayers.putIfAbsent(playerName, uuid);
}
}

View File

@ -28,7 +28,8 @@ public class ProxyPlayerListener {
@Subscribe(order = PostOrder.FIRST)
public void onPlayerLogin(LoginEvent event) {
UUID uuid = event.getPlayer().getUniqueId();
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
Party party = PartyManager.getParty(event.getPlayer().getUniqueId());
if (party == null) return;
ByteArrayDataOutput out = ByteStreams.newDataOutput();
@ -38,6 +39,8 @@ public class ProxyPlayerListener {
VelocityChat.getPlugin().getProxy().getAllServers().forEach(registeredServer -> registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), out.toByteArray()));
// TODO setup ChatUser on Proxy
//VelocityChat.getPlugin().getChatHandler().addPlayer(new ChatPlayer(event.getPlayer().getUniqueId()));
ServerHandler.addPlayerUUID(player.getUsername(), uuid);
}
@Subscribe