From daf6ab21a583693c673c17c0c02e6b9c12d8bcbf Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 30 Jan 2022 02:12:09 +0100 Subject: [PATCH] started migrating party to proxy --- .../java/com/alttd/chat/config/Config.java | 40 +++++-- .../com/alttd/chat/handler/ChatHandler.java | 11 +- .../alttd/velocitychat/commands/Party.java | 103 ++++++++++++++++++ .../velocitychat/commands/SubCommand.java | 21 ++++ .../commands/partysubcommands/Create.java | 58 ++++++++++ .../commands/partysubcommands/Invite.java | 72 ++++++++++++ .../commands/partysubcommands/Join.java | 59 ++++++++++ .../commands/partysubcommands/Leave.java | 66 +++++++++++ .../velocitychat/handlers/ChatHandler.java | 46 ++++++++ 9 files changed, 459 insertions(+), 17 deletions(-) create mode 100644 velocity/src/main/java/com/alttd/velocitychat/commands/Party.java create mode 100644 velocity/src/main/java/com/alttd/velocitychat/commands/SubCommand.java create mode 100644 velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Create.java create mode 100644 velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Invite.java create mode 100644 velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Join.java create mode 100644 velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Leave.java diff --git a/api/src/main/java/com/alttd/chat/config/Config.java b/api/src/main/java/com/alttd/chat/config/Config.java index c67349d..a576ec7 100755 --- a/api/src/main/java/com/alttd/chat/config/Config.java +++ b/api/src/main/java/com/alttd/chat/config/Config.java @@ -209,13 +209,6 @@ public final class Config { GCCOOLDOWN = getInt("commands.globalchat.cooldown", GCCOOLDOWN); } - public static String PARTY_FORMAT = "( > → Party) "; - public static String PARTY_SPY = "PC: : "; - private static void party() { - PARTY_FORMAT = getString("party.format", PARTY_FORMAT); - PARTY_SPY = getString("party.spy", PARTY_SPY); - } - public static String CHATFORMAT = " > >: "; public static String URLFORMAT = ">"; private static void Chat() { @@ -252,6 +245,39 @@ public final class Config { } + public static String PARTY_FORMAT = "( > → Party) "; + public static String PARTY_SPY = "PC: : "; + public static String PARTY_HELP = ""; + public static String NO_PERMISSION = "You don't have permission to use this command."; + public static String NO_CONSOLE = "This command can not be used by console"; + public static String CREATED_PARTY = "You created a chat party called: " + + "'' with the password: ''"; + public static String NOT_IN_A_PARTY = "You're not in a chat party."; + public static String NOT_YOUR_PARTY = "You don't own this chat party."; + public static String NOT_A_PARTY = "This chat party does not exist."; + public static String INVALID_PLAYER = "Invalid player."; + public static String NOT_ONLINE = " must be online to receive an invite."; + public static String INVALID_PASSWORD = "Invalid password."; + public static String JOINED_PARTY = "You joined !"; + public static String NOTIFY_FINDING_NEW_OWNER = "Since you own this chat party a new party owner will be chosen."; + public static String LEFT_PARTY = "You have left the chat party!"; + private static void party() { + PARTY_FORMAT = getString("party.format", PARTY_FORMAT); + PARTY_SPY = getString("party.spy", PARTY_SPY); + PARTY_HELP = getString("party.messages.help", PARTY_HELP); + NO_PERMISSION = getString("party.messages.no-permission", NO_PERMISSION); + NO_CONSOLE = getString("party.messages.no-console", NO_CONSOLE); + CREATED_PARTY = getString("party.messages.created-party", CREATED_PARTY); + NOT_IN_A_PARTY = getString("party.messages.not-in-a-party", NOT_IN_A_PARTY); + NOT_YOUR_PARTY = getString("party.messages.not-your-party", NOT_YOUR_PARTY); + NOT_A_PARTY = getString("party.messages.not-a-party", NOT_A_PARTY); + INVALID_PLAYER = getString("party.messages.invalid-player", INVALID_PLAYER); + NOT_ONLINE = getString("party.messages.not-online", NOT_ONLINE); + INVALID_PASSWORD = getString("party.messages.invalid-password", INVALID_PASSWORD); + NOTIFY_FINDING_NEW_OWNER = getString("party.messages.notify-finding-new-owner", NOTIFY_FINDING_NEW_OWNER); + LEFT_PARTY = getString("party.messages.left-party", LEFT_PARTY); + } + private static void chatChannels() { ConfigurationNode node = getNode("chat-channels"); if (node.empty()) { diff --git a/galaxy/src/main/java/com/alttd/chat/handler/ChatHandler.java b/galaxy/src/main/java/com/alttd/chat/handler/ChatHandler.java index 652280a..8d5fc70 100755 --- a/galaxy/src/main/java/com/alttd/chat/handler/ChatHandler.java +++ b/galaxy/src/main/java/com/alttd/chat/handler/ChatHandler.java @@ -193,7 +193,7 @@ public class ChatHandler { Template.template("[i]", itemComponent(player.getInventory().getItemInMainHand())))); Component component = Utility.parseMiniMessage(Config.PARTY_FORMAT, templates); - sendPartyMessage(player, party.getPartyId(), component); +// sendPartyMessage(player, party.getPartyId(), component); Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, templates); for(Player pl : Bukkit.getOnlinePlayers()) { @@ -238,15 +238,6 @@ public class ChatHandler { player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray()); } - private void sendPartyMessage(Player player, int partyId, Component component) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("party"); - out.writeUTF(String.valueOf(partyId)); - out.writeUTF(player.getUniqueId().toString()); - out.writeUTF(GsonComponentSerializer.gson().serialize(component)); - player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray()); - } - // Start - move these to util private boolean isMuted(Player player, String message, String prefix) { diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/Party.java b/velocity/src/main/java/com/alttd/velocitychat/commands/Party.java new file mode 100644 index 0000000..ad95c52 --- /dev/null +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/Party.java @@ -0,0 +1,103 @@ +package com.alttd.velocitychat.commands; + +import com.alttd.chat.config.Config; +import com.alttd.velocitychat.commands.partysubcommands.Create; +import com.alttd.velocitychat.commands.partysubcommands.Invite; +import com.alttd.velocitychat.commands.partysubcommands.Join; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.Player; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Party implements SimpleCommand { + private final List subCommands; + private final MiniMessage miniMessage; + + public Party() { + subCommands = Arrays.asList(new Create(), + new Invite(), + new Join()); + miniMessage = MiniMessage.get(); + } + + @Override + public void execute(SimpleCommand.Invocation invocation) { + String[] args = invocation.arguments(); + CommandSource source = invocation.source(); + + if (args.length < 1) { + if (!source.hasPermission("party.use")) + source.sendMessage(miniMessage.parse(Config.NO_PERMISSION)); + else if (source instanceof Player) + source.sendMessage(miniMessage.parse(Config.PARTY_HELP)); + else + source.sendMessage(miniMessage.parse(Config.NO_CONSOLE)); + return; + } + + subCommands.stream() + .filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0])) + .findFirst() + .ifPresentOrElse(subCommand -> subCommand.execute(args, source), + () -> source.sendMessage(getHelpMessage(source))); + } + + @Override + public List suggest(SimpleCommand.Invocation invocation) { + String[] args = invocation.arguments(); + List suggest = new ArrayList<>(); + + if (args.length == 0) { + subCommands.stream() + .filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission())) + .forEach(subCommand -> suggest.add(subCommand.getName())); + } else if (args.length <= 1) { + subCommands.stream() + .filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission())) + .filter(subCommand -> subCommand.getName().startsWith(args[0].toLowerCase())) + .forEach(subCommand -> suggest.add(subCommand.getName())); + } else { + subCommands.stream() + .filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission())) + .filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0])) + .findFirst() + .ifPresent(subCommand -> suggest.addAll(subCommand.suggest(args))); + } + + if (args.length == 0) + return suggest; + else + return finalizeSuggest(suggest, args[args.length - 1]); + } + + public List finalizeSuggest(List possibleValues, String remaining) { + List finalValues = new ArrayList<>(); + + for (String str : possibleValues) { + if (str.toLowerCase().startsWith(remaining)) { + finalValues.add(StringArgumentType.escapeIfRequired(str)); + } + } + + return finalValues; + } + + private Component getHelpMessage(CommandSource source) { + StringBuilder stringBuilder = new StringBuilder(); + + subCommands.stream() + .filter(subCommand -> source.hasPermission(subCommand.getPermission())) + .forEach(subCommand -> stringBuilder.append(subCommand.getHelpMessage()).append("\n")); + if (stringBuilder.length() != 0) + stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), ""); + + return miniMessage.parse(Config.PARTY_HELP, Template.of("commands", stringBuilder.toString())); + } +} \ No newline at end of file diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/SubCommand.java b/velocity/src/main/java/com/alttd/velocitychat/commands/SubCommand.java new file mode 100644 index 0000000..90e0e83 --- /dev/null +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/SubCommand.java @@ -0,0 +1,21 @@ +package com.alttd.velocitychat.commands; + +import com.velocitypowered.api.command.CommandSource; + +import java.util.List; + +public interface SubCommand { + + String getName(); + + default String getPermission() { + return "party." + getName(); + } + + void execute(String[] args, CommandSource source); + + List suggest(String[] args); + + String getHelpMessage(); + +} diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Create.java b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Create.java new file mode 100644 index 0000000..e687ab9 --- /dev/null +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Create.java @@ -0,0 +1,58 @@ +package com.alttd.velocitychat.commands.partysubcommands; + +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.Party; +import com.alttd.chat.util.Utility; +import com.alttd.velocitychat.commands.SubCommand; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.Player; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; + +import java.util.List; + +public class Create implements SubCommand { + + @Override + public String getName() { + return "create"; + } + + @Override + public void execute(String[] args, CommandSource source) { + if (!(source instanceof Player player)) { + source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE)); + return; + } + if (args.length < 3 || !args[1].matches("[\\w]{3,16}") || !args[2].matches("[\\w]{3,16}")) { + source.sendMessage(Utility.parseMiniMessage(getHelpMessage())); + return; + } + if (PartyManager.getParty(args[1]) != null) { + source.sendMessage(Utility.parseMiniMessage("A chat party with this name already exists.")); + return; + } + Party party = Queries.addParty(player.getUniqueId(), args[1], args[2]); +// party.addUser(ChatUserManager.getChatUser(player.getUniqueId())); //Removed until we can get nicknames to translate to colors correctly + party.addUser(ChatUserManager.getChatUser(player.getUniqueId()), player.getUsername()); + PartyManager.addParty(party); + source.sendMessage(Utility.parseMiniMessage(Config.CREATED_PARTY, + List.of(Template.template("party_name", party.getPartyName()), + Template.template("party_password", party.getPartyPassword())))); +// update(player, party.getPartyId()); + //TODO put party in active party list + } + + @Override + public List suggest(String[] args) { + return null; + } + + @Override + public String getHelpMessage() { + return null; + } +} diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Invite.java b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Invite.java new file mode 100644 index 0000000..7d0f468 --- /dev/null +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Invite.java @@ -0,0 +1,72 @@ +package com.alttd.velocitychat.commands.partysubcommands; + +import com.alttd.chat.config.Config; +import com.alttd.chat.managers.PartyManager; +import com.alttd.chat.objects.Party; +import com.alttd.chat.util.Utility; +import com.alttd.velocitychat.VelocityChat; +import com.alttd.velocitychat.commands.SubCommand; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.Player; +import net.kyori.adventure.text.minimessage.Template; + +import java.util.List; +import java.util.Optional; + +public class Invite implements SubCommand { + + @Override + public String getName() { + return "invite"; + } + + @Override + public void execute(String[] args, CommandSource source) { + if (!(source instanceof Player player)) { + source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE)); + return; + } + if (args.length < 2) { + source.sendMessage(Utility.parseMiniMessage(getHelpMessage())); + return; + } + Party party = PartyManager.getParty(player.getUniqueId()); + if (party == null) { + source.sendMessage(Utility.parseMiniMessage(Config.NOT_IN_A_PARTY)); + return; + } + if (!party.getOwnerUuid().equals(player.getUniqueId())) { + source.sendMessage(Utility.parseMiniMessage(Config.NOT_YOUR_PARTY)); + return; + } + Optional optional = VelocityChat.getPlugin().getProxy().getPlayer(args[1]); + if (optional.isEmpty()) { + source.sendMessage(Utility.parseMiniMessage(Config.INVALID_PLAYER)); + return; + } + Player target = optional.get(); + + if (!target.isActive()) { + source.sendMessage(Utility.parseMiniMessage(Config.NOT_ONLINE, List.of( + Template.template("player", target.getUsername()) + ))); + return; + } + + target.sendMessage(Utility.parseMiniMessage("You received an invite to join " + party.getPartyName() + " click this message to accept.")); + source.sendMessage(Utility.parseMiniMessage("You send a chat party invite to !", List.of( + Template.template("player", target.getUsername()) + ))); + } + + @Override + public List suggest(String[] args) { + return null; + } + + @Override + public String getHelpMessage() { + return null; + } +} diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Join.java b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Join.java new file mode 100644 index 0000000..582c729 --- /dev/null +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Join.java @@ -0,0 +1,59 @@ +package com.alttd.velocitychat.commands.partysubcommands; + +import com.alttd.chat.config.Config; +import com.alttd.chat.managers.ChatUserManager; +import com.alttd.chat.managers.PartyManager; +import com.alttd.chat.objects.Party; +import com.alttd.chat.util.Utility; +import com.alttd.velocitychat.commands.SubCommand; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.Player; +import net.kyori.adventure.text.minimessage.Template; + +import java.util.List; + +public class Join implements SubCommand { + + @Override + public String getName() { + return "join"; + } + + @Override + public void execute(String[] args, CommandSource source) { + if (!(source instanceof Player player)) { + source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE)); + return; + } + if (args.length < 3 || !args[1].matches("[\\w]{3,16}") || !args[2].matches("[\\w]{3,16}")) { + source.sendMessage(Utility.parseMiniMessage(getHelpMessage())); + return; + } + + Party party = PartyManager.getParty(args[1]); + if (party == null) { + source.sendMessage(Utility.parseMiniMessage(Config.NOT_A_PARTY)); + return; + } + if (!party.getPartyPassword().equals(args[2])) { + source.sendMessage(Utility.parseMiniMessage(Config.INVALID_PASSWORD)); + return; + } + +// party.addUser(ChatUserManager.getChatUser(player.getUniqueId())); //Removed until we can get nicknames to translate to colors correctly + party.addUser(ChatUserManager.getChatUser(player.getUniqueId()), player.getUsername()); + source.sendMessage(Utility.parseMiniMessage(Config.JOINED_PARTY, List.of( + Template.template("party_name", party.getPartyName())))); +// update(player, party.getPartyId()); TODO update party + } + + @Override + public List suggest(String[] args) { + return null; + } + + @Override + public String getHelpMessage() { + return null; + } +} diff --git a/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Leave.java b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Leave.java new file mode 100644 index 0000000..6525c9e --- /dev/null +++ b/velocity/src/main/java/com/alttd/velocitychat/commands/partysubcommands/Leave.java @@ -0,0 +1,66 @@ +package com.alttd.velocitychat.commands.partysubcommands; + +import com.alttd.chat.config.Config; +import com.alttd.chat.managers.PartyManager; +import com.alttd.chat.objects.Party; +import com.alttd.chat.util.Utility; +import com.alttd.velocitychat.VelocityChat; +import com.alttd.velocitychat.commands.SubCommand; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ServerConnection; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public class Leave implements SubCommand { + + @Override + public String getName() { + return "leave"; + } + + @Override + public void execute(String[] args, CommandSource source) { + if (!(source instanceof Player player)) { + source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE)); + return; + } + Party party = PartyManager.getParty(player.getUniqueId()); + if (party == null) { + source.sendMessage(Utility.parseMiniMessage(Config.NOT_IN_A_PARTY)); + return; + } + Optional currentServer = player.getCurrentServer(); + if (currentServer.isEmpty()) + return; + + party.removeUser(player.getUniqueId()); + if (party.getOwnerUuid().equals(player.getUniqueId())) { + if (party.getPartyUsers().size() > 0) { + UUID uuid = party.newOwner(); + source.sendMessage(Utility.parseMiniMessage(Config.NOTIFY_FINDING_NEW_OWNER)); + VelocityChat.getPlugin().getChatHandler().partyMessage(party, player, "" + + player.getUsername() + + " left the chat party, the new party owner is " + party.getPartyUser(uuid).getPlayerName(), + currentServer.get()); + } else { + party.delete(); + } + } else { + source.sendMessage(Utility.parseMiniMessage(Config.LEFT_PARTY)); + } +// update(player, party.getPartyId()); TODO update party + } + + @Override + public List suggest(String[] args) { + return null; + } + + @Override + public String getHelpMessage() { + return null; + } +} diff --git a/velocity/src/main/java/com/alttd/velocitychat/handlers/ChatHandler.java b/velocity/src/main/java/com/alttd/velocitychat/handlers/ChatHandler.java index eb8df44..29a4ccb 100755 --- a/velocity/src/main/java/com/alttd/velocitychat/handlers/ChatHandler.java +++ b/velocity/src/main/java/com/alttd/velocitychat/handlers/ChatHandler.java @@ -4,6 +4,7 @@ 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.managers.RegexManager; import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.Mail; import com.alttd.chat.objects.Party; @@ -16,6 +17,7 @@ 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; @@ -83,6 +85,50 @@ public class ChatHandler { // player2.sendMessage(receiverMessage); } + public void partyMessage(Party party, Player player, String message, ServerConnection serverConnection) { + ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); + Component senderName = user.getDisplayName(); + + String updatedMessage = RegexManager.replaceText(player.getUsername(), player.getUniqueId(), message); + if(updatedMessage == null) { +// GalaxyUtility.sendBlockedNotification("Party Language", player, message, ""); + return; // the message was blocked + } + + if(!player.hasPermission("chat.format")) { + updatedMessage = Utility.stripTokens(updatedMessage); + } + + if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>"); + + updatedMessage = Utility.formatText(updatedMessage); + + List