Compare commits

..

6 Commits

Author SHA1 Message Date
Teriuihi 42736bd566 Update APRIL_FOOLS_RESET string in Config class
APRIL_FOOLS_RESET string in Config class has been updated from "reverse" to "esrever". This change updates the behavior of the April Fools feature in the chat application.
2024-03-31 14:53:36 +02:00
Teriuihi 157edbd6b6 Merge branch 'main' into april_fools 2024-03-31 13:37:42 +02:00
Teriuihi a7e029b0a1 Refactor April 1st date check in ChatListener
Removed the more complex date logic used to check for April 1st in the ChatListener class. This check checked if it was April 1st in any timezone. Replaced it with a simpler check using LocalDate's methods for month and day comparison. This only checks if it's April 1st in UTC. This aims to enhance code readability and simplify date handling.
2024-03-31 10:56:21 +02:00
Teriuihi ab98222f06 Refactor April 1st check in ChatListener class
A new method, `isWithinApril1st`, is introduced to simplify April 1st date check logic in the `ChatListener` class. This new method calculates the start and end of April 1st for all timezones in UTC and determines if the current time falls within this range.
2024-03-24 17:24:24 +01:00
Teriuihi ad91cda0c0 Add method to remove string at start
A new method `removeStringAtStart` has been added to the `ModifiableString` class. It replaces the starting string in a given text. Also, a condition has been modified in the `ChatListener` class to invoke this new method when the input string starts with the `APRIL_FOOLS_RESET` string. Additional unit tests were created to validate these changes.
2024-03-24 17:12:55 +01:00
Teriuihi 9270423928 Add reverse chat feature for April Fools' and corresponding tests
This commit includes a new feature that reverses the text speech of chat users on April 1st as an April Fools' prank. It also includes a reset option, configurable via a new parameter in the configuration file. Furthermore, it provides tests for the reverse string functionality, ensuring that it works correctly, even with complex strings that include tags.
2024-03-24 16:58:09 +01:00
100 changed files with 1749 additions and 4966 deletions

23
Jenkinsfile vendored
View File

@ -1,23 +0,0 @@
pipeline {
agent any
environment {
NEXUS_CREDS = credentials('alttd-snapshot-user')
}
stages {
stage('Gradle') {
steps {
sh './gradlew build -PalttdSnapshotUsername=$NEXUS_CREDS_USR -PalttdSnapshotPassword=$NEXUS_CREDS_PSW'
}
}
stage('Archive') {
steps {
archiveArtifacts artifacts: 'build/libs/', followSymlinks: false
}
}
stage('discord') {
steps {
discordSend description: "Build: ${BUILD_NUMBER}", showChangeset: true, result: currentBuild.currentResult, title: currentBuild.fullProjectName, webhookURL: env.discordwebhook
}
}
}
}

View File

@ -3,20 +3,14 @@ plugins {
} }
dependencies { dependencies {
implementation(project(":web-api")) // Web-API compileOnly("com.alttd:Galaxy-API:1.19.2-R0.1-SNAPSHOT") {
compileOnly("org.projectlombok:lombok:1.18.46") // exclude("net.kyori")
annotationProcessor("org.projectlombok:lombok:1.18.46")
// Cosmos
compileOnly("com.alttd.cosmos:cosmos-api:1.21.8-R0.1-SNAPSHOT") {
isChanging = true
} }
compileOnly("org.spongepowered:configurate-yaml:4.2.0") // Configurate compileOnly("org.spongepowered:configurate-yaml:4.1.2") // Configurate
compileOnly("net.luckperms:api:5.5") // Luckperms compileOnly("net.luckperms:api:5.3") // Luckperms
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
//API validation testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
implementation("org.hibernate.validator:hibernate-validator:9.0.1.Final") testImplementation("com.alttd:Galaxy-API:1.19.2-R0.1-SNAPSHOT")
implementation("org.glassfish:jakarta.el:5.0.0-M1")
implementation("com.fasterxml.jackson.core:jackson-databind:2.22.1")
} }
publishing { publishing {
@ -26,7 +20,7 @@ publishing {
} }
} }
repositories { repositories{
maven { maven {
name = "maven" name = "maven"
url = uri("https://repo.destro.xyz/snapshots") url = uri("https://repo.destro.xyz/snapshots")
@ -34,3 +28,7 @@ publishing {
} }
} }
} }
tasks.test {
useJUnitPlatform()
}

View File

@ -15,9 +15,9 @@ public abstract interface ChatAPI {
DatabaseConnection getDataBase(); DatabaseConnection getDataBase();
void reloadConfig(); void ReloadConfig();
void reloadChatFilters(); void ReloadChatFilters();
HashMap<String, String> getPrefixes(); HashMap<String, String> getPrefixes();

View File

@ -5,12 +5,15 @@ import com.alttd.chat.config.PrefixConfig;
import com.alttd.chat.database.DatabaseConnection; import com.alttd.chat.database.DatabaseConnection;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.managers.RegexManager; import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.util.ALogger;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.group.Group; import net.luckperms.api.model.group.Group;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
public class ChatImplementation implements ChatAPI{ public class ChatImplementation implements ChatAPI{
@ -23,7 +26,7 @@ public class ChatImplementation implements ChatAPI{
public ChatImplementation() { public ChatImplementation() {
instance = this; instance = this;
reloadConfig(); ReloadConfig();
luckPerms = getLuckPerms(); luckPerms = getLuckPerms();
databaseConnection = getDataBase(); databaseConnection = getDataBase();
@ -54,13 +57,13 @@ public class ChatImplementation implements ChatAPI{
} }
@Override @Override
public void reloadConfig() { public void ReloadConfig() {
Config.init(); Config.init();
loadPrefixes(); loadPrefixes();
} }
@Override @Override
public void reloadChatFilters() { public void ReloadChatFilters() {
RegexManager.initialize(); RegexManager.initialize();
} }

View File

@ -1,11 +1,10 @@
package com.alttd.chat.config; package com.alttd.chat.config;
import com.alttd.chat.objects.channels.CustomChannel; import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.leangen.geantyref.TypeToken; import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions; import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.serialize.SerializationException; import org.spongepowered.configurate.serialize.SerializationException;
@ -20,7 +19,6 @@ import java.lang.reflect.Modifier;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@SuppressWarnings("unused")
public final class Config { public final class Config {
private static final Pattern PATH_PATTERN = Pattern.compile("\\."); private static final Pattern PATH_PATTERN = Pattern.compile("\\.");
private static final String HEADER = ""; private static final String HEADER = "";
@ -33,33 +31,32 @@ public final class Config {
static boolean verbose; static boolean verbose;
public static File CONFIGPATH; public static File CONFIGPATH;
public static void init() { public static void init() {
CONFIGPATH = new File(File.separator + "mnt" + File.separator + "configs" + File.separator + "ChatPlugin"); CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "ChatPlugin");
CONFIG_FILE = new File(CONFIGPATH, "config.yml"); CONFIG_FILE = new File(CONFIGPATH, "config.yml");
configLoader = YamlConfigurationLoader.builder() configLoader = YamlConfigurationLoader.builder()
.file(CONFIG_FILE) .file(CONFIG_FILE)
.nodeStyle(NodeStyle.BLOCK) .nodeStyle(NodeStyle.BLOCK)
.build(); .build();
if (!CONFIG_FILE.getParentFile().exists()) { if (!CONFIG_FILE.getParentFile().exists()) {
if (!CONFIG_FILE.getParentFile().mkdirs()) { if(!CONFIG_FILE.getParentFile().mkdirs()) {
return; return;
} }
} }
if (!CONFIG_FILE.exists()) { if (!CONFIG_FILE.exists()) {
try { try {
if (!CONFIG_FILE.createNewFile()) { if(!CONFIG_FILE.createNewFile()) {
return; return;
} }
} catch (IOException error) { } catch (IOException error) {
ALogger.error(error.getMessage(), error); error.printStackTrace();
} }
} }
try { try {
config = configLoader.load(ConfigurationOptions.defaults().header(HEADER).shouldCopyDefaults(false)); config = configLoader.load(ConfigurationOptions.defaults().header(HEADER).shouldCopyDefaults(false));
} catch (IOException e) { } catch (IOException e) {
ALogger.error(e.getMessage(), e); e.printStackTrace();
} }
verbose = getBoolean("verbose", true); verbose = getBoolean("verbose", true);
@ -69,7 +66,7 @@ public final class Config {
try { try {
configLoader.save(config); configLoader.save(config);
} catch (IOException e) { } catch (IOException e) {
ALogger.error(e.getMessage(), e); e.printStackTrace();
} }
} }
@ -81,7 +78,7 @@ public final class Config {
method.setAccessible(true); method.setAccessible(true);
method.invoke(instance); method.invoke(instance);
} catch (InvocationTargetException | IllegalAccessException ex) { } catch (InvocationTargetException | IllegalAccessException ex) {
ALogger.error(ex.getMessage(), ex); ex.printStackTrace();
} }
} }
} }
@ -89,7 +86,7 @@ public final class Config {
try { try {
configLoader.save(config); configLoader.save(config);
} catch (IOException ex) { } catch (IOException ex) {
ALogger.error(ex.getMessage(), ex); ex.printStackTrace();
} }
} }
@ -97,7 +94,7 @@ public final class Config {
try { try {
configLoader.save(config); configLoader.save(config);
} catch (IOException ex) { } catch (IOException ex) {
ALogger.error(ex.getMessage(), ex); ex.printStackTrace();
} }
} }
@ -106,22 +103,21 @@ public final class Config {
} }
private static void set(String path, Object def) { private static void set(String path, Object def) {
if (config.node(splitPath(path)).virtual()) { if(config.node(splitPath(path)).virtual()) {
try { try {
config.node(splitPath(path)).set(def); config.node(splitPath(path)).set(def);
} catch (SerializationException e) { } catch (SerializationException e) {
ALogger.error(e.getMessage(), e); e.printStackTrace();
} }
} }
} }
private static void setString(String path, String def) { private static void setString(String path, String def) {
try { try {
if (config.node(splitPath(path)).virtual()) { if(config.node(splitPath(path)).virtual())
config.node(splitPath(path)).set(io.leangen.geantyref.TypeToken.get(String.class), def); config.node(splitPath(path)).set(io.leangen.geantyref.TypeToken.get(String.class), def);
} } catch(SerializationException ex) {
} catch (SerializationException ex) { ex.printStackTrace();
ALogger.error(ex.getMessage(), ex);
} }
} }
@ -154,23 +150,21 @@ public final class Config {
try { try {
set(path, def); set(path, def);
return config.node(splitPath(path)).getList(TypeToken.get(String.class)); return config.node(splitPath(path)).getList(TypeToken.get(String.class));
} catch (SerializationException ex) { } catch(SerializationException ex) {
ALogger.error(ex.getMessage(), ex); ex.printStackTrace();
} }
return new ArrayList<>(); return new ArrayList<>();
} }
private static ConfigurationNode getNode(String path) { private static ConfigurationNode getNode(String path) {
if (config.node(splitPath(path)).virtual()) { if(config.node(splitPath(path)).virtual()) {
//new RegexConfig("Dummy"); //new RegexConfig("Dummy");
} }
config.childrenMap(); config.childrenMap();
return config.node(splitPath(path)); return config.node(splitPath(path));
} }
/** /** ONLY EDIT ANYTHING BELOW THIS LINE **/
* ONLY EDIT ANYTHING BELOW THIS LINE
**/
public static List<String> PREFIXGROUPS = new ArrayList<>(); public static List<String> PREFIXGROUPS = new ArrayList<>();
public static List<String> CONFLICTINGPREFIXGROUPS = new ArrayList<>(); public static List<String> CONFLICTINGPREFIXGROUPS = new ArrayList<>();
public static List<String> STAFFGROUPS = new ArrayList<>(); public static List<String> STAFFGROUPS = new ArrayList<>();
@ -179,19 +173,13 @@ public final class Config {
public static UUID CONSOLEUUID = UUID.randomUUID(); public static UUID CONSOLEUUID = UUID.randomUUID();
public static int EMOTELIMIT = 3; public static int EMOTELIMIT = 3;
public static String MENTIONPLAYERTAG = "<aqua>@</aqua>"; public static String MENTIONPLAYERTAG = "<aqua>@</aqua>";
private static void settings() { private static void settings() {
PREFIXGROUPS = getList("settings.prefix-groups", PREFIXGROUPS = getList("settings.prefix-groups",
Lists.newArrayList("discord", "socialmedia", "eventteam", "eventleader", "youtube", "twitch", Lists.newArrayList("discord", "socialmedia", "eventteam", "eventleader", "youtube", "twitch", "developer"));
"developer"
)
);
CONFLICTINGPREFIXGROUPS = getList("settings.prefix-conflicts-groups", CONFLICTINGPREFIXGROUPS = getList("settings.prefix-conflicts-groups",
Lists.newArrayList("eventteam", "eventleader") Lists.newArrayList("eventteam", "eventleader"));
);
STAFFGROUPS = getList("settings.staff-groups", STAFFGROUPS = getList("settings.staff-groups",
Lists.newArrayList("trainee", "moderator", "headmod", "admin", "manager", "owner") Lists.newArrayList("trainee", "moderator", "headmod", "admin", "manager", "owner"));
);
CONSOLENAME = getString("settings.console-name", CONSOLENAME); CONSOLENAME = getString("settings.console-name", CONSOLENAME);
CONSOLEUUID = UUID.fromString(getString("settings.console-uuid", CONSOLEUUID.toString())); CONSOLEUUID = UUID.fromString(getString("settings.console-uuid", CONSOLEUUID.toString()));
MINIMIUMSTAFFRANK = getString("settings.minimum-staff-rank", MINIMIUMSTAFFRANK); MINIMIUMSTAFFRANK = getString("settings.minimum-staff-rank", MINIMIUMSTAFFRANK);
@ -201,13 +189,10 @@ public final class Config {
public static List<String> MESSAGECOMMANDALIASES = new ArrayList<>(); public static List<String> MESSAGECOMMANDALIASES = new ArrayList<>();
public static List<String> REPLYCOMMANDALIASES = new ArrayList<>(); public static List<String> REPLYCOMMANDALIASES = new ArrayList<>();
public static String MESSAGESENDER = public static String MESSAGESENDER = "<hover:show_text:Click to reply><click:suggest_command:/msg <receivername> ><light_purple>(Me -> <gray><receiver></gray>)</hover> <message>";
"<hover:show_text:Click to reply><click:suggest_command:/msg <receivername> ><light_purple>(Me -> <gray><receiver></gray>)</hover> <message>"; public static String MESSAGERECIEVER = "<hover:show_text:Click to reply><click:suggest_command:/msg <sendername> ><light_purple>(<gray><sender></gray> on <server> -> Me)</hover> <message>";
public static String MESSAGERECIEVER =
"<hover:show_text:Click to reply><click:suggest_command:/msg <sendername> ><light_purple>(<gray><sender></gray> on <server> -> Me)</hover> <message>";
public static String MESSAGESPY = "<gray>(<gray><sendername></gray> -> <receivername>) <message>"; public static String MESSAGESPY = "<gray>(<gray><sendername></gray> -> <receivername>) <message>";
public static String RECEIVER_DOES_NOT_EXIST = "<red><player> is not a valid player.</red>"; public static String RECEIVER_DOES_NOT_EXIST = "<red><player> is not a valid player.</red>";
private static void messageCommand() { private static void messageCommand() {
MESSAGECOMMANDALIASES.clear(); MESSAGECOMMANDALIASES.clear();
REPLYCOMMANDALIASES.clear(); REPLYCOMMANDALIASES.clear();
@ -219,14 +204,12 @@ public final class Config {
RECEIVER_DOES_NOT_EXIST = getString("commands.message.receiver-does-not-exist", RECEIVER_DOES_NOT_EXIST); RECEIVER_DOES_NOT_EXIST = getString("commands.message.receiver-does-not-exist", RECEIVER_DOES_NOT_EXIST);
} }
public static String GCFORMAT = public static String GCFORMAT = "<white><light_purple><prefix></light_purple> <gray><sender></gray> <hover:show_text:on <server>><yellow>to Global</yellow></hover><gray>: <message>";
"<white><light_purple><prefix></light_purple> <gray><sender></gray> <hover:show_text:on <server>><yellow>to Global</yellow></hover><gray>: <message>";
public static String GCPERMISSION = "proxy.globalchat"; public static String GCPERMISSION = "proxy.globalchat";
public static List<String> GCALIAS = new ArrayList<>(); public static List<String> GCALIAS = new ArrayList<>();
public static String GCNOTENABLED = "You don't have global chat enabled."; public static String GCNOTENABLED = "You don't have global chat enabled.";
public static String GCONCOOLDOWN = "You have to wait <cooldown> seconds before using this feature again."; public static String GCONCOOLDOWN = "You have to wait <cooldown> seconds before using this feature again.";
public static int GCCOOLDOWN = 30; public static int GCCOOLDOWN = 30;
private static void globalChat() { private static void globalChat() {
GCFORMAT = getString("commands.globalchat.format", GCFORMAT); GCFORMAT = getString("commands.globalchat.format", GCFORMAT);
GCPERMISSION = getString("commands.globalchat.view-chat-permission", GCPERMISSION); GCPERMISSION = getString("commands.globalchat.view-chat-permission", GCPERMISSION);
@ -236,32 +219,26 @@ public final class Config {
GCCOOLDOWN = getInt("commands.globalchat.cooldown", GCCOOLDOWN); GCCOOLDOWN = getInt("commands.globalchat.cooldown", GCCOOLDOWN);
} }
public static String CHATFORMAT = public static String CHATFORMAT = "<white><light_purple><prefixall> <gray><hover:show_text:Click to message <sendername>><click:suggest_command:/msg <sendername> ><sender></hover>: <white><message>";
"<white><light_purple><prefixall> <gray><hover:show_text:Click to message <sendername>><click:suggest_command:/msg <sendername> ><sender></hover>: <white><message>";
public static String URLFORMAT = "<click:OPEN_URL:<clickurl>><url></click>"; public static String URLFORMAT = "<click:OPEN_URL:<clickurl>><url></click>";
private static void Chat() { private static void Chat() {
CHATFORMAT = getString("chat.format", CHATFORMAT); CHATFORMAT = getString("chat.format", CHATFORMAT);
URLFORMAT = getString("chat.urlformat", URLFORMAT); URLFORMAT = getString("chat.urlformat", URLFORMAT);
} }
public static List<String> GACECOMMANDALIASES = new ArrayList<>(); public static List<String> GACECOMMANDALIASES = new ArrayList<>();
public static String GACFORMAT = public static String GACFORMAT = "<hover:show_text:Click to reply><click:suggest_command:/acg ><yellow>(<sender> on <server> -> Team)</hover> <message>";
"<hover:show_text:Click to reply><click:suggest_command:/acg ><yellow>(<sender> on <server> -> Team)</hover> <message>";
private static void globalAdminChat() { private static void globalAdminChat() {
GACECOMMANDALIASES = getList("commands.globaladminchat.aliases", Lists.newArrayList("acg")); GACECOMMANDALIASES = getList("commands.globaladminchat.aliases", Lists.newArrayList("acg"));
GACFORMAT = getString("commands.globaladminchat.format", GACFORMAT); GACFORMAT = getString("commands.globaladminchat.format", GACFORMAT);
} }
public static String MESSAGECHANNEL = "altitude:chatplugin"; public static String MESSAGECHANNEL = "altitude:chatplugin";
private static void messageChannels() { private static void messageChannels() {
MESSAGECHANNEL = getString("settings.message-channel", MESSAGECHANNEL); MESSAGECHANNEL = getString("settings.message-channel", MESSAGECHANNEL);
} }
public static ConfigurationNode REGEXNODE = null; public static ConfigurationNode REGEXNODE = null;
private static void RegexNOde() { private static void RegexNOde() {
REGEXNODE = getNode("regex-settings"); REGEXNODE = getNode("regex-settings");
} }
@ -270,7 +247,6 @@ public final class Config {
public static String SERVERSWTICHMESSAGETO = "<gray>* <player> leaves to <to_server>..."; public static String SERVERSWTICHMESSAGETO = "<gray>* <player> leaves to <to_server>...";
public static String SERVERJOINMESSAGE = "<green>* <player> appears from thin air..."; public static String SERVERJOINMESSAGE = "<green>* <player> appears from thin air...";
public static String SERVERLEAVEMESSAGE = "<red>* <player> vanishes in the mist..."; public static String SERVERLEAVEMESSAGE = "<red>* <player> vanishes in the mist...";
private static void JoinLeaveMessages() { private static void JoinLeaveMessages() {
SERVERSWTICHMESSAGEFROM = getString("messages.switch-server-from", SERVERSWTICHMESSAGEFROM); SERVERSWTICHMESSAGEFROM = getString("messages.switch-server-from", SERVERSWTICHMESSAGEFROM);
SERVERSWTICHMESSAGETO = getString("messages.switch-server-to", SERVERSWTICHMESSAGETO); SERVERSWTICHMESSAGETO = getString("messages.switch-server-to", SERVERSWTICHMESSAGETO);
@ -279,14 +255,12 @@ public final class Config {
} }
public static String PARTY_FORMAT = public static String PARTY_FORMAT = "<dark_aqua>(<gray><sender></gray><hover:show_text:\"on <server>\"> → <party></hover>) <message>";
"<dark_aqua>(<gray><sender></gray><hover:show_text:\"on <server>\"> → <party></hover>) <message>"; public static String PARTY_SPY = "<i><gray>PC:</gray><dark_gray> <dark_gray><username></dark_gray>: <dark_gray><party></dark_gray> <message></dark_gray></i>";
public static String PARTY_SPY =
"<i><gray>PC:</gray><dark_gray> <dark_gray><username></dark_gray>: <dark_gray><party></dark_gray> <message></dark_gray></i>";
public static String NO_PERMISSION = "<red>You don't have permission to use this command.</red>"; public static String NO_PERMISSION = "<red>You don't have permission to use this command.</red>";
public static String NO_CONSOLE = "<red>This command can not be used by console</red>"; public static String NO_CONSOLE = "<red>This command can not be used by console</red>";
public static String CREATED_PARTY = "<green>You created a chat party called: " + public static String CREATED_PARTY = "<green>You created a chat party called: " +
"'<gold><party_name></gold>' with the password: '<gold><party_password></gold>'</green>"; "'<gold><party_name></gold>' with the password: '<gold><party_password></gold>'</green>";
public static String NOT_IN_A_PARTY = "<red>You're not in a chat party.</red>"; public static String NOT_IN_A_PARTY = "<red>You're not in a chat party.</red>";
public static String NOT_YOUR_PARTY = "<red>You don't own this chat party.</red>"; public static String NOT_YOUR_PARTY = "<red>You don't own this chat party.</red>";
public static String NOT_A_PARTY = "<red>This chat party does not exist.</red>"; public static String NOT_A_PARTY = "<red>This chat party does not exist.</red>";
@ -296,11 +270,9 @@ public final class Config {
public static String INVALID_PASSWORD = "<red>Invalid password.</red>"; public static String INVALID_PASSWORD = "<red>Invalid password.</red>";
public static String JOINED_PARTY = "<green>You joined <party_name>!</green>"; public static String JOINED_PARTY = "<green>You joined <party_name>!</green>";
public static String PLAYER_JOINED_PARTY = "<green><player_name> joined <party_name>!</green>"; public static String PLAYER_JOINED_PARTY = "<green><player_name> joined <party_name>!</green>";
public static String NOTIFY_FINDING_NEW_OWNER = public static String NOTIFY_FINDING_NEW_OWNER = "<dark_aqua>Since you own this chat party a new party owner will be chosen.<dark_aqua>";
"<dark_aqua>Since you own this chat party a new party owner will be chosen.<dark_aqua>";
public static String LEFT_PARTY = "<green>You have left the chat party!</green>"; public static String LEFT_PARTY = "<green>You have left the chat party!</green>";
public static String OWNER_LEFT_PARTY = public static String OWNER_LEFT_PARTY = "<dark_aqua>[ChatParty]: <old_owner> left the chat party, the new party owner is <new_owner></dark_aqua>";
"<dark_aqua>[ChatParty]: <old_owner> left the chat party, the new party owner is <new_owner></dark_aqua>";
public static String PLAYER_LEFT_PARTY = "<dark_aqua>[ChatParty]: <player_name> left the chat party!</dark_aqua>"; public static String PLAYER_LEFT_PARTY = "<dark_aqua>[ChatParty]: <player_name> left the chat party!</dark_aqua>";
public static String NEW_PARTY_OWNER = "<dark_aqua>[ChatParty]: <old_owner> transferred the party to <new_owner>!"; public static String NEW_PARTY_OWNER = "<dark_aqua>[ChatParty]: <old_owner> transferred the party to <new_owner>!";
public static String CANT_REMOVE_PARTY_OWNER = "<red>You can't remove yourself, please leave instead.</red>"; public static String CANT_REMOVE_PARTY_OWNER = "<red>You can't remove yourself, please leave instead.</red>";
@ -311,26 +283,23 @@ public final class Config {
public static String ALREADY_IN_THIS_PARTY = "<red>You're already in <party>!</red>"; public static String ALREADY_IN_THIS_PARTY = "<red>You're already in <party>!</red>";
public static String SENT_PARTY_INV = "<green>You send a chat party invite to <player>!</green>"; public static String SENT_PARTY_INV = "<green>You send a chat party invite to <player>!</green>";
public static String JOIN_PARTY_CLICK_MESSAGE = "<click:run_command:'/party join <party> <party_password>'>" + public static String JOIN_PARTY_CLICK_MESSAGE = "<click:run_command:'/party join <party> <party_password>'>" +
"<dark_aqua>You received an invite to join <party>, click this message to accept.</dark_aqua></click>"; "<dark_aqua>You received an invite to join <party>, click this message to accept.</dark_aqua></click>";
public static String PARTY_MEMBER_LOGGED_ON = "<dark_aqua>[ChatParty] <player> joined Altitude...</dark_aqua>"; public static String PARTY_MEMBER_LOGGED_ON = "<dark_aqua>[ChatParty] <player> joined Altitude...</dark_aqua>";
public static String PARTY_MEMBER_LOGGED_OFF = "<dark_aqua>[ChatParty] <player> left Altitude...</dark_aqua>"; public static String PARTY_MEMBER_LOGGED_OFF = "<dark_aqua>[ChatParty] <player> left Altitude...</dark_aqua>";
public static String RENAMED_PARTY = public static String RENAMED_PARTY = "<dark_aqua>[ChatParty] <owner> changed the party name from <old_name> to <new_name>!</dark_aqua>";
"<dark_aqua>[ChatParty] <owner> changed the party name from <old_name> to <new_name>!</dark_aqua>";
public static String CHANGED_PASSWORD = "<green>Password was set to <password></green>"; public static String CHANGED_PASSWORD = "<green>Password was set to <password></green>";
public static String DISBAND_PARTY_CONFIRM = "<green><bold>Are you sure you want to disband your party?</bold> " + public static String DISBAND_PARTY_CONFIRM = "<green><bold>Are you sure you want to disband your party?</bold> " +
"Type <gold>/party disband confirm <party></gold> to confirm."; "Type <gold>/party disband confirm <party></gold> to confirm.";
public static String DISBANDED_PARTY = public static String DISBANDED_PARTY = "<dark_aqua>[ChatParty] <owner> has disbanded <party>, everyone has been removed.</dark_aqua>";
"<dark_aqua>[ChatParty] <owner> has disbanded <party>, everyone has been removed.</dark_aqua>";
public static String PARTY_INFO = """ public static String PARTY_INFO = """
<gold><bold>Chat party info</bold>: <gold><bold>Chat party info</bold>:
</gold><green>Name: <dark_aqua><party></dark_aqua> </gold><green>Name: <dark_aqua><party></dark_aqua>
Password: <dark_aqua><password></dark_aqua> Password: <dark_aqua><password></dark_aqua>
Owner: <owner> Owner: <owner>
Members: <members>"""; Members: <members>""";
public static ComponentLike ONLINE_PREFIX = null; public static Component ONLINE_PREFIX = null;
public static ComponentLike OFFLINE_PREFIX = null; public static Component OFFLINE_PREFIX = null;
public static String PARTY_TOGGLED = "<dark_aqua>Party chat toggled <status>.</dark_aqua>"; public static String PARTY_TOGGLED = "<dark_aqua>Party chat toggled <status>.</dark_aqua>";
private static void party() { private static void party() {
PARTY_FORMAT = getString("party.format", PARTY_FORMAT); PARTY_FORMAT = getString("party.format", PARTY_FORMAT);
PARTY_SPY = getString("party.spy", PARTY_SPY); PARTY_SPY = getString("party.spy", PARTY_SPY);
@ -369,27 +338,17 @@ public final class Config {
public static String PARTY_HELP_WRAPPER = "<gold>ChatParty help:\n<commands></gold>"; public static String PARTY_HELP_WRAPPER = "<gold>ChatParty help:\n<commands></gold>";
public static String PARTY_HELP_HELP = "<green>Show this menu: <gold>/party help</gold></green>"; public static String PARTY_HELP_HELP = "<green>Show this menu: <gold>/party help</gold></green>";
public static String PARTY_HELP_CREATE = public static String PARTY_HELP_CREATE = "<green>Create a party: <gold>/party create <party_name> <party_password></gold></green>";
"<green>Create a party: <gold>/party create <party_name> <party_password></gold></green>"; public static String PARTY_HELP_INFO = "<green>Show info about your current party: <gold>/party info</gold></green>";
public static String PARTY_HELP_INFO = public static String PARTY_HELP_INVITE = "<green>Invite a user to your party: <gold>/party invite <username></gold></green>";
"<green>Show info about your current party: <gold>/party info</gold></green>"; public static String PARTY_HELP_JOIN = "<green>Join a party: <gold>/party join <party_name> <party_password></gold></green>";
public static String PARTY_HELP_INVITE =
"<green>Invite a user to your party: <gold>/party invite <username></gold></green>";
public static String PARTY_HELP_JOIN =
"<green>Join a party: <gold>/party join <party_name> <party_password></gold></green>";
public static String PARTY_HELP_LEAVE = "<green>Leave your current party: <gold>/party leave</gold></green>"; public static String PARTY_HELP_LEAVE = "<green>Leave your current party: <gold>/party leave</gold></green>";
public static String PARTY_HELP_NAME = public static String PARTY_HELP_NAME = "<green>Change the name of your party: <gold>/party name <new_name></gold></green>";
"<green>Change the name of your party: <gold>/party name <new_name></gold></green>"; public static String PARTY_HELP_OWNER = "<green>Change the owner of your party: <gold>/party owner <new_owner_name></gold></green>";
public static String PARTY_HELP_OWNER = public static String PARTY_HELP_PASSWORD = "<green>Change the password of your party: <gold>/party password <new_password></gold></green>";
"<green>Change the owner of your party: <gold>/party owner <new_owner_name></gold></green>"; public static String PARTY_HELP_REMOVE = "<green>Remove a member from your party: <gold>/party remove <member_name></gold></green>";
public static String PARTY_HELP_PASSWORD = public static String PARTY_HELP_DISBAND = "<green>Remove everyone from your party and disband it: <gold>/party disband</gold></green>";
"<green>Change the password of your party: <gold>/party password <new_password></gold></green>";
public static String PARTY_HELP_REMOVE =
"<green>Remove a member from your party: <gold>/party remove <member_name></gold></green>";
public static String PARTY_HELP_DISBAND =
"<green>Remove everyone from your party and disband it: <gold>/party disband</gold></green>";
public static String PARTY_HELP_CHAT = "<green>Talk in party chat: <gold>/p <message></gold></green>"; public static String PARTY_HELP_CHAT = "<green>Talk in party chat: <gold>/p <message></gold></green>";
private static void partyHelp() { private static void partyHelp() {
PARTY_HELP_WRAPPER = getString("party.help.wrapper", PARTY_HELP_WRAPPER); PARTY_HELP_WRAPPER = getString("party.help.wrapper", PARTY_HELP_WRAPPER);
PARTY_HELP_HELP = getString("party.help.help", PARTY_HELP_HELP); PARTY_HELP_HELP = getString("party.help.help", PARTY_HELP_HELP);
@ -407,18 +366,12 @@ public final class Config {
} }
public static String CUSTOM_CHANNEL_TOGGLED = "<yellow>Toggled <channel> <status>.</yellow>"; public static String CUSTOM_CHANNEL_TOGGLED = "<yellow>Toggled <channel> <status>.</yellow>";
public static ComponentLike TOGGLED_ON = null; public static Component TOGGLED_ON = null;
public static ComponentLike TOGGLED_OFF = null; public static Component TOGGLED_OFF = null;
public static double LOCAL_DISTANCE;
public static String CHANNEL_SPY =
"<i><gray>SPY:</gray> <dark_gray>(<dark_gray><sender> → <channel>) <message></dark_gray>";
private static void chatChannels() { private static void chatChannels() {
ConfigurationNode node = getNode("chat-channels"); ConfigurationNode node = getNode("chat-channels");
if (node.empty()) { if (node.empty()) {
getString("chat-channels.ac.format", getString("chat-channels.ac.format", "<white><gray><sender></gray> <hover:show_text:on <server>><yellow>to <channel></yellow></hover><gray>: <message>");
"<white><gray><sender></gray> <hover:show_text:on <server>><yellow>to <channel></yellow></hover><gray>: <message>"
);
getList("chat-channels.ac.servers", List.of("lobby")); getList("chat-channels.ac.servers", List.of("lobby"));
getBoolean("chat-channels.ac.proxy", false); getBoolean("chat-channels.ac.proxy", false);
node = getNode("chat-channels"); node = getNode("chat-channels");
@ -430,24 +383,16 @@ public final class Config {
new CustomChannel(channelName, new CustomChannel(channelName,
getString(key + "format", ""), getString(key + "format", ""),
getList(key + "servers", Collections.EMPTY_LIST), getList(key + "servers", Collections.EMPTY_LIST),
getList(key + "alias", Collections.EMPTY_LIST), getBoolean(key + "proxy", false));
getBoolean(key + "proxy", false),
getBoolean(key + "local", false),
getBoolean(key + "web", false)
);
} }
CUSTOM_CHANNEL_TOGGLED = getString("chat-channels-messages.channel-toggled", CUSTOM_CHANNEL_TOGGLED); CUSTOM_CHANNEL_TOGGLED = getString("chat-channels-messages.channel-toggled", CUSTOM_CHANNEL_TOGGLED);
TOGGLED_ON = TOGGLED_ON = Utility.parseMiniMessage(getString("chat-channels-messages.channel-on", "<green>on</green><gray>"));
Utility.parseMiniMessage(getString("chat-channels-messages.channel-on", "<green>on</green><gray>"));
TOGGLED_OFF = Utility.parseMiniMessage(getString("chat-channels-messages.channel-off", "<red>off</red><gray>")); TOGGLED_OFF = Utility.parseMiniMessage(getString("chat-channels-messages.channel-off", "<red>off</red><gray>"));
LOCAL_DISTANCE = getDouble("chat-channels-messages.local-distance", 200.0);
CHANNEL_SPY = getString("chat-channels-messages.spy", CHANNEL_SPY);
} }
public static String SERVERMUTEPERMISSION = "chat.command.mute-server"; public static String SERVERMUTEPERMISSION = "chat.command.mute-server";
public static String SPYPERMISSION = "chat.socialspy"; public static String SPYPERMISSION = "chat.socialspy";
private static void permissions() { private static void permissions() {
SERVERMUTEPERMISSION = getString("permissions.server-mute", SERVERMUTEPERMISSION); SERVERMUTEPERMISSION = getString("permissions.server-mute", SERVERMUTEPERMISSION);
SPYPERMISSION = getString("permissions.spy-permission", SPYPERMISSION); SPYPERMISSION = getString("permissions.spy-permission", SPYPERMISSION);
@ -458,7 +403,6 @@ public final class Config {
public static String DATABASE = "database"; public static String DATABASE = "database";
public static String USERNAME = "root"; public static String USERNAME = "root";
public static String PASSWORD = "root"; public static String PASSWORD = "root";
private static void database() { private static void database() {
IP = getString("database.ip", IP); IP = getString("database.ip", IP);
PORT = getString("database.port", PORT); PORT = getString("database.port", PORT);
@ -468,25 +412,18 @@ public final class Config {
} }
public static String NOTIFICATIONFORMAT = "<red>[<prefix>] <displayname> <target> <input>"; public static String NOTIFICATIONFORMAT = "<red>[<prefix>] <displayname> <target> <input>";
private static void notificationSettings() { private static void notificationSettings() {
NOTIFICATIONFORMAT = getString("settings.blockedmessage-notification", NOTIFICATIONFORMAT); NOTIFICATIONFORMAT = getString("settings.blockedmessage-notification", NOTIFICATIONFORMAT);
} }
public static String mailHeader = "===== List Mails ====='"; public static String mailHeader = "===== List Mails ====='";
public static String mailBody = public static String mailBody = "<white>From:</white> [<staffprefix>] <sender> <white><hover:show_text:'<date>'><time_ago> day(s) ago</hover>: </white><message>";
"<white>From:</white> [<staffprefix>] <sender> <white><hover:show_text:'<date>'><time_ago> day(s) ago</hover>: </white><message>";
public static String mailFooter = "======================"; public static String mailFooter = "======================";
public static String mailNoUser = "<red>A player with this name hasn't logged in recently."; public static String mailNoUser = "<red>A player with this name hasn't logged in recently.";
public static String mailReceived = public static String mailReceived = "<yellow><click:run_command:/mail list unread>New mail from <sender>, click to view</click></yellow>";
"<yellow><click:run_command:/mail list unread>New mail from <sender>, click to view</click></yellow>"; public static String mailUnread = "<green><click:run_command:/mail list unread>You have <amount> unread mail, click to view it.</click></green>";
public static String mailUnread = public static String mailSent = "<green>Successfully send mail to <player_name></green>: <#2e8b57><message></#2e8b57>";
"<green><click:run_command:/mail list unread>You have <amount> unread mail, click to view it.</click></green>";
public static String mailSent =
"<green>Successfully send mail to <player_name></green>: <#2e8b57><message></#2e8b57>";
public static List<String> mailCommandAlias = new ArrayList<>(); public static List<String> mailCommandAlias = new ArrayList<>();
public static int mailDisplayDelay = 5;
private static void mailSettings() { private static void mailSettings() {
mailHeader = getString("settings.mail.header", mailHeader); mailHeader = getString("settings.mail.header", mailHeader);
mailBody = getString("settings.mail.message", mailBody); mailBody = getString("settings.mail.message", mailBody);
@ -495,15 +432,11 @@ public final class Config {
mailReceived = getString("settings.mail.mail-received", mailReceived); mailReceived = getString("settings.mail.mail-received", mailReceived);
mailUnread = getString("settings.mail.mail-unread", mailUnread); mailUnread = getString("settings.mail.mail-unread", mailUnread);
mailSent = getString("settings.mail.mail-sent", mailSent); mailSent = getString("settings.mail.mail-sent", mailSent);
mailDisplayDelay = getInt("settings.mail.delay", mailDisplayDelay);
} }
public static HashMap<String, Long> serverChannelId = new HashMap<>(); public static HashMap<String, Long> serverChannelId = new HashMap<>();
public static String REPORT_SENT = public static String REPORT_SENT = "<green>Your report was sent, staff will contact you asap to help resolve your issue!</green>";
"<green>Your report was sent, staff will contact you asap to help resolve your issue!</green>"; public static String REPORT_TOO_SHORT = "<red>Please ensure your report is descriptive. We require at least 3 words per report</red>";
public static String REPORT_TOO_SHORT =
"<red>Please ensure your report is descriptive. We require at least 3 words per report</red>";
private static void loadChannelIds() { private static void loadChannelIds() {
serverChannelId.clear(); serverChannelId.clear();
serverChannelId.put("general", getLong("discord-channel-id.general", (long) -1)); serverChannelId.put("general", getLong("discord-channel-id.general", (long) -1));
@ -511,9 +444,8 @@ public final class Config {
Map<Object, ? extends ConfigurationNode> objectMap = node.childrenMap(); Map<Object, ? extends ConfigurationNode> objectMap = node.childrenMap();
for (Object o : objectMap.keySet()) { for (Object o : objectMap.keySet()) {
String key = (String) o; String key = (String) o;
if (key.equalsIgnoreCase("general")) { if (key.equalsIgnoreCase("general"))
continue; continue;
}
ConfigurationNode configurationNode = objectMap.get(o); ConfigurationNode configurationNode = objectMap.get(o);
long channelId = configurationNode.getLong(); long channelId = configurationNode.getLong();
serverChannelId.put(key.toLowerCase(), channelId); serverChannelId.put(key.toLowerCase(), channelId);
@ -537,9 +469,7 @@ public final class Config {
} }
public static String HELP_REPORT = "<red>/report <message></red>"; public static String HELP_REPORT = "<red>/report <message></red>";
public static String FIRST_JOIN = public static String FIRST_JOIN = "<green>* Welcome <light_purple><player></light_purple> to Altitude! They've joined for the first time.</green>";
"<green>* Welcome <light_purple><player></light_purple> to Altitude! They've joined for the first time.</green>";
private static void loadMessages() { private static void loadMessages() {
HELP_REPORT = getString("settings.mail.mail-sent", HELP_REPORT); HELP_REPORT = getString("settings.mail.mail-sent", HELP_REPORT);
FIRST_JOIN = getString("settings.first-join.message", FIRST_JOIN); FIRST_JOIN = getString("settings.first-join.message", FIRST_JOIN);
@ -547,9 +477,7 @@ public final class Config {
public static String EMOTELIST_HEADER = "<bold>Available Chat Emotes</bold><newline>"; public static String EMOTELIST_HEADER = "<bold>Available Chat Emotes</bold><newline>";
public static String EMOTELIST_ITEM = "<insert:\"<regex>\"><gold><regex></gold> : <emote></insert><newline>"; public static String EMOTELIST_ITEM = "<insert:\"<regex>\"><gold><regex></gold> : <emote></insert><newline>";
public static String EMOTELIST_FOOTER = public static String EMOTELIST_FOOTER = "<green>----<< <gray>Prev</gray> <page> <gray>/</gray> <pages> <gray>Next</gray> >>----";
"<green>----<< <gray>Prev</gray> <page> <gray>/</gray> <pages> <gray>Next</gray> >>----";
private static void emoteListCommand() { private static void emoteListCommand() {
EMOTELIST_HEADER = getString("commands.emotelist.header", EMOTELIST_HEADER); EMOTELIST_HEADER = getString("commands.emotelist.header", EMOTELIST_HEADER);
EMOTELIST_ITEM = getString("commands.emotelist.item", EMOTELIST_ITEM); EMOTELIST_ITEM = getString("commands.emotelist.item", EMOTELIST_ITEM);
@ -560,42 +488,31 @@ public final class Config {
public static String NICK_CHANGED = "<yellow>Your nickname was changed to <nickname><yellow>."; public static String NICK_CHANGED = "<yellow>Your nickname was changed to <nickname><yellow>.";
public static String NICK_NOT_CHANGED = "<yellow>Your nickname request was denied."; public static String NICK_NOT_CHANGED = "<yellow>Your nickname request was denied.";
public static String NICK_RESET = "<yellow>Nickname changed back to normal."; public static String NICK_RESET = "<yellow>Nickname changed back to normal.";
public static String NICK_CHANGED_OTHERS = public static String NICK_CHANGED_OTHERS = "<gold><targetplayer><yellow>'s nickname was changed to <nickname><yellow>.";
"<gold><targetplayer><yellow>'s nickname was changed to <nickname><yellow>."; public static String NICK_TARGET_NICK_CHANGE = "<yellow>Your nickname was changed to <nickname> <yellow>by <sendernick><yellow>";
public static String NICK_TARGET_NICK_CHANGE =
"<yellow>Your nickname was changed to <nickname> <yellow>by <sendernick><yellow>";
public static String NICK_RESET_OTHERS = "<gold><player><gold>'s <yellow>nickname was reset back to normal."; public static String NICK_RESET_OTHERS = "<gold><player><gold>'s <yellow>nickname was reset back to normal.";
public static String NICK_INVALID_CHARACTERS = "<yellow>You can only use letters and numbers in nicknames."; public static String NICK_INVALID_CHARACTERS = "<yellow>You can only use letters and numbers in nicknames.";
public static String NICK_INVALID_LENGTH = "<yellow>Nicknames need to be between 3 to 16 characters long."; public static String NICK_INVALID_LENGTH = "<yellow>Nicknames need to be between 3 to 16 characters long.";
public static String NICK_PLAYER_NOT_ONLINE = "<red>That player is not online."; public static String NICK_PLAYER_NOT_ONLINE = "<red>That player is not online.";
public static String NICK_BLOCKED_COLOR_CODES = "<yellow>You have blocked color codes in that nickname."; public static String NICK_BLOCKED_COLOR_CODES = "<yellow>You have blocked color codes in that nickname.";
public static String NICK_USER_NOT_FOUND = public static String NICK_USER_NOT_FOUND = "<red>Failed to set nickname from player, try again from a server this player has been on before.";
"<red>Failed to set nickname from player, try again from a server this player has been on before."; public static String NICK_ACCEPTED = "<green>You accepted <targetplayer><green>'s nickname. They are now called <newnick><green>.";
public static String NICK_ACCEPTED = public static String NICK_DENIED = "<green>You denied <targetplayer><green>'s nickname. They are still called <oldnick><green>.";
"<green>You accepted <targetplayer><green>'s nickname. They are now called <newnick><green>.";
public static String NICK_DENIED =
"<green>You denied <targetplayer><green>'s nickname. They are still called <oldnick><green>.";
public static String NICK_ALREADY_HANDLED = "<red><targetplayer><red>'s nickname was already accepted or denied."; public static String NICK_ALREADY_HANDLED = "<red><targetplayer><red>'s nickname was already accepted or denied.";
public static String NICK_NO_LUCKPERMS = "<red>Due to an issue with LuckPerms /nick try won't work at the moment."; public static String NICK_NO_LUCKPERMS = "<red>Due to an issue with LuckPerms /nick try won't work at the moment.";
public static String NICK_TOO_SOON = "<red>Please wait <time><red> until requesting a new nickname"; public static String NICK_TOO_SOON = "<red>Please wait <time><red> until requesting a new nickname";
public static String NICK_REQUEST_PLACED = public static String NICK_REQUEST_PLACED = "<green>Replaced your previous request <oldrequestednick><green> with <newrequestednick><green>.";
"<green>Replaced your previous request <oldrequestednick><green> with <newrequestednick><green>.";
public static String NICK_REQUEST_NEW = "<green>New nickname request by <player><green>!"; public static String NICK_REQUEST_NEW = "<green>New nickname request by <player><green>!";
public static String NICK_TRYOUT = public static String NICK_TRYOUT = "<white><prefix><white> <nick><gray>: <white>Hi, this is what my new nickname could look like!";
"<white><prefix><white> <nick><gray>: <white><click:suggest_command:/nick request <nickrequest>>Hi, this is what my new nickname could look like! Click this message to request."; public static String NICK_REQUESTED = "<green>Your requested to be nicknamed <nick><green> has been received. Staff will accept or deny this request asap!";
public static String NICK_REQUESTED =
"<green>Your requested to be nicknamed <nick><green> has been received. Staff will accept or deny this request asap!";
public static String NICK_REVIEW_WAITING = "<green>There are <amount> nicknames waiting for review!"; public static String NICK_REVIEW_WAITING = "<green>There are <amount> nicknames waiting for review!";
public static String NICK_TAKEN = public static String NICK_TAKEN = "<red>Someone else already has this nickname, or has this name as their username.";
"<red>Someone else already has this nickname, or has this name as their username.";
public static String NICK_REQUESTS_ON_LOGIN = "<green>Current nick requests: <amount>"; public static String NICK_REQUESTS_ON_LOGIN = "<green>Current nick requests: <amount>";
public static long NICK_WAIT_TIME = 86400000; public static long NICK_WAIT_TIME = 86400000;
public static List<String> NICK_ITEM_LORE = new ArrayList<>(); public static List<String> NICK_ITEM_LORE = new ArrayList<>();
public static List<String> NICK_BLOCKED_COLOR_CODESLIST = new ArrayList<>(); public static List<String> NICK_BLOCKED_COLOR_CODESLIST = new ArrayList<>();
public static List<String> NICK_ALLOWED_COLOR_CODESLIST = new ArrayList<>(); public static List<String> NICK_ALLOWED_COLOR_CODESLIST = new ArrayList<>();
public static String NICK_CURRENT = public static String NICK_CURRENT = "<gold>Current nickname: <nickname><white>(<insert:\"<currentnickname>\"><currentnickname></insert>)";
"<gold>Current nickname: <nickname><white>(<insert:\"<currentnickname>\"><currentnickname></insert>)";
private static void nicknameSettings() { private static void nicknameSettings() {
NICK_CHANGED = getString("nicknames.messages.nick-changed", NICK_CHANGED); NICK_CHANGED = getString("nicknames.messages.nick-changed", NICK_CHANGED);
NICK_NOT_CHANGED = getString("nicknames.messages.nick-not-changed", NICK_NOT_CHANGED); NICK_NOT_CHANGED = getString("nicknames.messages.nick-not-changed", NICK_NOT_CHANGED);
@ -621,44 +538,14 @@ public final class Config {
NICK_TAKEN = getString("nicknames.messages.nick-taken", NICK_TAKEN); NICK_TAKEN = getString("nicknames.messages.nick-taken", NICK_TAKEN);
NICK_REQUESTS_ON_LOGIN = getString("nicknames.messages.nick-reauests-on-login", NICK_REQUESTS_ON_LOGIN); NICK_REQUESTS_ON_LOGIN = getString("nicknames.messages.nick-reauests-on-login", NICK_REQUESTS_ON_LOGIN);
NICK_WAIT_TIME = getLong("nicknames.wait-time", NICK_WAIT_TIME); NICK_WAIT_TIME = getLong("nicknames.wait-time", NICK_WAIT_TIME);
NICK_ITEM_LORE = getList("nicknames.item-lore", NICK_ITEM_LORE = getList("nicknames.item-lore", List.of("<aqua>New nick: <newnick>", "<aqua>Old nick: <oldnick>", "<aqua>Last changed: <lastchanged>", "<green>Left click to Accept <light_purple>| <red>Right click to Deny"));
List.of("<aqua>New nick: <newnick>", "<aqua>Old nick: <oldnick>", "<aqua>Last changed: <lastchanged>",
"<green>Left click to Accept <light_purple>| <red>Right click to Deny"
)
);
NICK_BLOCKED_COLOR_CODESLIST = getList("nicknames.blocked-color-codes", List.of("&k", "&l", "&n", "&m", "&o")); NICK_BLOCKED_COLOR_CODESLIST = getList("nicknames.blocked-color-codes", List.of("&k", "&l", "&n", "&m", "&o"));
NICK_ALLOWED_COLOR_CODESLIST = getList("nicknames.allowed-color-codes", NICK_ALLOWED_COLOR_CODESLIST = getList("nicknames.allowed-color-codes", List.of("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f", "&r"));
List.of("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f",
"&r"
)
);
NICK_CURRENT = getString("nicknames.messages.nick-current", NICK_CURRENT); NICK_CURRENT = getString("nicknames.messages.nick-current", NICK_CURRENT);
} }
public static int DEATH_MESSAGES_MAX_PER_PERIOD = 5; public static String APRIL_FOOLS_RESET = "esrever";
public static int DEATH_MESSAGES_LIMIT_PERIOD_MINUTES = 15; private static void aprilFools() {
APRIL_FOOLS_RESET = getString("april-fools.reset", APRIL_FOOLS_RESET);
private static void deathMessagesSettings() {
DEATH_MESSAGES_MAX_PER_PERIOD = getInt("death-messages.max-per-period", DEATH_MESSAGES_MAX_PER_PERIOD);
DEATH_MESSAGES_LIMIT_PERIOD_MINUTES =
getInt("death-messages.limit-period-minutes", DEATH_MESSAGES_LIMIT_PERIOD_MINUTES);
}
public static long CHAT_LOG_DELETE_OLDER_THAN_DAYS = 31;
public static long CHAT_LOG_SAVE_DELAY_MINUTES = 5;
private static void chatLogSettings() {
CHAT_LOG_DELETE_OLDER_THAN_DAYS = getLong("chat-log.delete-older-than-days", CHAT_LOG_DELETE_OLDER_THAN_DAYS);
CHAT_LOG_SAVE_DELAY_MINUTES = getLong("chat-log.save-delay-minutes", CHAT_LOG_SAVE_DELAY_MINUTES);
}
public static String CHAT_WEB_SERVER_BASE_URL = "http://10.0.0.121:8080";
public static String CHAT_WEB_REGISTER_TO_BASE_URL = "https://alttd.com";
public static String CHAT_WEB_TOKEN = "invalid-token";
private static void webServerSettings() {
CHAT_WEB_SERVER_BASE_URL = getString("web-server.base-url", CHAT_WEB_SERVER_BASE_URL);
CHAT_WEB_REGISTER_TO_BASE_URL = getString("web-server.register-to-base-url", CHAT_WEB_REGISTER_TO_BASE_URL);
CHAT_WEB_TOKEN = getString("web-server.token", CHAT_WEB_TOKEN);
} }
} }

View File

@ -1,114 +0,0 @@
package com.alttd.chat.database;
import com.alttd.chat.objects.chat_log.ChatLog;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.util.ALogger;
import org.jetbrains.annotations.NotNull;
import java.sql.*;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
public class ChatLogQueries {
protected static void createChatLogTable() {
String nicknamesTableQuery = """
CREATE TABLE IF NOT EXISTS chat_log (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
uuid CHAR(36) NOT NULL,
time_stamp TIMESTAMP(6) NOT NULL,
server VARCHAR(50) NOT NULL,
type VARCHAR(16) NOT NULL DEFAULT 'public',
channel VARCHAR(36) DEFAULT NULL,
receiver CHAR(36) DEFAULT NULL,
chat_message VARCHAR(300) NOT NULL,
mini_message JSON,
blocked BOOLEAN NOT NULL DEFAULT FALSE,
INDEX idx_time_stamp (time_stamp)
)
""";
try (PreparedStatement preparedStatement = DatabaseConnection.getConnection().prepareStatement(
nicknamesTableQuery)) {
preparedStatement.executeUpdate();
} catch (Throwable throwable) {
ALogger.error("Failed to create chat log table", throwable);
}
}
public static @NotNull CompletableFuture<Boolean> storeMessages(HashMap<UUID, List<ChatLog>> chatMessages) {
String insertQuery = "INSERT INTO chat_log (uuid, time_stamp, server, type, channel, receiver, chat_message, mini_message, blocked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = DatabaseConnection.createTransactionConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
for (List<ChatLog> chatLogList : chatMessages.values()) {
for (ChatLog chatLog : chatLogList) {
chatLog.prepareStatement(preparedStatement);
preparedStatement.addBatch();
}
}
int[] updatedRowsCount = preparedStatement.executeBatch();
boolean isSuccess = Arrays.stream(updatedRowsCount).allMatch(i -> i >= 0);
if (isSuccess) {
connection.commit();
return true;
} else {
connection.rollback();
ALogger.warn("Failed to store messages");
return false;
}
} catch (SQLException sqlException) {
ALogger.error("Failed to store chat messages", sqlException);
throw new CompletionException("Failed to store chat messages", sqlException);
}
});
}
public static @NotNull CompletableFuture<List<ChatLog>> retrieveMessages(ChatLogHandler chatLogHandler, UUID uuid, Duration duration, String server) {
String query = "SELECT * FROM chat_log WHERE uuid = ? AND time_stamp > ? AND server = ? AND type = 'public'";
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = DatabaseConnection.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, uuid.toString());
preparedStatement.setTimestamp(2, Timestamp.from(Instant.now().minus(duration)));
preparedStatement.setString(3, server);
ResultSet resultSet = preparedStatement.executeQuery();
List<ChatLog> chatLogs = new ArrayList<>();
while (resultSet.next()) {
chatLogHandler.loadFromResultSet(resultSet).ifPresent(chatLogs::add);
}
return chatLogs;
} catch (SQLException sqlException) {
ALogger.error(String.format("Failed to retrieve messages for user %s", uuid), sqlException);
throw new CompletionException(String.format("Failed to retrieve messages for user %s", uuid),
sqlException
);
}
});
}
public static CompletableFuture<Boolean> deleteOldMessages(Duration duration) {
String query = "DELETE FROM chat_log WHERE time_stamp < ?";
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = DatabaseConnection.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setTimestamp(1, Timestamp.from(Instant.now().minus(duration)));
return preparedStatement.execute();
} catch (SQLException sqlException) {
ALogger.error(String.format("Failed to delete messages older than %s days", duration.toDays()),
sqlException
);
throw new CompletionException(String.format("Failed to delete messages older than %s days",
duration.toDays()
), sqlException
);
}
});
}
}

View File

@ -1,6 +1,8 @@
package com.alttd.chat.database; package com.alttd.chat.database;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.util.ALogger;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -26,7 +28,6 @@ public class DatabaseConnection {
/** /**
* Opens the connection if it's not already open. * Opens the connection if it's not already open.
*
* @throws SQLException If it can't create the connection. * @throws SQLException If it can't create the connection.
*/ */
public void openConnection() throws SQLException { public void openConnection() throws SQLException {
@ -45,16 +46,14 @@ public class DatabaseConnection {
} }
connection = DriverManager.getConnection( connection = DriverManager.getConnection(
"jdbc:mysql://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE + "?autoReconnect=true" + "jdbc:mysql://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE + "?autoReconnect=true"+
"&useSSL=false&preserveInstants=true", "&useSSL=false",
Config.USERNAME, Config.PASSWORD Config.USERNAME, Config.PASSWORD);
);
} }
} }
/** /**
* Returns the connection for the database * Returns the connection for the database
*
* @return Returns the connection. * @return Returns the connection.
*/ */
public static Connection getConnection() { public static Connection getConnection() {
@ -67,23 +66,6 @@ public class DatabaseConnection {
return connection; return connection;
} }
/**
* Creates a transactional database connection.
*
* @return A {@code Connection} object representing the transactional database connection.
*
* @throws SQLException If there is an error creating the database connection.
*/
public static Connection createTransactionConnection() throws SQLException {
connection = DriverManager.getConnection(
"jdbc:mysql://" + Config.IP + ":" + Config.PORT + "/" + Config.DATABASE + "?autoReconnect=true" +
"&useSSL=false&preserveInstants=true",
Config.USERNAME, Config.PASSWORD
);
connection.setAutoCommit(false);
return connection;
}
/** /**
* Sets the connection for this instance * Sets the connection for this instance
*/ */
@ -92,4 +74,4 @@ public class DatabaseConnection {
return connection != null; return connection != null;
} }
} }

View File

@ -21,7 +21,6 @@ public class Queries {
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`))"); 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`))");
createNicknamesTable(); createNicknamesTable();
createRequestedNicknamesTable(); createRequestedNicknamesTable();
ChatLogQueries.createChatLogTable();
try { try {
Connection connection = DatabaseConnection.getConnection(); Connection connection = DatabaseConnection.getConnection();

View File

@ -1,10 +0,0 @@
package com.alttd.chat.objects;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public interface BatchInsertable {
void prepareStatement(PreparedStatement preparedStatement) throws SQLException;
}

View File

@ -4,9 +4,6 @@ import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.channels.Channel; import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -16,20 +13,20 @@ import java.util.stream.Collectors;
public class ChatUser { public class ChatUser {
private final UUID uuid; // player uuid private final UUID uuid; // player uuid
private int partyId; // the party they are in private int partyId; // the party they are in
private final Channel toggledChannel; private Channel toggledChannel;
private String name; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview private String name; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview
private ComponentLike displayName; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview private Component displayName; // the nickname, doesn't need to be saved with the chatuser object, could be saved but we can get it from the nicknamesview
// private Component prefix; // doesn't need saving, we get this from luckperms // private Component prefix; // doesn't need saving, we get this from luckperms
// private Component staffPrefix; // doesn't need saving, we get this from luckperms // private Component staffPrefix; // doesn't need saving, we get this from luckperms
// private Component prefixAll; // doesn't need saving, we get this from luckperms // private Component prefixAll; // doesn't need saving, we get this from luckperms
//private boolean toggleGc; // should be saved, this toggles if the player can see and use global chat //private boolean toggleGc; // should be saved, this toggles if the player can see and use global chat
private String replyTarget; // reply target for use in /msg i don't mind setting this to null on login, feedback? private String replyTarget; // reply target for use in /msg i don't mind setting this to null on login, feedback?
private String replyContinueTarget; // reply target for use in /c private String replyContinueTarget; // reply target for use in /c
private long gcCooldown; // the time when they last used gc, is used for the cooldown, i wouldn't save this, but setting this to the login time means they can't use gc for 30 seconds after logging in private long gcCooldown; // the time when they last used gc, is used for the cooldown, i wouldn't save this, but setting this to the login time means they can't use gc for 30 seconds after logging in
private boolean spy; private boolean spy;
private final List<Mail> mails; // mails aren't finalized yet, so for now a table sender, reciever, sendtime, readtime(if emtpy mail isn't read yet?, could also do a byte to control this), the actual message private List<Mail> mails; // mails aren't finalized yet, so for now a table sender, reciever, sendtime, readtime(if emtpy mail isn't read yet?, could also do a byte to control this), the actual message
private final List<UUID> ignoredPlayers; // a list of UUID, a new table non unique, where one is is the player select * from ignores where ignoredby = thisplayer? where the result is the uuid of the player ignored by this player? private List<UUID> ignoredPlayers; // a list of UUID, a new table non unique, where one is is the player select * from ignores where ignoredby = thisplayer? where the result is the uuid of the player ignored by this player?
private final List<UUID> ignoredBy; // a list of UUID, same table as above but select * from ignores where ignored = thisplayer? result should be the other user that ignored this player? private List<UUID> ignoredBy; // a list of UUID, same table as above but select * from ignores where ignored = thisplayer? result should be the other user that ignored this player?
private boolean isMuted; private boolean isMuted;
public ChatUser(UUID uuid, int partyId, Channel toggledChannel) { public ChatUser(UUID uuid, int partyId, Channel toggledChannel) {
@ -43,10 +40,10 @@ public class ChatUser {
} }
setDisplayName(name); setDisplayName(name);
// prefix = Utility.getPrefix(uuid, true); // TODO we need to update this, so cache and update when needed or always request it? // prefix = Utility.getPrefix(uuid, true); // TODO we need to update this, so cache and update when needed or always request it?
// staffPrefix = Utility.getStaffPrefix(uuid); // staffPrefix = Utility.getStaffPrefix(uuid);
// //
// prefixAll = Utility.getPrefix(uuid, false); // prefixAll = Utility.getPrefix(uuid, false);
replyTarget = ""; replyTarget = "";
replyContinueTarget = ""; replyContinueTarget = "";
@ -74,7 +71,12 @@ public class ChatUser {
return toggledChannel; return toggledChannel;
} }
public ComponentLike getDisplayName() { public void setToggledChannel(Channel channel) {
toggledChannel = channel;
Queries.setToggledChannel(toggledChannel, uuid); //TODO: Async pls - no CompleteableFuture<>!
}
public Component getDisplayName() {
return displayName; return displayName;
} }
@ -82,25 +84,19 @@ public class ChatUser {
this.displayName = Utility.applyColor(displayName); this.displayName = Utility.applyColor(displayName);
} }
public ComponentLike getPrefix() { public Component getPrefix() {
//return prefix; //return prefix;
return Utility.getPrefix(uuid, true); // No longer cache this data return Utility.getPrefix(uuid, true); // No longer cache this data
} }
public ComponentLike getStaffPrefix() { public Component getStaffPrefix() {
//return staffPrefix; //return staffPrefix;
return Utility.getStaffPrefix(uuid); return Utility.getStaffPrefix(uuid);
} }
public ComponentLike getPrefixAll(boolean isWebMessage) { public Component getPrefixAll() {
//return prefixAll; //return prefixAll;
ComponentLike prefix = Utility.getPrefix(uuid, false); return Utility.getPrefix(uuid, false);
if (isWebMessage) {
//TODO [Stijn] [2026-08-08]: Check icon
TextComponent webIcon = Component.text("\uD83D\uDEDC").color(NamedTextColor.DARK_AQUA);
prefix = webIcon.append(prefix);
}
return prefix;
} }
public String getReplyTarget() { public String getReplyTarget() {
@ -149,6 +145,10 @@ public class ChatUser {
return ignoredBy; return ignoredBy;
} }
public void addIgnoredBy(UUID uuid) {
ignoredBy.add(uuid);
}
public long getGcCooldown() { public long getGcCooldown() {
return gcCooldown; return gcCooldown;
} }

View File

@ -4,9 +4,9 @@ import com.alttd.chat.config.Config;
import com.alttd.chat.managers.RegexManager; import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -16,7 +16,7 @@ public class EmoteList {
public static final Map<UUID, EmoteList> emoteLists = new HashMap<>(); public static final Map<UUID, EmoteList> emoteLists = new HashMap<>();
public static final int pageSize = 7; public static final int pageSize = 7;
// public static int pages = RegexManager.getEmoteFilters().size() / pageSize;// TODO reload this when config is reloaded. // public static int pages = RegexManager.getEmoteFilters().size() / pageSize;// TODO reload this when config is reloaded.
public static EmoteList getEmoteList(UUID uuid) { public static EmoteList getEmoteList(UUID uuid) {
synchronized (emoteLists) { synchronized (emoteLists) {
@ -25,23 +25,22 @@ public class EmoteList {
} }
private int page; private int page;
public Component showEmotePage() {
public ComponentLike showEmotePage() {
int startIndex = page * pageSize; int startIndex = page * pageSize;
int pages = RegexManager.getEmoteFilters().size() / pageSize; int pages = RegexManager.getEmoteFilters().size() / pageSize;
int endIndex = Math.min(startIndex + pageSize, RegexManager.getEmoteFilters().size()); int endIndex = Math.min(startIndex + pageSize, RegexManager.getEmoteFilters().size());
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.unparsed("page", String.valueOf(page)), Placeholder.unparsed("page", String.valueOf(page)),
Placeholder.unparsed("pages", String.valueOf(pages)) Placeholder.unparsed("pages", String.valueOf(pages))
); );
Component list = Utility.parseMiniMessage(Config.EMOTELIST_HEADER, placeholders).asComponent(); Component list = Utility.parseMiniMessage(Config.EMOTELIST_HEADER, placeholders);
for (int i = startIndex; i < endIndex; i++) { for (int i = startIndex; i < endIndex; i++) {
ChatFilter emote = RegexManager.getEmoteFilters().get(i); ChatFilter emote = RegexManager.getEmoteFilters().get(i);
TagResolver emotes = TagResolver.resolver( TagResolver emotes = TagResolver.resolver(
Placeholder.parsed("regex", emote.getRegex()), Placeholder.parsed("regex", emote.getRegex()),
Placeholder.parsed("emote", emote.getReplacement()) Placeholder.parsed("emote", emote.getReplacement())
); );
list = list.append(Utility.parseMiniMessage(Config.EMOTELIST_ITEM, emotes)); list = list.append(Utility.parseMiniMessage(Config.EMOTELIST_ITEM, emotes));
} }
list = list.append(Utility.parseMiniMessage(Config.EMOTELIST_FOOTER, placeholders)); list = list.append(Utility.parseMiniMessage(Config.EMOTELIST_FOOTER, placeholders));
@ -61,4 +60,5 @@ public class EmoteList {
this.page = Math.max(page - 1, 0); this.page = Math.max(page - 1, 0);
} }
} }

View File

@ -1,9 +1,15 @@
package com.alttd.chat.objects; package com.alttd.chat.objects;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class ModifiableString { public class ModifiableString {
private Component text; private Component text;
@ -26,4 +32,37 @@ public class ModifiableString {
public Component component() { public Component component() {
return text; return text;
} }
public void reverse() {
text = reverseComponent(text);
}
public Component reverseComponent(Component component) {
if (!(component instanceof TextComponent textComponent)) {
return Component.text("")
.append(Component.join(JoinConfiguration.noSeparators(), reverseChildren(component.children())));
}
String content = textComponent.content();
String reversedContent = new StringBuilder(content).reverse().toString();
List<Component> reversedChildren = reverseChildren(component.children());
return Component.text("")
.append(Component.join(JoinConfiguration.noSeparators(), reversedChildren)
.append(Component.text(reversedContent, component.style())));
}
public List<Component> reverseChildren(List<Component> children) {
return children.stream()
.map(this::reverseComponent)
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> {
Collections.reverse(list);
return list;
}));
}
public void removeStringAtStart(String s) {
text = text.replaceText(TextReplacementConfig.builder().match("^" + s).replacement("").build());
}
} }

View File

@ -1,21 +1,21 @@
package com.alttd.chat.objects; package com.alttd.chat.objects;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import java.util.UUID; import java.util.UUID;
public class PartyUser { public class PartyUser {
protected UUID uuid; protected UUID uuid;
protected ComponentLike displayName; protected Component displayName;
protected String playerName; protected String playerName;
public PartyUser(UUID uuid, String displayName, String playerName) { public PartyUser(UUID uuid, String displayName, String playerName) {
this(uuid, Utility.applyColor(displayName), playerName); this(uuid, Utility.applyColor(displayName), playerName);
} }
public PartyUser(UUID uuid, ComponentLike displayName, String playerName) { public PartyUser(UUID uuid, Component displayName, String playerName) {
this.uuid = uuid; this.uuid = uuid;
this.displayName = displayName; this.displayName = displayName;
this.playerName = playerName; this.playerName = playerName;
@ -25,7 +25,7 @@ public class PartyUser {
return uuid; return uuid;
} }
public ComponentLike getDisplayName() { public Component getDisplayName() {
return displayName; return displayName;
} }

View File

@ -2,8 +2,6 @@ package com.alttd.chat.objects;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.luckperms.api.model.user.User;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,9 +18,8 @@ public abstract class Toggleable {
public static Toggleable getToggleable(UUID uuid) { public static Toggleable getToggleable(UUID uuid) {
for (Toggleable toggleableClass : togglableClasses) { for (Toggleable toggleableClass : togglableClasses) {
if (toggleableClass.isToggled(uuid)) { if (toggleableClass.isToggled(uuid))
return toggleableClass; return toggleableClass;
}
} }
return null; return null;
} }
@ -62,5 +59,4 @@ public abstract class Toggleable {
public abstract void sendMessage(Player player, String message); public abstract void sendMessage(Player player, String message);
public abstract void sendMessage(User user, OfflinePlayer offlinePlayer, String message);
} }

View File

@ -1,37 +1,44 @@
package com.alttd.chat.objects.channels; package com.alttd.chat.objects.channels;
import lombok.Getter;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@Getter
public class Channel { public class Channel {
public static HashMap<String, Channel> channels = new HashMap<>(); public static HashMap<String, Channel> channels = new HashMap<>();
private final String permission; protected String permission;
private final String channelName; protected String channelName;
private final String format; protected String format;
private final boolean proxy; protected boolean proxy;
private final boolean local;
private final boolean web;
private final String webPath;
public Channel(String channelName, String format, boolean proxy, boolean local, boolean web) { public Channel(String channelName, String format, boolean proxy) {
this.permission = "chat.channel." + channelName.toLowerCase(); this.permission = "chat.channel." + channelName.toLowerCase();
this.channelName = channelName; this.channelName = channelName;
this.format = format; this.format = format;
this.proxy = proxy; this.proxy = proxy;
this.local = local;
channels.put(channelName.toLowerCase(), this); channels.put(channelName.toLowerCase(), this);
this.web = web;
this.webPath = web ? "web_" + channelName + "_chat" : null;
} }
public static Collection<Channel> getChannels() { public static Collection<Channel> getChannels() {
return channels.values(); return channels.values();
} }
public String getPermission() {
return permission;
}
public String getChannelName() {
return channelName;
}
public String getFormat() {
return format;
}
public boolean isProxy() {
return proxy;
}
public static Channel getChatChannel(String channelName) { public static Channel getChatChannel(String channelName) {
return channels.get(channelName.toLowerCase()); return channels.get(channelName.toLowerCase());
} }

View File

@ -1,19 +1,20 @@
package com.alttd.chat.objects.channels; package com.alttd.chat.objects.channels;
import lombok.Getter; import java.util.*;
import java.util.List;
@Getter
public class CustomChannel extends Channel { public class CustomChannel extends Channel {
private final List<String> servers; private final List<String> servers;
private final List<String> aliases;
public CustomChannel(String channelName, String format, List<String> servers, List<String> aliases, boolean proxy, public CustomChannel(String channelName, String format, List<String> servers, boolean proxy) {
boolean local, boolean web) { super(channelName, format, proxy);
super(channelName, format, proxy, local, web); this.permission = "chat.channel." + channelName.toLowerCase();
this.channelName = channelName;
this.format = format;
this.servers = servers; this.servers = servers;
this.aliases = aliases; this.proxy = proxy;
channels.put(channelName.toLowerCase(), this);
}
public List<String> getServers() {
return servers;
} }
} }

View File

@ -1,7 +1,7 @@
package com.alttd.chat.objects.channels; package com.alttd.chat.objects.channels;
public abstract class DefaultChannel extends Channel { public abstract class DefaultChannel extends Channel{
public DefaultChannel(String channelName, String format, boolean proxy) { public DefaultChannel(String channelName, String format, boolean proxy) {
super(channelName, format, proxy, false, false); super(channelName, format, proxy);
} }
} }

View File

@ -1,46 +0,0 @@
package com.alttd.chat.objects.chat_log;
import com.alttd.chat.objects.BatchInsertable;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogTypeMapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jetbrains.annotations.NotNull;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.UUID;
@AllArgsConstructor
@Getter
public class ChatLog implements BatchInsertable {
private final UUID uuid;
private final Instant timestamp;
private final String server;
private final ChatLogType type;
private final String channel;
private final String receiver;
private final String message;
private final Component miniMessage;
private final boolean blocked;
@Override
public void prepareStatement(@NotNull PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, uuid.toString());
preparedStatement.setTimestamp(2, Timestamp.from(timestamp));
preparedStatement.setString(3, server);
preparedStatement.setString(4, ChatLogTypeMapper.toDb(type));
preparedStatement.setString(5, channel);
preparedStatement.setString(6, receiver);
preparedStatement.setString(7, message);
preparedStatement.setString(8,
miniMessage == null ? null : GsonComponentSerializer.gson().serialize(miniMessage)
);
preparedStatement.setInt(9, blocked ? 1 : 0);
}
}

View File

@ -1,177 +0,0 @@
package com.alttd.chat.objects.chat_log;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.ChatLogQueries;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogTypeMapper;
import com.alttd.chat.util.ALogger;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jetbrains.annotations.NotNull;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.*;
@Slf4j
public class ChatLogHandler {
private final WebHandler webHandler;
private static ChatLogHandler instance = null;
private ScheduledExecutorService executorService = null;
public static ChatLogHandler getInstance(WebHandler webHandler, boolean enableLogging) {
if (instance == null) {
instance = new ChatLogHandler(webHandler, enableLogging);
}
return instance;
}
private boolean isSaving;
private final Queue<ChatLog> chatLogQueue = new ConcurrentLinkedQueue<>();
private final HashMap<UUID, List<ChatLog>> chatLogs = new HashMap<>();
public ChatLogHandler(WebHandler webHandler, boolean enableLogging) {
this.webHandler = webHandler;
if (!enableLogging) {
ALogger.info("Logging is not enabled on this server.");
return;
}
Duration deleteThreshold = Duration.ofDays(Config.CHAT_LOG_DELETE_OLDER_THAN_DAYS);
ChatLogQueries.deleteOldMessages(deleteThreshold).thenAccept(success -> {
if (success) {
ALogger.info(String.format("Deleted all messages older than %s days from chat log database.",
deleteThreshold.toDays()
));
} else {
ALogger.warn(String.format("Failed to delete all messages older than %s days from chat log database.",
deleteThreshold.toDays()
));
}
});
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(() -> {
saveToDatabase(false);
ALogger.info(String.format("Running scheduler to save messages with a %d delay",
Config.CHAT_LOG_SAVE_DELAY_MINUTES
));
},
Config.CHAT_LOG_SAVE_DELAY_MINUTES, Config.CHAT_LOG_SAVE_DELAY_MINUTES, TimeUnit.MINUTES
);
ALogger.info("Logging has started!");
}
/**
* Shuts down the executor service and saves the chat logs to the database. Will throw an error if called on a
* ChatLogHandler that was started without logging
*/
public void shutDown() {
executorService.shutdown();
saveToDatabase(true);
}
private synchronized void savingToDatabase(boolean saving) {
isSaving = saving;
}
private synchronized boolean isBlocked() {
return isSaving;
}
private synchronized void addLog(ChatLog chatLog) {
if (isBlocked()) {
chatLogQueue.add(chatLog);
} else {
chatLogs.computeIfAbsent(chatLog.getUuid(), k -> new ArrayList<>()).add(chatLog);
}
}
private void saveToDatabase(boolean onMainThread) {
savingToDatabase(true);
ALogger.info(String.format("Saving %d messages to database", chatLogs.size()));
CompletableFuture<Boolean> booleanCompletableFuture = ChatLogQueries.storeMessages(chatLogs);
if (onMainThread) {
booleanCompletableFuture.join();
ALogger.info("Finished saving messages on main thread");
return;
}
booleanCompletableFuture.whenComplete((result, throwable) -> {
if (throwable == null && result) {
chatLogs.clear();
} else {
ALogger.error("Failed to save chat messages.");
}
savingToDatabase(false);
if (!chatLogQueue.isEmpty()) {
ALogger.info("Adding back messages from queue to chatLogs map");
}
while (!chatLogQueue.isEmpty()) {
addLog(chatLogQueue.remove());
}
ALogger.info("Finished saving messages");
});
}
public Optional<ChatLog> loadFromResultSet(@NotNull ResultSet resultSet) throws SQLException {
UUID chatLogUUID = UUID.fromString(resultSet.getString("uuid"));
Instant chatTimestamp = resultSet.getTimestamp("time_stamp").toInstant();
String server = resultSet.getString("server");
String stringType = resultSet.getString("type");
ChatLogType type;
try {
type = ChatLogTypeMapper.fromDb(stringType);
} catch (Exception e) {
log.error("Failed to load chat log from result set: {}", e.getMessage());
return Optional.empty();
}
String channel = resultSet.getString("channel");
String receiver = resultSet.getString("receiver");
String chatMessage = resultSet.getString("chat_message");
String stringMiniMessage = resultSet.getString("mini_message");
Component miniMessage = stringMiniMessage == null ? null : GsonComponentSerializer.gson().deserialize(
stringMiniMessage);
boolean chatMessageBlocked = resultSet.getInt("blocked") == 1;
return Optional.of(new ChatLog(chatLogUUID,
chatTimestamp,
server,
type,
channel,
receiver,
chatMessage,
miniMessage,
chatMessageBlocked
));
}
public void addChatLog(UUID uuid, String server, String message, ChatLogType chatLogType, String channel, String receiver, Component miniMessage, boolean blocked) {
ChatLog chatLog = new ChatLog(uuid,
Instant.now(),
server,
chatLogType,
channel,
receiver,
message,
miniMessage,
blocked
);
addLog(chatLog);
webHandler.forwardChatLogToWeb(chatLog);
}
public CompletableFuture<List<ChatLog>> retrieveChatLogs(UUID uuid, Duration duration, String server) {
List<ChatLog> chatLogList = chatLogs.getOrDefault(uuid, new ArrayList<>());
return ChatLogQueries.retrieveMessages(this, uuid, duration, server)
.thenCompose(chatLogs -> CompletableFuture.supplyAsync(() -> {
chatLogList.addAll(chatLogs);
return chatLogList;
}))
.exceptionally(ex -> {
throw new CompletionException(ex);
});
}
}

View File

@ -1,31 +0,0 @@
package com.alttd.chat.objects.chat_log;
import com.alttd.altitudeweb.invoker.ApiCallback;
import com.alttd.altitudeweb.invoker.ApiException;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
@Slf4j
public class SilentApiHandler implements ApiCallback<Void> {
@Override
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
log.error("Failed to update server states", e);
}
@Override
public void onSuccess(Void result, int statusCode, Map<String, List<String>> responseHeaders) {
}
@Override
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
}
@Override
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
}
}

View File

@ -1,112 +0,0 @@
package com.alttd.chat.objects.chat_log;
import com.alttd.altitudeweb.api.ChatApi;
import com.alttd.altitudeweb.invoker.ApiClient;
import com.alttd.altitudeweb.invoker.ApiException;
import com.alttd.altitudeweb.model.ChatMessageDto;
import com.alttd.chat.config.Config;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogMapper;
import com.alttd.chat.objects.chat_log.mapper.server_state.ServerMapper;
import com.alttd.chat.objects.chat_log.mapper.server_state.WebPlayer;
import com.alttd.chat.util.ALogger;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class WebHandler {
private static final int MAX_QUEUE_SIZE = 100;
private final Queue<ChatLog> queue = new ConcurrentLinkedQueue<>();
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final ChatApi chatApi;
public WebHandler() {
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(Config.CHAT_WEB_SERVER_BASE_URL);
chatApi = new ChatApi(apiClient);
}
private volatile boolean sending;
public void forwardChatLogToWeb(ChatLog chatLog) {
if (queue.size() >= MAX_QUEUE_SIZE) {
queue.clear();
log.error("Chat log queue overflow, is the web backend still running?");
}
queue.add(chatLog);
triggerSend();
}
public void updateServerState(String serverName, List<WebPlayer> playerList) {
try {
chatApi.updateServerStatesAsync(ServerMapper.toDto(serverName, playerList), new SilentApiHandler());
} catch (ApiException e) {
log.error("Failed to update server state", e);
}
}
private void triggerSend() {
if (sending) {
return;
}
synchronized (this) {
if (sending || queue.isEmpty()) {
return;
}
sending = true;
}
executor.execute(this::sendBatch);
}
private void sendBatch() {
try {
ArrayList<ChatLog> chatLogList = new ArrayList<>();
ChatLog log;
while ((log = queue.poll()) != null) {
if (log.getMiniMessage() == null) {
ALogger.warn("No mini message for message, skipping");
continue;
}
chatLogList.add(log);
}
List<ChatMessageDto> batch = chatLogList.stream()
.map(ChatLogMapper::toDto)
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
if (!chatLogList.isEmpty()) {
try {
chatApi.sendChatMessages(batch);
} catch (ApiException e) {
ALogger.error("Failed to send chat messages to web backend, " +
"adding messages back to queue for another try", e
);
queue.addAll(chatLogList);
}
}
} finally {
synchronized (this) {
sending = false;
}
triggerSend();
}
}
}

View File

@ -1,51 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.chat_log;
import com.alttd.altitudeweb.model.ChatMessageDto;
import com.alttd.chat.objects.chat_log.ChatLog;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.time.ZoneOffset;
import java.util.Optional;
import java.util.Set;
@Slf4j
@UtilityClass
public class ChatLogMapper {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
public Optional<ChatMessageDto> toDto(ChatLog chatLog) {
ChatMessageDto chatMessageDto = new ChatMessageDto();
if (chatLog.getMiniMessage() == null) {
throw new IllegalArgumentException("MiniMessage cannot be null");
}
chatMessageDto.setUuid(chatLog.getUuid());
chatMessageDto.setTimestamp(chatLog.getTimestamp().atOffset(ZoneOffset.UTC));
chatMessageDto.setServer(chatLog.getServer());
chatMessageDto.setType(ChatLogTypeMapper.toDto(chatLog.getType()));
chatMessageDto.setChannel(chatLog.getChannel());
chatMessageDto.setReceiver(chatLog.getReceiver());
chatMessageDto.setMessage(GsonComponentSerializer.gson().serialize(chatLog.getMiniMessage()));
chatMessageDto.setBlocked(chatLog.isBlocked());
if (checkIsInvalid(chatMessageDto)) {
return Optional.empty();
}
return Optional.of(chatMessageDto);
}
private boolean checkIsInvalid(ChatMessageDto chatMessageDto) {
Set<ConstraintViolation<ChatMessageDto>> violations = validator.validate(chatMessageDto);
if (violations.isEmpty()) {
return false;
}
log.error("Validation failed for chat message: {}, {}", chatMessageDto, violations);
return true;
}
}

View File

@ -1,10 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.chat_log;
public enum ChatLogType {
PUBLIC,
GLOBAL,
PARTY,
GAC,
MSG,
CUSTOM;
}

View File

@ -1,43 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.chat_log;
import com.alttd.altitudeweb.model.ChatMessageDto;
import lombok.experimental.UtilityClass;
@UtilityClass
public class ChatLogTypeMapper {
public static String toDb(ChatLogType chatLogType) {
return switch (chatLogType) {
case PUBLIC -> "public";
case GLOBAL -> "global";
case PARTY -> "party";
case GAC -> "gac";
case MSG -> "msg";
case CUSTOM -> "custom";
};
}
public ChatMessageDto.TypeEnum toDto(ChatLogType chatLogType) {
return switch (chatLogType) {
case PUBLIC -> ChatMessageDto.TypeEnum.PUBLIC;
case GLOBAL -> ChatMessageDto.TypeEnum.GLOBAL;
case PARTY -> ChatMessageDto.TypeEnum.PARTY;
case GAC -> ChatMessageDto.TypeEnum.GAC;
case MSG -> ChatMessageDto.TypeEnum.MSG;
case CUSTOM -> ChatMessageDto.TypeEnum.CUSTOM;
};
}
public ChatLogType fromDb(String dbType) {
return switch (dbType) {
case "public" -> ChatLogType.PUBLIC;
case "global" -> ChatLogType.GLOBAL;
case "party" -> ChatLogType.PARTY;
case "gac" -> ChatLogType.GAC;
case "msg" -> ChatLogType.MSG;
case "custom" -> ChatLogType.CUSTOM;
default -> throw new IllegalArgumentException("Invalid chat log type: " + dbType);
};
}
}

View File

@ -1,22 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.server_state;
import com.alttd.altitudeweb.model.ServerDto;
import com.alttd.altitudeweb.model.ServerStateDto;
import lombok.experimental.UtilityClass;
import java.util.List;
@UtilityClass
public class ServerMapper {
public static ServerStateDto toDto(String serverName, List<WebPlayer> playerList) {
ServerStateDto serverStateDto = new ServerStateDto();
ServerDto serverDto = new ServerDto();
serverDto.setName(serverName);
serverDto.setPlayers(playerList.stream().map(WebPlayerMapper::toDto).toList());
serverStateDto.setServers(List.of(serverDto));
return serverStateDto;
}
}

View File

@ -1,17 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.server_state;
import com.alttd.altitudeweb.model.UserDto;
import lombok.experimental.UtilityClass;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.entity.Player;
@UtilityClass
public class UserMapper {
public static UserDto toDto(Player player) {
UserDto userDto = new UserDto();
userDto.setName(player.getName());
userDto.setUuid(player.getUniqueId());
userDto.setStyledName(GsonComponentSerializer.gson().serialize(player.displayName()));
return userDto;
}
}

View File

@ -1,16 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.server_state;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
public class WebPlayer {
private UUID uuid;
private String name;
private String styledName;
}

View File

@ -1,17 +0,0 @@
package com.alttd.chat.objects.chat_log.mapper.server_state;
import com.alttd.altitudeweb.model.UserDto;
import lombok.experimental.UtilityClass;
@UtilityClass
public class WebPlayerMapper {
public static UserDto toDto(WebPlayer webPlayer) {
UserDto userDto = new UserDto();
userDto.setName(webPlayer.getName());
userDto.setUuid(webPlayer.getUuid());
userDto.setStyledName(webPlayer.getStyledName());
return userDto;
}
}

View File

@ -16,10 +16,6 @@ public class ALogger {
logger.warn(message); logger.warn(message);
} }
public static void warn(String message, Throwable throwable) {
logger.warn(message, throwable);
}
public static void info(String message) { public static void info(String message) {
logger.info(message); logger.info(message);
} }
@ -27,8 +23,4 @@ public class ALogger {
public static void error(String message) { public static void error(String message) {
logger.error(message); logger.error(message);
} }
public static void error(String message, Throwable throwable) {
logger.error(message, throwable);
}
} }

View File

@ -3,33 +3,32 @@ package com.alttd.chat.util;
import com.alttd.chat.ChatAPI; import com.alttd.chat.ChatAPI;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import net.luckperms.api.context.ImmutableContextSet;
import net.luckperms.api.model.group.Group; import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User; import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node; import net.luckperms.api.node.Node;
import net.luckperms.api.query.QueryOptions; import org.bukkit.permissions.Permission;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Utility { public class Utility {
private static final List<String> EMPTY_LIST = new ArrayList<>(); private static final List<String> EMPTY_LIST = new ArrayList<>();
static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(https?://)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)"); static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
private static MiniMessage miniMessage = null; private static MiniMessage miniMessage = null;
public static String stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}"; public static String stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}";
public static HashMap<String, String> colors; public static HashMap<String, String> colors;
private static LegacyComponentSerializer legacySerializer;
static { // this might be in minimessage already? static { // this might be in minimessage already?
colors = new HashMap<>(); colors = new HashMap<>();
colors.put("&0", "<black>"); colors.put("&0", "<black>");
@ -49,44 +48,41 @@ public class Utility {
colors.put("&e", "<yellow>"); colors.put("&e", "<yellow>");
colors.put("&f", "<white>"); colors.put("&f", "<white>");
} }
public static HashMap<String, Pair<TagResolver, List<String>>> formattingPerms = new HashMap<>(); public static HashMap<String, Pair<TagResolver, List<String>>> formattingPerms = new HashMap<>();
static { static {
formattingPerms.put("chat.format.color", formattingPerms.put("chat.format.color",
new Pair<>(StandardTags.color(), colors.values().stream().toList())); new Pair<>(StandardTags.color(), colors.values().stream().toList()));
formattingPerms.put("chat.format.bold", formattingPerms.put("chat.format.bold",
new Pair<>(StandardTags.decorations(TextDecoration.BOLD), List.of("<bold>", "<b>"))); new Pair<>(StandardTags.decorations(TextDecoration.BOLD), List.of("<bold>", "<b>")));
formattingPerms.put("chat.format.italic", formattingPerms.put("chat.format.italic",
new Pair<>(StandardTags.decorations(TextDecoration.ITALIC), List.of("<italic>", "<i>"))); new Pair<>(StandardTags.decorations(TextDecoration.ITALIC), List.of("<italic>", "<i>")));
formattingPerms.put("chat.format.underlined", formattingPerms.put("chat.format.underlined",
new Pair<>(StandardTags.decorations(TextDecoration.UNDERLINED), List.of("<underlined>", "<u>"))); new Pair<>(StandardTags.decorations(TextDecoration.UNDERLINED), List.of("<underlined>", "<u>")));
formattingPerms.put("chat.format.strikethrough", formattingPerms.put("chat.format.strikethrough",
new Pair<>(StandardTags.decorations(TextDecoration.STRIKETHROUGH), List.of("<strikethrough>", "<st>"))); new Pair<>(StandardTags.decorations(TextDecoration.STRIKETHROUGH), List.of("<strikethrough>", "<st>")));
formattingPerms.put("chat.format.obfuscated", formattingPerms.put("chat.format.obfuscated",
new Pair<>(StandardTags.decorations(TextDecoration.OBFUSCATED), List.of("<obfuscated>", "<obf>"))); new Pair<>(StandardTags.decorations(TextDecoration.OBFUSCATED), List.of("<obfuscated>", "<obf>")));
formattingPerms.put("chat.format.gradient", formattingPerms.put("chat.format.gradient",
new Pair<>(StandardTags.gradient(), EMPTY_LIST)); new Pair<>(StandardTags.gradient(), EMPTY_LIST));
formattingPerms.put("chat.format.font", formattingPerms.put("chat.format.font",
new Pair<>(StandardTags.font(), EMPTY_LIST)); new Pair<>(StandardTags.font(), EMPTY_LIST));
formattingPerms.put("chat.format.rainbow", formattingPerms.put("chat.format.rainbow",
new Pair<>(StandardTags.rainbow(), List.of("<rainbow>"))); new Pair<>(StandardTags.rainbow(), List.of("<rainbow>")));
formattingPerms.put("chat.format.hover", formattingPerms.put("chat.format.hover",
new Pair<>(StandardTags.hoverEvent(), EMPTY_LIST)); new Pair<>(StandardTags.hoverEvent(), EMPTY_LIST));
formattingPerms.put("chat.format.click", formattingPerms.put("chat.format.click",
new Pair<>(StandardTags.clickEvent(), EMPTY_LIST)); new Pair<>(StandardTags.clickEvent(), EMPTY_LIST));
formattingPerms.put("chat.format.transition", formattingPerms.put("chat.format.transition",
new Pair<>(StandardTags.transition(), EMPTY_LIST)); new Pair<>(StandardTags.transition(), EMPTY_LIST));
formattingPerms.put("chat.format.reset", formattingPerms.put("chat.format.reset",
new Pair<>(StandardTags.reset(), List.of("<reset>", "<r>"))); new Pair<>(StandardTags.reset(), List.of("<reset>", "<r>")));
formattingPerms.put("chat.format.newline", formattingPerms.put("chat.format.newline",
new Pair<>(StandardTags.newline(), List.of("<newline>"))); new Pair<>(StandardTags.newline(), List.of("<newline>")));
} }
public static String parseColors(String message) { public static String parseColors(String message) {
if (message == null) { if (message == null) return "";
return "";
}
// split string in sections and check those vs looping hashmap?:/ // split string in sections and check those vs looping hashmap?:/
// think this is better, but will check numbers on this // think this is better, but will check numbers on this
for (String key : colors.keySet()) { for (String key : colors.keySet()) {
@ -97,17 +93,15 @@ public class Utility {
return message; return message;
} }
public static ComponentLike getPrefix(UUID uuid, boolean single) { public static Component getPrefix(UUID uuid, boolean single) {
StringBuilder prefix = new StringBuilder(); StringBuilder prefix = new StringBuilder();
LuckPerms luckPerms = ChatAPI.get().getLuckPerms(); LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(uuid); User user = luckPerms.getUserManager().getUser(uuid);
List<String> prefixGroups = Config.PREFIXGROUPS; List<String> prefixGroups = Config.PREFIXGROUPS;
if (user == null) { if(user == null) return Component.empty();
return Component.empty(); if(!single) {
}
if (!single) {
Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions()); Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions());
if (inheritedGroups.stream().anyMatch(group -> group.getName().equals("eventleader"))) { if(inheritedGroups.stream().map(Group::getName).collect(Collectors.toList()).contains("eventleader")) {
prefixGroups.remove("eventteam"); // hardcoded for now, new prefix system would load this from config prefixGroups.remove("eventteam"); // hardcoded for now, new prefix system would load this from config
} }
inheritedGroups.stream() inheritedGroups.stream()
@ -119,23 +113,21 @@ public class Utility {
}); });
} }
prefix.append(getUserPrefix(user)); prefix.append(getUserPrefix(user));
// prefix.append(user.getCachedData().getMetaData().getPrefix());
return applyColor(prefix.toString()); return applyColor(prefix.toString());
} }
public static ComponentLike getStaffPrefix(UUID uuid) { public static Component getStaffPrefix(UUID uuid) {
StringBuilder prefix = new StringBuilder(); StringBuilder prefix = new StringBuilder();
LuckPerms luckPerms = ChatAPI.get().getLuckPerms(); LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(uuid); User user = luckPerms.getUserManager().getUser(uuid);
if (user == null) { if(user == null) return Component.empty();
return Component.empty(); if(user.getCachedData().getPermissionData().checkPermission("group." + Config.MINIMIUMSTAFFRANK).asBoolean()) {
}
if (user.getCachedData().getPermissionData().checkPermission("group." + Config.MINIMIUMSTAFFRANK).asBoolean()) {
Group group = luckPerms.getGroupManager().getGroup(user.getPrimaryGroup()); Group group = luckPerms.getGroupManager().getGroup(user.getPrimaryGroup());
if (group != null) { if(group != null)
prefix.append(getGroupPrefix(group)); prefix.append(getGroupPrefix(group));
} // prefix.append(group.getCachedData().getMetaData().getPrefix());
// prefix.append(group.getCachedData().getMetaData().getPrefix());
} }
return applyColor(prefix.toString()); return applyColor(prefix.toString());
} }
@ -146,32 +138,26 @@ public class Utility {
if (group == null) { if (group == null) {
return ""; return "";
} }
String prefix = user.getCachedData().getMetaData().getPrefix(); return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", user.getCachedData().getMetaData().getPrefix());
if (prefix == null) { }
ALogger.warn("User " + user.getUsername() + " has no prefix set!");
public static String getGroupPrefix(String groupName) {
Group group = ChatAPI.get().getLuckPerms().getGroupManager().getGroup(groupName);
if (group == null) {
return ""; return "";
} }
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", prefix); return getGroupPrefix(group);
} }
public static String getGroupPrefix(Group group) { public static String getGroupPrefix(Group group) {
String prefix = group.getCachedData().getMetaData().getPrefix(); return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", group.getCachedData().getMetaData().getPrefix());
if (prefix == null) {
ALogger.warn("Group " + group.getName() + " has no prefix set!");
return "";
}
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", prefix);
} }
public static String getDisplayName(UUID uuid, String playerName) { public static String getDisplayName(UUID uuid, String playerName) {
if (!playerName.isBlank()) { if (!playerName.isBlank()) return playerName;
return playerName;
}
LuckPerms luckPerms = ChatAPI.get().getLuckPerms(); LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(uuid); User user = luckPerms.getUserManager().getUser(uuid);
if (user == null) { if(user == null) return "";
return "";
}
return user.getUsername(); return user.getUsername();
} }
@ -179,36 +165,20 @@ public class Utility {
ChatAPI.get().getLuckPerms().getUserManager().modifyUser(uuid, user -> { ChatAPI.get().getLuckPerms().getUserManager().modifyUser(uuid, user -> {
// Add the permission // Add the permission
user.data().add(Node.builder(permission) user.data().add(Node.builder(permission)
.value(!user.getCachedData().getPermissionData().checkPermission(permission).asBoolean()).build()); .value(!user.getCachedData().getPermissionData().checkPermission(permission).asBoolean()).build());
}); });
} }
public static CompletableFuture<User> getOrLoadUser(UUID uuid) { public static boolean hasPermission(UUID uuid, String permission) {
return ChatAPI.get().getLuckPerms().getUserManager().loadUser(uuid);
}
public static CompletableFuture<Boolean> hasPermission(UUID uuid, String permission) {
LuckPerms luckPerms = ChatAPI.get().getLuckPerms(); LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(uuid); User user = luckPerms.getUserManager().getUser(uuid);
if (user == null) { if(user == null) return false;
return getOrLoadUser(uuid) return user.getCachedData().getPermissionData().checkPermission(permission).asBoolean();
.thenApply(loadedUser -> hasPermission(loadedUser, permission))
.exceptionally(throwable -> false);
}
return CompletableFuture.completedFuture(hasPermission(user, permission));
} }
public static boolean hasPermission(User user, String permission) { public static Component applyColor(String message) {
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
return user.getCachedData()
.getPermissionData(QueryOptions.contextual(ImmutableContextSet.of("server", luckPerms.getServerName())))
.checkPermission(permission)
.asBoolean();
}
public static ComponentLike applyColor(String message) {
String hexColor1 = ""; String hexColor1 = "";
String hexColor2; String hexColor2 = "";
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
message = parseColors(message); message = parseColors(message);
boolean startsWithColor = false; boolean startsWithColor = false;
@ -226,7 +196,7 @@ public class Utility {
for (String s : split) { for (String s : split) {
nextIndex += s.length(); nextIndex += s.length();
int tmp = message.indexOf("}", nextIndex); int tmp = message.indexOf("}", nextIndex);
if (tmp < message.length() && tmp >= 0) { if (tmp < message.length() && tmp>=0) {
list.add(message.substring(nextIndex, tmp + 1)); list.add(message.substring(nextIndex, tmp + 1));
nextIndex = tmp + 1; nextIndex = tmp + 1;
} }
@ -250,7 +220,7 @@ public class Utility {
} else if (bigger || lesser) { } else if (bigger || lesser) {
hexColor2 = s.substring(1, s.length() - 2); hexColor2 = s.substring(1, s.length() - 2);
} else { } else {
hexColor2 = s.substring(1, s.length() - 1); hexColor2 = s.substring(1, s.length() -1);
} }
if (firstLoop) { if (firstLoop) {
@ -271,12 +241,12 @@ public class Utility {
lastColorMatters = bigger; lastColorMatters = bigger;
i++; i++;
} }
if (split.length > i) { if (split.length > i){
stringBuilder.append("<").append(hexColor1).append(">").append(split[i]); stringBuilder.append("<").append(hexColor1).append(">").append(split[i]);
} }
} }
return stringBuilder.isEmpty() ? Utility.parseMiniMessage(message) return stringBuilder.length() == 0 ? Utility.parseMiniMessage(message)
: Utility.parseMiniMessage(stringBuilder.toString()); : Utility.parseMiniMessage(stringBuilder.toString());
} }
public static boolean checkNickBrightEnough(String nickname) { public static boolean checkNickBrightEnough(String nickname) {
@ -321,29 +291,32 @@ public class Utility {
} }
public static String formatText(String message) { public static String formatText(String message) {
/*
.match(pattern)
.replacement(url -> {
String clickUrl = url.content();
if (!URL_SCHEME_PATTERN.matcher(clickUrl).find()) {
clickUrl = "http://" + clickUrl;
}
return (style == null ? url : url.style(style)).clickEvent(ClickEvent.openUrl(clickUrl));
})
.build();
*/
Matcher matcher = DEFAULT_URL_PATTERN.matcher(message); Matcher matcher = DEFAULT_URL_PATTERN.matcher(message);
while (matcher.find()) { while (matcher.find()) {
String url = matcher.group(); String url = matcher.group();
String clickUrl = url;
String urlFormat = Config.URLFORMAT; String urlFormat = Config.URLFORMAT;
message = message.replace(url, urlFormat message = message.replace(url, urlFormat.replaceAll("<url>", url).replaceAll("<clickurl>", clickUrl));
.replaceAll("<url>", "<u>" + url + "</u>")
.replaceAll("<clickurl>", formatUrl(url)));
} }
return message; return message;
} }
private static String formatUrl(String url) { public static Component parseMiniMessage(String message) {
if (url.startsWith("http://") || url.startsWith("https://")) {
return url;
}
return "https://" + url;
}
public static ComponentLike parseMiniMessage(String message) {
return getMiniMessage().deserialize(message); return getMiniMessage().deserialize(message);
} }
public static ComponentLike parseMiniMessage(String message, TagResolver placeholders) { public static Component parseMiniMessage(String message, TagResolver placeholders) {
if (placeholders == null) { if (placeholders == null) {
return getMiniMessage().deserialize(message); return getMiniMessage().deserialize(message);
} else { } else {
@ -351,7 +324,7 @@ public class Utility {
} }
} }
public static ComponentLike parseMiniMessage(String message, TagResolver... placeholders) { public static Component parseMiniMessage(String message, TagResolver ... placeholders) {
if (placeholders == null) { if (placeholders == null) {
return getMiniMessage().deserialize(message); return getMiniMessage().deserialize(message);
} else { } else {
@ -364,9 +337,7 @@ public class Utility {
} }
public static MiniMessage getMiniMessage() { public static MiniMessage getMiniMessage() {
if (miniMessage == null) { if (miniMessage == null) miniMessage = MiniMessage.miniMessage();
miniMessage = MiniMessage.miniMessage();
}
return miniMessage; return miniMessage;
} }

View File

@ -1,194 +0,0 @@
package com.alttd.chat.web;
import com.alttd.chat.config.Config;
import com.alttd.chat.util.ALogger;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Client for subscribing to: GET /api/chat/send/subscribe/{server} served as text/event-stream (SseEmitter) by
* CommandsToServerController.
* <p>
* Handlers for individual SSE event types can be registered up front via {@link #register(String, WebHandler)}. Each
* handler says "when an event named X arrives, deserialize its JSON payload as type T and pass it to this handler."
* Unregistered event names are logged and skipped, not thrown.
* <p>
* If the connection fails or drops for any reason, the client logs it and retries once a minute, indefinitely, until
* stop() is called or the thread is interrupted.
* <p>
* Server-side auth: the controller checks @AuthenticationPrincipal Token token, then token.getKey().equals(validToken).
* That principal is populated from the "Authorization: Bearer <token>" header. validToken is currently hardcoded
* server-side; swap TOKEN below for a config value once that TODO is resolved.
*/
public class SseSubscribeClient implements Runnable {
private static final Logger log = Logger.getLogger(SseSubscribeClient.class.getName());
private static final Duration RETRY_DELAY = Duration.ofMinutes(1);
private final String baseUrl;
private final String server;
private final HttpClient httpClient;
private final ObjectMapper objectMapper = new ObjectMapper();
private final Map<String, WebHandler<?>> handlers = new ConcurrentHashMap<>();
private volatile boolean running = false;
public SseSubscribeClient(String baseUrl, String server, String token) {
this.baseUrl = baseUrl;
this.server = server;
this.httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
}
/**
* Register a handler for a given SSE event name. When an event with this name arrives, its data payload is
* deserialized using the handler's type and passed to the handler.
*/
public <T> void register(String eventName, WebHandler<T> handler) {
handlers.put(eventName, handler);
}
/**
* Runs the subscribe/retry loop on the calling thread. Blocks until stop() is called or the thread is interrupted.
*/
@Override
public void run() {
running = true;
while (running && !Thread.currentThread().isInterrupted()) {
String token = Config.CHAT_WEB_TOKEN;
if (token.equals("invalid-token")) {
log.warning("Invalid token provided for SSE connection as '" + server + "'");
sleepBeforeRetry();
continue;
}
try {
connectAndListen(token);
log.warning("SSE stream as '" + server + "' ended; reconnecting in " + RETRY_DELAY.toMinutes() + " minute(s)");
} catch (Exception e) {
log.log(Level.WARNING, "SSE connection to '" + server + "' failed; retrying in "
+ RETRY_DELAY.toMinutes() + " minute(s)", e
);
}
if (running) {
sleepBeforeRetry();
}
}
}
public void stop() {
running = false;
}
private void connectAndListen(String token) throws Exception {
HttpResponse<InputStream> response = openConnection(token);
checkStatus(response);
readEvents(response.body());
}
private HttpResponse<InputStream> openConnection(String token) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/api/chat/server/send/subscribe/" + server))
.header("X-Altitude-Token", token)
.header("Accept", "text/event-stream")
.GET()
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
}
private void checkStatus(HttpResponse<InputStream> response) {
int status = response.statusCode();
if (status == 401) {
throw new RuntimeException("Unauthorized - missing/invalid token");
}
if (status == 403) {
throw new RuntimeException("Forbidden - token did not match server's validToken");
}
if (status != 200) {
throw new RuntimeException("Unexpected status: " + status);
}
ALogger.info("SSE connection as '" + server + "' established");
}
/**
* Reads the SSE stream line by line, accumulating one event at a time, and dispatches each complete event as it's
* parsed.
*/
private void readEvents(InputStream body) throws Exception {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8))) {
String eventName = null;
StringBuilder dataBuffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
if (line.isEmpty()) {
if (!dataBuffer.isEmpty()) {
dispatch(eventName, dataBuffer.toString());
}
eventName = null;
dataBuffer.setLength(0);
continue;
}
if (line.startsWith("event:")) {
eventName = line.substring(6).trim();
} else if (line.startsWith("data:")) {
if (!dataBuffer.isEmpty()) {
dataBuffer.append('\n');
}
dataBuffer.append(line.substring(5).trim());
}
// lines starting with ":" are comments/heartbeats - ignored
}
}
}
private void dispatch(String eventName, String data) {
if (eventName == null) {
log.warning("Received event with no 'event:' name, ignoring. Data: " + data);
return;
}
WebHandler<?> handler = handlers.get(eventName);
if (handler == null) {
log.warning("No handler registered for event type '" + eventName + "', ignoring");
return;
}
dispatchTyped(eventName, handler, data);
}
// Captures the wildcard type from the handler so parsing/casting is type-safe.
private <T> void dispatchTyped(String eventName, WebHandler<T> handler, String data) {
try {
T event = objectMapper.readValue(data, handler.type());
handler.handle(event);
} catch (Exception e) {
log.log(Level.WARNING, "Failed to parse/handle event '" + eventName + "'", e);
}
}
private void sleepBeforeRetry() {
try {
Thread.sleep(RETRY_DELAY.toMillis());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
running = false;
}
}
}

View File

@ -1,9 +0,0 @@
package com.alttd.chat.web;
public interface WebHandler<T> {
Class<T> type();
void handle(T event);
}

View File

@ -1,15 +0,0 @@
package com.alttd.chat.web.handler_class;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
public class ChatFromWeb {
private UUID sender;
private String message;
}

View File

@ -1,16 +0,0 @@
package com.alttd.chat.web.handler_class;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
public class PartyChatFromWeb {
private UUID sender;
private String message;
private String partyId;
}

View File

@ -1,16 +0,0 @@
package com.alttd.chat.web.handler_class;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
public class PrivateChatFromWeb {
private UUID sender;
private String message;
private UUID recipient;
}

View File

@ -1,18 +0,0 @@
package com.alttd.chat.web.handler_class;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
public class PunishFromWeb {
private UUID executor;
private UUID target;
private String type;
private String reason;
private String time;
}

View File

@ -0,0 +1,86 @@
import com.alttd.chat.config.Config;
import com.alttd.chat.objects.ModifiableString;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import net.kyori.adventure.text.minimessage.MiniMessage;
public class ReverseTest {
@Test
public void testReverseString() {
String input = "Hello how are you doing today?";
String expectedOutput = new StringBuilder(input).reverse().toString();
MiniMessage miniMessage = MiniMessage.miniMessage();
ModifiableString modifiableString = new ModifiableString(miniMessage.deserialize(input));
modifiableString.reverse();
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void testRemoveKeyword() {
String input = "Hello how are you doing today?";
MiniMessage miniMessage = MiniMessage.miniMessage();
ModifiableString modifiableString = new ModifiableString(miniMessage.deserialize(Config.APRIL_FOOLS_RESET + " " + input));
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
assertEquals(input, modifiableString.string());
}
@Test
public void testRemoveKeywordWithTags() {
MiniMessage miniMessage = MiniMessage.miniMessage();
String input = "<blue>" + Config.APRIL_FOOLS_RESET + " <red>Hello how are</red> you <blue>doing today</blue>?</blue>";
String expectedOutput = "Hello how are you doing today?";
Component deserialize = miniMessage.deserialize(input);
ModifiableString modifiableString = new ModifiableString(deserialize);
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void testReverseStringWithTags() {
String input = "<red>Hello how are</red> you <blue>doing today</blue>?";
MiniMessage miniMessage = MiniMessage.miniMessage();
Component deserialize = miniMessage.deserialize(input);
ModifiableString modifiableString = new ModifiableString(deserialize);
String expectedOutput = new StringBuilder(PlainTextComponentSerializer.plainText().serialize(deserialize)).reverse().toString();
modifiableString.reverse();
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void complexTestReverseStringWithTags() {
String input = "<green><red>Hello <b>how</b> are</red> you <blue>doing today</blue><gold>?</gold></green>";
MiniMessage miniMessage = MiniMessage.miniMessage();
Component deserialize = miniMessage.deserialize(input);
ModifiableString modifiableString = new ModifiableString(deserialize);
String expectedOutput = new StringBuilder(PlainTextComponentSerializer.plainText().serialize(deserialize)).reverse().toString();
modifiableString.reverse();
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void extraComplexTestReverseStringWithTags() {
String input = "<gold>This <red>is</red> longer<green> <name> <red>Hello <b>how</b> are</red> you <test> <blue>doing <name> today</blue><gold>?</gold></green></gold>";
MiniMessage miniMessage = MiniMessage.miniMessage();
Component deserialize = miniMessage.deserialize(input, TagResolver.resolver(
Placeholder.component("name", miniMessage.deserialize("<red>Cool<blue><rainbow>_player_</rainbow>name</red>")),
Placeholder.parsed("test", "test replacement")
));
ModifiableString modifiableString = new ModifiableString(deserialize);
String expectedOutput = new StringBuilder(PlainTextComponentSerializer.plainText().serialize(deserialize)).reverse().toString();
modifiableString.reverse();
System.out.println(expectedOutput);
assertEquals(expectedOutput, modifiableString.string());
}
}

View File

@ -1,13 +1,25 @@
plugins { plugins {
`java-library` `java-library`
id("com.gradleup.shadow") version "9.6.1" id("com.github.johnrengelman.shadow") version "7.1.0"
id("com.github.ben-manes.versions") version "0.52.0"
} }
allprojects { allprojects {
group = "com.alttd.chat" group = "com.alttd.chat"
version = "2.0.0-SNAPSHOT" version = "2.0.0-SNAPSHOT"
description = "All in one minecraft chat plugin" description = "All in one minecraft chat plugin"
// repositories {
// mavenCentral()
// maven("https://repo.destro.xyz/snapshots") // Altitude - Galaxy
// maven("https://oss.sonatype.org/content/groups/public/") // Adventure
// maven("https://oss.sonatype.org/content/repositories/snapshots/") // Minimessage
// maven("https://oss.sonatype.org/content/repositories/") // Minimessage
// maven("https://nexus.velocitypowered.com/repository/") // Velocity
// maven("https://nexus.velocitypowered.com/repository/maven-public/") // Velocity
// maven("https://repo.spongepowered.org/maven") // Configurate
// maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") // Papi
// maven("https://jitpack.io")
// }
} }
subprojects { subprojects {
@ -15,7 +27,7 @@ subprojects {
java { java {
toolchain { toolchain {
languageVersion.set(JavaLanguageVersion.of(25)) languageVersion.set(JavaLanguageVersion.of(17))
} }
} }
@ -68,4 +80,4 @@ tasks {
jar { jar {
enabled = false enabled = false
} }
} }

View File

@ -1,30 +1,58 @@
import java.io.FileOutputStream
import java.net.URL
plugins { plugins {
`maven-publish` `maven-publish`
id("com.gradleup.shadow") id("com.github.johnrengelman.shadow")
id("xyz.jpenilla.run-paper") version "1.0.6"
} }
val nexusUser = providers.gradleProperty("alttdSnapshotUsername").orNull ?: System.getenv("NEXUS_USERNAME")
val nexusPass = providers.gradleProperty("alttdSnapshotPassword").orNull ?: System.getenv("NEXUS_PASSWORD")
dependencies { dependencies {
implementation(project(":api")) // API implementation(project(":api")) // API
compileOnly("com.alttd.cosmos:cosmos-api:26.2.build.17-stable") compileOnly("com.alttd:Galaxy-API:1.20.4-R0.1-SNAPSHOT") // Galaxy
compileOnly("org.projectlombok:lombok:1.18.46") compileOnly("com.gitlab.ruany:LiteBansAPI:0.3.5") // move to proxy
annotationProcessor("org.projectlombok:lombok:1.18.46") compileOnly("org.apache.commons:commons-lang3:3.12.0") // needs an alternative, already removed from upstream api and will be removed in server
compileOnly("com.gitlab.ruany:LiteBansAPI:0.6.1") // move to proxy compileOnly("net.luckperms:api:5.3") // Luckperms
compileOnly("org.apache.commons:commons-lang3:3.17.0") // needs an alternative, already removed from upstream api and will be removed in server compileOnly(files("../libs/CMI.jar"))
compileOnly("net.luckperms:api:5.5") // Luckperms
implementation("com.alttd.inventory_gui:InventoryGUI:1.1.5-SNAPSHOT")
} }
tasks { tasks {
shadowJar { shadowJar {
archiveFileName.set("${rootProject.name}-${project.name}-${project.version}.jar") archiveFileName.set("${rootProject.name}-${project.name}-${project.version}.jar")
// minimize()
} }
build { build {
// setBuildDir("${rootProject.buildDir}")
dependsOn(shadowJar) dependsOn(shadowJar)
} }
runServer {
val dir = File(System.getProperty("user.home") + "/share/devserver/");
if (!dir.parentFile.exists()) {
dir.parentFile.mkdirs()
}
runDirectory.set(dir)
val fileName = "/galaxy.jar"
var file = File(dir.path + fileName)
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
}
if (!file.exists()) {
download("https://repo.destro.xyz/snapshots/com/alttd/Galaxy-Server/Galaxy-paperclip-1.19.2-R0.1-SNAPSHOT-reobf.jar", file)
}
serverJar(file)
minecraftVersion("1.19.2")
}
} }
fun download(link: String, path: File) {
URL(link).openStream().use { input ->
FileOutputStream(path).use { output ->
input.copyTo(output)
}
}
}

View File

@ -1,61 +1,48 @@
package com.alttd.chat; package com.alttd.chat;
import com.alttd.chat.chat_web.ChatMessageSender;
import com.alttd.chat.chat_web.handlers.WebChannelChatHandler;
import com.alttd.chat.chat_web.handlers.WebChatHandler;
import com.alttd.chat.commands.*; import com.alttd.chat.commands.*;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.config.ServerConfig; import com.alttd.chat.config.ServerConfig;
import com.alttd.chat.database.DatabaseConnection; import com.alttd.chat.database.DatabaseConnection;
import com.alttd.chat.handler.ChatHandler; import com.alttd.chat.handler.ChatHandler;
import com.alttd.chat.listeners.*; import com.alttd.chat.listeners.BookListener;
import com.alttd.chat.listeners.ChatListener;
import com.alttd.chat.listeners.PlayerListener;
import com.alttd.chat.listeners.PluginMessage;
import com.alttd.chat.nicknames.Nicknames; import com.alttd.chat.nicknames.Nicknames;
import com.alttd.chat.nicknames.NicknamesEvents; import com.alttd.chat.nicknames.NicknamesEvents;
import com.alttd.chat.objects.channels.Channel; import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.objects.channels.CustomChannel; import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.objects.chat_log.WebHandler;
import com.alttd.chat.util.ALogger; import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.alttd.chat.web.SseSubscribeClient;
import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects; import java.util.List;
public class ChatPlugin extends JavaPlugin { public class ChatPlugin extends JavaPlugin {
@Getter
private static ChatPlugin instance; private static ChatPlugin instance;
private ChatAPI chatAPI; private ChatAPI chatAPI;
@Getter
private ChatHandler chatHandler; private ChatHandler chatHandler;
private String messageChannel;
private ServerConfig serverConfig; private ServerConfig serverConfig;
private SseSubscribeClient sseSubscribeClient;
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
ALogger.init(getSLF4JLogger()); ALogger.init(getSLF4JLogger());
chatAPI = new ChatImplementation(); chatAPI = new ChatImplementation();
WebHandler webHandler = new WebHandler(); chatHandler = new ChatHandler();
ChatLogHandler chatLogHandler = ChatLogHandler.getInstance(webHandler, true);
chatHandler = new ChatHandler(chatLogHandler);
DatabaseConnection.initialize(); DatabaseConnection.initialize();
serverConfig = new ServerConfig(ServerName.getServerName()); serverConfig = new ServerConfig(Bukkit.getServerName());
ChatMessageSender chatMessageSender = new ChatMessageSender(chatLogHandler, chatAPI.getLuckPerms()); registerListener(new PlayerListener(serverConfig), new ChatListener(), new BookListener());
registerListener(new PlayerListener(webHandler, serverConfig), if(serverConfig.GLOBALCHAT) {
new ChatListener(chatMessageSender),
new BookListener(),
new ShutdownListener(chatLogHandler, this)
);
if (serverConfig.GLOBALCHAT) {
registerCommand("globalchat", new GlobalChat()); registerCommand("globalchat", new GlobalChat());
registerCommand("toggleglobalchat", new ToggleGlobalChat()); registerCommand("toggleglobalchat", new ToggleGlobalChat());
} }
@ -67,47 +54,26 @@ public class ChatPlugin extends JavaPlugin {
registerCommand("muteserver", new MuteServer()); registerCommand("muteserver", new MuteServer());
registerCommand("spy", new Spy()); registerCommand("spy", new Spy());
registerCommand("chatclear", new ChatClear()); registerCommand("chatclear", new ChatClear());
// registerCommand("chatparty", new ChatParty());
registerCommand("p", new PartyChat()); registerCommand("p", new PartyChat());
registerCommand("emotes", new Emotes()); registerCommand("emotes", new Emotes());
for (Channel channel : Channel.getChannels()) { for (Channel channel : Channel.getChannels()) {
if (!(channel instanceof CustomChannel customChannel)) { if (!(channel instanceof CustomChannel customChannel)) continue;
continue; this.getServer().getCommandMap().register(channel.getChannelName().toLowerCase(), new ChatChannel(customChannel));
}
ChatChannel chatChannel = new ChatChannel(customChannel, chatAPI.getLuckPerms());
this.getServer()
.getCommandMap()
.register(channel.getChannelName().toLowerCase(), chatChannel);
if (customChannel.getWebPath() != null) {
sseSubscribeClient.register(customChannel.getWebPath(), new WebChannelChatHandler(chatChannel));
}
} }
String messageChannel = Config.MESSAGECHANNEL; messageChannel = Config.MESSAGECHANNEL;
getServer().getMessenger().registerOutgoingPluginChannel(this, messageChannel); getServer().getMessenger().registerOutgoingPluginChannel(this, messageChannel);
getServer().getMessenger() getServer().getMessenger().registerIncomingPluginChannel(this, messageChannel, new PluginMessage());
.registerIncomingPluginChannel(this, messageChannel, new PluginMessage(chatLogHandler));
NicknamesEvents nicknamesEvents = new NicknamesEvents(); NicknamesEvents nicknamesEvents = new NicknamesEvents();
getServer().getMessenger().registerIncomingPluginChannel(this, messageChannel, nicknamesEvents); getServer().getMessenger().registerIncomingPluginChannel(this, messageChannel, nicknamesEvents);
getServer().getPluginManager().registerEvents(nicknamesEvents, this); getServer().getPluginManager().registerEvents(nicknamesEvents, this);
registerCommand("nick", new Nicknames()); registerCommand("nick", new Nicknames());
sseSubscribeClient = new SseSubscribeClient(
Config.CHAT_WEB_REGISTER_TO_BASE_URL,
getServer().getServerName(),
Config.CHAT_WEB_TOKEN
);
new Thread(sseSubscribeClient).start();
registerWebHandlers(chatMessageSender, sseSubscribeClient);
}
private void registerWebHandlers(ChatMessageSender chatMessageSender, SseSubscribeClient sseSubscribeClient) {
sseSubscribeClient.register("web_chat", new WebChatHandler(chatMessageSender));
} }
@Override @Override
public void onDisable() { public void onDisable() {
sseSubscribeClient.stop();
instance = null; instance = null;
} }
@ -118,7 +84,25 @@ public class ChatPlugin extends JavaPlugin {
} }
public void registerCommand(String commandName, CommandExecutor commandExecutor) { public void registerCommand(String commandName, CommandExecutor commandExecutor) {
Objects.requireNonNull(getCommand(commandName)).setExecutor(commandExecutor); getCommand(commandName).setExecutor(commandExecutor);
}
public void registerCommand(String commandName, CommandExecutor commandExecutor, List<String> aliases) {
PluginCommand command = getCommand(commandName);
command.setAliases(aliases);
command.setExecutor(commandExecutor);
}
public static ChatPlugin getInstance() {
return instance;
}
public ChatAPI getChatAPI() {
return chatAPI;
}
public ChatHandler getChatHandler() {
return chatHandler;
} }
public boolean serverGlobalChatEnabled() { public boolean serverGlobalChatEnabled() {
@ -133,13 +117,11 @@ public class ChatPlugin extends JavaPlugin {
serverConfig.MUTED = !serverConfig.MUTED; serverConfig.MUTED = !serverConfig.MUTED;
} }
public void reloadConfig() { public void ReloadConfig() {
chatAPI.reloadConfig(); chatAPI.ReloadConfig();
chatAPI.reloadChatFilters(); chatAPI.ReloadChatFilters();
serverConfig = new ServerConfig(ServerName.getServerName()); serverConfig = new ServerConfig(Bukkit.getServerName());
Bukkit.broadcast(Utility.parseMiniMessage("Reloaded ChatPlugin Config.").asComponent(), Bukkit.broadcast(Utility.parseMiniMessage("Reloaded ChatPlugin Config."), "command.chat.reloadchat");
"command.chat.reloadchat"
);
ALogger.info("Reloaded ChatPlugin config."); ALogger.info("Reloaded ChatPlugin config.");
} }
} }

View File

@ -1,273 +0,0 @@
package com.alttd.chat.chat_web;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.FilterType;
import com.alttd.chat.objects.ModifiableString;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.user.User;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RequiredArgsConstructor
public class ChatMessageSender {
private final ChatLogHandler chatLogHandler;
private final LuckPerms luckPerms;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
public void sendMessage(UUID sender, String message) {
//TODO [Stijn] [2026-08-02]: Mark as from website
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(sender);
Component component = miniMessage.deserialize(message);
sendMessage(offlinePlayer, component, true);
}
public void sendMessage(@NotNull OfflinePlayer offlinePlayer, Component message, boolean isFromWeb) {
UUID uuid = offlinePlayer.getUniqueId();
if (luckPerms.getUserManager().isLoaded(uuid)) {
User user = luckPerms.getUserManager().getUser(uuid);
sendMessage(user, offlinePlayer, message, isFromWeb);
} else {
luckPerms.getUserManager().loadUser(uuid).whenComplete((user, throwable) -> {
if (throwable != null) {
ALogger.error("Failed to load user: " + uuid, throwable);
}
sendMessage(user, offlinePlayer, message, isFromWeb);
});
}
}
private void sendMessage(User user, OfflinePlayer offlinePlayer, Component message, boolean isFromWeb) {
if (offlinePlayer == null) {
ALogger.error("OfflinePlayer is null");
return;
}
if (ChatPlugin.getInstance().serverMuted() && !Utility.hasPermission(user, "chat.bypass-server-muted")) {
sendBlockNotifIfOnline(offlinePlayer, message);
return;
}
UUID uuid = offlinePlayer.getUniqueId();
ComponentLike input = message.colorIfAbsent(NamedTextColor.WHITE);
Component inputComponent = input.asComponent();
ModifiableString modifiableString = new ModifiableString(inputComponent);
if (parseMessage(offlinePlayer, uuid, modifiableString, inputComponent, isFromWeb)) {
//Parse message failed likely due to something the player said, already logged
return;
}
if (sendMessageAndPing(user, offlinePlayer, uuid, modifiableString)) {
//Send message and ping failed, likely due to the offlinePlayer not being valid, already logged
return;
}
ALogger.info(PlainTextComponentSerializer.plainText().serialize(inputComponent));
}
private boolean sendMessageAndPing(User user, OfflinePlayer offlinePlayer, UUID uuid, ModifiableString modifiableString) {
ComponentLike input;
Stream<Player> stream = Bukkit.getOnlinePlayers().stream()
.map(audience -> (Player) audience);
if (!Utility.hasPermission(user, "chat.ignorebypass")) {
stream = stream.filter(receiver -> {
boolean isPlayerIgnored = ChatUserManager
.getChatUser(receiver.getUniqueId())
.getIgnoredPlayers()
.contains(uuid);
return !isPlayerIgnored || receiver.hasPermission("chat.ignorebypass");
});
}
Set<Player> receivers = stream.collect(Collectors.toSet());
Set<Player> playersToPing = new HashSet<>();
pingPlayers(playersToPing, modifiableString, offlinePlayer, user);
Optional<ComponentLike> render = render(offlinePlayer, modifiableString.component(), false);
if (render.isEmpty()) {
//Already logged
return true;
}
input = render.get();
for (Player receiver : receivers) {
receiver.sendMessage(input);
}
for (Player pingPlayer : playersToPing) {
pingPlayer.playSound(pingPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1);
}
chatLogHandler.addChatLog(uuid,
ServerName.getServerName(),
modifiableString.string(),
ChatLogType.PUBLIC,
null,
null,
input.asComponent(),
false
);
return false;
}
private boolean parseMessage(OfflinePlayer offlinePlayer, UUID uuid, ModifiableString modifiableString, Component inputComponent, boolean isWebMessage) {
// todo a better way for this
if (!RegexManager.filterText(offlinePlayer.getName(), uuid, modifiableString, true, "chat",
filterType -> punishOnlinePlayer(filterType, uuid, modifiableString)
)) {
GalaxyUtility.sendBlockedNotification("Language", offlinePlayer,
modifiableString.component(),
""
);
String originalMessage = PlainTextComponentSerializer.plainText().serialize(inputComponent);
Optional<Component> component = render(offlinePlayer,
inputComponent,
isWebMessage
).map(ComponentLike::asComponent);
if (component.isEmpty()) {
//Already logged
return true;
}
chatLogHandler.addChatLog(uuid,
ServerName.getServerName(),
originalMessage,
ChatLogType.PUBLIC,
null,
null,
component.get(),
true
);
return true;
}
return false;
}
private void pingPlayers(Set<Player> playersToPing, ModifiableString modifiableString, OfflinePlayer offlinePlayer, User user) {
Component mention = MiniMessage.miniMessage().deserialize(Config.MENTIONPLAYERTAG);
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
String name = onlinePlayer.getName();
String nickName = PlainTextComponentSerializer.plainText().serialize(onlinePlayer.displayName());
Pattern namePattern = Pattern.compile("\\b(?<!\\\\)" + name + "\\b", Pattern.CASE_INSENSITIVE);
// Pattern escapedNamePattern = Pattern.compile("\\b\\\\" + name + "\\b", Pattern.CASE_INSENSITIVE);
Pattern nickPattern = Pattern.compile("\\b(?<!\\\\)" + nickName + "\\b", Pattern.CASE_INSENSITIVE);
// Pattern escapedNickPattern = Pattern.compile("\\b\\\\" + nickName + "\\b", Pattern.CASE_INSENSITIVE);
ChatUser onlinePlayerUser = ChatUserManager.getChatUser(onlinePlayer.getUniqueId());
if (namePattern.matcher(modifiableString.string()).find()) {
modifiableString.replace(TextReplacementConfig.builder()
.once()
.match(namePattern)
.replacement(mention.append(onlinePlayerUser.getDisplayName()))
.build());
//TODO replace all instances of \name with just name but using the match result so the capitalization doesn't change
// modifiableString.replace(TextReplacementConfig.builder()
// .once()
// .match(escapedNamePattern)
// .replacement((a, b) -> {
// String substring = a.group().substring(1);
// return ;
// });
if (!ChatUserManager.getChatUser(onlinePlayer.getUniqueId())
.getIgnoredPlayers()
.contains(offlinePlayer.getUniqueId())
|| Utility.hasPermission(user, "chat.ignorebypass")) {
playersToPing.add(onlinePlayer);
}
} else if (nickPattern.matcher(modifiableString.string()).find()) {
modifiableString.replace(TextReplacementConfig.builder()
.once()
.match(nickPattern)
.replacement(mention.append(onlinePlayerUser.getDisplayName()))
.build());
if (!ChatUserManager.getChatUser(onlinePlayer.getUniqueId())
.getIgnoredPlayers()
.contains(offlinePlayer.getUniqueId())
|| Utility.hasPermission(user, "chat.ignorebypass")) {
playersToPing.add(onlinePlayer);
}
}
}
}
private Optional<ComponentLike> render(@NotNull OfflinePlayer offlinePlayer, @NotNull Component message, boolean isWebMessage) {
if (offlinePlayer.getName() == null) {
ALogger.error("Invalid offline player");
return Optional.empty();
}
ChatUser user = ChatUserManager.getChatUser(offlinePlayer.getUniqueId());
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", user.getDisplayName()),
Placeholder.parsed("sendername", offlinePlayer.getName()),
Placeholder.component("prefix", user.getPrefix()),
Placeholder.component("prefixall", user.getPrefixAll(isWebMessage)),
Placeholder.component("staffprefix", user.getStaffPrefix()),
Placeholder.component("message", message)
);
return Optional.of(Utility.parseMiniMessage(Config.CHATFORMAT, placeholders));
}
private static void punishOnlinePlayer(FilterType filterType, UUID uuid, ModifiableString modifiableString) {
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
return;
}
if (!filterType.equals(FilterType.PUNISH)) {
ALogger.warn("Received another FilterType than punish when filtering chat and executing a filter action");
return;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("punish");
out.writeUTF(player.getName());
out.writeUTF(uuid.toString());
out.writeUTF(modifiableString.string());
player.sendPluginMessage(ChatPlugin.getInstance(), Config.MESSAGECHANNEL, out.toByteArray());
}
private static void sendBlockNotifIfOnline(OfflinePlayer offlinePlayer, Component message) {
if (!offlinePlayer.isOnline()) {
return;
}
Player player = offlinePlayer.getPlayer();
if (player == null) {
ALogger.error("Failed to load player: " + offlinePlayer.getName());
return;
}
GalaxyUtility.sendBlockedNotification("Chat Muted", player, message, "");
}
}

View File

@ -1,22 +0,0 @@
package com.alttd.chat.chat_web.handlers;
import com.alttd.chat.commands.ChatChannel;
import com.alttd.chat.web.WebHandler;
import com.alttd.chat.web.handler_class.ChatFromWeb;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class WebChannelChatHandler implements WebHandler<ChatFromWeb> {
private final ChatChannel chatChannel;
@Override
public Class<ChatFromWeb> type() {
return ChatFromWeb.class;
}
@Override
public void handle(ChatFromWeb chatFromWeb) {
this.chatChannel.execute(chatFromWeb);
}
}

View File

@ -1,24 +0,0 @@
package com.alttd.chat.chat_web.handlers;
import com.alttd.chat.chat_web.ChatMessageSender;
import com.alttd.chat.web.WebHandler;
import com.alttd.chat.web.handler_class.ChatFromWeb;
public class WebChatHandler implements WebHandler<ChatFromWeb> {
private final ChatMessageSender chatMessageSender;
public WebChatHandler(ChatMessageSender chatMessageSender) {
this.chatMessageSender = chatMessageSender;
}
@Override
public Class<ChatFromWeb> type() {
return ChatFromWeb.class;
}
@Override
public void handle(ChatFromWeb chatFromWeb) {
this.chatMessageSender.sendMessage(chatFromWeb.getSender(), chatFromWeb.getMessage());
}
}

View File

@ -2,60 +2,46 @@ package com.alttd.chat.commands;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.objects.channels.CustomChannel; import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.ToggleableForCustomChannel; import com.alttd.chat.util.ToggleableForCustomChannel;
import com.alttd.chat.util.Utility;
import com.alttd.chat.web.handler_class.ChatFromWeb;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.user.User;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.UUID;
public class ChatChannel extends BukkitCommand { public class ChatChannel extends BukkitCommand {
CustomChannel channel; CustomChannel channel;
String command; String command;
ToggleableForCustomChannel toggleableForCustomChannel; ToggleableForCustomChannel toggleableForCustomChannel;
private final LuckPerms luckPerms; private static List<ChatChannel> activeCommands = new ArrayList<>();
private static final List<ChatChannel> activeCommands = new ArrayList<>();
public ChatChannel(CustomChannel channel, LuckPerms luckPerms) { public ChatChannel(CustomChannel channel) {
super(channel.getChannelName().toLowerCase()); super(channel.getChannelName().toLowerCase());
this.luckPerms = luckPerms;
this.channel = channel; this.channel = channel;
this.command = channel.getChannelName().toLowerCase(); this.command = channel.getChannelName().toLowerCase();
this.description = "Chat channel named " + channel.getChannelName() + "."; this.description = "Chat channel named " + channel.getChannelName() + ".";
this.usageMessage = "/" + command + " <message>"; this.usageMessage = "/" + command + " <message>";
this.setAliases(channel.getAliases()); this.setAliases(Collections.emptyList());
activeCommands.add(this); activeCommands.add(this);
this.toggleableForCustomChannel = new ToggleableForCustomChannel(channel); this.toggleableForCustomChannel = new ToggleableForCustomChannel(channel);
} }
@Override @Override
public boolean execute(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) { public boolean execute(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
if (!(sender instanceof Player player)) { // must be a player if(!(sender instanceof Player player)) { // must be a player
return true; return true;
} }
if (args.length == 0 && player.hasPermission(channel.getPermission())) { if(args.length == 0 && player.hasPermission(channel.getPermission())) {
player.sendRichMessage(Config.CUSTOM_CHANNEL_TOGGLED, TagResolver.resolver( player.sendMiniMessage(Config.CUSTOM_CHANNEL_TOGGLED, TagResolver.resolver(
Placeholder.unparsed("channel", channel.getChannelName()), Placeholder.unparsed("channel", channel.getChannelName()),
Placeholder.component("status", toggleableForCustomChannel.toggle(player.getUniqueId()) Placeholder.component("status", toggleableForCustomChannel.toggle(player.getUniqueId())
? Config.TOGGLED_ON : Config.TOGGLED_OFF ? Config.TOGGLED_ON : Config.TOGGLED_OFF)));
)
)
);
return false; return false;
} }
@ -65,28 +51,4 @@ public class ChatChannel extends BukkitCommand {
return false; return false;
} }
public void execute(ChatFromWeb chatFromWeb) {
UUID uuid = chatFromWeb.getSender();
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
if (luckPerms.getUserManager().isLoaded(uuid)) {
User user = luckPerms.getUserManager().getUser(uuid);
if (user == null) {
ALogger.error("Failed to load loaded user: " + uuid);
return;
}
if (!Utility.hasPermission(user, channel.getPermission())) {
ALogger.warn("Web user %s does not have permission to use this channel".formatted(uuid.toString()));
return;
}
toggleableForCustomChannel.sendMessage(user, offlinePlayer, chatFromWeb.getMessage());
} else {
luckPerms.getUserManager().loadUser(uuid).whenComplete((user, throwable) -> {
if (throwable != null) {
ALogger.error("Failed to load user: " + uuid, throwable);
}
toggleableForCustomChannel.sendMessage(user, offlinePlayer, chatFromWeb.getMessage());
});
}
}
} }

View File

@ -1,5 +1,6 @@
package com.alttd.chat.commands; package com.alttd.chat.commands;
import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
@ -8,29 +9,26 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class ChatClear implements CommandExecutor { public class ChatClear implements CommandExecutor {
private static final Component component = MiniMessage.miniMessage().deserialize("\n".repeat(100)); private static final Component component = MiniMessage.miniMessage().deserialize("\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n\n \n");
MiniMessage miniMessage = MiniMessage.miniMessage(); MiniMessage miniMessage = MiniMessage.miniMessage();
@Override @Override
public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("chat.command.clear-chat")) { if (!sender.hasPermission("chat.command.clear-chat")) {
sender.sendRichMessage("<red>You don't have permission to use this command.</red>"); sender.sendMessage(Utility.parseMiniMessage("<red>You don't have permission to use this command.</red>"));
return true; return true;
} }
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers())
if (!player.hasPermission("chat.clear-bypass")) { if (!player.hasPermission("chat.clear-bypass"))
player.sendMessage(component); player.sendMessage(component);
}
}
Bukkit.getServer().sendMessage(miniMessage.deserialize( Bukkit.getServer().sendMessage(miniMessage.deserialize(
"<gold><player> cleared chat.</gold>", "<gold><player> cleared chat.</gold>",
Placeholder.component("player", sender.name())) Placeholder.component("player",sender.name()))
); );
return true; return true;
} }
} }

View File

@ -8,22 +8,17 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Continue implements CommandExecutor { public class Continue implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { if(!(sender instanceof Player player)) {
return true; return true;
} }
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
if (user.getReplyContinueTarget() == null) { if (user.getReplyContinueTarget() == null) return false;
return false; if(args.length == 0) return false; // todo error message or command info
}
if (args.length == 0) {
return false; // todo error message or command info
}
String message = StringUtils.join(args, " ", 0, args.length); String message = StringUtils.join(args, " ", 0, args.length);
ChatPlugin.getInstance().getChatHandler().continuePrivateMessage(player, user.getReplyContinueTarget(), message); ChatPlugin.getInstance().getChatHandler().continuePrivateMessage(player, user.getReplyContinueTarget(), message);

View File

@ -7,18 +7,15 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
public class GlobalChat implements CommandExecutor { public class GlobalChat implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { // must be a player if(!(sender instanceof Player player)) { // must be a player
return true; return true;
} }
if (args.length == 0) { if(args.length == 0) return false;
return false;
}
String message = StringUtils.join(args, " ", 0, args.length); String message = StringUtils.join(args, " ", 0, args.length);

View File

@ -5,17 +5,19 @@ import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.UUID; import java.util.UUID;
public class Ignore implements CommandExecutor { public class Ignore implements CommandExecutor {
@ -27,13 +29,11 @@ public class Ignore implements CommandExecutor {
} }
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { if(!(sender instanceof Player player)) { // must be a player
return true; return true;
} }
if (args.length > 1) { if(args.length > 1) return false; // todo error message or command info
return false; // todo error message or command info
}
String targetName = args[0]; String targetName = args[0];
if (targetName.equals("?")) { if (targetName.equals("?")) {
new BukkitRunnable() { new BukkitRunnable() {
@ -44,7 +44,7 @@ public class Ignore implements CommandExecutor {
StringBuilder ignoredMessage = new StringBuilder(); StringBuilder ignoredMessage = new StringBuilder();
if (userNames.isEmpty()) { if (userNames.isEmpty()) {
player.sendRichMessage("You don't have anyone ignored!"); //TODO load from config player.sendMessage(Utility.parseMiniMessage("You don't have anyone ignored!")); //TODO load from config
return; return;
} }
@ -52,21 +52,21 @@ public class Ignore implements CommandExecutor {
userNames.forEach(username -> ignoredMessage.append(username).append("\n")); userNames.forEach(username -> ignoredMessage.append(username).append("\n"));
ignoredMessage.delete(ignoredMessage.length() - 1, ignoredMessage.length()); ignoredMessage.delete(ignoredMessage.length() - 1, ignoredMessage.length());
player.sendRichMessage(ignoredMessage.toString()); player.sendMessage(Utility.parseMiniMessage(ignoredMessage.toString()));
} }
}.runTaskAsynchronously(plugin); }.runTaskAsynchronously(plugin);
return false; return false;
} }
Player targetPlayer = Bukkit.getPlayer(targetName); Player targetPlayer = Bukkit.getPlayer(targetName);
if (targetPlayer == null) { // can't ignore offline players if(targetPlayer == null) { // can't ignore offline players
sender.sendMessage("You can't ignore offline players"); sender.sendMessage("You can't ignore offline players");
//sender.sendMessage("Target not found..."); // TODO load from config and minimessage //sender.sendMessage("Target not found..."); // TODO load from config and minimessage
return false; return false;
} }
UUID target = targetPlayer.getUniqueId(); UUID target = targetPlayer.getUniqueId();
if (targetPlayer.hasPermission("chat.ignorebypass") || target.equals(player.getUniqueId())) { if(targetPlayer.hasPermission("chat.ignorebypass") || target.equals(player.getUniqueId())) {
sender.sendMessage("You can't ignore this player"); // TODO load from config and minimessage sender.sendMessage("You can't ignore this player"); // TODO load from config and minimessage
return false; return false;
} }
@ -74,9 +74,9 @@ public class Ignore implements CommandExecutor {
@Override @Override
public void run() { public void run() {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
if (!chatUser.getIgnoredPlayers().contains(target)) { if(!chatUser.getIgnoredPlayers().contains(target)) {
chatUser.addIgnoredPlayers(target); chatUser.addIgnoredPlayers(target);
Queries.ignoreUser(player.getUniqueId(), target); Queries.ignoreUser(((Player) sender).getUniqueId(), target);
sender.sendMessage("You have ignored " + targetName + "."); // TODO load from config and minimessage sender.sendMessage("You have ignored " + targetName + "."); // TODO load from config and minimessage
sendPluginMessage("ignore", player, target); sendPluginMessage("ignore", player, target);
} else { } else {

View File

@ -8,18 +8,15 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Message implements CommandExecutor { public class Message implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { if(!(sender instanceof Player player)) {
return true; return true;
} }
if (args.length < 2) { if(args.length < 2) return false; // todo error message or command info
return false; // todo error message or command info
}
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
user.setReplyContinueTarget(args[0]); user.setReplyContinueTarget(args[0]);

View File

@ -3,42 +3,45 @@ package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.bukkit.scheduler.BukkitRunnable;
import java.util.UUID;
public class MuteServer implements CommandExecutor { public class MuteServer implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { // must be a player if(!(sender instanceof Player player)) { // must be a player
return true; return true;
} }
Utility.getOrLoadUser(player.getUniqueId()).thenAcceptAsync(user -> { new BukkitRunnable() {
if (!Utility.hasPermission(user, Config.SERVERMUTEPERMISSION)) { @Override
sender.sendRichMessage("<red>You don't have permission to use this command.</red>"); public void run() {
return; UUID uuid = player.getUniqueId();
if (!Utility.hasPermission(uuid, Config.SERVERMUTEPERMISSION)) {
sender.sendMessage(Utility.parseMiniMessage("<red>You don't have permission to use this command.</red>"));
return;
}
ChatPlugin.getInstance().toggleServerMuted();
Component component;
if (ChatPlugin.getInstance().serverMuted()) {
component = Utility.parseMiniMessage(Utility.getDisplayName(player.getUniqueId(), player.getName()) + " <red>muted</red><white> chat.");
} else {
component = Utility.parseMiniMessage(Utility.getDisplayName(player.getUniqueId(), player.getName()) + " <green>un-muted</green><white> chat.");
}
Bukkit.getOnlinePlayers().forEach(player -> player.sendMessage(component));
} }
}.runTaskAsynchronously(ChatPlugin.getInstance());
ChatPlugin.getInstance().toggleServerMuted();
ComponentLike component;
if (ChatPlugin.getInstance().serverMuted()) {
component = Utility.parseMiniMessage(Utility.getDisplayName(player.getUniqueId(),
player.getName()
) + " <red>muted</red><white> chat.");
} else {
component = Utility.parseMiniMessage(Utility.getDisplayName(player.getUniqueId(),
player.getName()
) + " <green>un-muted</green><white> chat.");
}
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> onlinePlayer.sendMessage(component));
});
return false; return false;
} }

View File

@ -5,9 +5,7 @@ import com.alttd.chat.config.Config;
import com.alttd.chat.objects.Toggleable; import com.alttd.chat.objects.Toggleable;
import com.alttd.chat.util.ALogger; import com.alttd.chat.util.ALogger;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.luckperms.api.model.user.User;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -29,7 +27,7 @@ public class PartyChat extends Toggleable implements CommandExecutor {
} }
if(args.length == 0) { if(args.length == 0) {
player.sendRichMessage(Config.PARTY_TOGGLED, Placeholder.component("status", player.sendMiniMessage(Config.PARTY_TOGGLED, Placeholder.component("status",
toggle(player.getUniqueId()) ? Config.TOGGLED_ON : Config.TOGGLED_OFF)); toggle(player.getUniqueId()) ? Config.TOGGLED_ON : Config.TOGGLED_OFF));
return true; return true;
} }
@ -72,9 +70,4 @@ public class PartyChat extends Toggleable implements CommandExecutor {
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
} }
@Override
public void sendMessage(User user, OfflinePlayer offlinePlayer, String message) {
//TODO [Stijn] [2026-08-09]: Implement
}
} }

View File

@ -8,22 +8,18 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Reply implements CommandExecutor { public class Reply implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { if(!(sender instanceof Player)) {
return true; return true;
} }
Player player = (Player) sender;
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
if (user.getReplyTarget() == null) { if (user.getReplyTarget() == null) return false;
return false; if(args.length == 0) return false; // todo error message or command info
}
if (args.length == 0) {
return false; // todo error message or command info
}
String message = StringUtils.join(args, " ", 0, args.length); String message = StringUtils.join(args, " ", 0, args.length);
ChatPlugin.getInstance().getChatHandler().privateMessage(player, user.getReplyTarget(), message); ChatPlugin.getInstance().getChatHandler().privateMessage(player, user.getReplyTarget(), message);

View File

@ -1,29 +1,30 @@
package com.alttd.chat.commands; package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
public class Spy implements CommandExecutor { public class Spy implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { if(!(sender instanceof Player)) { // must be a player
return true; return true;
} }
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
UUID uuid = player.getUniqueId(); UUID uuid = ((Player) sender).getUniqueId();
ChatUser user = ChatUserManager.getChatUser(uuid); ChatUser user = ChatUserManager.getChatUser(uuid);
user.toggleSpy(); user.toggleSpy();
sender.sendMessage(Utility.parseMiniMessage("You have turned spy " + (user.isSpy() ? "<green>on." : "<red>off."))); // TODO load from config and minimessage sender.sendMessage(Utility.parseMiniMessage("You have turned spy " + (user.isSpy() ? "<green>on." : "<red>off."))); // TODO load from config and minimessage

View File

@ -1,31 +1,41 @@
package com.alttd.chat.commands; package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import jdk.jshell.execution.Util;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.bukkit.scheduler.BukkitRunnable;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class ToggleGlobalChat implements CommandExecutor { public class ToggleGlobalChat implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { if(!(sender instanceof Player)) { // must be a player
return true; return true;
} }
UUID uuid = player.getUniqueId(); new BukkitRunnable() {
Utility.getOrLoadUser(uuid).thenAcceptAsync(user -> { @Override
ChatUserManager.getChatUser(uuid); public void run() {
Utility.flipPermission(uuid, Config.GCPERMISSION); UUID uuid = ((Player) sender).getUniqueId();
sender.sendRichMessage("You have turned globalchat " + (!Utility.hasPermission(user, ChatUser chatUser = ChatUserManager.getChatUser(uuid);
Config.GCPERMISSION //chatUser.toggleGc();
) ? "<green>on." : "<red>off.")); // TODO load from config and minimessage Utility.flipPermission(uuid, Config.GCPERMISSION);
}); //Queries.setGlobalChatState(chatUser.isGcOn(), chatUser.getUuid());
sender.sendMessage(Utility.parseMiniMessage("You have turned globalchat " + (!Utility.hasPermission(uuid, Config.GCPERMISSION) ? "<green>on." : "<red>off."))); // TODO load from config and minimessage
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
return false; return false;
} }

View File

@ -3,17 +3,21 @@ package com.alttd.chat.commands;
import com.alttd.chat.ChatPlugin; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.listeners.PluginMessage;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
@ -26,20 +30,22 @@ public class Unignore implements CommandExecutor {
} }
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) { // must be a player if(!(sender instanceof Player player)) { // must be a player
return true; return true;
} }
if (args.length > 1) { if(args.length > 1) return false; // todo error message or command info
return false; // todo error message or command info
}
String targetName = args[0]; String targetName = args[0];
UUID target = Bukkit.getOfflinePlayer(targetName).getUniqueId(); UUID target = Bukkit.getOfflinePlayer(targetName).getUniqueId();
if(target == null) {
//sender.sendMessage("Target not found..."); // TODO load from config and minimessage
return false;
}
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
if (chatUser.getIgnoredPlayers().contains(target)) { if(chatUser.getIgnoredPlayers().contains(target)) {
chatUser.removeIgnoredPlayers(target); chatUser.removeIgnoredPlayers(target);
Queries.unIgnoreUser(player.getUniqueId(), target); Queries.unIgnoreUser(player.getUniqueId(), target);
sender.sendMessage("You no longer ignore " + targetName + "."); // TODO load from config and minimessage sender.sendMessage("You no longer ignore " + targetName + "."); // TODO load from config and minimessage

View File

@ -8,104 +8,74 @@ import com.alttd.chat.objects.ChatFilter;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.ModifiableString; import com.alttd.chat.objects.ModifiableString;
import com.alttd.chat.objects.channels.CustomChannel; import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.GalaxyUtility; import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.luckperms.api.model.user.User;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jspecify.annotations.Nullable;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@Slf4j
public class ChatHandler { public class ChatHandler {
private final ChatPlugin plugin; private final ChatPlugin plugin;
private final ComponentLike GCNOTENABLED; private final Component GCNOTENABLED;
private final ChatLogHandler chatLogHandler;
public ChatHandler(ChatLogHandler chatLogHandler) { public ChatHandler() {
this.chatLogHandler = chatLogHandler;
plugin = ChatPlugin.getInstance(); plugin = ChatPlugin.getInstance();
GCNOTENABLED = Utility.parseMiniMessage(Config.GCNOTENABLED); GCNOTENABLED = Utility.parseMiniMessage(Config.GCNOTENABLED);
} }
public void continuePrivateMessage(Player player, String target, String message) { public void continuePrivateMessage(Player player, String target, String message) {
Utility.getOrLoadUser(player.getUniqueId()) // ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
.thenAccept(user -> continuePrivateMessage(user, player, target, message)); // user.setReplyTarget(target);
}
public void continuePrivateMessage(User user, Player player, String target, String message) {
// ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
// user.setReplyTarget(target);
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.component("message", parseMessageContent(user, player, message)), Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.component("sendername", player.name()), Placeholder.component("sendername", player.name()),
Placeholder.parsed("receivername", target) Placeholder.parsed("receivername", target)
); );
ComponentLike component = Utility.parseMiniMessage("<message>", placeholders); Component component = Utility.parseMiniMessage("<message>", placeholders);
ModifiableString modifiableString = new ModifiableString(component.asComponent()); ModifiableString modifiableString = new ModifiableString(component);
// todo a better way for this // todo a better way for this
if (!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) { if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) {
GalaxyUtility.sendBlockedNotification("DM Language", GalaxyUtility.sendBlockedNotification("DM Language",
player, player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())), Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
target target);
);
return; // the message was blocked return; // the message was blocked
} }
component = modifiableString.component(); component = modifiableString.component();
sendPrivateMessage(player, target, "privatemessage", component.asComponent()); sendPrivateMessage(player, target, "privatemessage", component);
ComponentLike spymessage = Utility.parseMiniMessage(Config.MESSAGESPY, placeholders); Component spymessage = Utility.parseMiniMessage(Config.MESSAGESPY, placeholders);
for (Player pl : Bukkit.getOnlinePlayers()) { for(Player pl : Bukkit.getOnlinePlayers()) {
if (pl.hasPermission(Config.SPYPERMISSION) && ChatUserManager.getChatUser(pl.getUniqueId()) if(pl.hasPermission(Config.SPYPERMISSION) && ChatUserManager.getChatUser(pl.getUniqueId()).isSpy() && !pl.equals(player) && !pl.getName().equalsIgnoreCase(target)) {
.isSpy() && !pl.equals(player) && !pl.getName().equalsIgnoreCase(target)) {
pl.sendMessage(spymessage); pl.sendMessage(spymessage);
} }
} }
} }
public void privateMessage(Player player, String target, String message) { public void privateMessage(Player player, String target, String message) {
Utility.getOrLoadUser(player.getUniqueId()) // ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
.thenAccept(user -> privateMessage(user, player, target, message)); // user.setReplyTarget(target);
}
public void privateMessage(User user, Player player, String target, String message) { Component messageComponent = parseMessageContent(player, message);
// ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
// user.setReplyTarget(target);
Component messageComponent = parseMessageContent(user, player, message);
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.component("message", messageComponent), Placeholder.component("message", messageComponent),
Placeholder.component("sendername", player.name()), Placeholder.component("sendername", player.name()),
@ -114,64 +84,56 @@ public class ChatHandler {
ModifiableString modifiableString = new ModifiableString(messageComponent); ModifiableString modifiableString = new ModifiableString(messageComponent);
// todo a better way for this // todo a better way for this
if (!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) { if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, "privatemessage")) {
GalaxyUtility.sendBlockedNotification("DM Language", GalaxyUtility.sendBlockedNotification("DM Language",
player, player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())), Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
target target);
);
return; // the message was blocked return; // the message was blocked
} }
messageComponent = modifiableString.component(); messageComponent = modifiableString.component();
// Component component = Utility.parseMiniMessage("<message>", placeholders) // Component component = Utility.parseMiniMessage("<message>", placeholders)
// .replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand())).build()); // .replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand())).build());
sendPrivateMessage(player, target, "privatemessage", messageComponent); sendPrivateMessage(player, target, "privatemessage", messageComponent);
ComponentLike spymessage = Utility.parseMiniMessage(Config.MESSAGESPY, placeholders); Component spymessage = Utility.parseMiniMessage(Config.MESSAGESPY, placeholders);
for (Player pl : Bukkit.getOnlinePlayers()) { for(Player pl : Bukkit.getOnlinePlayers()) {
if (pl.hasPermission(Config.SPYPERMISSION) && ChatUserManager.getChatUser(pl.getUniqueId()) if(pl.hasPermission(Config.SPYPERMISSION) && ChatUserManager.getChatUser(pl.getUniqueId()).isSpy() && !pl.equals(player) && !pl.getName().equalsIgnoreCase(target)) {
.isSpy() && !pl.equals(player) && !pl.getName().equalsIgnoreCase(target)) {
pl.sendMessage(spymessage); pl.sendMessage(spymessage);
} }
} }
} }
public void globalChat(Player player, String message) { public void globalChat(Player player, String message) {
Utility.getOrLoadUser(player.getUniqueId()) ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
.thenAccept(user -> globalChat(user, player, message)); if(!Utility.hasPermission(player.getUniqueId(), Config.GCPERMISSION)) {
}
public void globalChat(User user, Player player, String message) {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
if (!Utility.hasPermission(user, Config.GCPERMISSION)) {
player.sendMessage(GCNOTENABLED);// GC IS OFF INFORM THEM ABOUT THIS and cancel player.sendMessage(GCNOTENABLED);// GC IS OFF INFORM THEM ABOUT THIS and cancel
return; return;
} }
if (isMuted(user, player, message, "[GC Muted] ")) { if (isMuted(player, message, "[GC Muted] ")) {
return; return;
} }
long timeLeft = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - chatUser.getGcCooldown()); long timeLeft = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - user.getGcCooldown());
if (timeLeft <= Config.GCCOOLDOWN && !player.hasPermission("chat.globalchat.cooldownbypass")) { // player is on cooldown and should wait x seconds if(timeLeft <= Config.GCCOOLDOWN && !player.hasPermission("chat.globalchat.cooldownbypass")) { // player is on cooldown and should wait x seconds
player.sendRichMessage(Config.GCONCOOLDOWN, player.sendMessage(Utility.parseMiniMessage(Config.GCONCOOLDOWN, Placeholder.parsed("cooldown", Config.GCCOOLDOWN-timeLeft+"")));
Placeholder.parsed("cooldown", Config.GCCOOLDOWN - timeLeft + "")
);
return; return;
} }
ComponentLike senderName = chatUser.getDisplayName(); Component senderName = user.getDisplayName();
ComponentLike prefix = chatUser.getPrefix(); Component prefix = user.getPrefix();
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName), Placeholder.component("sender", senderName),
Placeholder.component("prefix", prefix), Placeholder.component("prefix", prefix),
Placeholder.component("message", parseMessageContent(user, player, message)), Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.parsed("server", ServerName.getServerName()) Placeholder.parsed("server", Bukkit.getServerName())
); );
Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders).asComponent(); Component component = Utility.parseMiniMessage(Config.GCFORMAT, placeholders);
ModifiableString modifiableString = new ModifiableString(component); ModifiableString modifiableString = new ModifiableString(component);
// todo a better way for this // todo a better way for this
@ -179,76 +141,49 @@ public class ChatHandler {
GalaxyUtility.sendBlockedNotification("GC Language", GalaxyUtility.sendBlockedNotification("GC Language",
player, player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())), Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
"" "");
);
return; // the message was blocked return; // the message was blocked
} }
component = modifiableString.component(); component = modifiableString.component();
chatLogHandler.addChatLog(player.getUniqueId(), user.setGcCooldown(System.currentTimeMillis());
Bukkit.getServerName(),
message,
ChatLogType.GLOBAL,
null,
null,
component,
false
);
chatUser.setGcCooldown(System.currentTimeMillis());
sendPluginMessage(player, "globalchat", component); sendPluginMessage(player, "globalchat", component);
} }
public void chatChannel(User user, OfflinePlayer offlinePlayer, CustomChannel channel, String message) { public void chatChannel(Player player, CustomChannel channel, String message) {
if (!Utility.hasPermission(user, channel.getPermission())) { if (!player.hasPermission(channel.getPermission())) {
if (offlinePlayer.isOnline()) { player.sendMessage(Utility.parseMiniMessage("<red>You don't have permission to use this channel.</red>"));
Player onlinePlayer = offlinePlayer.getPlayer();
if (onlinePlayer == null) {
log.error("Player is online but getPlayer() returned null");
return;
}
onlinePlayer.sendRichMessage("<red>You don't have permission to use this channel.</red>");
}
return; return;
} }
if (isMuted(user, offlinePlayer, message, "[" + channel.getChannelName() + " Muted] ")) { if (isMuted(player, message, "[" + channel.getChannelName() + " Muted] ")) return;
ALogger.info("Refusing to send message by muted user");
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(offlinePlayer.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
ComponentLike senderName = chatUser.getDisplayName(); Component senderName = user.getDisplayName();
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName), Placeholder.component("sender", senderName),
Placeholder.component("message", parseMessageContent(user, offlinePlayer, message)), Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.parsed("server", ServerName.getServerName()), Placeholder.parsed("server", Bukkit.getServerName()),
Placeholder.parsed("channel", channel.getChannelName()) Placeholder.parsed("channel", channel.getChannelName())
); );
Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders).asComponent(); Component component = Utility.parseMiniMessage(channel.getFormat(), placeholders);
ModifiableString modifiableString = new ModifiableString(component); ModifiableString modifiableString = new ModifiableString(component);
if (!RegexManager.filterText(offlinePlayer.getName(), if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, channel.getChannelName())) {
offlinePlayer.getUniqueId(),
modifiableString,
channel.getChannelName()
)) {
GalaxyUtility.sendBlockedNotification(channel.getChannelName() + " Language", GalaxyUtility.sendBlockedNotification(channel.getChannelName() + " Language",
offlinePlayer, player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())), Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
"" "");
); return; // the message was blocked
ALogger.info("Refusing to send blocked chat message");
return;
} }
component = modifiableString.component(); component = modifiableString.component();
if (channel.isProxy()) { if (channel.isProxy()) {
sendChatChannelMessage(offlinePlayer, channel.getChannelName(), "chatchannel", component, message); sendChatChannelMessage(player, channel.getChannelName(), "chatchannel", component);
} else { } else {
sendChatChannelMessage(channel, offlinePlayer.getUniqueId(), component, message); sendChatChannelMessage(channel, player.getUniqueId(), component);
} }
} }
@ -262,118 +197,52 @@ public class ChatHandler {
)); ));
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray()); player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
// if (isMuted(player, message, "[" + party.getPartyName() + " Muted] ")) return;
// // if (isMuted(player, message, "[" + party.getPartyName() + " Muted] ")) return;
// ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); //
// Component senderName = user.getDisplayName(); // ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
// // Component senderName = user.getDisplayName();
// String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message); //
// if(updatedMessage == null) { // String updatedMessage = RegexManager.replaceText(player.getName(), player.getUniqueId(), message);
// GalaxyUtility.sendBlockedNotification("Party Language", player, message, ""); // if(updatedMessage == null) {
// return; // the message was blocked // GalaxyUtility.sendBlockedNotification("Party Language", player, message, "");
// } // return; // the message was blocked
// // }
// if(!player.hasPermission("chat.format")) { //
// updatedMessage = Utility.stripTokens(updatedMessage); // if(!player.hasPermission("chat.format")) {
// } // updatedMessage = Utility.stripTokens(updatedMessage);
// // }
// if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replaceFirst("[i]", "<item>"); //
// // if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replaceFirst("[i]", "<item>");
// updatedMessage = Utility.formatText(updatedMessage); //
// // updatedMessage = Utility.formatText(updatedMessage);
// List<Placeholder> Placeholders = new ArrayList<>(List.of( //
// Placeholder.miniMessage("sender", senderName), // List<Placeholder> Placeholders = new ArrayList<>(List.of(
// Placeholder.miniMessage("sendername", senderName), // Placeholder.miniMessage("sender", senderName),
// Placeholder.miniMessage("partyname", party.getPartyName()), // Placeholder.miniMessage("sendername", senderName),
// Placeholder.miniMessage("message", updatedMessage), // Placeholder.miniMessage("partyname", party.getPartyName()),
// Placeholder.miniMessage("server", Bukkit.getServerName()), // Placeholder.miniMessage("message", updatedMessage),
// Placeholder.miniMessage("[i]", itemComponent(player.getInventory().getItemInMainHand())))); // Placeholder.miniMessage("server", Bukkit.getServerName()),
// // Placeholder.miniMessage("[i]", itemComponent(player.getInventory().getItemInMainHand()))));
// Component component = Utility.parseMiniMessage(Config.PARTY_FORMAT, Placeholders); //
//// sendPartyMessage(player, party.getPartyId(), component); // Component component = Utility.parseMiniMessage(Config.PARTY_FORMAT, Placeholders);
// //// sendPartyMessage(player, party.getPartyId(), component);
// Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, Placeholders); //
// for(Player pl : Bukkit.getOnlinePlayers()) { // Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, Placeholders);
// if(pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) { // for(Player pl : Bukkit.getOnlinePlayers()) {
// pl.sendMessage(spyMessage); // if(pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) {
// } // pl.sendMessage(spyMessage);
// } // }
// }
} }
private void sendChatChannelMessage(CustomChannel chatChannel, UUID uuid, ComponentLike component, String message) { private void sendChatChannelMessage(CustomChannel chatChannel, UUID uuid, Component component) {
Player player = Bukkit.getPlayer(uuid); if (!chatChannel.getServers().contains(Bukkit.getServerName())) return;
if (player == null) {
ALogger.warn("Failed to send chat message from non existent player");
return;
}
if (!chatChannel.getServers().contains(ServerName.getServerName())) {
player.sendRichMessage("<red>Unable to send messages to <channel> in this server.</red>",
Placeholder.parsed("channel", chatChannel.getChannelName())
);
ALogger.info(String.format("Not sending chat message due to [%s] not being in this channels config",
ServerName.getServerName()
));
return;
}
Stream<? extends Player> stream = Bukkit.getServer().getOnlinePlayers().stream()
.filter(p -> p.hasPermission(chatChannel.getPermission()));
if (!player.hasPermission("chat.ignorebypass")) {
stream = stream.filter(receiver -> !ChatUserManager.getChatUser(receiver.getUniqueId())
.getIgnoredPlayers()
.contains(uuid)
|| receiver.hasPermission("chat.ignorebypass"));
}
if (chatChannel.isLocal()) {
Location location = player.getLocation();
stream = stream.filter(receiver -> {
Player receiverPlayer = Bukkit.getPlayer(receiver.getUniqueId());
if (receiverPlayer == null) {
return false;
}
if (!location.getWorld().getUID().equals(receiverPlayer.getLocation().getWorld().getUID())) {
return false;
}
return !(receiverPlayer.getLocation().distance(location) > Config.LOCAL_DISTANCE);
});
}
List<? extends Player> recipientPlayers = stream.toList();
recipientPlayers.forEach(p -> p.sendMessage(component));
chatLogHandler.addChatLog(player.getUniqueId(),
Bukkit.getServerName(),
message,
ChatLogType.CUSTOM,
chatChannel.getChannelName(),
getLocationIfLocal(chatChannel, player),
component.asComponent(),
false
);
List<UUID> recipientUUIDs = recipientPlayers.stream().map(Entity::getUniqueId).toList();
Bukkit.getServer().getOnlinePlayers().stream() Bukkit.getServer().getOnlinePlayers().stream()
.filter(onlinePlayer -> onlinePlayer.hasPermission(Config.SPYPERMISSION)) .filter(p -> p.hasPermission(chatChannel.getPermission()))
.filter(onlinePlayer -> !recipientUUIDs.contains(onlinePlayer.getUniqueId())) .filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
.forEach(onlinePlayer -> onlinePlayer.sendRichMessage(Config.CHANNEL_SPY, .forEach(p -> p.sendMessage(component));
Placeholder.component("sender", player.name()),
Placeholder.parsed("channel", chatChannel.getChannelName()),
Placeholder.parsed("message", message)
));
}
private static @Nullable String getLocationIfLocal(CustomChannel chatChannel, Player player) {
if (!chatChannel.isLocal()) {
return null;
}
String format = String.format("%s;%s",
player.getLocation().getBlockX(),
player.getLocation().getBlockZ()
);
if (format.length() > 36) {
return null;
}
return format;
} }
private void sendPluginMessage(Player player, String channel, Component component) { private void sendPluginMessage(Player player, String channel, Component component) {
@ -393,39 +262,23 @@ public class ChatHandler {
player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray()); player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
} }
public void sendChatChannelMessage(OfflinePlayer player, String chatChannelName, String channel, Component component, String ignored) { public void sendChatChannelMessage(Player player, String chatChannelName, String channel, Component component) {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(channel); out.writeUTF(channel);
out.writeUTF(chatChannelName); out.writeUTF(chatChannelName);
out.writeUTF(player.getUniqueId().toString()); out.writeUTF(player.getUniqueId().toString());
out.writeUTF(GsonComponentSerializer.gson().serialize(component)); out.writeUTF(GsonComponentSerializer.gson().serialize(component));
Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers(); player.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
if (player.isOnline() && player.getPlayer() != null) {
player.getPlayer().sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray());
} else {
//TODO [Stijn] [2026-08-09]: Validate that this works
onlinePlayers.stream().findFirst().ifPresent(p ->
p.sendPluginMessage(plugin, Config.MESSAGECHANNEL, out.toByteArray()));
}
} }
// Start - move these to util // Start - move these to util
private boolean isMuted(User user, OfflinePlayer offlinePlayer, String message, String prefix) { private boolean isMuted(Player player, String message, String prefix) {
ChatUser chatUser = ChatUserManager.getChatUser(offlinePlayer.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
if (chatUser == null) { if (user == null) return false;
return false; if (user.isMuted() || (ChatPlugin.getInstance().serverMuted() && !player.hasPermission("chat.bypass-server-muted"))) {
} // if (Database.get().isPlayerMuted(player.getUniqueId(), null) || (ChatPlugin.getInstance().serverMuted() && !player.hasPermission("chat.bypass-server-muted"))) {
if (chatUser.isMuted() || (ChatPlugin.getInstance() GalaxyUtility.sendBlockedNotification(prefix, player, Utility.parseMiniMessage(Utility.stripTokens(message)), "");
.serverMuted() && !Utility.hasPermission(user,
"chat.bypass-server-muted"
))) {
// if (Database.get().isPlayerMuted(player.getUniqueId(), null) || (ChatPlugin.getInstance().serverMuted() && !player.hasPermission("chat.bypass-server-muted"))) {
GalaxyUtility.sendBlockedNotification(prefix,
offlinePlayer,
Utility.parseMiniMessage(Utility.stripTokens(message)),
""
);
return true; return true;
} }
return false; return false;
@ -433,12 +286,11 @@ public class ChatHandler {
public static Component itemComponent(ItemStack item) { public static Component itemComponent(ItemStack item) {
Component component = Component.text("[i]", NamedTextColor.AQUA); Component component = Component.text("[i]", NamedTextColor.AQUA);
if (item.getType().equals(Material.AIR)) { if(item.getType().equals(Material.AIR))
return component.color(NamedTextColor.WHITE); return component.color(NamedTextColor.WHITE);
}
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
if (dname) { if(dname) {
component = component.append(Objects.requireNonNull(item.getItemMeta().displayName())); component = component.append(item.getItemMeta().displayName());
} else { } else {
component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE)); component = component.append(Component.text(materialToName(item.getType()), NamedTextColor.WHITE));
} }
@ -460,12 +312,10 @@ public class ChatHandler {
int loc = sb.lastIndexOf(split); int loc = sb.lastIndexOf(split);
char charLoc = sb.charAt(loc); char charLoc = sb.charAt(loc);
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) { split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
sb.setCharAt(loc, Character.toUpperCase(charLoc)); sb.setCharAt(loc, Character.toUpperCase(charLoc));
} if (pos != splits.length - 1)
if (pos != splits.length - 1) {
sb.append(' '); sb.append(' ');
}
++pos; ++pos;
} }
@ -473,18 +323,18 @@ public class ChatHandler {
} }
// end - move these to util // end - move these to util
private Component parseMessageContent(User user, OfflinePlayer offlinePlayer, String rawMessage) { private Component parseMessageContent(Player player, String rawMessage) {
TagResolver.Builder tagResolver = TagResolver.builder(); TagResolver.Builder tagResolver = TagResolver.builder();
Utility.formattingPerms.forEach((perm, pair) -> { Utility.formattingPerms.forEach((perm, pair) -> {
if (Utility.hasPermission(user, perm)) { if (player.hasPermission(perm)) {
tagResolver.resolver(pair.getX()); tagResolver.resolver(pair.getX());
} }
}); });
MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build(); MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build();
Component component = miniMessage.deserialize(rawMessage); Component component = miniMessage.deserialize(rawMessage);
for (ChatFilter chatFilter : RegexManager.getEmoteFilters()) { for(ChatFilter chatFilter : RegexManager.getEmoteFilters()) {
component = component.replaceText( component = component.replaceText(
TextReplacementConfig.builder() TextReplacementConfig.builder()
.times(Config.EMOTELIMIT) .times(Config.EMOTELIMIT)
@ -492,16 +342,13 @@ public class ChatHandler {
.replacement(chatFilter.getReplacement()).build()); .replacement(chatFilter.getReplacement()).build());
} }
if (offlinePlayer.isOnline() && offlinePlayer.getPlayer() != null) { component = component
component = component .replaceText(
.replaceText( TextReplacementConfig.builder()
TextReplacementConfig.builder() .once()
.once() .matchLiteral("[i]")
.matchLiteral("[i]") .replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand()))
.replacement(ChatHandler.itemComponent(offlinePlayer.getPlayer().getInventory() .build());
.getItemInMainHand()))
.build());
}
return component; return component;

View File

@ -1,67 +1,66 @@
package com.alttd.chat.listeners; package com.alttd.chat.listeners;
import com.alttd.chat.chat_web.ChatMessageSender; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.handler.ChatHandler; import com.alttd.chat.handler.ChatHandler;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.RegexManager; import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatFilter; import com.alttd.chat.objects.*;
import com.alttd.chat.objects.Toggleable; import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import io.papermc.paper.event.player.AsyncChatCommandDecorateEvent; import io.papermc.paper.event.player.AsyncChatCommandDecorateEvent;
import io.papermc.paper.event.player.AsyncChatDecorateEvent; import io.papermc.paper.event.player.AsyncChatDecorateEvent;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.time.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public class ChatListener implements Listener { public class ChatListener implements Listener {
private final PlainTextComponentSerializer plainTextComponentSerializer = PlainTextComponentSerializer.plainText(); private final PlainTextComponentSerializer plainTextComponentSerializer = PlainTextComponentSerializer.plainText();
private final ChatMessageSender chatMessageSender;
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onChatCommandDecorate(AsyncChatCommandDecorateEvent event) { public void onChatCommandDecorate(AsyncChatCommandDecorateEvent event) {
if (event.player() == null) { if (event.player() == null) return;
return;
}
Component formatComponent = Component.text("%message%"); Component formatComponent = Component.text("%message%");
ComponentLike message = parseMessageContent(event.player(), Component message = parseMessageContent(event.player(), plainTextComponentSerializer.serialize(event.originalMessage()));
plainTextComponentSerializer.serialize(event.originalMessage())
);
event.result(formatComponent.replaceText(TextReplacementConfig.builder() event.result(formatComponent.replaceText(TextReplacementConfig.builder().match("%message%").replacement(message).build()));
.match("%message%")
.replacement(message)
.build()));
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onChatDecorate(AsyncChatDecorateEvent event) { public void onChatDecorate(AsyncChatDecorateEvent event) {
if (event.player() == null) { if (event.player() == null) return;
return;
}
Component formatComponent = Component.text("%message%"); Component formatComponent = Component.text("%message%");
ComponentLike message = parseMessageContent(event.player(), Component message = parseMessageContent(event.player(), plainTextComponentSerializer.serialize(event.originalMessage()));
plainTextComponentSerializer.serialize(event.originalMessage())
);
event.result(formatComponent.replaceText(TextReplacementConfig.builder() event.result(formatComponent.replaceText(TextReplacementConfig.builder().match("%message%").replacement(message).build()));
.match("%message%")
.replacement(message)
.build()));
} }
private final Component mention = MiniMessage.miniMessage().deserialize(Config.MENTIONPLAYERTAG);
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerChat(AsyncChatEvent event) { public void onPlayerChat(AsyncChatEvent event) {
event.setCancelled(true); //Always cancel the event because we do not want to deal with Microsoft's stupid bans event.setCancelled(true); //Always cancel the event because we do not want to deal with Microsoft's stupid bans
@ -70,10 +69,116 @@ public class ChatListener implements Listener {
toggleable.sendMessage(event.getPlayer(), event.message()); toggleable.sendMessage(event.getPlayer(), event.message());
return; return;
} }
chatMessageSender.sendMessage(event.getPlayer(), event.message(), false); if (ChatPlugin.getInstance().serverMuted() && !event.getPlayer().hasPermission("chat.bypass-server-muted")) {
Player player = event.getPlayer();
GalaxyUtility.sendBlockedNotification("Chat Muted", player, event.message(), "");
return;
}
Player player = event.getPlayer();
Set<Player> receivers = event.viewers().stream().filter(audience -> audience instanceof Player)
.map(audience -> (Player) audience)
.filter(receiver -> !ChatUserManager.getChatUser(receiver.getUniqueId()).getIgnoredPlayers().contains(player.getUniqueId()))
.collect(Collectors.toSet());
Component input = event.message().colorIfAbsent(NamedTextColor.WHITE);
ModifiableString modifiableString = new ModifiableString(input);
// todo a better way for this
if(!RegexManager.filterText(player.getName(), player.getUniqueId(), modifiableString, true, "chat", filterType -> {
if (!filterType.equals(FilterType.PUNISH)) {
ALogger.warn("Received another FilterType than punish when filtering chat and executing a filter action");
return;
}
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("punish");
out.writeUTF(player.getName());
out.writeUTF(player.getUniqueId().toString());
out.writeUTF(modifiableString.string());
player.sendPluginMessage(ChatPlugin.getInstance(), Config.MESSAGECHANNEL, out.toByteArray());
})) {
event.setCancelled(true);
GalaxyUtility.sendBlockedNotification("Language", player,
modifiableString.component(),
"");
return; // the message was blocked
}
Set<Player> playersToPing = new HashSet<>();
pingPlayers(playersToPing, modifiableString, player);
LocalDate now = LocalDate.now();
if (now.getMonth().equals(Month.APRIL) && now.getDayOfMonth() == 1) {
if (modifiableString.string().startsWith(Config.APRIL_FOOLS_RESET + " ")) {
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
} else {
modifiableString.reverse();
}
}
input = render(player, modifiableString.component());
for (Player receiver : receivers) {
receiver.sendMessage(input);
}
for (Player pingPlayer : playersToPing) {
pingPlayer.playSound(pingPlayer.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1);
}
ALogger.info(PlainTextComponentSerializer.plainText().serialize(input));
} }
private ComponentLike parseMessageContent(Player player, String rawMessage) { private void pingPlayers(Set<Player> playersToPing, ModifiableString modifiableString, Player player) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
String name = onlinePlayer.getName();
String nickName = PlainTextComponentSerializer.plainText().serialize(onlinePlayer.displayName());
Pattern namePattern = Pattern.compile("\\b(?<!\\\\)" + name + "\\b", Pattern.CASE_INSENSITIVE);
// Pattern escapedNamePattern = Pattern.compile("\\b\\\\" + name + "\\b", Pattern.CASE_INSENSITIVE);
Pattern nickPattern = Pattern.compile("\\b(?<!\\\\)" + nickName + "\\b", Pattern.CASE_INSENSITIVE);
// Pattern escapedNickPattern = Pattern.compile("\\b\\\\" + nickName + "\\b", Pattern.CASE_INSENSITIVE);
if (namePattern.matcher(modifiableString.string()).find()) {
modifiableString.replace(TextReplacementConfig.builder()
.once()
.match(namePattern)
.replacement(mention.append(onlinePlayer.displayName()))
.build());
//TODO replace all instances of \name with just name but using the match result so the capitalization doesn't change
// modifiableString.replace(TextReplacementConfig.builder()
// .once()
// .match(escapedNamePattern)
// .replacement((a, b) -> {
// String substring = a.group().substring(1);
// return ;
// });
if (!ChatUserManager.getChatUser(onlinePlayer.getUniqueId()).getIgnoredPlayers().contains(player.getUniqueId()))
playersToPing.add(onlinePlayer);
} else if (nickPattern.matcher(modifiableString.string()).find()) {
modifiableString.replace(TextReplacementConfig.builder()
.once()
.match(nickPattern)
.replacement(mention.append(onlinePlayer.displayName()))
.build());
if (!ChatUserManager.getChatUser(onlinePlayer.getUniqueId()).getIgnoredPlayers().contains(player.getUniqueId()))
playersToPing.add(onlinePlayer);
}
}
}
public @NotNull Component render(@NotNull Player player, @NotNull Component message) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", user.getDisplayName()),
Placeholder.parsed("sendername", player.getName()),
Placeholder.component("prefix", user.getPrefix()),
Placeholder.component("prefixall", user.getPrefixAll()),
Placeholder.component("staffprefix", user.getStaffPrefix()),
Placeholder.component("message", message)
);
return Utility.parseMiniMessage(Config.CHATFORMAT, placeholders);
}
private Component parseMessageContent(Player player, String rawMessage) {
TagResolver.Builder tagResolver = TagResolver.builder(); TagResolver.Builder tagResolver = TagResolver.builder();
Utility.formattingPerms.forEach((perm, pair) -> { Utility.formattingPerms.forEach((perm, pair) -> {
@ -83,8 +188,8 @@ public class ChatListener implements Listener {
}); });
MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build(); MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build();
Component component = miniMessage.deserialize(Utility.formatText(rawMessage)); Component component = miniMessage.deserialize(rawMessage);
for (ChatFilter chatFilter : RegexManager.getEmoteFilters()) { for(ChatFilter chatFilter : RegexManager.getEmoteFilters()) {
component = component.replaceText( component = component.replaceText(
TextReplacementConfig.builder() TextReplacementConfig.builder()
.times(Config.EMOTELIMIT) .times(Config.EMOTELIMIT)
@ -94,14 +199,14 @@ public class ChatListener implements Listener {
component = component component = component
.replaceText( .replaceText(
TextReplacementConfig.builder() TextReplacementConfig.builder()
.once() .once()
.matchLiteral("[i]") .matchLiteral("[i]")
.replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand())) .replacement(ChatHandler.itemComponent(player.getInventory().getItemInMainHand()))
.build()); .build());
return component; return component;
} }
} }

View File

@ -8,42 +8,26 @@ import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.ModifiableString; import com.alttd.chat.objects.ModifiableString;
import com.alttd.chat.objects.Toggleable; import com.alttd.chat.objects.Toggleable;
import com.alttd.chat.objects.chat_log.WebHandler;
import com.alttd.chat.util.GalaxyUtility; import com.alttd.chat.util.GalaxyUtility;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
private final ServerConfig serverConfig; private final ServerConfig serverConfig;
private final WebHandler webHandler;
public PlayerListener(WebHandler webHandler, ServerConfig serverConfig) { public PlayerListener(ServerConfig serverConfig) {
this.webHandler = webHandler;
this.serverConfig = serverConfig; this.serverConfig = serverConfig;
} }
@ -54,23 +38,19 @@ public class PlayerListener implements Listener {
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
Toggleable.disableToggles(uuid); Toggleable.disableToggles(uuid);
if (serverConfig.FIRST_JOIN_MESSAGES && (!player.hasPlayedBefore() || System.currentTimeMillis() - player.getFirstPlayed() < TimeUnit.SECONDS.toMillis( if (serverConfig.FIRST_JOIN_MESSAGES && System.currentTimeMillis() - player.getFirstPlayed() < TimeUnit.SECONDS.toMillis(10)) {
10))) { player.getServer().sendMessage(MiniMessage.miniMessage().deserialize(Config.FIRST_JOIN, Placeholder.parsed("player", player.getName())));
Bukkit.broadcast(MiniMessage.miniMessage()
.deserialize(Config.FIRST_JOIN, Placeholder.parsed("player", player.getName())));
} }
ChatUser user = ChatUserManager.getChatUser(uuid); ChatUser user = ChatUserManager.getChatUser(uuid);
if (user != null) { if(user != null) return;
updateServerState(event.getPlayer(), user, true);
return;
}
// user failed to load - create a new one // user failed to load - create a new one
ChatUser chatUser = new ChatUser(uuid, -1, null); ChatUser chatUser = new ChatUser(uuid, -1, null);
ChatUserManager.addUser(chatUser); ChatUserManager.addUser(chatUser);
Queries.saveUser(chatUser); Queries.saveUser(chatUser);
updateServerState(event.getPlayer(), chatUser, true);
//TODO load player on other servers with plugin message? //TODO load player on other servers with plugin message?
} }
@ -79,26 +59,8 @@ public class PlayerListener implements Listener {
UUID uuid = event.getPlayer().getUniqueId(); UUID uuid = event.getPlayer().getUniqueId();
ChatUser user = ChatUserManager.getChatUser(uuid); ChatUser user = ChatUserManager.getChatUser(uuid);
ChatUserManager.removeUser(user); ChatUserManager.removeUser(user);
updateServerState(event.getPlayer(), user, false);
} }
private void updateServerState(Player triggerPlayer, ChatUser chatUser, boolean add) {
ArrayList<Player> playerList = new ArrayList<>(Bukkit.getOnlinePlayers());
if (add) {
if (playerList.stream()
.filter(player -> player.getUniqueId().equals(triggerPlayer.getUniqueId()))
.findFirst()
.isEmpty()) {
playerList.add(triggerPlayer);
}
} else {
playerList.removeIf(player -> player.getUniqueId().equals(triggerPlayer.getUniqueId()));
}
String name = Bukkit.getServer().getName();
webHandler.updateServerState(name,
playerList.stream().map(player -> WebPlayerMapper.fromPlayer(player, chatUser)).toList()
);
}
@EventHandler(ignoreCancelled = true) // untested @EventHandler(ignoreCancelled = true) // untested
public void onSignChangeE(SignChangeEvent event) { public void onSignChangeE(SignChangeEvent event) {
@ -114,8 +76,7 @@ public class PlayerListener implements Listener {
GalaxyUtility.sendBlockedNotification("Sign Language", GalaxyUtility.sendBlockedNotification("Sign Language",
player, player,
Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())), Utility.parseMiniMessage(Utility.parseColors(modifiableString.string())),
"" "");
);
} }
component = modifiableString.component() == null ? Component.empty() : modifiableString.component(); component = modifiableString.component() == null ? Component.empty() : modifiableString.component();
@ -125,46 +86,4 @@ public class PlayerListener implements Listener {
} }
} }
private final HashMap<UUID, Stack<Instant>> sendPlayerDeaths = new HashMap<>();
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerDeath(@NotNull PlayerDeathEvent event) {
UUID uuid = event.getPlayer().getUniqueId();
Stack<Instant> playerDeathsStack = sendPlayerDeaths.computeIfAbsent(uuid, key -> new Stack<>());
Instant cutOff = Instant.now().minus(Config.DEATH_MESSAGES_LIMIT_PERIOD_MINUTES, ChronoUnit.MINUTES);
while (!playerDeathsStack.isEmpty() && playerDeathsStack.peek().isBefore(cutOff)) {
playerDeathsStack.pop();
}
if (playerDeathsStack.size() > Config.DEATH_MESSAGES_MAX_PER_PERIOD || serverConfig.MUTED) {
event.deathMessage(Component.empty());
return;
}
Component component = event.deathMessage();
playerDeathsStack.push(Instant.now());
if (component == null) {
return;
}
TextReplacementConfig playerReplacement = TextReplacementConfig.builder()
.match(event.getPlayer().getName())
.replacement(event.getPlayer().displayName())
.build();
component = component.replaceText(playerReplacement);
Player killer = event.getPlayer().getKiller();
if (killer != null) {
TextReplacementConfig killerReplacement = TextReplacementConfig.builder()
.match(killer.getName())
.replacement(killer.displayName())
.build();
component = component.replaceText(killerReplacement);
}
component = MiniMessage.miniMessage()
.deserialize("<dark_red>[</dark_red><red>☠</red><dark_red>]</dark_red> ")
.append(component);
component = component.style(Style.style(TextColor.color(255, 155, 48), TextDecoration.ITALIC));
event.deathMessage(component);
}
} }

View File

@ -5,142 +5,106 @@ import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager; import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Party; import com.alttd.chat.objects.Party;
import com.alttd.chat.objects.PartyUser; import com.alttd.chat.objects.PartyUser;
import com.alttd.chat.objects.channels.Channel; import com.alttd.chat.objects.channels.Channel;
import com.alttd.chat.objects.channels.CustomChannel; import com.alttd.chat.objects.channels.CustomChannel;
import com.alttd.chat.objects.chat_log.ChatLogHandler; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
import com.alttd.chat.util.ALogger; import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.ServerName;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener; import org.bukkit.plugin.messaging.PluginMessageListener;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
public class PluginMessage implements PluginMessageListener { public class PluginMessage implements PluginMessageListener {
private final ChatLogHandler chatLogHandler;
public PluginMessage(ChatLogHandler chatLogHandler) {
this.chatLogHandler = chatLogHandler;
}
@Override @Override
public void onPluginMessageReceived(String channel, @NotNull Player ignored, byte[] bytes) { public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
if (!channel.equals(Config.MESSAGECHANNEL)) { if (!channel.equals(Config.MESSAGECHANNEL)) {
return; return;
} }
ByteArrayDataInput in = ByteStreams.newDataInput(bytes); ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
String subChannel = in.readUTF(); String subChannel = in.readUTF();
switch (subChannel) { switch (subChannel) {
case "privatemessagein" -> { case "privatemessagein": {
UUID uuid = UUID.fromString(in.readUTF()); UUID uuid = UUID.fromString(in.readUTF());
String target = in.readUTF(); String target = in.readUTF();
Player player = Bukkit.getPlayer(uuid); Player p = Bukkit.getPlayer(uuid);
String message = in.readUTF(); String message = in.readUTF();
UUID targetuuid = UUID.fromString(in.readUTF()); UUID targetuuid = UUID.fromString(in.readUTF());
if (player == null) { if (p != null) {
break; ChatUser chatUser = ChatUserManager.getChatUser(uuid);
} if (!chatUser.getIgnoredPlayers().contains(targetuuid)) {
ChatUser chatUser = ChatUserManager.getChatUser(uuid); p.sendMessage(GsonComponentSerializer.gson().deserialize(message));
if (isTargetNotIgnored(chatUser, targetuuid)) { p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 1); // todo load this from config
Component component = GsonComponentSerializer.gson().deserialize(message); ChatUser user = ChatUserManager.getChatUser(uuid);
player.sendMessage(component); if (!user.getReplyContinueTarget().equalsIgnoreCase(target))
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, user.setReplyTarget(target);
1
); // todo load this from config
ChatUser user = ChatUserManager.getChatUser(uuid);
if (!user.getReplyContinueTarget().equalsIgnoreCase(target)) {
user.setReplyTarget(target);
} }
// Handled here since this is after the ignore check and is only send once
chatLogHandler.addChatLog(uuid,
ServerName.getServerName(),
PlainTextComponentSerializer.plainText().serialize(component),
ChatLogType.MSG,
null,
player.getUniqueId().toString(),
component,
false
);
} }
break;
} }
case "privatemessageout" -> { case "privatemessageout": {
UUID uuid = UUID.fromString(in.readUTF()); UUID uuid = UUID.fromString(in.readUTF());
String target = in.readUTF(); String target = in.readUTF();
Player player = Bukkit.getPlayer(uuid); Player p = Bukkit.getPlayer(uuid);
String message = in.readUTF(); String message = in.readUTF();
UUID targetuuid = UUID.fromString(in.readUTF()); UUID targetuuid = UUID.fromString(in.readUTF());
if (player == null) { if (p != null) {
break; ChatUser chatUser = ChatUserManager.getChatUser(uuid);
} if (!chatUser.getIgnoredPlayers().contains(targetuuid)) {
ChatUser chatUser = ChatUserManager.getChatUser(uuid); chatUser.setReplyTarget(target);
if (isTargetNotIgnored(chatUser, targetuuid)) { p.sendMessage(GsonComponentSerializer.gson().deserialize(message));
chatUser.setReplyTarget(target); // ChatUser user = ChatUserManager.getChatUser(uuid);
Component component = GsonComponentSerializer.gson().deserialize(message); // user.setReplyTarget(target);
player.sendMessage(component); }
// Handled here since this is after the ignore check and is only send once
chatLogHandler.addChatLog(uuid,
ServerName.getServerName(),
PlainTextComponentSerializer.plainText().serialize(component),
ChatLogType.MSG,
null,
player.getUniqueId().toString(),
component,
false
);
} }
break;
} }
case "globalchat" -> { case "globalchat": {
if (!ChatPlugin.getInstance().serverGlobalChatEnabled() || ChatPlugin.getInstance().serverMuted()) { if (!ChatPlugin.getInstance().serverGlobalChatEnabled() || ChatPlugin.getInstance().serverMuted()) break;
break;
}
UUID uuid = UUID.fromString(in.readUTF()); UUID uuid = UUID.fromString(in.readUTF());
String message = in.readUTF(); String message = in.readUTF();
Component component = GsonComponentSerializer.gson().deserialize(message);
Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission(Config.GCPERMISSION)).forEach(p -> { Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission(Config.GCPERMISSION)).forEach(p -> {
ChatUser chatUser = ChatUserManager.getChatUser(p.getUniqueId()); ChatUser chatUser = ChatUserManager.getChatUser(p.getUniqueId());
if (isTargetNotIgnored(chatUser, uuid)) { if (!chatUser.getIgnoredPlayers().contains(uuid)) {
p.sendMessage(component); p.sendMessage(GsonComponentSerializer.gson().deserialize(message));
} }
}); });
break;
} }
case "ignore" -> { case "ignore": {
ChatUser chatUser = ChatUserManager.getChatUser(UUID.fromString(in.readUTF())); ChatUser chatUser = ChatUserManager.getChatUser(UUID.fromString(in.readUTF()));
UUID targetUUID = UUID.fromString(in.readUTF()); UUID targetUUID = UUID.fromString(in.readUTF());
if (!chatUser.getIgnoredPlayers().contains(targetUUID)) { if(!chatUser.getIgnoredPlayers().contains(targetUUID)) {
chatUser.addIgnoredPlayers(targetUUID); chatUser.addIgnoredPlayers(targetUUID);
} }
break;
} }
case "unignore" -> { case "unignore": {
ChatUser chatUser = ChatUserManager.getChatUser(UUID.fromString(in.readUTF())); ChatUser chatUser = ChatUserManager.getChatUser(UUID.fromString(in.readUTF()));
chatUser.removeIgnoredPlayers(UUID.fromString(in.readUTF())); chatUser.removeIgnoredPlayers(UUID.fromString(in.readUTF()));
break;
} }
case "chatchannel" -> { case "chatchannel": {
if (ChatPlugin.getInstance().serverMuted()) { if (ChatPlugin.getInstance().serverMuted()) break;
break;
}
chatChannel(in); chatChannel(in);
//TODO [Stijn] [2026-07-19]: handle custom channels break;
} }
case "tmppartyupdate" -> { case "tmppartyupdate" : {
int id = Integer.parseInt(in.readUTF()); int id = Integer.parseInt(in.readUTF());
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
@ -148,8 +112,9 @@ public class PluginMessage implements PluginMessageListener {
Queries.loadPartyUsers(id); Queries.loadPartyUsers(id);
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
break;
} }
case "partylogin" -> { case "partylogin": {
int id = Integer.parseInt(in.readUTF()); int id = Integer.parseInt(in.readUTF());
Party party = PartyManager.getParty(id); Party party = PartyManager.getParty(id);
if (party == null) { if (party == null) {
@ -161,20 +126,19 @@ public class PluginMessage implements PluginMessageListener {
@Override @Override
public void run() { public void run() {
PartyUser user = party.getPartyUser(uuid); PartyUser user = party.getPartyUser(uuid);
if (user != null) { if(user != null) {
ComponentLike component = Utility.parseMiniMessage( Component component = Utility.parseMiniMessage("<dark_aqua>* " + user.getPlayerName() + " logged in to Altitude.");
"<dark_aqua>* " + user.getPlayerName() + " logged in to Altitude.");
Bukkit.getOnlinePlayers().stream() Bukkit.getOnlinePlayers().stream()
.filter(p -> party.getPartyUsersUuid().contains(p.getUniqueId())) .filter(p -> party.getPartyUsersUuid().contains(p.getUniqueId()))
.filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers() .filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
.contains(uuid))
.forEach(p -> p.sendMessage(component)); .forEach(p -> p.sendMessage(component));
} }
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
break;
} }
case "partylogout" -> { case "partylogout": {
int id = Integer.parseInt(in.readUTF()); int id = Integer.parseInt(in.readUTF());
Party party = PartyManager.getParty(id); Party party = PartyManager.getParty(id);
if (party == null) { if (party == null) {
@ -186,31 +150,30 @@ public class PluginMessage implements PluginMessageListener {
@Override @Override
public void run() { public void run() {
PartyUser user = party.getPartyUser(uuid); PartyUser user = party.getPartyUser(uuid);
if (user != null) { if(user != null) {
ComponentLike component = Utility.parseMiniMessage( Component component = Utility.parseMiniMessage("<dark_aqua>* " + user.getPlayerName() + " logged out of Altitude.");
"<dark_aqua>* " + user.getPlayerName() + " logged out of Altitude.");
Bukkit.getOnlinePlayers().stream() Bukkit.getOnlinePlayers().stream()
.filter(p -> party.getPartyUsersUuid().contains(p.getUniqueId())) .filter(p -> party.getPartyUsersUuid().contains(p.getUniqueId()))
.filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers() .filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
.contains(uuid))
.forEach(p -> p.sendMessage(component)); .forEach(p -> p.sendMessage(component));
} }
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
break;
} }
case "reloadconfig" -> ChatPlugin.getInstance().reloadConfig(); case "reloadconfig":
case "chatpunishments" -> { ChatPlugin.getInstance().ReloadConfig();
break;
case "chatpunishments":
UUID uuid = UUID.fromString(in.readUTF()); UUID uuid = UUID.fromString(in.readUTF());
boolean mute = in.readBoolean(); boolean mute = in.readBoolean();
ChatUser user = ChatUserManager.getChatUser(uuid); ChatUser user = ChatUserManager.getChatUser(uuid);
if (user == null) { if (user == null) return;
return;
}
user.setMuted(mute); user.setMuted(mute);
} break;
default -> { default:
} break;
} }
} }
@ -222,15 +185,15 @@ public class PluginMessage implements PluginMessageListener {
chatChannel = (CustomChannel) Channel.getChatChannel(in.readUTF()); chatChannel = (CustomChannel) Channel.getChatChannel(in.readUTF());
uuid = UUID.fromString(in.readUTF()); uuid = UUID.fromString(in.readUTF());
component = GsonComponentSerializer.gson().deserialize(in.readUTF()); component = GsonComponentSerializer.gson().deserialize(in.readUTF());
} catch (Exception e) { } catch (Exception e) { //Idk the exception for reading too far into in.readUTF()
ALogger.error("Failed to read ChatChannel message.", e); e.printStackTrace();
} }
if (chatChannel == null) { if (chatChannel == null) {
ALogger.warn("Received ChatChannel message for non existent channel."); ALogger.warn("Received ChatChannel message for non existent channel.");
return; return;
} }
if (!chatChannel.getServers().contains(ServerName.getServerName())) { if (!chatChannel.getServers().contains(Bukkit.getServerName())) {
ALogger.warn("Received ChatChannel message for the wrong server."); ALogger.warn("Received ChatChannel message for the wrong server.");
return; return;
} }
@ -247,22 +210,10 @@ public class PluginMessage implements PluginMessageListener {
public void run() { public void run() {
Bukkit.getOnlinePlayers().stream() Bukkit.getOnlinePlayers().stream()
.filter(p -> p.hasPermission(finalChatChannel.getPermission())) .filter(p -> p.hasPermission(finalChatChannel.getPermission()))
.filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers() .filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(finalUuid))
.contains(finalUuid))
.forEach(p -> p.sendMessage(finalComponent)); .forEach(p -> p.sendMessage(finalComponent));
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
} }
private boolean isTargetNotIgnored(ChatUser chatUser, UUID targetUUID) { }
if (!chatUser.getIgnoredPlayers().contains(targetUUID)) {
return true;
}
Player target = Bukkit.getPlayer(targetUUID);
if (target == null) {
return true;
}
return target.hasPermission("chat.ignorebypass");
}
}

View File

@ -1,27 +0,0 @@
package com.alttd.chat.listeners;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.plugin.Plugin;
public class ShutdownListener implements Listener {
private final ChatLogHandler chatLogHandler;
private final Plugin thisPlugin;
public ShutdownListener(ChatLogHandler chatLogHandler, Plugin thisPlugin) {
this.chatLogHandler = chatLogHandler;
this.thisPlugin = thisPlugin;
}
@EventHandler
public void onShutdown(PluginDisableEvent event) {
if (!event.getPlugin().getName().equals(thisPlugin.getName())){
return;
}
chatLogHandler.shutDown();
}
}

View File

@ -1,19 +0,0 @@
package com.alttd.chat.listeners;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.chat_log.mapper.server_state.WebPlayer;
import lombok.experimental.UtilityClass;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.entity.Player;
@UtilityClass
public class WebPlayerMapper {
public static WebPlayer fromPlayer(Player player, ChatUser chatUser) {
WebPlayer webPlayer = new WebPlayer();
webPlayer.setUuid(player.getUniqueId());
webPlayer.setName(player.getName());
webPlayer.setStyledName(GsonComponentSerializer.gson().serialize(chatUser.getDisplayName().asComponent()));
return webPlayer;
}
}

View File

@ -4,21 +4,100 @@ import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.objects.Nick; import com.alttd.chat.objects.Nick;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.awt.*;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
public class NickUtilities { public class NickUtilities
{
public static String stringRegen; public static String stringRegen;
public static String applyColor(String message) {
ChatColor hexColor1 = null;
ChatColor hexColor2;
StringBuilder stringBuilder = new StringBuilder();
message = ChatColor.translateAlternateColorCodes('&', message);
boolean startsWithColor = false;
boolean lastColorMatters = false;
if (message.matches(".*" + NickUtilities.stringRegen + ".*")) {
String[] split = message.split(NickUtilities.stringRegen);
ArrayList<String> list = new ArrayList<>();
int nextIndex = 0;
if (message.indexOf("}") <= 11) {
startsWithColor = true;
list.add(message.substring(0, message.indexOf("}") + 1));
}
for (String s : split) {
nextIndex += s.length();
int tmp = message.indexOf("}", nextIndex);
if (tmp < message.length() && tmp>=0) {
list.add(message.substring(nextIndex, tmp + 1));
nextIndex = tmp + 1;
}
}
int i;
boolean firstLoop = true;
if (startsWithColor) {
i = -1;
} else {
i = 0;
stringBuilder.append(split[i]);
}
for (String s : list) {
boolean lesser = s.contains("<");
boolean bigger = s.contains(">");
if (bigger && lesser) {
hexColor2 = ChatColor.of(s.substring(1, s.length() - 3));
} else if (bigger || lesser) {
hexColor2 = ChatColor.of(s.substring(1, s.length() - 2));
} else {
hexColor2 = ChatColor.of(s.substring(1, s.length() -1));
}
if (firstLoop) {
lastColorMatters = bigger;
hexColor1 = hexColor2;
firstLoop = false;
i++;
continue;
}
if (lesser && lastColorMatters) {
stringBuilder.append(hexGradient(hexColor1.getColor(), hexColor2.getColor(), split[i]));
} else {
stringBuilder.append(hexColor1).append(split[i]);
}
hexColor1 = hexColor2;
lastColorMatters = bigger;
i++;
}
if (split.length > i){
stringBuilder.append(hexColor1).append(split[i]);
}
}
return stringBuilder.length()==0 ? message : stringBuilder.toString();
}
public static String removeAllColors(String string) { public static String removeAllColors(String string) {
for (final String colorCodes : Config.NICK_ALLOWED_COLOR_CODESLIST) { for (final String colorCodes : Config.NICK_ALLOWED_COLOR_CODESLIST) {
@ -32,11 +111,34 @@ public class NickUtilities {
NickUtilities.stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}"; NickUtilities.stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}";
} }
public static String hexGradient(Color color1, Color color2, String text){
double r = color1.getRed();
double g = color1.getGreen();
double b = color1.getBlue();
double rDifference = (color1.getRed() - color2.getRed()) / ((double) text.length() - 1);
double gDifference = (color1.getGreen() - color2.getGreen()) / ((double) text.length() - 1);
double bDifference = (color1.getBlue() - color2.getBlue()) / ((double) text.length() - 1);
StringBuilder stringBuilder = new StringBuilder();
char[] chars = text.toCharArray();
for (int i = 0; i < text.length(); i++) {
if (i > 0) {
r = r - rDifference;
g = g - gDifference;
b = b - bDifference;
}
stringBuilder.append(ChatColor.of(new Color((int) r, (int) g, (int) b))).append(chars[i]);
}
return stringBuilder.toString();
}
public static void updateCache() { public static void updateCache() {
if (!Nicknames.getInstance().nickCacheUpdate.isEmpty()) { if (!Nicknames.getInstance().nickCacheUpdate.isEmpty()){
Nicknames.getInstance().nickCacheUpdate.forEach(uuid -> { Nicknames.getInstance().nickCacheUpdate.forEach(uuid ->{
Nick nick = Queries.getNick(uuid); Nick nick = Queries.getNick(uuid);
if (nick == null) { if (nick == null){
Nicknames.getInstance().NickCache.remove(uuid); Nicknames.getInstance().NickCache.remove(uuid);
} else { } else {
Nicknames.getInstance().NickCache.put(uuid, nick); Nicknames.getInstance().NickCache.put(uuid, nick);
@ -47,24 +149,24 @@ public class NickUtilities {
public static boolean validNick(Player sender, OfflinePlayer target, String nickName) { public static boolean validNick(Player sender, OfflinePlayer target, String nickName) {
if (!noBlockedCodes(nickName)) { if (!noBlockedCodes(nickName)) {
sender.sendRichMessage(Config.NICK_BLOCKED_COLOR_CODES); sender.sendMiniMessage(Config.NICK_BLOCKED_COLOR_CODES, null);
return false; return false;
} }
if (!Utility.checkNickBrightEnough(nickName)) { if (!Utility.checkNickBrightEnough(nickName)) {
sender.sendRichMessage("<red>At least one color must be brighter than 30 for each color</red>"); sender.sendMiniMessage("<red>At least one color must be brighter than 30 for each color</red>", null);
return false; return false;
} }
String cleanNick = NickUtilities.removeAllColors(nickName); String cleanNick = NickUtilities.removeAllColors(nickName);
if (cleanNick.length() < 3 || cleanNick.length() > 16) { if (cleanNick.length() < 3 || cleanNick.length() > 16) {
sender.sendRichMessage(Config.NICK_INVALID_LENGTH); sender.sendMiniMessage(Config.NICK_INVALID_LENGTH, null);
return false; return false;
} }
if (!cleanNick.matches("[a-zA-Z0-9_]*") || nickName.length() > 192) { //192 is if someone puts {#xxxxxx<>} in front of every letter if (!cleanNick.matches("[a-zA-Z0-9_]*") || nickName.length() > 192) { //192 is if someone puts {#xxxxxx<>} in front of every letter
sender.sendRichMessage(Config.NICK_INVALID_CHARACTERS); sender.sendMiniMessage(Config.NICK_INVALID_CHARACTERS, null);
return false; return false;
} }
@ -72,16 +174,16 @@ public class NickUtilities {
return true; return true;
} }
for (Nick nick : Nicknames.getInstance().NickCache.values()) { for (Nick nick : Nicknames.getInstance().NickCache.values()){
if (!nick.getUuid().equals(target.getUniqueId()) if (!nick.getUuid().equals(target.getUniqueId())
&& ((nick.getCurrentNickNoColor() != null && nick.getCurrentNickNoColor().equalsIgnoreCase(cleanNick)) && ((nick.getCurrentNickNoColor() != null && nick.getCurrentNickNoColor().equalsIgnoreCase(cleanNick))
|| (nick.getNewNickNoColor() != null && nick.getNewNickNoColor().equalsIgnoreCase(cleanNick)))) { || (nick.getNewNickNoColor() != null && nick.getNewNickNoColor().equalsIgnoreCase(cleanNick)))){
UUID uuid = nick.getUuid(); UUID uuid = nick.getUuid();
UUID uniqueId = target.getUniqueId(); UUID uniqueId = target.getUniqueId();
if (uniqueId.equals(uuid)) { if (uniqueId.equals(uuid)){
ChatPlugin.getInstance().getLogger().info(uuid + " " + uniqueId); ChatPlugin.getInstance().getLogger().info(uuid + " " + uniqueId);
} }
sender.sendRichMessage(Config.NICK_TAKEN); sender.sendMiniMessage(Config.NICK_TAKEN, null);
return false; return false;
} }
} }
@ -102,16 +204,16 @@ public class NickUtilities {
public static void bungeeMessageHandled(UUID uniqueId, Player player, String channel) { public static void bungeeMessageHandled(UUID uniqueId, Player player, String channel) {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
// out.writeUTF("Forward"); // So BungeeCord knows to forward it // out.writeUTF("Forward"); // So BungeeCord knows to forward it
// out.writeUTF("ALL"); // out.writeUTF("ALL");
out.writeUTF("NickName" + channel); // The channel name to check if this your data out.writeUTF("NickName" + channel); // The channel name to check if this your data
ByteArrayOutputStream msgbytes = new ByteArrayOutputStream(); ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
DataOutputStream msgout = new DataOutputStream(msgbytes); DataOutputStream msgout = new DataOutputStream(msgbytes);
try { try {
msgout.writeUTF(uniqueId.toString()); msgout.writeUTF(uniqueId.toString());
} catch (IOException exception) { } catch (IOException exception){
ALogger.error("Failed to write UUID to byte array", exception); exception.printStackTrace();
return; return;
} }
byte[] bytes = msgbytes.toByteArray(); byte[] bytes = msgbytes.toByteArray();

View File

@ -1,5 +1,7 @@
package com.alttd.chat.nicknames; package com.alttd.chat.nicknames;
import com.Zrips.CMI.CMI;
import com.Zrips.CMI.Containers.CMIUser;
import com.alttd.chat.ChatAPI; import com.alttd.chat.ChatAPI;
import com.alttd.chat.ChatPlugin; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
@ -8,10 +10,10 @@ import com.alttd.chat.events.NickEvent;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Nick; import com.alttd.chat.objects.Nick;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
@ -22,6 +24,7 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -45,158 +48,137 @@ public class Nicknames implements CommandExecutor, TabCompleter {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
if (!(sender instanceof Player player)) { if (sender instanceof Player player) {
if (args.length == 0) {
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.ALL)));
return true;
}
switch (args[0].toLowerCase()) {
case "set":
if (args.length == 2 && hasPermission(sender, "chat.command.nick.set")) {
handleNick(player, player, args[1]);
} else if (args.length == 3 && hasPermission(sender, "chat.command.nick.set.others")) {
OfflinePlayer offlinePlayer = sender.getServer().getOfflinePlayer(args[1]);
if (offlinePlayer.isOnline() || offlinePlayer.hasPlayedBefore()) {
handleNick(player, offlinePlayer, args[2]);
} else {
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.SET_OTHERS)));
}
} else if (args.length > 3) {
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.SET_SELF, HelpType.SET_OTHERS)));
}
break;
case "review":
if (args.length == 1 && hasPermission(sender, "chat.command.nick.review")) {
NicknamesGui nicknamesGui = new NicknamesGui();
ChatPlugin.getInstance().getServer().getPluginManager().registerEvents(nicknamesGui, ChatPlugin.getInstance());
nicknamesGui.openInventory(player);
} else {
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.REVIEW)));
}
break;
case "request":
if (args.length == 2 && hasPermission(sender, "chat.command.nick.request")) {
new BukkitRunnable() {
@Override
public void run() {
handleNickRequest(player, args[1]);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
} else {
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.REQUEST)));
}
break;
case "try":
if (args.length == 2 && hasPermission(sender, "chat.command.nick.try")) {
LuckPerms api = ChatAPI.get().getLuckPerms();
if (api != null) {
if (NickUtilities.validNick(player, player, args[1])) {
sender.sendMessage(Utility.parseMiniMessage(Config.NICK_TRYOUT,
Placeholder.component("prefix", Utility.applyColor(api.getUserManager().getUser(player.getUniqueId())
.getCachedData().getMetaData().getPrefix())), // TODO pull this from chatuser?
Placeholder.component("nick", Utility.applyColor(args[1]))));
}
} else {
sender.sendMessage(Utility.parseMiniMessage(Config.NICK_NO_LUCKPERMS));
}
} else {
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.TRY)));
}
break;
case "current":
if (hasPermission(sender, "chat.command.nick.current")) {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("nickname", chatUser.getDisplayName()),
Placeholder.parsed("currentnickname", chatUser.getNickNameString())
);
player.sendMiniMessage(Config.NICK_CURRENT, placeholders);
}
break;
case "help":
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.ALL)
+ "For more info on nicknames and how to use rgb colors go to: <aqua>https://alttd.com/nicknames<white>"));
break;
default:
sender.sendMessage(Utility.parseMiniMessage(helpMessage(sender, HelpType.ALL)));
}
} else {
sender.sendMessage("Console commands are disabled."); sender.sendMessage("Console commands are disabled.");
return true;
}
if (args.length == 0) {
sender.sendRichMessage(helpMessage(sender, HelpType.ALL));
return true;
}
switch (args[0].toLowerCase()) {
case "set" -> setNickname(sender, args, player);
case "review" -> reviewNickname(sender, args, player);
case "request" -> requestNickname(sender, args, player);
case "try" -> tryNickname(sender, args, player);
case "current" -> showCurrentNickname(sender, player);
case "help" ->
sender.sendRichMessage(helpMessage(sender, HelpType.ALL) + "For more info on nicknames and how to use rgb colors go to: <aqua>https://alttd.com/nicknames<white>");
default -> sender.sendRichMessage(helpMessage(sender, HelpType.ALL));
} }
return true; return true;
} }
private void showCurrentNickname(@NotNull CommandSender sender, Player player) {
if (!hasPermission(sender, "chat.command.nick.current")) {
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("nickname", chatUser.getDisplayName()),
Placeholder.parsed("currentnickname", chatUser.getNickNameString())
);
player.sendRichMessage(Config.NICK_CURRENT, placeholders);
}
private void tryNickname(@NotNull CommandSender sender, String[] args, Player player) {
if (args.length != 2 || !hasPermission(sender, "chat.command.nick.try")) {
sender.sendRichMessage(helpMessage(sender, HelpType.TRY));
return;
}
LuckPerms api = ChatAPI.get().getLuckPerms();
if (api == null) {
sender.sendRichMessage(Config.NICK_NO_LUCKPERMS);
return;
}
if (!NickUtilities.validNick(player, player, args[1])) {
return;
}
sender.sendRichMessage(Config.NICK_TRYOUT,
Placeholder.component("prefix", Utility.applyColor(api.getUserManager().getUser(player.getUniqueId())
.getCachedData().getMetaData().getPrefix())), // TODO pull this from chatuser?
Placeholder.component("nick", Utility.applyColor(args[1])),
Placeholder.unparsed("nickrequest", args[1]));
}
private void requestNickname(@NotNull CommandSender sender, String[] args, Player player) {
if (args.length != 2 || !hasPermission(sender, "chat.command.nick.request")) {
sender.sendRichMessage(helpMessage(sender, HelpType.REQUEST));
return;
}
new BukkitRunnable() {
@Override
public void run() {
handleNickRequest(player, args[1]);
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
private void reviewNickname(@NotNull CommandSender sender, String[] args, Player player) {
if (args.length != 1 || !hasPermission(sender, "chat.command.nick.review")) {
sender.sendRichMessage(helpMessage(sender, HelpType.REVIEW));
return;
}
NicknamesGui nicknamesGui = new NicknamesGui(player);
ChatPlugin.getInstance().getServer().getPluginManager().registerEvents(nicknamesGui, ChatPlugin.getInstance());
nicknamesGui.openInventory(player);
}
private void setNickname(@NotNull CommandSender sender, String[] args, Player player) {
if (args.length == 2 && hasPermission(sender, "chat.command.nick.set")) {
handleNick(player, player, args[1]);
} else if (args.length == 3 && hasPermission(sender, "chat.command.nick.set.others")) {
OfflinePlayer offlinePlayer = sender.getServer().getOfflinePlayer(args[1]);
if (offlinePlayer.isOnline() || offlinePlayer.hasPlayedBefore()) {
handleNick(player, offlinePlayer, args[2]);
} else {
sender.sendRichMessage(helpMessage(sender, HelpType.SET_OTHERS));
}
} else if (args.length > 3) {
sender.sendRichMessage(helpMessage(sender, HelpType.SET_SELF, HelpType.SET_OTHERS));
}
}
@Override @Override
public List<String> onTabComplete(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public List<String> onTabComplete(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
List<String> completions = new ArrayList<>(); List<String> completions = new ArrayList<>();
if (!sender.hasPermission("chat.command.nick")) { if (!sender.hasPermission("chat.command.nick")) return completions;
return completions;
}
if (args.length == 1) { if (args.length == 1) {
tabCompleteArgLengthOne(sender, args, completions); List<String> choices = new ArrayList<>();
if (sender.hasPermission("chat.command.nick.set")) {
choices.add("set");
}
if (sender.hasPermission("chat.command.nick.review")) {
choices.add("review");
}
if (sender.hasPermission("chat.command.nick.request")) {
choices.add("request");
}
if (sender.hasPermission("chat.command.nick.try")) {
choices.add("try");
}
if (sender.hasPermission("chat.command.nick.current")) {
choices.add("current");
}
choices.add("help");
for (String s : choices) {
if (s.startsWith(args[0])) {
completions.add(s);
}
}
} else if (args.length == 2) { } else if (args.length == 2) {
tabCompleteArgLengthTwo(sender, args, completions); if (args[0].equalsIgnoreCase("set")) {
List<String> choices = new ArrayList<>();
List<String> onlinePlayers = new ArrayList<>();
Bukkit.getOnlinePlayers().forEach(a -> onlinePlayers.add(a.getName()));
if (sender.hasPermission("chat.command.nick.set.others")) {
choices.addAll(onlinePlayers);
}
for (String s : choices) {
if (s.startsWith(args[1])) {
completions.add(s);
}
}
}
} }
return completions; return completions;
} }
private static void tabCompleteArgLengthTwo(CommandSender sender, String[] args, List<String> completions) {
if (!args[0].equalsIgnoreCase("set")) {
return;
}
List<String> choices = new ArrayList<>();
List<String> onlinePlayers = new ArrayList<>();
Bukkit.getOnlinePlayers().forEach(a -> onlinePlayers.add(a.getName()));
if (sender.hasPermission("chat.command.nick.set.others")) {
choices.addAll(onlinePlayers);
}
for (String s : choices) {
if (s.startsWith(args[1])) {
completions.add(s);
}
}
}
private static void tabCompleteArgLengthOne(CommandSender sender, String[] args, List<String> completions) {
List<String> choices = new ArrayList<>();
if (sender.hasPermission("chat.command.nick.set")) {
choices.add("set");
}
if (sender.hasPermission("chat.command.nick.review")) {
choices.add("review");
}
if (sender.hasPermission("chat.command.nick.request")) {
choices.add("request");
}
if (sender.hasPermission("chat.command.nick.try")) {
choices.add("try");
}
if (sender.hasPermission("chat.command.nick.current")) {
choices.add("current");
}
choices.add("help");
for (String s : choices) {
if (s.startsWith(args[0])) {
completions.add(s);
}
}
}
private void handleNickRequest(Player player, String nickName) { private void handleNickRequest(Player player, String nickName) {
if (!NickUtilities.validNick(player, player, nickName)) { if (!NickUtilities.validNick(player, player, nickName)) {
return; return;
@ -211,15 +193,15 @@ public class Nicknames implements CommandExecutor, TabCompleter {
long waitTime = Config.NICK_WAIT_TIME; long waitTime = Config.NICK_WAIT_TIME;
if (timeSinceLastChange > waitTime || player.hasPermission("chat.command.nick.bypasswaittime")) { if (timeSinceLastChange > waitTime || player.hasPermission("chat.command.nick.bypasswaittime")) {
if (nick.hasRequest()) { if (nick.hasRequest()) {
player.sendRichMessage(Config.NICK_REQUEST_PLACED, player.sendMessage(Utility.parseMiniMessage(Config.NICK_REQUEST_PLACED,
Placeholder.component("oldrequestednick", Utility.applyColor(nick.getNewNick())), Placeholder.component("oldrequestednick", Utility.applyColor(nick.getNewNick())),
Placeholder.component("newrequestednick", Utility.applyColor(nickName))); Placeholder.component("newrequestednick", Utility.applyColor(nickName))));
} }
nick.setNewNick(nickName); nick.setNewNick(nickName);
nick.setRequestedDate(new Date().getTime()); nick.setRequestedDate(new Date().getTime());
} else { } else {
player.sendRichMessage(Config.NICK_TOO_SOON, player.sendMessage(Utility.parseMiniMessage(Config.NICK_TOO_SOON,
Placeholder.unparsed("time", formatTime((timeSinceLastChange - waitTime) * -1))); Placeholder.unparsed("time", formatTime((timeSinceLastChange-waitTime)*-1))));
return; return;
} }
} else { } else {
@ -227,8 +209,8 @@ public class Nicknames implements CommandExecutor, TabCompleter {
} }
Queries.newNicknameRequest(uniqueId, nickName); Queries.newNicknameRequest(uniqueId, nickName);
bungeeMessageRequest(player); bungeeMessageRequest(player);
player.sendRichMessage(Config.NICK_REQUESTED, player.sendMessage(Utility.parseMiniMessage(Config.NICK_REQUESTED,
Placeholder.component("nick", Utility.applyColor(nickName))); Placeholder.component("nick", Utility.applyColor(nickName))));
} }
private void bungeeMessageRequest(Player player) { private void bungeeMessageRequest(Player player) {
@ -236,8 +218,8 @@ public class Nicknames implements CommandExecutor, TabCompleter {
UUID uniqueId = player.getUniqueId(); UUID uniqueId = player.getUniqueId();
// out.writeUTF("Forward"); // So BungeeCord knows to forward it // out.writeUTF("Forward"); // So BungeeCord knows to forward it
// out.writeUTF("ALL"); // out.writeUTF("ALL");
out.writeUTF("NickNameRequest"); // The channel name to check if this your data out.writeUTF("NickNameRequest"); // The channel name to check if this your data
ByteArrayOutputStream msgbytes = new ByteArrayOutputStream(); ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
@ -245,7 +227,7 @@ public class Nicknames implements CommandExecutor, TabCompleter {
try { try {
msgout.writeUTF(uniqueId.toString()); msgout.writeUTF(uniqueId.toString());
} catch (IOException exception) { } catch (IOException exception) {
ALogger.error("Failed to write UUID to ByteArrayOutputStream", exception); exception.printStackTrace();
return; return;
} }
byte[] bytes = msgbytes.toByteArray(); byte[] bytes = msgbytes.toByteArray();
@ -264,13 +246,13 @@ public class Nicknames implements CommandExecutor, TabCompleter {
long days = (timeInMillis / (1000 * 60 * 60 * 24)); long days = (timeInMillis / (1000 * 60 * 60 * 24));
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
if (days != 0) { if (days!=0) {
stringBuilder.append(days).append(" days "); stringBuilder.append(days).append(" days ");
} }
if (days != 0 || hour != 0) { if (days!=0 || hour!=0) {
stringBuilder.append(hour).append(" hours "); stringBuilder.append(hour).append(" hours ");
} }
if (days != 0 || hour != 0 || minute != 0) { if (days!=0 || hour!=0 || minute != 0) {
stringBuilder.append(minute).append(" minutes and "); stringBuilder.append(minute).append(" minutes and ");
} }
stringBuilder.append(second).append(" seconds"); stringBuilder.append(second).append(" seconds");
@ -279,73 +261,65 @@ public class Nicknames implements CommandExecutor, TabCompleter {
private void handleNick(Player sender, OfflinePlayer target, final String nickName) { private void handleNick(Player sender, OfflinePlayer target, final String nickName) {
if (nickName.equalsIgnoreCase("off")) { if (nickName.equalsIgnoreCase("off")) {
handleNickOff(sender, target);
try {
if (target.isOnline()) {
resetNick(target.getPlayer());
}
Queries.removePlayerFromDataBase(target.getUniqueId());
NickCache.remove(target.getUniqueId());
nickCacheUpdate.add(target.getUniqueId());
} catch (SQLException e) {
e.printStackTrace();
}
if (!sender.equals(target)) {
sender.sendMessage(Utility.parseMiniMessage(Config.NICK_RESET_OTHERS,
Placeholder.unparsed("player", target.getName())));
}
if (target.isOnline() && target.getPlayer() != null) {
target.getPlayer().sendMessage(Utility.parseMiniMessage(Config.NICK_RESET));
}
NickEvent nickEvent = new NickEvent(sender.getName(), target.getName(), null, NickEvent.NickEventType.RESET);
nickEvent.callEvent();
} else if (NickUtilities.validNick(sender, target, nickName)) { } else if (NickUtilities.validNick(sender, target, nickName)) {
setValidNick(sender, target, nickName);
}
}
private void setValidNick(Player sender, OfflinePlayer target, String nickName) {
if (target.isOnline()) {
setNick(target.getPlayer(), nickName);
} else {
NickUtilities.bungeeMessageHandled(target.getUniqueId(), sender, "Set");
}
Queries.setNicknameInDatabase(target.getUniqueId(), nickName);
NickEvent nickEvent = new NickEvent(sender.getName(), target.getName(), nickName, NickEvent.NickEventType.SET);
nickEvent.callEvent();
if (NickCache.containsKey(target.getUniqueId())) {
Nick nick = NickCache.get(target.getUniqueId());
nick.setCurrentNick(nickName);
nick.setLastChangedDate(new Date().getTime());
setNick(target.getPlayer(), nickName);
} else {
NickCache.put(target.getUniqueId(), new Nick(target.getUniqueId(), nickName, new Date().getTime()));
}
if (!sender.equals(target)) {
sender.sendMessage(Utility.parseMiniMessage(Config.NICK_CHANGED_OTHERS,
Placeholder.unparsed("targetplayer", Objects.requireNonNull(target.getName())),
Placeholder.unparsed("nickname", nickName)));
if (target.isOnline()) { if (target.isOnline()) {
Objects.requireNonNull(target.getPlayer()) setNick(target.getPlayer(), nickName);
.sendRichMessage(Config.NICK_TARGET_NICK_CHANGE, } else {
Placeholder.unparsed("nickname", getNick(target.getPlayer())), NickUtilities.bungeeMessageHandled(target.getUniqueId(), sender, "Set");
Placeholder.unparsed("sendernick", getNick(sender)),
Placeholder.unparsed("player", target.getName()));
} }
} else if (target.isOnline()) {
Objects.requireNonNull(target.getPlayer())
.sendRichMessage(Config.NICK_CHANGED,
Placeholder.unparsed("nickname", getNick(target.getPlayer())));
}
}
private void handleNickOff(Player sender, OfflinePlayer target) { Queries.setNicknameInDatabase(target.getUniqueId(), nickName);
try { NickEvent nickEvent = new NickEvent(sender.getName(), target.getName(), nickName, NickEvent.NickEventType.SET);
if (target.isOnline()) { nickEvent.callEvent();
resetNick(Objects.requireNonNull(target.getPlayer()));
if (NickCache.containsKey(target.getUniqueId())) {
Nick nick = NickCache.get(target.getUniqueId());
nick.setCurrentNick(nickName);
nick.setLastChangedDate(new Date().getTime());
setNick(target.getPlayer(), nickName);
} else {
NickCache.put(target.getUniqueId(), new Nick(target.getUniqueId(), nickName, new Date().getTime()));
} }
Queries.removePlayerFromDataBase(target.getUniqueId());
NickCache.remove(target.getUniqueId());
nickCacheUpdate.add(target.getUniqueId());
} catch (SQLException e) {
ALogger.error("Failed to remove nickname from database", e);
}
if (!sender.equals(target)) { if (!sender.equals(target)) {
sender.sendRichMessage(Config.NICK_RESET_OTHERS, sender.sendMessage(Utility.parseMiniMessage(Config.NICK_CHANGED_OTHERS,
Placeholder.unparsed("player", Objects.requireNonNull(target.getName()))); Placeholder.unparsed("targetplayer", target.getName()),
Placeholder.unparsed("nickname", nickName)));
if (target.isOnline()) {
target.getPlayer().sendMessage(Utility.parseMiniMessage(Config.NICK_TARGET_NICK_CHANGE,
Placeholder.unparsed("nickname", getNick(target.getPlayer())),
Placeholder.unparsed("sendernick", getNick(sender)),
Placeholder.unparsed("player", target.getName())));
}
} else if (target.isOnline()) {
target.getPlayer().sendMessage(Utility.parseMiniMessage(Config.NICK_CHANGED,
Placeholder.unparsed("nickname", getNick(target.getPlayer()))));
}
} }
if (target.isOnline() && target.getPlayer() != null) {
target.getPlayer().sendRichMessage(Config.NICK_RESET);
}
NickEvent nickEvent = new NickEvent(sender.getName(), target.getName(), null, NickEvent.NickEventType.RESET);
nickEvent.callEvent();
} }
private String helpMessage(final CommandSender sender, final HelpType... helpTypes) { private String helpMessage(final CommandSender sender, final HelpType... helpTypes) {
@ -381,10 +355,8 @@ public class Nicknames implements CommandExecutor, TabCompleter {
break; break;
case REQUEST: case REQUEST:
if (sender.hasPermission("chat.command.nick.request")) { if (sender.hasPermission("chat.command.nick.request")) {
message.append(""" message.append("<gold>/nick request <nickname><white> - Requests a username to be reviewed by staff.\n" +
<gold>/nick request <nickname><white> - Requests a username to be reviewed by staff. " <gray>Try using <dark_gray>/nick try <nickname><gray> to see if you like the name, you can only change it once per day!\n");
<gray>Try using <dark_gray>/nick try <nickname><gray> to see if you like the name, you can only change it once per day!
""");
} }
break; break;
case REVIEW: case REVIEW:
@ -411,8 +383,8 @@ public class Nicknames implements CommandExecutor, TabCompleter {
public void resetNick(final Player player) { public void resetNick(final Player player) {
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
user.setDisplayName(player.getName()); user.setDisplayName(player.getName());
player.displayName(user.getDisplayName().asComponent()); player.displayName(user.getDisplayName());
// updateCMIUser(player, null); updateCMIUser(player, null);
} }
public String getNick(final Player player) { public String getNick(final Player player) {
@ -421,13 +393,42 @@ public class Nicknames implements CommandExecutor, TabCompleter {
} }
public void setNick(final Player player, final String nickName) { public void setNick(final Player player, final String nickName) {
if (player == null) { if (player == null)
return; return;
}
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
user.setDisplayName(nickName); user.setDisplayName(nickName);
player.displayName(user.getDisplayName().asComponent()); player.displayName(user.getDisplayName());
updateCMIUser(player, nickName);
}
// public static String format(final String m) {
// return NickUtilities.applyColor(m);
// }
public void updateCMIUser(Player player, String nickName) {
if (!isCMIEnabled())
return;
CMIUser cmiUser = CMI.getInstance().getPlayerManager().getUser(player);
if (nickName == null){
cmiUser.setNickName(null, true);
} else {
cmiUser.setNickName(NickUtilities.applyColor(nickName), true);
}
cmiUser.updateDisplayName();
}
private Boolean isCMIEnabled = null;
private Boolean isCMIEnabled() {
if (!(isCMIEnabled == null))
return isCMIEnabled;
Plugin plugin = Bukkit.getPluginManager().getPlugin("CMI");
if (plugin != null && plugin.isEnabled())
return isCMIEnabled = true;
return isCMIEnabled = false;
} }
public static Nicknames getInstance() { public static Nicknames getInstance() {

View File

@ -1,15 +1,17 @@
package com.alttd.chat.nicknames; package com.alttd.chat.nicknames;
import com.Zrips.CMI.commands.list.colorlimits;
import com.Zrips.CMI.utils.Util;
import com.alttd.chat.ChatPlugin; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Nick; import com.alttd.chat.objects.Nick;
import com.alttd.chat.util.ALogger; import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
@ -31,6 +33,7 @@ import java.util.UUID;
public class NicknamesEvents implements Listener, PluginMessageListener { public class NicknamesEvents implements Listener, PluginMessageListener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
@ -55,9 +58,9 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
strippedNick = MiniMessage.miniMessage().stripTags(Nicknames.getInstance().getNick(player)); strippedNick = MiniMessage.miniMessage().stripTags(Nicknames.getInstance().getNick(player));
} catch (NullPointerException ignored) { } catch (NullPointerException ignored) {
} }
// final String strippedNick = CMIChatColor.stripColor(Nicknames.getInstance().getNick(player)); // final String strippedNick = CMIChatColor.stripColor(Nicknames.getInstance().getNick(player));
// final String cmiNick = Util.CMIChatColor.deColorize(Nicknames.getInstance().getNick(player)); // final String cmiNick = Util.CMIChatColor.deColorize(Nicknames.getInstance().getNick(player));
if (nickName == null) { if (nickName == null) {
Nicknames.getInstance().resetNick(player); Nicknames.getInstance().resetNick(player);
@ -76,8 +79,8 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
} }
if (i > 0) { if (i > 0) {
player.sendRichMessage(Config.NICK_REQUESTS_ON_LOGIN, player.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_REQUESTS_ON_LOGIN,
Placeholder.unparsed("amount", String.valueOf(i))); Placeholder.unparsed("amount", String.valueOf(i))));
} }
} }
} }
@ -94,7 +97,7 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
String subChannel = in.readUTF(); String subChannel = in.readUTF();
ALogger.info(channel + ": " + subChannel); ALogger.info(channel + ": " + subChannel);
if (!subChannel.equals("NickNameRequest") && !subChannel.equals("NickNameAccepted") if (!subChannel.equals("NickNameRequest") && !subChannel.equals("NickNameAccepted")
&& !subChannel.equals("NickNameDenied") && !subChannel.equals("NickNameSet")) { && !subChannel.equals("NickNameDenied") && !subChannel.equals("NickNameSet")) {
return; return;
} }
UUID playerUUID; UUID playerUUID;
@ -111,17 +114,18 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
name = offlinePlayer.getName() == null ? playerUUID.toString() : offlinePlayer.getName(); name = offlinePlayer.getName() == null ? playerUUID.toString() : offlinePlayer.getName();
} catch (Exception e) { } catch (Exception e) {
ALogger.error("Failed to read plugin message", e); e.printStackTrace();
return; return;
} }
MiniMessage miniMessage = MiniMessage.miniMessage(); MiniMessage miniMessage = MiniMessage.miniMessage();
switch (subChannel) { switch (subChannel) {
case "NickNameRequest": case "NickNameRequest":
ComponentLike component = miniMessage.deserialize(Config.NICK_REQUEST_NEW, Placeholder.parsed("player", name)) Component component = miniMessage.deserialize(Config.NICK_REQUEST_NEW, Placeholder.parsed("player", name))
.clickEvent(ClickEvent.runCommand("/nick review")) .clickEvent(ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND,
"/nick review"))
.hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, .hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT,
miniMessage.deserialize("<gold>Click this text to review the request!"))); miniMessage.deserialize("<gold>Click this text to review the request!")));
ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> { ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> {
if (p.hasPermission("chat.command.nick.review")) { if (p.hasPermission("chat.command.nick.review")) {
@ -138,9 +142,8 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
} }
break; break;
case "NickNameAccepted": case "NickNameAccepted":
Component deserialize = miniMessage.deserialize("<green><name>'s nickname was accepted!",
ComponentLike deserialize = Utility.parseMiniMessage("<green><name>'s nickname was accepted!", Placeholder.unparsed("name", name));
Placeholder.unparsed("name", name));
ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> { ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> {
if (p.hasPermission("chat.command.nick.review")) { if (p.hasPermission("chat.command.nick.review")) {
p.sendMessage(deserialize); p.sendMessage(deserialize);
@ -154,14 +157,14 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
Player target = Bukkit.getPlayer(playerUUID); Player target = Bukkit.getPlayer(playerUUID);
if (target != null && nick != null && nick.getCurrentNick() != null) { if (target != null && nick != null && nick.getCurrentNick() != null) {
Nicknames.getInstance().setNick(target, nick.getCurrentNick()); Nicknames.getInstance().setNick(target, nick.getCurrentNick());
target.sendRichMessage(Config.NICK_CHANGED, target.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_CHANGED,
Placeholder.unparsed("nickname", nick.getCurrentNick())); Placeholder.unparsed("nickname", nick.getCurrentNick())));
} }
} }
break; break;
case "NickNameDenied": case "NickNameDenied":
final Component messageDenied = miniMessage.deserialize("<red><name>'s nickname was denied", final Component messageDenied = miniMessage.deserialize("<red><name>'s nickname was denied",
Placeholder.unparsed("name", name)); Placeholder.unparsed("name", name));
Nick nick = Nicknames.getInstance().NickCache.get(playerUUID); Nick nick = Nicknames.getInstance().NickCache.get(playerUUID);
ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> { ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> {
@ -182,11 +185,9 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
if (offlinePlayer.isOnline()) { if (offlinePlayer.isOnline()) {
Player target = Bukkit.getPlayer(playerUUID); Player target = Bukkit.getPlayer(playerUUID);
if (target == null) { if (target == null) break;
break; target.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_NOT_CHANGED,
} Placeholder.unparsed("nickname", nick.getCurrentNick())));
target.sendRichMessage(Config.NICK_NOT_CHANGED,
Placeholder.unparsed("nickname", nick.getCurrentNick()));
} }
break; break;
} }

View File

@ -5,22 +5,23 @@ import com.alttd.chat.config.Config;
import com.alttd.chat.database.Queries; import com.alttd.chat.database.Queries;
import com.alttd.chat.events.NickEvent; import com.alttd.chat.events.NickEvent;
import com.alttd.chat.objects.Nick; import com.alttd.chat.objects.Nick;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.alttd.inventory_gui.click.GuiItem;
import com.alttd.inventory_gui.gui.InventoryGui;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
@ -29,45 +30,39 @@ import org.bukkit.scheduler.BukkitRunnable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class NicknamesGui implements Listener { public class NicknamesGui implements Listener {
public static final String UNKNOWN_PLAYER_NAME = "UNKNOWN PLAYER NAME"; private final Inventory inv;
private final int currentPage; private final int currentPage;
private final ChatPlugin plugin = ChatPlugin.getInstance();
private final InventoryGui nicknamesGui;
public NicknamesGui(Player player) { public NicknamesGui() {
nicknamesGui = InventoryGui.builder() // Create a new inventory, with no owner (as this isn't a real inventory)
.plugin(plugin) inv = Bukkit.createInventory(null, 36, Utility.parseMiniMessage("Nicknames GUI"));
.title(Component.text("Nicknames GUI"))
.rows(6) // Put the items into the inventory
.build();
currentPage = 1; currentPage = 1;
setItems(currentPage, player); setItems(currentPage);
} }
public void setItems(int currentPage, Player player) { public void setItems(int currentPage) {
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
inv.clear();
NickUtilities.updateCache(); NickUtilities.updateCache();
boolean hasNextPage = false; boolean hasNextPage = false;
int i = (currentPage - 1) * 27; //TODO set to 1 or 2 to test int i = (currentPage - 1) * 27; //TODO set to 1 or 2 to test
int limit = i / 27; int limit = i / 27;
MiniMessage miniMessage = MiniMessage.miniMessage();
for (Nick nick : Nicknames.getInstance().NickCache.values()) { for (Nick nick : Nicknames.getInstance().NickCache.values()) {
if (nick.hasRequest()) { if (nick.hasRequest()) {
if (limit >= i / 27) { if (limit >= i / 27) {
ItemStack playerSkull = createPlayerSkull(nick, Config.NICK_ITEM_LORE); inv.setItem(i % 27, createPlayerSkull(nick, Config.NICK_ITEM_LORE));
nicknamesGui.getRoot().setItem(i % 27, GuiItem.clickable(playerSkull, inventoryClickEvent ->
handleInventoryClick(nick, inventoryClickEvent, miniMessage, playerSkull)));
ALogger.info("Added nick " + i + " to gui: " + nick.getUuid());
i++; i++;
} else { } else {
ALogger.info("Reached end of nicknames gui page");
hasNextPage = true; hasNextPage = true;
break; break;
} }
@ -75,137 +70,20 @@ public class NicknamesGui implements Listener {
} }
if (currentPage != 1) { if (currentPage != 1) {
ItemStack itemStack = createGuiItem(Material.PAPER, "§bPrevious page", inv.setItem(28, createGuiItem(Material.PAPER, "§bPrevious page",
"§aCurrent page: %page%".replace("%page%", String.valueOf(currentPage)), "§aCurrent page: %page%".replace("%page%", String.valueOf(currentPage)),
"§aPrevious page: %previousPage%".replace("%previousPage%", String.valueOf(currentPage - 1))); "§aPrevious page: %previousPage%".replace("%previousPage%", String.valueOf(currentPage - 1))));
GuiItem previousPage = GuiItem.clickable(itemStack, e -> setItems(currentPage - 1, player));
nicknamesGui.getRoot().setItem(28, previousPage);
} }
if (hasNextPage) { if (hasNextPage) {
ItemStack itemStack = createGuiItem(Material.PAPER, "§bNext page", inv.setItem(36, createGuiItem(Material.PAPER, "§bNext page",
"§aCurrent page: %page%".replace("%page%", String.valueOf(currentPage)), "§aCurrent page: %page%".replace("%page%", String.valueOf(currentPage)),
"§aNext page: §b%nextPage%".replace("%nextPage%", String.valueOf(currentPage + 1))); "§aNext page: §b%nextPage%".replace("%nextPage%", String.valueOf(currentPage + 1))));
GuiItem nextPage = GuiItem.clickable(itemStack, e -> setItems(currentPage + 1, player));
nicknamesGui.getRoot().setItem(36, nextPage);
} }
nicknamesGui.render(player);
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
} }
private void handleInventoryClick(Nick nick, InventoryClickEvent inventoryClickEvent, MiniMessage miniMessage, ItemStack playerSkull) {
final Player playerWhoClicked = (Player) inventoryClickEvent.getWhoClicked();
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(nick.getUuid());
Component offlinePlayerName = miniMessage.deserialize(getOfflinePlayerName(offlinePlayer));
if (!nick.hasRequest()) {
playerWhoClicked.sendRichMessage(Config.NICK_ALREADY_HANDLED,
Placeholder.component("targetplayer", offlinePlayerName));
return;
}
if (inventoryClickEvent.isLeftClick()) {
handleLeftClickPlayerSkull(nick, inventoryClickEvent, offlinePlayer, playerWhoClicked, offlinePlayerName, playerSkull);
} else if (inventoryClickEvent.isRightClick()) {
handleRightClickPlayerSkull(nick, inventoryClickEvent, offlinePlayer, playerWhoClicked, offlinePlayerName, playerSkull);
}
//TODO what do we do no click?
}
private void handleRightClickPlayerSkull(Nick nick, InventoryClickEvent inventoryClickEvent, OfflinePlayer offlinePlayer, Player playerWhoClicked, Component offlinePlayerName, ItemStack playerSkull) {
Queries.denyNewNickname(nick.getUuid());
String newNick = nick.getNewNick();
new BukkitRunnable() {
@Override
public void run() {
NickEvent nickEvent = new NickEvent(inventoryClickEvent.getWhoClicked().getName(), getOfflinePlayerName(offlinePlayer), newNick, NickEvent.NickEventType.DENIED);
nickEvent.callEvent();
}
}.runTask(ChatPlugin.getInstance());
playerWhoClicked.sendRichMessage(Config.NICK_DENIED,
Placeholder.unparsed("targetplayer", getOfflinePlayerName(offlinePlayer)),
Placeholder.component("newnick", Utility.applyColor(nick.getNewNick())),
Placeholder.component("oldnick", Utility.applyColor(nick.getCurrentNick() == null ? getOfflinePlayerName(offlinePlayer) : nick.getCurrentNick())));
if (Nicknames.getInstance().NickCache.containsKey(nick.getUuid())
&& Nicknames.getInstance().NickCache.get(nick.getUuid()).getCurrentNick() != null) {
nick.setNewNick(null);
nick.setRequestedDate(0);
Nicknames.getInstance().NickCache.put(nick.getUuid(), nick);
} else {
Nicknames.getInstance().NickCache.remove(nick.getUuid());
}
if (offlinePlayer.isOnline() && offlinePlayer.getPlayer() != null) {
Nicknames.getInstance().setNick(offlinePlayer.getPlayer(), nick.getCurrentNick() == null ? getOfflinePlayerName(offlinePlayer) : nick.getCurrentNick());
offlinePlayer.getPlayer().sendRichMessage(Config.NICK_NOT_CHANGED);
}
NickUtilities.bungeeMessageHandled(nick.getUuid(), inventoryClickEvent.getWhoClicked().getServer().getPlayer(inventoryClickEvent.getWhoClicked().getName()), "Denied");
final ComponentLike messageDenied = MiniMessage.miniMessage().deserialize("<red><name>'s nickname was denied!",
Placeholder.unparsed("name", getOfflinePlayerName(offlinePlayer)));
ChatPlugin.getInstance().getServer().getOnlinePlayers().stream()
.filter(player -> player.hasPermission("chat.command.nick.review"))
.forEach(player -> player.sendMessage(messageDenied));
ItemStack completedNickRequestItem = createCompletedNickRequestItem(offlinePlayerName, playerSkull);
inventoryClickEvent.getInventory().setItem(inventoryClickEvent.getSlot(), completedNickRequestItem);
nicknamesGui.render(playerWhoClicked);
}
private void handleLeftClickPlayerSkull(Nick nick, InventoryClickEvent inventoryClickEvent, OfflinePlayer offlinePlayer, Player playerWhoClicked, Component offlinePlayerName, ItemStack playerSkull) {
Queries.acceptNewNickname(nick.getUuid(), nick.getNewNick());
String newNick = nick.getNewNick();
new BukkitRunnable() {
@Override
public void run() {
NickEvent nickEvent = new NickEvent(inventoryClickEvent.getWhoClicked().getName(), getOfflinePlayerName(offlinePlayer), newNick, NickEvent.NickEventType.ACCEPTED);
nickEvent.callEvent();
}
}.runTask(ChatPlugin.getInstance());
playerWhoClicked.sendRichMessage(Config.NICK_ACCEPTED,
Placeholder.component("targetplayer", offlinePlayerName),
Placeholder.component("newnick", Utility.applyColor(nick.getNewNick())),
Placeholder.component("oldnick", nick.getCurrentNick() == null ? offlinePlayerName : Utility.applyColor(nick.getCurrentNick())));
Player affectedPlayer = offlinePlayer.getPlayer();
if (offlinePlayer.isOnline() && affectedPlayer != null) {
Nicknames.getInstance().setNick(affectedPlayer, nick.getNewNick());
}
NickUtilities.bungeeMessageHandled(nick.getUuid(), inventoryClickEvent.getWhoClicked().getServer().getPlayer(inventoryClickEvent.getWhoClicked().getName()), "Accepted");
nick.setCurrentNick(nick.getNewNick());
nick.setLastChangedDate(new Date().getTime());
nick.setNewNick(null);
nick.setRequestedDate(0);
Nicknames.getInstance().NickCache.put(nick.getUuid(), nick);
ItemStack completedNickRequestItem = createCompletedNickRequestItem(offlinePlayerName, playerSkull);
inventoryClickEvent.getInventory().setItem(inventoryClickEvent.getSlot(), completedNickRequestItem);
nicknamesGui.render(playerWhoClicked);
}
private static ItemStack createCompletedNickRequestItem(Component offlinePlayerName, ItemStack playerSkull) {
ItemStack itemStack = new ItemStack(Material.SKELETON_SKULL);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(offlinePlayerName);
itemMeta.lore(playerSkull.lore());
itemStack.setItemMeta(itemMeta);
return itemStack;
}
private static String getOfflinePlayerName(OfflinePlayer offlinePlayer) {
return offlinePlayer.getName() == null ? UNKNOWN_PLAYER_NAME : offlinePlayer.getName();
}
private ItemStack createPlayerSkull(Nick nick, List<String> lore) { private ItemStack createPlayerSkull(Nick nick, List<String> lore) {
MiniMessage miniMessage = MiniMessage.miniMessage(); MiniMessage miniMessage = MiniMessage.miniMessage();
ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD); ItemStack playerHead = new ItemStack(Material.PLAYER_HEAD);
@ -214,11 +92,10 @@ public class NicknamesGui implements Listener {
meta.setOwningPlayer(offlinePlayer); meta.setOwningPlayer(offlinePlayer);
String name = offlinePlayer.getName(); String name = offlinePlayer.getName();
if (name == null) { if (name == null)
meta.displayName(miniMessage.deserialize("<red>" + getOfflinePlayerName(offlinePlayer) + "</red>")); meta.displayName(miniMessage.deserialize("UNKNOWN PLAYER NAME"));
} else { else
meta.displayName(miniMessage.deserialize(getOfflinePlayerName(offlinePlayer))); meta.displayName(miniMessage.deserialize(offlinePlayer.getName()));
}
TagResolver resolver = TagResolver.resolver( TagResolver resolver = TagResolver.resolver(
Placeholder.component("newnick", Utility.applyColor(nick.getNewNick())), Placeholder.component("newnick", Utility.applyColor(nick.getNewNick())),
@ -249,6 +126,172 @@ public class NicknamesGui implements Listener {
// You can open the inventory with this // You can open the inventory with this
public void openInventory(final HumanEntity ent) {//Possibly with a boolean to show if it should get from cache or update cache public void openInventory(final HumanEntity ent) {//Possibly with a boolean to show if it should get from cache or update cache
nicknamesGui.open(ent); ent.openInventory(inv);
}
// Check for clicks on items
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onInventoryClick(InventoryClickEvent e) {
if (e.getInventory() != inv) return;
e.setCancelled(true);
final ItemStack clickedItem = e.getCurrentItem();
if (clickedItem == null || clickedItem.getType() == Material.AIR) return;
final Player p = (Player) e.getWhoClicked();
if (clickedItem.getType().equals(Material.PAPER)) {
String serialize = PlainTextComponentSerializer.plainText().serialize(clickedItem.getItemMeta().displayName());
if (serialize.equals("Next Page")) {
setItems(currentPage + 1);
}
} else if (clickedItem.getType().equals(Material.PLAYER_HEAD)) {
SkullMeta meta = (SkullMeta) clickedItem.getItemMeta();
if (meta.hasEnchants()) {
return;
}
OfflinePlayer owningPlayer = meta.getOwningPlayer();
if (owningPlayer == null) {
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_USER_NOT_FOUND));
return;
}
new BukkitRunnable() {
@Override
public void run() {
NickUtilities.updateCache();
Nick nick;
UUID uniqueId = owningPlayer.getUniqueId();
if (Nicknames.getInstance().NickCache.containsKey(uniqueId)) {
nick = Nicknames.getInstance().NickCache.get(uniqueId);
} else {
nick = Queries.getNick(uniqueId);
}
if (nick == null || !nick.hasRequest()) {
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_ALREADY_HANDLED,
Placeholder.component("targetplayer", clickedItem.getItemMeta().displayName())));
return;
}
if (e.isLeftClick()) {
if (owningPlayer.hasPlayedBefore()) {
Queries.acceptNewNickname(uniqueId, nick.getNewNick());
String newNick = nick.getNewNick();
new BukkitRunnable() {
@Override
public void run() {
NickEvent nickEvent = new NickEvent(e.getWhoClicked().getName(), clickedItem.getItemMeta().getDisplayName(), newNick, NickEvent.NickEventType.ACCEPTED);
nickEvent.callEvent();
}
}.runTask(ChatPlugin.getInstance());
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_ACCEPTED,
Placeholder.component("targetplayer", clickedItem.getItemMeta().displayName()),
Placeholder.component("newnick", Utility.applyColor(nick.getNewNick())),
Placeholder.component("oldnick", Utility.applyColor(nick.getCurrentNick() == null ? clickedItem.getItemMeta().getDisplayName() : nick.getCurrentNick()))));
if (owningPlayer.isOnline() && owningPlayer.getPlayer() != null) {
Nicknames.getInstance().setNick(owningPlayer.getPlayer(), nick.getNewNick());
// owningPlayer.getPlayer().sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_CHANGED // This message is also send when the plugin message is received
// .replace("%nickname%", nick.getNewNick())));
}
NickUtilities.bungeeMessageHandled(uniqueId, e.getWhoClicked().getServer().getPlayer(e.getWhoClicked().getName()), "Accepted");
nick.setCurrentNick(nick.getNewNick());
nick.setLastChangedDate(new Date().getTime());
nick.setNewNick(null);
nick.setRequestedDate(0);
Nicknames.getInstance().NickCache.put(uniqueId, nick);
ItemStack itemStack = new ItemStack(Material.SKELETON_SKULL);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(clickedItem.getItemMeta().displayName());
itemMeta.lore(clickedItem.lore());
itemStack.setItemMeta(itemMeta);
e.getInventory().setItem(e.getSlot(), itemStack);
p.updateInventory();
} else {
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_PLAYER_NOT_ONLINE,
Placeholder.component("playerName", clickedItem.getItemMeta().displayName())));
}
} else if (e.isRightClick()) {
Component displayName = clickedItem.getItemMeta().displayName();
if (owningPlayer.hasPlayedBefore()) {
Queries.denyNewNickname(uniqueId);
String newNick = nick.getNewNick();
new BukkitRunnable() {
@Override
public void run() {
NickEvent nickEvent = new NickEvent(e.getWhoClicked().getName(), clickedItem.getItemMeta().getDisplayName(), newNick, NickEvent.NickEventType.DENIED);
nickEvent.callEvent();
}
}.runTask(ChatPlugin.getInstance());
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_DENIED,
Placeholder.unparsed("targetplayer", owningPlayer.getName()),
Placeholder.component("newnick", Utility.applyColor(nick.getNewNick())),
Placeholder.component("oldnick", Utility.applyColor(nick.getCurrentNick() == null ? owningPlayer.getName() : nick.getCurrentNick()))));
if (Nicknames.getInstance().NickCache.containsKey(uniqueId)
&& Nicknames.getInstance().NickCache.get(uniqueId).getCurrentNick() != null) {
nick.setNewNick(null);
nick.setRequestedDate(0);
Nicknames.getInstance().NickCache.put(uniqueId, nick);
} else {
Nicknames.getInstance().NickCache.remove(uniqueId);
}
if (owningPlayer.isOnline() && owningPlayer.getPlayer() != null) {
Nicknames.getInstance().setNick(owningPlayer.getPlayer(), nick.getCurrentNick() == null ? owningPlayer.getName() : nick.getCurrentNick());
owningPlayer.getPlayer().sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_NOT_CHANGED));
}
NickUtilities.bungeeMessageHandled(uniqueId, e.getWhoClicked().getServer().getPlayer(e.getWhoClicked().getName()), "Denied");
final Component messageDenied = MiniMessage.miniMessage().deserialize("<red><name>'s nickname was denied!",
Placeholder.unparsed("name", owningPlayer.getName()));
ChatPlugin.getInstance().getServer().getOnlinePlayers().forEach(p -> {
if (p.hasPermission("chat.command.nick.review")) {
p.sendMessage(messageDenied);
}
});
ItemStack itemStack = new ItemStack(Material.SKELETON_SKULL);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(displayName);
itemMeta.lore(clickedItem.lore());
itemStack.setItemMeta(itemMeta);
e.getInventory().setItem(e.getSlot(), itemStack);
p.updateInventory();
} else {
if (displayName == null)
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_PLAYER_NOT_ONLINE, Placeholder.parsed("playerName", "UNKNOWN PLAYER NAME")));
else
p.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_PLAYER_NOT_ONLINE, Placeholder.component("playerName", displayName)));
}
}
}
}.runTaskAsynchronously(ChatPlugin.getInstance());
}
}
// Cancel dragging in our inventory
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onInventoryClick(InventoryDragEvent e) {
if (e.getInventory() == inv) {
e.setCancelled(true);
}
} }
} }

View File

@ -2,11 +2,13 @@ package com.alttd.chat.util;
import com.alttd.chat.config.Config; import com.alttd.chat.config.Config;
import com.alttd.chat.managers.RegexManager; import com.alttd.chat.managers.RegexManager;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,30 +16,40 @@ import java.util.List;
public class GalaxyUtility { public class GalaxyUtility {
public static void sendBlockedNotification(String prefix, OfflinePlayer offlinePlayer, ComponentLike input, String target) { public static void sendBlockedNotification(String prefix, Player player, String input, String target) {
String playerName = offlinePlayer.getName() == null ? "unknown_player" : offlinePlayer.getName();
TagResolver placeholders = TagResolver.resolver( TagResolver placeholders = TagResolver.resolver(
Placeholder.parsed("prefix", prefix), Placeholder.parsed("prefix", prefix),
Placeholder.parsed("displayname", Utility.getDisplayName(offlinePlayer.getUniqueId(), playerName)), Placeholder.parsed("displayname", Utility.getDisplayName(player.getUniqueId(), player.getName())),
Placeholder.parsed("target", (target.isEmpty() ? "tried to say:" : "-> " + target + ":")), Placeholder.parsed("target", (target.isEmpty() ? "tried to say:" : "-> " + target + ":")),
Placeholder.component("input", input) Placeholder.parsed("input", input)
); );
ComponentLike blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, placeholders); Component blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, placeholders);
Bukkit.getOnlinePlayers().forEach(a -> { Bukkit.getOnlinePlayers().forEach(a ->{
if (a.hasPermission("chat.alert-blocked")) { if (a.hasPermission("chat.alert-blocked")) {
a.sendMessage(blockedNotification); a.sendMessage(blockedNotification);
} }
}); });
if (offlinePlayer.isOnline()) { player.sendMessage(Utility.parseMiniMessage("<red>The language you used in your message is not allowed, " +
Player player = offlinePlayer.getPlayer(); "this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
if (player == null) { }
ALogger.error("Player is offline but isOnline() returned true");
return; public static void sendBlockedNotification(String prefix, Player player, Component input, String target) {
TagResolver placeholders = TagResolver.resolver(
Placeholder.parsed("prefix", prefix),
Placeholder.parsed("displayname", Utility.getDisplayName(player.getUniqueId(), player.getName())),
Placeholder.parsed("target", (target.isEmpty() ? "tried to say:" : "-> " + target + ":")),
Placeholder.component("input", input)
);
Component blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, placeholders);
Bukkit.getOnlinePlayers().forEach(a ->{
if (a.hasPermission("chat.alert-blocked")) {
a.sendMessage(blockedNotification);
} }
player.sendRichMessage("<red>The language you used in your message is not allowed, " + });
"this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"); player.sendMessage(Utility.parseMiniMessage("<red>The language you used in your message is not allowed, " +
} "this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
} }
public static void addAdditionalChatCompletions(Player player) { public static void addAdditionalChatCompletions(Player player) {

View File

@ -1,41 +0,0 @@
package com.alttd.chat.util;
import org.bukkit.Bukkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ServerName {
private static final String serverName = loadServerName();
private static String loadServerName() {
String serverName = "unknown";
try {
File serverPropertiesFile = new File(Bukkit.getWorldContainer().getParentFile(), "server.properties");
if (!serverPropertiesFile.exists()) {
ALogger.warn(String.format("server.properties file not found in %s!", serverPropertiesFile.getAbsolutePath()));
return serverName;
}
Properties properties = new Properties();
FileInputStream fis = new FileInputStream(serverPropertiesFile);
properties.load(fis);
fis.close();
serverName = properties.getProperty("server-name", serverName);
ALogger.info(String.format("Found server name [%s]", serverName));
} catch (IOException e) {
ALogger.error("Failed to read server.properties", e);
}
return serverName;
}
public static String getServerName() {
return serverName;
}
}

View File

@ -3,8 +3,6 @@ package com.alttd.chat.util;
import com.alttd.chat.ChatPlugin; import com.alttd.chat.ChatPlugin;
import com.alttd.chat.objects.Toggleable; import com.alttd.chat.objects.Toggleable;
import com.alttd.chat.objects.channels.CustomChannel; import com.alttd.chat.objects.channels.CustomChannel;
import net.luckperms.api.model.user.User;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -40,27 +38,10 @@ public class ToggleableForCustomChannel extends Toggleable {
@Override @Override
public void sendMessage(Player player, String message) { public void sendMessage(Player player, String message) {
Utility.getOrLoadUser(player.getUniqueId()).thenAcceptAsync(user -> {
ALogger.info(String.format("%s sent %s message: %s",
player.getName(),
customChannel.getChannelName(),
message
));
ChatPlugin.getInstance().getChatHandler().chatChannel(user, player, customChannel, message);
});
}
@Override
public void sendMessage(User user, OfflinePlayer offlinePlayer, String message) {
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
ALogger.info(String.format("%s sent %s message: %s", ChatPlugin.getInstance().getChatHandler().chatChannel(player, customChannel, message);
offlinePlayer.getName(),
customChannel.getChannelName(),
message
));
ChatPlugin.getInstance().getChatHandler().chatChannel(user, offlinePlayer, customChannel, message);
} }
}.runTaskAsynchronously(ChatPlugin.getInstance()); }.runTaskAsynchronously(ChatPlugin.getInstance());
} }

Binary file not shown.

View File

@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000 networkTimeout=10000
validateDistributionUrl=true validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME

15
gradlew vendored
View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# #
# Copyright © 2015 the original authors. # Copyright © 2015-2021 the original authors.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -15,8 +15,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
# SPDX-License-Identifier: Apache-2.0
#
############################################################################## ##############################################################################
# #
@ -57,7 +55,7 @@
# Darwin, MinGW, and NonStop. # Darwin, MinGW, and NonStop.
# #
# (3) This script is generated from the Groovy template # (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/b631911858264c0b6e4d6603d677ff5218766cee/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project. # within the Gradle project.
# #
# You can find Gradle at https://github.com/gradle/gradle/. # You can find Gradle at https://github.com/gradle/gradle/.
@ -86,7 +84,7 @@ done
# shellcheck disable=SC2034 # shellcheck disable=SC2034
APP_BASE_NAME=${0##*/} APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value. # Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum MAX_FD=maximum
@ -114,6 +112,7 @@ case "$( uname )" in #(
NONSTOP* ) nonstop=true ;; NONSTOP* ) nonstop=true ;;
esac esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM. # Determine the Java command to use to start the JVM.
@ -171,6 +170,7 @@ fi
# For Cygwin or MSYS, switch paths to Windows format before running java # For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" ) JAVACMD=$( cygpath --unix "$JAVACMD" )
@ -203,14 +203,15 @@ fi
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command: # Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped. # and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line. # treated as '${Hostname}' itself on the command line.
set -- \ set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \ "-Dorg.gradle.appname=$APP_BASE_NAME" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ -classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@" "$@"
# Stop when "xargs" is not available. # Stop when "xargs" is not available.

25
gradlew.bat vendored
View File

@ -13,8 +13,6 @@
@rem See the License for the specific language governing permissions and @rem See the License for the specific language governing permissions and
@rem limitations under the License. @rem limitations under the License.
@rem @rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off @if "%DEBUG%"=="" @echo off
@rem ########################################################################## @rem ##########################################################################
@ -45,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1 %JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2 echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo. 1>&2 echo.
echo Please set the JAVA_HOME variable in your environment to match the 1>&2 echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation. 1>&2 echo location of your Java installation.
goto fail goto fail
@ -59,21 +57,22 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute if exist "%JAVA_EXE%" goto execute
echo. 1>&2 echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo. 1>&2 echo.
echo Please set the JAVA_HOME variable in your environment to match the 1>&2 echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation. 1>&2 echo location of your Java installation.
goto fail goto fail
:execute :execute
@rem Setup the command line @rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle @rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end :end
@rem End local scope for the variables with windows NT shell @rem End local scope for the variables with windows NT shell

BIN
libs/CMI.jar Executable file

Binary file not shown.

View File

@ -4,34 +4,18 @@ include(":api")
include(":galaxy") include(":galaxy")
include(":velocity") include(":velocity")
val nexusUser = providers.gradleProperty("alttdSnapshotUsername").get()
val nexusPass = providers.gradleProperty("alttdSnapshotPassword").get()
dependencyResolutionManagement { dependencyResolutionManagement {
repositories { repositories {
mavenLocal() // mavenLocal()
mavenCentral() mavenCentral()
maven("https://repo.alttd.com/snapshots") // Altitude - Galaxy maven("https://repo.destro.xyz/snapshots") // Altitude - Galaxy
maven("https://oss.sonatype.org/content/groups/public/") // Adventure maven("https://oss.sonatype.org/content/groups/public/") // Adventure
maven("https://oss.sonatype.org/content/repositories/snapshots/") // Minimessage maven("https://oss.sonatype.org/content/repositories/snapshots/") // Minimessage
maven("https://repo.papermc.io/repository/maven-public/") maven("https://nexus.velocitypowered.com/repository/") // Velocity
maven("https://nexus.velocitypowered.com/repository/maven-public/") // Velocity
maven("https://repo.spongepowered.org/maven") // Configurate maven("https://repo.spongepowered.org/maven") // Configurate
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") // Papi maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") // Papi
maven("https://jitpack.io") maven("https://jitpack.io")
maven {
url = uri("https://repo.alttd.com/repository/alttd-snapshot/")
credentials {
username = nexusUser
password = nexusPass
}
}
maven {
url = uri("https://repo.alttd.com/repository/alttd/")
credentials {
username = nexusUser
password = nexusPass
}
}
} }
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
} }
@ -41,5 +25,3 @@ pluginManagement {
gradlePluginPortal() gradlePluginPortal()
} }
} }
include("web-api")

View File

@ -1,32 +1,21 @@
plugins { plugins {
`maven-publish` `maven-publish`
id("com.gradleup.shadow") id("com.github.johnrengelman.shadow")
} }
dependencies { dependencies {
implementation(project(":api")) // API implementation(project(":api")) // API
compileOnly("org.projectlombok:lombok:1.18.46") compileOnly("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
annotationProcessor("org.projectlombok:lombok:1.18.46") annotationProcessor("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
compileOnly("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT") implementation("mysql:mysql-connector-java:8.0.27") // mysql
annotationProcessor("com.velocitypowered:velocity-api:3.5.0-SNAPSHOT") implementation("org.spongepowered", "configurate-yaml", "4.1.2")
implementation("mysql:mysql-connector-java:8.0.33") // mysql compileOnly("net.kyori:adventure-text-minimessage:4.10.1")
implementation("org.spongepowered", "configurate-yaml", "4.2.0")
compileOnly("net.kyori:adventure-text-minimessage:4.23.0")
compileOnly("com.gitlab.ruany:LiteBansAPI:0.3.5") compileOnly("com.gitlab.ruany:LiteBansAPI:0.3.5")
compileOnly("com.alttd.proxydiscordlink:ProxyDiscordLink:1.0.1-SNAPSHOT") compileOnly("com.alttd.proxydiscordlink:ProxyDiscordLink:1.0.0-BETA-SNAPSHOT")
compileOnly("net.luckperms:api:5.5") // Luckperms
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
} }
tasks { tasks {
test {
useJUnitPlatform()
}
shadowJar { shadowJar {
archiveFileName.set("${rootProject.name}-${project.name}-${project.version}.jar") archiveFileName.set("${rootProject.name}-${project.name}-${project.version}.jar")
// minimize() // minimize()
@ -41,4 +30,4 @@ tasks {
dependsOn(shadowJar) dependsOn(shadowJar)
} }
} }

View File

@ -2,24 +2,19 @@ package com.alttd.velocitychat;
import com.alttd.chat.ChatAPI; import com.alttd.chat.ChatAPI;
import com.alttd.chat.ChatImplementation; import com.alttd.chat.ChatImplementation;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.DatabaseConnection;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager; import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.objects.chat_log.WebHandler;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.web.SseSubscribeClient;
import com.alttd.velocitychat.chat_web.handlers.PunishFromWebHandler;
import com.alttd.velocitychat.chat_web.handlers.WebPartyChatHandler;
import com.alttd.velocitychat.commands.*; import com.alttd.velocitychat.commands.*;
import com.alttd.chat.config.Config;
import com.alttd.chat.database.DatabaseConnection;
import com.alttd.velocitychat.handlers.ChatHandler; import com.alttd.velocitychat.handlers.ChatHandler;
import com.alttd.velocitychat.handlers.ServerHandler; import com.alttd.velocitychat.handlers.ServerHandler;
import com.alttd.velocitychat.listeners.ChatListener; import com.alttd.velocitychat.listeners.ChatListener;
import com.alttd.velocitychat.listeners.LiteBansListener; import com.alttd.velocitychat.listeners.LiteBansListener;
import com.alttd.velocitychat.listeners.PluginMessageListener;
import com.alttd.velocitychat.listeners.ProxyPlayerListener; import com.alttd.velocitychat.listeners.ProxyPlayerListener;
import com.alttd.velocitychat.listeners.PluginMessageListener;
import com.alttd.chat.util.ALogger;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -40,7 +35,7 @@ import java.nio.file.Path;
description = "A chat plugin for Altitude Minecraft Server", description = "A chat plugin for Altitude Minecraft Server",
authors = {"destro174", "teri"}, authors = {"destro174", "teri"},
dependencies = {@Dependency(id = "luckperms"), @Dependency(id = "litebans"), @Dependency(id = "proxydiscordlink")} dependencies = {@Dependency(id = "luckperms"), @Dependency(id = "litebans"), @Dependency(id = "proxydiscordlink")}
) )
public class VelocityChat { public class VelocityChat {
private static VelocityChat plugin; private static VelocityChat plugin;
@ -53,7 +48,6 @@ public class VelocityChat {
private ServerHandler serverHandler; private ServerHandler serverHandler;
private ChannelIdentifier channelIdentifier; private ChannelIdentifier channelIdentifier;
private SseSubscribeClient sseSubscribeClient;
@Inject @Inject
public VelocityChat(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory) { public VelocityChat(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory) {
@ -72,10 +66,7 @@ public class VelocityChat {
PartyManager.initialize(); // load the parties from the db and add the previously loaded users to them PartyManager.initialize(); // load the parties from the db and add the previously loaded users to them
serverHandler = new ServerHandler(); serverHandler = new ServerHandler();
WebHandler webHandler = new WebHandler(); chatHandler = new ChatHandler();
ChatLogHandler chatLogHandler = new ChatLogHandler(webHandler, true);
chatHandler = new ChatHandler(chatLogHandler, chatAPI.getLuckPerms());
server.getEventManager().register(this, new ChatListener()); server.getEventManager().register(this, new ChatListener());
server.getEventManager().register(this, new ProxyPlayerListener()); server.getEventManager().register(this, new ProxyPlayerListener());
new LiteBansListener().init(); // init the litebans api listeners new LiteBansListener().init(); // init the litebans api listeners
@ -83,37 +74,21 @@ public class VelocityChat {
channelIdentifier = MinecraftChannelIdentifier.create(channels[0], channels[1]); channelIdentifier = MinecraftChannelIdentifier.create(channels[0], channels[1]);
server.getChannelRegistrar().register(channelIdentifier); server.getChannelRegistrar().register(channelIdentifier);
server.getEventManager().register(this, new PluginMessageListener(channelIdentifier)); server.getEventManager().register(this, new PluginMessageListener(channelIdentifier));
loadCommands(webHandler); loadCommands();
// setup console chatuser // setup console chatuser
ChatUser console = new ChatUser(Config.CONSOLEUUID, -1, null); ChatUser console = new ChatUser(Config.CONSOLEUUID, -1, null);
console.setDisplayName(Config.CONSOLENAME); console.setDisplayName(Config.CONSOLENAME);
ChatUserManager.addUser(console); ChatUserManager.addUser(console);
sseSubscribeClient = new SseSubscribeClient(
Config.CHAT_WEB_REGISTER_TO_BASE_URL,
"proxy", //TODO [Stijn] [2026-08-09]: Make configurable if needed
Config.CHAT_WEB_TOKEN
);
new Thread(sseSubscribeClient).start();
registerWebHandlers(sseSubscribeClient);
} }
private void registerWebHandlers(SseSubscribeClient sseSubscribeClient) { public void ReloadConfig() {
sseSubscribeClient.register("web_party_chat", new WebPartyChatHandler(chatHandler)); chatAPI.ReloadConfig();
sseSubscribeClient.register("web_punish", new PunishFromWebHandler(server)); chatAPI.ReloadChatFilters();
}
public void reloadConfig() {
chatAPI.reloadConfig();
chatAPI.reloadChatFilters();
serverHandler.cleanup(); serverHandler.cleanup();
ByteArrayDataOutput buf = ByteStreams.newDataOutput(); ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("reloadconfig"); buf.writeUTF("reloadconfig");
ALogger.info("Reloaded ChatPlugin proxy config."); ALogger.info("Reloaded ChatPlugin proxy config.");
getProxy().getAllServers() getProxy().getAllServers().stream().forEach(registeredServer -> registeredServer.sendPluginMessage(getChannelIdentifier(), buf.toByteArray()));
.stream()
.forEach(registeredServer -> registeredServer.sendPluginMessage(getChannelIdentifier(),
buf.toByteArray()
));
} }
public File getDataDirectory() { public File getDataDirectory() {
@ -132,15 +107,12 @@ public class VelocityChat {
return server; return server;
} }
public void loadCommands(WebHandler webHandler) { public void loadCommands() {
ChatLogHandler instance = ChatLogHandler.getInstance(webHandler, false);
new SilentJoinCommand(server); new SilentJoinCommand(server);
new GlobalAdminChat(server); new GlobalAdminChat(server);
new Reload(server); new Reload(server);
new MailCommand(server); new MailCommand(server);
new Report(server); new Report(server);
new VoteToMute(server, instance);
new VoteToMuteHelper(server);
server.getCommandManager().register("party", new PartyCommand()); server.getCommandManager().register("party", new PartyCommand());
// all (proxy)commands go here // all (proxy)commands go here
} }

View File

@ -1,120 +0,0 @@
package com.alttd.velocitychat.chat_web;
import com.alttd.chat.web.handler_class.PunishFromWeb;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
public class PunishmentCommandBuilder {
private static final Set<String> ALLOWED_TYPES = Set.of("BAN", "MUTE", "WARN");
private static final String WARN_DURATION = "30d";
public static String buildCommand(String executorName, PunishFromWeb event) {
String rawType = event.getType();
UUID executorUuid = event.getExecutor();
String target = event.getTarget().toString();
String reason = event.getReason();
String type = validateType(rawType);
validateReason(reason);
String time = resolveTime(type, event.getTime());
StringBuilder commandBuilder = new StringBuilder();
commandBuilder.append(type).append(" ").append(target);
if (time != null) {
commandBuilder.append(" ").append(time);
}
commandBuilder.append(" --sender=").append(executorName)
.append(" --sender-uuid=").append(executorUuid);
commandBuilder.append(" ").append(reason);
return commandBuilder.toString();
}
private static String validateType(String rawType) {
if (rawType == null || !ALLOWED_TYPES.contains(rawType.toUpperCase(Locale.ROOT))) {
throw new IllegalArgumentException("Invalid punishment type: " + rawType
+ ". Allowed types are: " + ALLOWED_TYPES);
}
return rawType.toLowerCase(Locale.ROOT);
}
private static void validateReason(String reason) {
if (reason == null || reason.isBlank()) {
throw new IllegalArgumentException("A reason is required for all punishments");
}
}
/**
* Resolves the litebans-formatted time argument for a given (already-normalized,
* lowercase) punishment type, applying the per-type rules:
* - warn: always 30d, regardless of what was supplied
* - mute: a duration is required
* - ban: optional, permanent (null) if not supplied
*/
private static String resolveTime(String type, String rawTime) {
switch (type) {
case "warn":
return WARN_DURATION;
case "mute":
if (rawTime == null || rawTime.isBlank()) {
throw new IllegalArgumentException("Mutes must have a duration");
}
return parseDuration(rawTime);
case "ban":
if (rawTime == null || rawTime.isBlank()) {
return null; // permanent ban
}
return parseDuration(rawTime);
default:
// unreachable, type is already validated before this is called
throw new IllegalArgumentException("Unsupported type: " + type);
}
}
/**
* Parses an ISO-8601 duration (e.g. "PT30M", "P7D", "P1DT2H3M4S") as sent by the
* OpenAPI spec's `time` field, and converts it into a litebans-style duration
* string. Litebans only accepts a single unit of d, h, or m (no combined units,
* no seconds), so this picks the single largest whole unit and drops the rest,
* e.g. "P1DT2H3M4S" -> "1d", "PT2H3M" -> "2h", "PT45M" -> "45m".
*/
static String parseDuration(String isoDuration) {
Duration duration;
try {
duration = Duration.parse(isoDuration);
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid duration format: " + isoDuration, e);
}
long totalSeconds = duration.getSeconds();
if (totalSeconds <= 0) {
throw new IllegalArgumentException("Duration must be positive: " + isoDuration);
}
long days = totalSeconds / 86400;
if (days > 0) {
return days + "d";
}
long hours = totalSeconds / 3600;
if (hours > 0) {
return hours + "h";
}
long minutes = totalSeconds / 60;
if (minutes > 0) {
return minutes + "m";
}
//Default minimum
return "1m";
}
}

View File

@ -1,42 +0,0 @@
package com.alttd.velocitychat.chat_web.handlers;
import com.alttd.chat.util.Utility;
import com.alttd.chat.web.WebHandler;
import com.alttd.chat.web.handler_class.PunishFromWeb;
import com.alttd.velocitychat.chat_web.PunishmentCommandBuilder;
import com.velocitypowered.api.proxy.ProxyServer;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PunishFromWebHandler implements WebHandler<PunishFromWeb> {
private final ProxyServer server;
public PunishFromWebHandler(ProxyServer server) {
this.server = server;
}
@Override
public Class<PunishFromWeb> type() {
return PunishFromWeb.class;
}
@Override
public void handle(PunishFromWeb event) {
String permission = "litebans." + event.getType().toLowerCase();
Utility.getOrLoadUser(event.getExecutor()).thenAccept(user -> {
if (user == null) {
log.warn("User {} does not exist", event.getExecutor());
return;
}
if (!Utility.hasPermission(user, permission)) {
log.warn("User {} does not have permission {}", user.getUsername(), permission);
return;
}
String executorName = (user.getUsername() != null) ? user.getUsername() : event.getExecutor().toString();
server.getCommandManager()
.executeAsync(server.getConsoleCommandSource(),
PunishmentCommandBuilder.buildCommand(executorName, event)
);
});
}
}

View File

@ -1,49 +0,0 @@
package com.alttd.velocitychat.chat_web.handlers;
import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Party;
import com.alttd.chat.web.WebHandler;
import com.alttd.chat.web.handler_class.PartyChatFromWeb;
import com.alttd.velocitychat.handlers.ChatHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.UUID;
@Slf4j
@RequiredArgsConstructor
public class WebPartyChatHandler implements WebHandler<PartyChatFromWeb> {
private final ChatHandler chatHandler;
@Override
public Class<PartyChatFromWeb> type() {
return PartyChatFromWeb.class;
}
@Override
public void handle(PartyChatFromWeb event) {
int partyId;
try {
partyId = Integer.parseInt(event.getPartyId());
} catch (NumberFormatException e) {
log.error("Invalid party id: {}", event.getPartyId());
return;
}
UUID sender = event.getSender();
String message = event.getMessage();
Party party = PartyManager.getParty(sender);
if (party == null) {
log.error("Party not found for sender: {}", sender);
return;
}
if (party.getPartyId() != partyId) {
log.error("Party id mismatch: {} != {}", party.getPartyId(), partyId);
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(sender);//TODO [Stijn] [2026-08-09]: Async since it can do a query
chatHandler.sendPartyMessageFromWeb(sender, party, chatUser.getDisplayName().asComponent(), message);
}
}

View File

@ -7,7 +7,7 @@ import com.mojang.brigadier.arguments.StringArgumentType;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.util.ArrayList; import java.util.ArrayList;
@ -39,13 +39,12 @@ public class PartyCommand implements SimpleCommand {
CommandSource source = invocation.source(); CommandSource source = invocation.source();
if (args.length < 1) { if (args.length < 1) {
if (!source.hasPermission("party.use")) { if (!source.hasPermission("party.use"))
source.sendMessage(Utility.parseMiniMessage(Config.NO_PERMISSION)); source.sendMessage(Utility.parseMiniMessage(Config.NO_PERMISSION));
} else if (source instanceof Player) { else if (source instanceof Player)
source.sendMessage(getHelpMessage(source)); source.sendMessage(getHelpMessage(source));
} else { else
source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE)); source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE));
}
return; return;
} }
@ -53,12 +52,11 @@ public class PartyCommand implements SimpleCommand {
.filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0])) .filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0]))
.findFirst() .findFirst()
.ifPresentOrElse(subCommand -> { .ifPresentOrElse(subCommand -> {
if (source.hasPermission(subCommand.getPermission())) { if (source.hasPermission(subCommand.getPermission()))
subCommand.execute(args, source); subCommand.execute(args, source);
} else { else
source.sendMessage(Utility.parseMiniMessage(Config.NO_PERMISSION)); source.sendMessage(Utility.parseMiniMessage(Config.NO_PERMISSION));
} }, () -> source.sendMessage(getHelpMessage(source)));
}, () -> source.sendMessage(getHelpMessage(source)));
} }
@Override @Override
@ -66,9 +64,9 @@ public class PartyCommand implements SimpleCommand {
String[] args = invocation.arguments(); String[] args = invocation.arguments();
List<String> suggest = new ArrayList<>(); List<String> suggest = new ArrayList<>();
if (!invocation.source().hasPermission("party.use")) { if (!invocation.source().hasPermission("party.use"))
return suggest; return suggest;
} else if (args.length == 0) { else if (args.length == 0) {
subCommands.stream() subCommands.stream()
.filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission())) .filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission()))
.forEach(subCommand -> suggest.add(subCommand.getName())); .forEach(subCommand -> suggest.add(subCommand.getName()));
@ -85,11 +83,10 @@ public class PartyCommand implements SimpleCommand {
.ifPresent(subCommand -> suggest.addAll(subCommand.suggest(args, invocation.source()))); .ifPresent(subCommand -> suggest.addAll(subCommand.suggest(args, invocation.source())));
} }
if (args.length == 0) { if (args.length == 0)
return suggest; return suggest;
} else { else
return finalizeSuggest(suggest, args[args.length - 1]); return finalizeSuggest(suggest, args[args.length - 1]);
}
} }
public List<String> finalizeSuggest(List<String> possibleValues, String remaining) { public List<String> finalizeSuggest(List<String> possibleValues, String remaining) {
@ -104,21 +101,19 @@ public class PartyCommand implements SimpleCommand {
return finalValues; return finalValues;
} }
public ComponentLike getHelpMessage(CommandSource source) { public Component getHelpMessage(CommandSource source) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
subCommands.stream() subCommands.stream()
.filter(subCommand -> source.hasPermission(subCommand.getPermission())) .filter(subCommand -> source.hasPermission(subCommand.getPermission()))
.forEach(subCommand -> stringBuilder.append(subCommand.getHelpMessage()).append("\n")); .forEach(subCommand -> stringBuilder.append(subCommand.getHelpMessage()).append("\n"));
if (source.hasPermission("command.chat.p")) { if (source.hasPermission("command.chat.p"))
stringBuilder.append(Config.PARTY_HELP_CHAT).append("\n"); stringBuilder.append(Config.PARTY_HELP_CHAT).append("\n");
} if (stringBuilder.length() != 0)
if (!stringBuilder.isEmpty()) {
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), ""); stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "");
}
return Utility.parseMiniMessage(Config.PARTY_HELP_WRAPPER, return Utility.parseMiniMessage(Config.PARTY_HELP_WRAPPER,
Placeholder.component("commands", Utility.parseMiniMessage(stringBuilder.toString())) Placeholder.component("commands", Utility.parseMiniMessage(stringBuilder.toString()))
); );
} }
} }

View File

@ -15,7 +15,7 @@ public class Reload {
.<CommandSource>literal("reloadchat") .<CommandSource>literal("reloadchat")
.requires(ctx -> ctx.hasPermission("command.chat.reloadchat")) .requires(ctx -> ctx.hasPermission("command.chat.reloadchat"))
.executes(context -> { .executes(context -> {
VelocityChat.getPlugin().reloadConfig(); VelocityChat.getPlugin().ReloadConfig();
return 1; return 1;
}) })
.build(); .build();

View File

@ -1,163 +0,0 @@
package com.alttd.velocitychat.commands;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.commands.vote_to_mute.VoteToMuteStarter;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class VoteToMute {
public VoteToMute(ProxyServer proxyServer, ChatLogHandler chatLogHandler) {
RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder
.<CommandSource, String>argument("player", StringArgumentType.string())
.suggests((context, builder) -> {
List<Player> possiblePlayers;
if (context.getSource() instanceof Player player) {
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isPresent()) {
possiblePlayers = getEligiblePlayers(currentServer.get().getServer());
} else {
possiblePlayers = getEligiblePlayers(proxyServer);
}
} else {
possiblePlayers = getEligiblePlayers(proxyServer);
}
Collection<String> possibleValues = possiblePlayers.stream()
.map(Player::getUsername)
.toList();
if (possibleValues.isEmpty())
return Suggestions.empty();
String remaining = builder.getRemaining().toLowerCase();
possibleValues.stream()
.filter(str -> str.toLowerCase().startsWith(remaining))
.map(StringArgumentType::escapeIfRequired)
.forEach(builder::suggest);
return builder.buildFuture();
})
.executes(context -> {
sendHelpMessage(context.getSource());
return 1;
});
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
.<CommandSource>literal("votetomute")
.requires(commandSource -> commandSource.hasPermission("chat.vote-to-mute"))
.requires(commandSource -> commandSource instanceof Player)
.then(playerNode
.suggests(((commandContext, suggestionsBuilder) -> {
if (!(commandContext.getSource() instanceof Player player)) {
return suggestionsBuilder.buildFuture();
}
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isEmpty()) {
sendHelpMessage(commandContext.getSource());
return suggestionsBuilder.buildFuture();
}
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
currentServer.get().getServer().getPlayersConnected().stream()
.filter(connectedPlayer -> connectedPlayer.hasPermission("chat.affected-by-vote-to-mute"))
.map(Player::getUsername)
.filter((String str) -> str.toLowerCase().startsWith(remaining))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestionsBuilder::suggest);
return suggestionsBuilder.buildFuture();
}))
.executes(commandContext -> {
String playerName = commandContext.getArgument("player", String.class);
Optional<Player> optionalPlayer = proxyServer.getPlayer(playerName);
if (optionalPlayer.isEmpty()) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<red>Player <player> is not online.</red>",
Placeholder.parsed("player", playerName)));
return 1;
}
Player voteTarget = optionalPlayer.get();
if (!voteTarget.hasPermission("chat.affected-by-vote-to-mute")) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<red>Player <player> can not be muted by a vote.</red>",
Placeholder.parsed("player", playerName)));
return 1;
}
Player player = (Player) commandContext.getSource();
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isEmpty()) {
sendHelpMessage(commandContext.getSource());
return 1;
}
RegisteredServer server = currentServer.get().getServer();
if (currentServer.get().getServer().getPlayersConnected().stream().anyMatch(onlinePlayer -> onlinePlayer.hasPermission("chat.staff"))) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>There is a staff member online, so vote to mute can not be used. Please contact a staff member for help instead.</red>"));
return 1;
}
boolean countLowerRanks = false;
long count = getTotalEligiblePlayers(server, false);
if (count < 6) {
countLowerRanks = true;
count = getTotalEligiblePlayers(server, true);
if (count < 6) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Not enough eligible players online to vote.</red>"));
return 1;
}
}
new VoteToMuteStarter(chatLogHandler, voteTarget, player, server.getServerInfo().getName(), countLowerRanks)
.start();
return 1;
}))
.executes(context -> {
sendHelpMessage(context.getSource());
return 1;
})
.build();
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
CommandMeta.Builder metaBuilder = proxyServer.getCommandManager().metaBuilder(brigadierCommand);
CommandMeta meta = metaBuilder.build();
proxyServer.getCommandManager().register(meta, brigadierCommand);
}
private int getTotalEligiblePlayers(RegisteredServer server, boolean countLowerRanks) {
return (int) server.getPlayersConnected().stream()
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.count();
}
private void sendHelpMessage(CommandSource commandSource) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>Use: <gold>/votetomute <player></gold>.</red>"));
}
private List<Player> getEligiblePlayers(ProxyServer proxyServer) {
return proxyServer.getAllPlayers().stream()
.filter(player -> player.hasPermission("chat.affected-by-vote-to-mute"))
.collect(Collectors.toList());
}
private List<Player> getEligiblePlayers(RegisteredServer registeredServer) {
return registeredServer.getPlayersConnected().stream()
.filter(player -> player.hasPermission("chat.affected-by-vote-to-mute"))
.collect(Collectors.toList());
}
}

View File

@ -1,287 +0,0 @@
package com.alttd.velocitychat.commands;
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.commands.vote_to_mute.ActiveVoteToMute;
import com.alttd.velocitychat.commands.vote_to_mute.VoteToMuteStarter;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class VoteToMuteHelper {
private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>").asComponent();
public VoteToMuteHelper(ProxyServer proxyServer) {
RequiredArgumentBuilder<CommandSource, String> playerNode = RequiredArgumentBuilder
.<CommandSource, String>argument("player", StringArgumentType.string())
.suggests((context, builder) -> {
List<Player> possiblePlayers;
if (context.getSource() instanceof Player player) {
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isPresent()) {
possiblePlayers = getEligiblePlayers(currentServer.get().getServer());
} else {
possiblePlayers = getEligiblePlayers(proxyServer);
}
} else {
possiblePlayers = getEligiblePlayers(proxyServer);
}
Collection<String> possibleValues = possiblePlayers.stream()
.map(Player::getUsername)
.toList();
if (possibleValues.isEmpty()) {
return Suggestions.empty();
}
String remaining = builder.getRemaining().toLowerCase();
possibleValues.stream()
.filter(str -> str.toLowerCase().startsWith(remaining))
.map(StringArgumentType::escapeIfRequired)
.forEach(builder::suggest);
return builder.buildFuture();
})
.executes(context -> {
sendHelpMessage(context.getSource());
return 1;
});
RequiredArgumentBuilder<CommandSource, String> yesNoNode = RequiredArgumentBuilder.
<CommandSource, String>argument("yesNo", StringArgumentType.string())
.suggests(((commandContext, suggestionsBuilder) -> {
List<String> yesNoValues = Arrays.asList("yes", "no");
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
yesNoValues.stream()
.filter((String str) -> str.toLowerCase().startsWith(remaining))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestionsBuilder::suggest);
return suggestionsBuilder.buildFuture();
}));
LiteralArgumentBuilder<CommandSource> pageNode = LiteralArgumentBuilder
.<CommandSource>literal("page")
.requires(commandSource -> commandSource.hasPermission("chat.vote-to-mute"))
.then(RequiredArgumentBuilder.<CommandSource, Integer>argument("page number", IntegerArgumentType.integer(1))
.suggests(((commandContext, suggestionsBuilder) -> {
if (!(commandContext.getSource() instanceof Player player)) {
return suggestionsBuilder.buildFuture();
}
Optional<VoteToMuteStarter> instance = VoteToMuteStarter.getInstance(player.getUniqueId());
if (instance.isEmpty()) {
return suggestionsBuilder.buildFuture();
}
VoteToMuteStarter voteToMuteStarter = instance.get();
String remaining = suggestionsBuilder.getRemaining().toLowerCase();
int totalPages = voteToMuteStarter.getTotalPages();
IntStream.range(1, totalPages + 1)
.mapToObj(String::valueOf)
.filter((String str) -> str.toLowerCase().startsWith(remaining))
.map(StringArgumentType::escapeIfRequired)
.forEach(suggestionsBuilder::suggest);
return suggestionsBuilder.buildFuture();
}))
.executes(commandContext -> {
if (!(commandContext.getSource() instanceof Player player)) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Only players can use this command.</red>"));
return 1;
}
Optional<VoteToMuteStarter> instance = VoteToMuteStarter.getInstance(player.getUniqueId());
if (instance.isEmpty()) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>You don't have an active vote to mute.</red>"));
return 1;
}
int pageNumber = commandContext.getArgument("page number", Integer.class);
instance.get().showPage(pageNumber);
return 1;
})
).executes(commandContext -> {
sendHelpMessage(commandContext.getSource());
return 1;
});
LiteralArgumentBuilder<CommandSource> enterMessagesNode = LiteralArgumentBuilder
.<CommandSource>literal("messages")
.requires(commandSource -> commandSource.hasPermission("chat.vote-to-mute"))
.then(RequiredArgumentBuilder.<CommandSource, String>argument("list of messages", StringArgumentType.greedyString())
.executes(commandContext -> {
if (!(commandContext.getSource() instanceof Player player)) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Only players can use this command.</red>"));
return 1;
}
Optional<VoteToMuteStarter> instance = VoteToMuteStarter.getInstance(player.getUniqueId());
if (instance.isEmpty()) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>You don't have an active vote to mute.</red>"));
return 1;
}
String listOfPages = commandContext.getArgument("list of messages", String.class);
if (!listOfPages.matches("([1-9][0-9]*, )*[1-9][0-9]*")) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Please make sure to format the command correctly.</red>"));
return 1;
}
VoteToMuteStarter voteToMuteStarter = instance.get();
List<Integer> collect = Arrays.stream(listOfPages.split(", "))
.map(Integer::parseInt)
.collect(Collectors.toList());
Optional<Integer> max = collect.stream().max(Integer::compare);
if (max.isEmpty()) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Some of your selected messages do not exist.</red>"));
return 1;
}
int highestLogEntry = max.get();
if (voteToMuteStarter.getTotalLogEntries() < highestLogEntry) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage("<red>Some of your selected messages do not exist.</red>"));
return 1;
}
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isEmpty()) {
sendHelpMessage(commandContext.getSource());
return 1;
}
Component chatLogs = voteToMuteStarter.getChatLogsAndClose(collect);
RegisteredServer server = currentServer.get().getServer();
long count = getTotalEligiblePlayers(server, voteToMuteStarter.countLowerRanks());
new ActiveVoteToMute(voteToMuteStarter.getVotedPlayer(), server, proxyServer, Duration.ofMinutes(5),
(int) count, voteToMuteStarter.countLowerRanks(), chatLogs, player)
.start();
return 1;
})
).executes(commandContext -> {
sendHelpMessage(commandContext.getSource());
return 1;
});
LiteralArgumentBuilder<CommandSource> voteNode = LiteralArgumentBuilder
.<CommandSource>literal("vote")
.then(playerNode
.then(yesNoNode
.executes(commandContext -> {
if (!(commandContext.getSource() instanceof Player player)) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<red>Only players are allowed to vote</red>"));
return 1;
}
String playerName = commandContext.getArgument("player", String.class);
Optional<ActiveVoteToMute> optionalActiveVoteToMute = ActiveVoteToMute.getInstance(playerName);
if (optionalActiveVoteToMute.isEmpty()) {
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<red>This player does not have an active vote to mute them.</red>"));
return 1;
}
ActiveVoteToMute activeVoteToMute = optionalActiveVoteToMute.get();
if (!activeVoteToMute.countLowerRanks()) {
if (!player.hasPermission("chat.vote-to-mute")) {
player.sendMessage(Utility.parseMiniMessage("<red>You are not eligible to vote.</red>"));
return 1;
}
}
String vote = commandContext.getArgument("yesNo", String.class);
switch (vote.toLowerCase()) {
case "yes" -> {
activeVoteToMute.vote(player.getUniqueId(), true);
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<green>You voted to mute. Thanks for voting, staff will be online soon to review!</green>"));
player.getCurrentServer().ifPresent(serverConnection -> notifyEligiblePlayers(serverConnection.getServer(), activeVoteToMute));
}
case "no" -> {
activeVoteToMute.vote(player.getUniqueId(), false);
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<green>You voted <red>not</red> to mute. Thanks for voting, staff will be online soon to review!</green>"));
}
default ->
commandContext.getSource().sendMessage(Utility.parseMiniMessage(
"<red><vote> is not a valid vote option</red>", Placeholder.parsed("vote", vote)));
}
return 1;
})).executes(context -> {
sendHelpMessage(context.getSource());
return 1;
})).executes(context -> {
sendHelpMessage(context.getSource());
return 1;
});
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
.<CommandSource>literal("votetomutehelper")
.requires(commandSource -> commandSource.hasPermission("chat.backup-vote-to-mute"))
.requires(commandSource -> commandSource instanceof Player)
.then(voteNode)
.then(pageNode)
.then(enterMessagesNode)
.executes(context -> {
sendHelpMessage(context.getSource());
return 1;
})
.build();
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
CommandMeta.Builder metaBuilder = proxyServer.getCommandManager().metaBuilder(brigadierCommand);
CommandMeta meta = metaBuilder.build();
proxyServer.getCommandManager().register(meta, brigadierCommand);
}
private int getTotalEligiblePlayers(RegisteredServer server, boolean countLowerRanks) {
return (int) server.getPlayersConnected().stream()
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.count();
}
private void notifyEligiblePlayers(RegisteredServer server, ActiveVoteToMute activeVoteToMute) {
ComponentLike message = Utility.parseMiniMessage("<prefix><green><voted_for> out of <total_votes> players have voted to mute <player></green>",
Placeholder.component("prefix", prefix),
Placeholder.parsed("voted_for", String.valueOf(activeVoteToMute.getVotedFor())),
Placeholder.parsed("total_votes", String.valueOf(activeVoteToMute.getTotalEligibleVoters())),
Placeholder.parsed("player", activeVoteToMute.getVotedPlayer().getUsername()));
boolean countLowerRanks = activeVoteToMute.countLowerRanks();
server.getPlayersConnected().stream()
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.forEach(player -> player.sendMessage(message));
}
private void sendHelpMessage(CommandSource commandSource) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>Use: <gold>/votetomutehelper <player></gold>.</red>"));
}
private List<Player> getEligiblePlayers(ProxyServer proxyServer) {
return proxyServer.getAllPlayers().stream()
.filter(player -> player.hasPermission("chat.affected-by-vote-to-mute"))
.collect(Collectors.toList());
}
private List<Player> getEligiblePlayers(RegisteredServer registeredServer) {
return registeredServer.getPlayersConnected().stream()
.filter(player -> player.hasPermission("chat.affected-by-vote-to-mute"))
.collect(Collectors.toList());
}
}

View File

@ -46,12 +46,11 @@ public class Disband implements SubCommand {
source.sendMessage(Utility.parseMiniMessage(getHelpMessage())); source.sendMessage(Utility.parseMiniMessage(getHelpMessage()));
return; return;
} }
VelocityChat.getPlugin().getChatHandler() VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
.sendPartyMessage(party, Utility.parseMiniMessage(Config.DISBANDED_PARTY,
Utility.parseMiniMessage(Config.DISBANDED_PARTY, Placeholder.unparsed("owner", player.getUsername()),
Placeholder.unparsed("owner", player.getUsername()), Placeholder.unparsed("party", party.getPartyName())
Placeholder.unparsed("party", party.getPartyName()) ), null);
).asComponent(), null);
party.delete(); party.delete();
} }

View File

@ -40,20 +40,19 @@ public class Info implements SubCommand {
List<Component> displayNames = new ArrayList<>(); List<Component> displayNames = new ArrayList<>();
for (PartyUser partyUser : party.getPartyUsers()) { for (PartyUser partyUser : party.getPartyUsers()) {
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(partyUser.getUuid()); Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(partyUser.getUuid());
if (optionalPlayer.isPresent() && optionalPlayer.get().isActive()) { if (optionalPlayer.isPresent() && optionalPlayer.get().isActive())
displayNames.add(Config.ONLINE_PREFIX.asComponent().append(partyUser.getDisplayName())); displayNames.add(Config.ONLINE_PREFIX.append(partyUser.getDisplayName()));
} else { else
displayNames.add(Config.OFFLINE_PREFIX.asComponent().append(partyUser.getDisplayName())); displayNames.add(Config.OFFLINE_PREFIX.append(partyUser.getDisplayName()));
}
} }
PartyUser owner = party.getPartyUser(party.getOwnerUuid()); PartyUser owner = party.getPartyUser(party.getOwnerUuid());
source.sendMessage(Utility.parseMiniMessage(Config.PARTY_INFO, source.sendMessage(Utility.parseMiniMessage(Config.PARTY_INFO,
Placeholder.unparsed("party", party.getPartyName()), Placeholder.unparsed("party", party.getPartyName()),
Placeholder.unparsed("password", party.getPartyPassword()), Placeholder.unparsed("password", party.getPartyPassword()),
Placeholder.component("owner", owner == null ? MiniMessage.miniMessage().deserialize("Unknown Owner") : owner.getDisplayName()), Placeholder.component("owner", owner == null ? MiniMessage.miniMessage().deserialize("Unknown Owner") : owner.getDisplayName()),
Placeholder.component("members", Component.join(JoinConfiguration.separator(Component.text(", ")), displayNames)) Placeholder.component("members", Component.join(JoinConfiguration.separator(Component.text(", ")), displayNames))
)); ));
} }
@Override @Override

View File

@ -43,7 +43,7 @@ public class Join implements SubCommand {
return; return;
} }
// party.addUser(ChatUserManager.getChatUser(player.getUniqueId())); //Removed until we can get nicknames to translate to colors correctly // party.addUser(ChatUserManager.getChatUser(player.getUniqueId())); //Removed until we can get nicknames to translate to colors correctly
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
if (chatUser.getPartyId() == party.getPartyId()) { if (chatUser.getPartyId() == party.getPartyId()) {
@ -52,12 +52,11 @@ public class Join implements SubCommand {
} }
party.addUser(chatUser, player.getUsername()); party.addUser(chatUser, player.getUsername());
source.sendMessage(Utility.parseMiniMessage(Config.JOINED_PARTY, Placeholder.parsed("party_name", party.getPartyName()))); source.sendMessage(Utility.parseMiniMessage(Config.JOINED_PARTY, Placeholder.parsed("party_name", party.getPartyName())));
VelocityChat.getPlugin().getChatHandler() VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
.sendPartyMessage(party, Utility.parseMiniMessage(Config.PLAYER_JOINED_PARTY,
Utility.parseMiniMessage(Config.PLAYER_JOINED_PARTY, Placeholder.component("player_name", chatUser.getDisplayName()),
Placeholder.component("player_name", chatUser.getDisplayName()), Placeholder.parsed("party_name", party.getPartyName())
Placeholder.parsed("party_name", party.getPartyName()) ), null);
).asComponent(), null);
} }
@Override @Override

View File

@ -35,31 +35,28 @@ public class Leave implements SubCommand {
return; return;
} }
Optional<ServerConnection> currentServer = player.getCurrentServer(); Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isEmpty()) { if (currentServer.isEmpty())
return; return;
}
party.removeUser(player.getUniqueId()); party.removeUser(player.getUniqueId());
if (party.getOwnerUuid().equals(player.getUniqueId())) { if (party.getOwnerUuid().equals(player.getUniqueId())) {
if (!party.getPartyUsers().isEmpty()) { if (party.getPartyUsers().size() > 0) {
UUID uuid = party.setNewOwner(); UUID uuid = party.setNewOwner();
source.sendMessage(Utility.parseMiniMessage(Config.NOTIFY_FINDING_NEW_OWNER)); source.sendMessage(Utility.parseMiniMessage(Config.NOTIFY_FINDING_NEW_OWNER));
VelocityChat.getPlugin().getChatHandler() VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
.sendPartyMessage(party, Utility.parseMiniMessage(Config.OWNER_LEFT_PARTY,
Utility.parseMiniMessage(Config.OWNER_LEFT_PARTY, Placeholder.unparsed("old_owner", player.getUsername()),
Placeholder.unparsed("old_owner", player.getUsername()), Placeholder.unparsed("new_owner", party.getPartyUser(uuid).getPlayerName())
Placeholder.unparsed("new_owner", party.getPartyUser(uuid).getPlayerName()) ), null);
).asComponent(), null);
} else { } else {
party.delete(); party.delete();
} }
} else { } else {
source.sendMessage(Utility.parseMiniMessage(Config.LEFT_PARTY)); source.sendMessage(Utility.parseMiniMessage(Config.LEFT_PARTY));
VelocityChat.getPlugin().getChatHandler() VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
.sendPartyMessage(party, Utility.parseMiniMessage(Config.PLAYER_LEFT_PARTY,
Utility.parseMiniMessage(Config.PLAYER_LEFT_PARTY, Placeholder.unparsed("player_name", player.getUsername())
Placeholder.unparsed("player_name", player.getUsername()) ), null);
).asComponent(), null);
} }
} }

View File

@ -45,19 +45,18 @@ public class Name implements SubCommand {
} }
if (PartyManager.getParty(args[1]) != null) { if (PartyManager.getParty(args[1]) != null) {
source.sendMessage(Utility.parseMiniMessage(Config.PARTY_EXISTS, source.sendMessage(Utility.parseMiniMessage(Config.PARTY_EXISTS,
Placeholder.unparsed("party", args[1]) Placeholder.unparsed("party", args[1])
)); ));
return; return;
} }
String oldName = party.getPartyName(); String oldName = party.getPartyName();
party.setPartyName(args[1]); party.setPartyName(args[1]);
VelocityChat.getPlugin().getChatHandler() VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party, Utility.parseMiniMessage(Config.RENAMED_PARTY,
.sendPartyMessage(party, Utility.parseMiniMessage(Config.RENAMED_PARTY, Placeholder.component("owner", ChatUserManager.getChatUser(player.getUniqueId()).getDisplayName()),
Placeholder.component("owner", ChatUserManager.getChatUser(player.getUniqueId()).getDisplayName()), Placeholder.unparsed("old_name", oldName),
Placeholder.unparsed("old_name", oldName), Placeholder.unparsed("new_name", args[1])
Placeholder.unparsed("new_name", args[1]) ), null);
).asComponent(), null);
} }
@Override @Override

View File

@ -14,6 +14,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
public class Owner implements SubCommand { public class Owner implements SubCommand {
@Override @Override
@ -43,36 +44,31 @@ public class Owner implements SubCommand {
PartyUser partyUser = party.getPartyUser(args[1]); PartyUser partyUser = party.getPartyUser(args[1]);
if (partyUser == null) { if (partyUser == null) {
source.sendMessage(Utility.parseMiniMessage(Config.NOT_A_PARTY_MEMBER, source.sendMessage(Utility.parseMiniMessage(Config.NOT_A_PARTY_MEMBER,
Placeholder.unparsed("player", args[1]) Placeholder.unparsed("player", args[1])
)); ));
return; return;
} }
party.setNewOwner(partyUser.getUuid()); party.setNewOwner(partyUser.getUuid());
VelocityChat.getPlugin().getChatHandler() VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
.sendPartyMessage(party, Utility.parseMiniMessage(Config.NEW_PARTY_OWNER,
Utility.parseMiniMessage(Config.NEW_PARTY_OWNER, Placeholder.unparsed("old_owner", player.getUsername()),
Placeholder.unparsed("old_owner", player.getUsername()), Placeholder.unparsed("new_owner", partyUser.getPlayerName())
Placeholder.unparsed("new_owner", partyUser.getPlayerName()) ), null);
).asComponent(), null);
} }
@Override @Override
public List<String> suggest(String[] args, CommandSource source) { public List<String> suggest(String[] args, CommandSource source) {
ArrayList<String> suggest = new ArrayList<>(); ArrayList<String> suggest = new ArrayList<>();
if (!(source instanceof Player player)) { if (!(source instanceof Player player))
return suggest; return suggest;
}
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
Party party = PartyManager.getParty(uuid); Party party = PartyManager.getParty(uuid);
if (party == null) { if (party == null)
return suggest; return suggest;
} if (args.length == 1 || args.length == 2)
if (args.length == 1 || args.length == 2) {
suggest.addAll(party.getPartyUsers().stream() suggest.addAll(party.getPartyUsers().stream()
.filter(partyUser -> !partyUser.getUuid().equals(uuid)) .filter(partyUser -> !partyUser.getUuid().equals(uuid))
.map(PartyUser::getPlayerName) .map(PartyUser::getPlayerName).collect(Collectors.toList()));
.toList());
}
return suggest; return suggest;
} }

View File

@ -1,244 +0,0 @@
package com.alttd.velocitychat.commands.vote_to_mute;
import com.alttd.chat.config.Config;
import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility;
import com.alttd.proxydiscordlink.DiscordLink;
import com.alttd.proxydiscordlink.lib.net.dv8tion.jda.api.EmbedBuilder;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class ActiveVoteToMute {
private static final HashMap<String, ActiveVoteToMute> instances = new HashMap<>();
private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>").asComponent();
private final Player votedPlayer;
private final Player startedByPlayer;
private final HashSet<UUID> votedFor = new HashSet<>();
private final HashSet<UUID> votedAgainst = new HashSet<>();
private int totalEligibleVoters;
private final boolean countLowerRanks;
private final RegisteredServer server;
private final ProxyServer proxyServer;
private final Component chatLogs;
private boolean endedVote = false;
public static Optional<ActiveVoteToMute> getInstance(String username) {
if (!instances.containsKey(username)) {
return Optional.empty();
}
return Optional.of(instances.get(username));
}
public static void removePotentialVoter(Player player, RegisteredServer previousServer) {
if (!player.hasPermission("chat.backup-vote-to-mute")) {
return;
}
if (player.hasPermission("chat.vote-to-mute")) {
instances.values().stream()
.filter(activeVoteToMute -> previousServer == null || activeVoteToMute.getServer().getServerInfo().hashCode() == previousServer.getServerInfo().hashCode())
.forEach(inst -> inst.removeEligibleVoter(player.getUniqueId()));
} else {
instances.values().stream()
.filter(ActiveVoteToMute::countLowerRanks)
.filter(activeVoteToMute -> previousServer == null || activeVoteToMute.getServer().getServerInfo().hashCode() == previousServer.getServerInfo().hashCode())
.forEach(inst -> inst.removeEligibleVoter(player.getUniqueId()));
}
}
public static void addPotentialVoter(Player player, ServerConnection server) {
if (!player.hasPermission("chat.backup-vote-to-mute")) {
return;
}
if (player.hasPermission("chat.vote-to-mute")) {
instances.values().stream()
.filter(activeVoteToMute -> activeVoteToMute.getServer().getServerInfo().hashCode() == server.getServerInfo().hashCode())
.forEach(activeVoteToMute -> activeVoteToMute.addEligibleVoter(player));
} else {
instances.values().stream()
.filter(ActiveVoteToMute::countLowerRanks)
.filter(activeVoteToMute -> activeVoteToMute.getServer().getServerInfo().hashCode() == server.getServerInfo().hashCode())
.forEach(activeVoteToMute -> activeVoteToMute.addEligibleVoter(player));
}
}
public ActiveVoteToMute(@NotNull Player votedPlayer, @NotNull RegisteredServer server, ProxyServer proxyServer, Duration duration,
int totalEligibleVoters, boolean countLowerRanks, Component chatLogs, @NotNull Player startedByPlayer) {
this.chatLogs = chatLogs;
this.votedPlayer = votedPlayer;
this.totalEligibleVoters = totalEligibleVoters;
this.countLowerRanks = countLowerRanks;
this.server = server;
this.proxyServer = proxyServer;
instances.put(votedPlayer.getUsername(), this);
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.schedule(this::endVote,
duration.toMinutes(), TimeUnit.MINUTES);
this.startedByPlayer = startedByPlayer;
}
private RegisteredServer getServer() {
return server;
}
private void endVote() {
if (endedVote) {
return;
}
instances.remove(votedPlayer.getUsername());
if (votePassed()) {
mutePlayer();
return;
}
ComponentLike message = Utility.parseMiniMessage("<prefix> <red>The vote to mute <player> has failed, they will not be muted.</red>",
Placeholder.component("prefix", prefix), Placeholder.parsed("player", votedPlayer.getUsername()));
server.getPlayersConnected().stream()
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.forEach(player -> player.sendMessage(message));
}
public void start() {
ComponentLike message = getVoteStartMessage();
server.getPlayersConnected().stream()
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.forEach(player -> player.sendMessage(message));
}
public void vote(UUID uuid, boolean votedToMute) {
if (votedToMute) {
votedFor.add(uuid);
votedAgainst.remove(uuid);
if (!votePassed()) {
return;
}
endedVote = true;
instances.remove(votedPlayer.getUsername());
mutePlayer();
} else {
votedAgainst.add(uuid);
votedFor.remove(uuid);
}
}
public boolean votePassed() {
double totalVotes = (votedFor.size() + votedAgainst.size());
if (totalVotes == 0 || votedFor.isEmpty()) {
return false;
}
if (totalVotes / totalEligibleVoters < 0.6) {
return false;
}
return votedFor.size() / totalVotes > 0.6;
}
public boolean countLowerRanks() {
return countLowerRanks;
}
private void mutePlayer() {
ComponentLike message = Utility.parseMiniMessage("<prefix> <green>The vote to mute <player> has passed, they will be muted.</green>",
Placeholder.component("prefix", prefix), Placeholder.parsed("player", votedPlayer.getUsername()));
server.getPlayersConnected().stream()
.filter(player -> countLowerRanks ? player.hasPermission("chat.backup-vote-to-mute") : player.hasPermission("chat.vote-to-mute"))
.forEach(player -> player.sendMessage(message));
proxyServer.getCommandManager().executeAsync(proxyServer.getConsoleCommandSource(),
String.format("tempmute %s 1h Muted by the community - under review. -p", votedPlayer.getUsername()));
String chatLogsString = PlainTextComponentSerializer.plainText().serialize(chatLogs);
EmbedBuilder embedBuilder = buildMutedEmbed(chatLogsString);
ALogger.info(String.format("Player %s muted by vote\nLogs:\n%s\n\nVotes for:\n%s\nVotes against:\n%s\n",
votedPlayer.getUsername(),
chatLogsString,
parseUUIDsToPlayerOrString(votedFor),
parseUUIDsToPlayerOrString(votedAgainst)
));
long id = Config.serverChannelId.get("general");
DiscordLink.getPlugin().getBot().sendEmbedToDiscord(id, embedBuilder, -1);
}
@NotNull
private EmbedBuilder buildMutedEmbed(String chatLogsString) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor(votedPlayer.getUsername(), null, "https://crafatar.com/avatars/" + votedPlayer.getUniqueId() + "?overlay");
embedBuilder.setTitle("Player muted by vote");
embedBuilder.setColor(Color.CYAN);
embedBuilder.addField("Logs",
chatLogsString.substring(0, Math.min(chatLogsString.length(), 1024)),
false);
embedBuilder.addField("Server",
server.getServerInfo().getName().substring(0, 1).toUpperCase() + server.getServerInfo().getName().substring(1),
true);
embedBuilder.addField("Started by",
String.format("Username: %s\nUUID: %s", startedByPlayer.getUsername(), startedByPlayer.getUniqueId().toString()),
true);
return embedBuilder;
}
private String parseUUIDsToPlayerOrString(Collection<UUID> uuids) {
return uuids.stream().map(uuid -> {
Optional<Player> player = proxyServer.getPlayer(uuid);
if (player.isPresent()) {
return player.get().getUsername();
}
return uuid.toString();
}).collect(Collectors.joining("\n"));
}
public void addEligibleVoter(Player player) {
UUID uuid = player.getUniqueId();
if (votedAgainst.contains(uuid) || votedFor.contains(uuid)) {
return;
}
totalEligibleVoters++;
player.sendMessage(getVoteStartMessage());
}
public void removeEligibleVoter(UUID uuid) {
if (votedFor.contains(uuid) || votedAgainst.contains(uuid)) {
return;
}
totalEligibleVoters--;
}
private ComponentLike getVoteStartMessage() {
return Utility.parseMiniMessage(
String.format("""
<prefix> <green>A vote to mute <player> for one hour has been started, please read the logs below before voting.</green>
<logs>
<prefix> Click: <click:run_command:'/votetomutehelper vote %s yes'><red>Mute</red></click> --- <click:run_command:'/votetomutehelper vote %s no'><yellow>Don't mute</yellow></click>""",
votedPlayer.getUsername(), votedPlayer.getUsername()),
Placeholder.component("prefix", prefix),
Placeholder.parsed("player", votedPlayer.getUsername()),
Placeholder.component("logs", chatLogs));
}
public Player getVotedPlayer() {
return votedPlayer;
}
public int getVotedFor() {
return votedFor.size();
}
public int getTotalEligibleVoters() {
return totalEligibleVoters;
}
}

View File

@ -1,132 +0,0 @@
package com.alttd.velocitychat.commands.vote_to_mute;
import com.alttd.chat.objects.chat_log.ChatLog;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.util.Utility;
import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class VoteToMuteStarter {
private static final HashMap<UUID, VoteToMuteStarter> instanceMap = new HashMap<>();
private static final Component prefix = Utility.parseMiniMessage("<gold>[VoteMute]</gold>").asComponent();
private final ChatLogHandler chatLogHandler;
private final Player votedPlayer;
private final Player commandSource;
private final String serverName;
private List<Component> parsedChatLogs;
private final boolean countLowerRanks;
public static Optional<VoteToMuteStarter> getInstance(UUID uuid) {
if (!instanceMap.containsKey(uuid)) {
return Optional.empty();
}
return Optional.of(instanceMap.get(uuid));
}
public VoteToMuteStarter(ChatLogHandler chatLogHandler, Player votedPlayer, Player commandSource, String serverName, boolean countLowerRanks) {
this.chatLogHandler = chatLogHandler;
this.votedPlayer = votedPlayer;
this.commandSource = commandSource;
this.serverName = serverName;
this.countLowerRanks = countLowerRanks;
instanceMap.put(commandSource.getUniqueId(), this);
}
public void start() {
chatLogHandler.retrieveChatLogs(votedPlayer.getUniqueId(), Duration.ofMinutes(10), serverName).whenCompleteAsync((chatLogs, throwable) -> {
if (throwable != null) {
commandSource.sendMessage(Utility.parseMiniMessage("<prefix> <red>Unable to retrieve messages</red> for player <player>",
Placeholder.component("prefix", prefix),
Placeholder.parsed("player", votedPlayer.getUsername())));
return;
}
parseChatLogs(chatLogs);
commandSource.sendMessage(Utility.parseMiniMessage(
"<prefix> <green>Please select up to 10 messages other players should see to decide their vote, seperated by comma's. " +
"Example: <gold>/votetomutehelper messages 1, 2, 5, 8</gold></green>", Placeholder.component("prefix", prefix)));
showPage(1);
});
}
private void parseChatLogs(List<ChatLog> chatLogs) {
TagResolver.Single playerTag = Placeholder.parsed("player", votedPlayer.getUsername());
TagResolver.Single prefixTag = Placeholder.component("prefix", prefix);
chatLogs.sort(Comparator.comparing(ChatLog::getTimestamp).reversed());
parsedChatLogs = IntStream.range(0, chatLogs.size())
.mapToObj(i -> Utility.parseMiniMessage(
"<number>. <prefix> <player>: <message>",
TagResolver.resolver(
Placeholder.unparsed("message", chatLogs.get(i).getMessage()),
Placeholder.parsed("number", String.valueOf(i + 1)),
playerTag,
prefixTag
)).asComponent()
)
.toList();
}
public void showPage(int page) {
List<Component> collect = parsedChatLogs.stream().skip((page - 1) * 10L).limit(10L).toList();
Component chatLogsComponent = Component.join(JoinConfiguration.newlines(), collect);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<prefix> ChatLogs for <player>\n<logs>\n");
if (page > 1) {
stringBuilder.append("<click:run_command:/votetomutehelper page ")
.append(page - 1)
.append("><hover:show_text:'<gold>Click to go to previous page'><gold><previous page></gold></hover></click> ");
}
if (parsedChatLogs.size() > page * 10) {
stringBuilder.append("<click:run_command:/votetomutehelper page ")
.append(page + 1)
.append("><hover:show_text:'<gold>Click to go to next page'><gold><next page></gold></hover></click> ");
}
commandSource.sendMessage(Utility.parseMiniMessage(stringBuilder.toString(),
Placeholder.parsed("player", votedPlayer.getUsername()),
Placeholder.component("prefix", prefix),
Placeholder.component("logs", chatLogsComponent)));
}
/**
* Retrieves the chat logs for the given list of IDs. It removes 1 from the IDs before using them It removes the
* instance from the hashmap after this function call
*
* @param ids A list of integers representing the IDs of the chat logs to retrieve.
*
* @return A Component object containing the selected chat logs joined by newlines.
*/
public Component getChatLogsAndClose(List<Integer> ids) {
List<Component> selectedChatLogs = ids.stream()
.filter(id -> id >= 1 && id <= parsedChatLogs.size())
.map(id -> parsedChatLogs.get(id - 1))
.collect(Collectors.toList());
instanceMap.remove(commandSource.getUniqueId());
return Component.join(JoinConfiguration.newlines(), selectedChatLogs);
}
public int getTotalPages() {
return (int) Math.ceil((double) parsedChatLogs.size() / 10);
}
public Player getVotedPlayer() {
return votedPlayer;
}
public int getTotalLogEntries() {
return parsedChatLogs.size();
}
public boolean countLowerRanks() {
return countLowerRanks;
}
}

View File

@ -3,7 +3,7 @@ package com.alttd.velocitychat.data;
import com.alttd.chat.config.ServerConfig; import com.alttd.chat.config.ServerConfig;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import java.util.UUID; import java.util.UUID;
@ -31,7 +31,8 @@ public class ServerWrapper {
return serverName; return serverName;
} }
public boolean globalChat() { public boolean globalChat()
{
return globalChat; return globalChat;
} }
@ -39,11 +40,10 @@ public class ServerWrapper {
return joinMessages; return joinMessages;
} }
public void sendJoinLeaveMessage(UUID uuid, ComponentLike component) { public void sendJoinLeaveMessage(UUID uuid, Component component) {
if (joinMessages()) { if(joinMessages())
getRegisteredServer().getPlayersConnected().stream() getRegisteredServer().getPlayersConnected().stream()
.filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid)) .filter(p -> !ChatUserManager.getChatUser(p.getUniqueId()).getIgnoredPlayers().contains(uuid))
.forEach(p -> p.sendMessage(component)); .forEach(p -> p.sendMessage(component));
}
} }
} }

View File

@ -6,8 +6,6 @@ import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager; import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.managers.RegexManager; import com.alttd.chat.managers.RegexManager;
import com.alttd.chat.objects.*; import com.alttd.chat.objects.*;
import com.alttd.chat.objects.chat_log.ChatLogHandler;
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
import com.alttd.chat.util.ALogger; import com.alttd.chat.util.ALogger;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.VelocityChat; import com.alttd.velocitychat.VelocityChat;
@ -16,50 +14,29 @@ import com.google.common.io.ByteStreams;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.ServerConnection;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.user.User;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NonNull;
import java.time.Duration; import java.time.Duration;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Slf4j
public class ChatHandler { public class ChatHandler {
private final ChatLogHandler chatLogHandler;
private final LuckPerms luckPerms;
public ChatHandler(ChatLogHandler chatLogHandler, LuckPerms luckPerms) {
this.chatLogHandler = chatLogHandler;
this.luckPerms = luckPerms;
}
public void privateMessage(String sender, String target, String message) { public void privateMessage(String sender, String target, String message) {
UUID uuid = UUID.fromString(sender); UUID uuid = UUID.fromString(sender);
ChatUser senderUser = ChatUserManager.getChatUser(uuid); ChatUser senderUser = ChatUserManager.getChatUser(uuid);
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid); Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid);
if (optionalPlayer.isEmpty()) { if(optionalPlayer.isEmpty()) return;
return;
}
Player player = optionalPlayer.get(); Player player = optionalPlayer.get();
Optional<Player> optionalPlayer2 = VelocityChat.getPlugin().getProxy().getPlayer(target); Optional<Player> optionalPlayer2 = VelocityChat.getPlugin().getProxy().getPlayer(target);
if (optionalPlayer2.isEmpty()) { if(optionalPlayer2.isEmpty()) return;
return;
}
Player player2 = optionalPlayer2.get(); Player player2 = optionalPlayer2.get();
ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId()); ChatUser targetUser = ChatUserManager.getChatUser(player2.getUniqueId());
@ -69,23 +46,15 @@ public class ChatHandler {
Placeholder.component("receiver", targetUser.getDisplayName()), Placeholder.component("receiver", targetUser.getDisplayName()),
Placeholder.unparsed("receivername", player2.getUsername()), Placeholder.unparsed("receivername", player2.getUsername()),
Placeholder.component("message", GsonComponentSerializer.gson().deserialize(message)), Placeholder.component("message", GsonComponentSerializer.gson().deserialize(message)),
Placeholder.unparsed("server", Placeholder.unparsed("server", player.getCurrentServer().isPresent() ? player.getCurrentServer().get().getServerInfo().getName() : "Altitude"));
player.getCurrentServer().isPresent() ? player.getCurrentServer()
.get()
.getServerInfo()
.getName() : "Altitude"
)
);
ServerConnection serverConnection; ServerConnection serverConnection;
//Log handled on receiving server since it is only received on one server and that's where the ignore check happens if(player.getCurrentServer().isPresent() && player2.getCurrentServer().isPresent()) {
if (player.getCurrentServer().isPresent() && player2.getCurrentServer().isPresent()) {
// redirect to the sender // redirect to the sender
serverConnection = player.getCurrentServer().get(); serverConnection = player.getCurrentServer().get();
Component component = Utility.parseMiniMessage(Config.MESSAGESENDER Component component = Utility.parseMiniMessage(Config.MESSAGESENDER
.replaceAll("<sendername>", player.getUsername()) .replaceAll("<sendername>", player.getUsername())
.replaceAll("<receivername>", player2.getUsername()), Placeholders .replaceAll("<receivername>", player2.getUsername()), Placeholders);
).asComponent();
ByteArrayDataOutput buf = ByteStreams.newDataOutput(); ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessageout"); buf.writeUTF("privatemessageout");
buf.writeUTF(player.getUniqueId().toString()); buf.writeUTF(player.getUniqueId().toString());
@ -98,8 +67,7 @@ public class ChatHandler {
serverConnection = player2.getCurrentServer().get(); serverConnection = player2.getCurrentServer().get();
component = Utility.parseMiniMessage(Config.MESSAGERECIEVER component = Utility.parseMiniMessage(Config.MESSAGERECIEVER
.replaceAll("<sendername>", player.getUsername()) .replaceAll("<sendername>", player.getUsername())
.replaceAll("<receivername>", player2.getUsername()), Placeholders .replaceAll("<receivername>", player2.getUsername()), Placeholders);
).asComponent();
buf = ByteStreams.newDataOutput(); buf = ByteStreams.newDataOutput();
buf.writeUTF("privatemessagein"); buf.writeUTF("privatemessagein");
buf.writeUTF(player2.getUniqueId().toString()); buf.writeUTF(player2.getUniqueId().toString());
@ -118,24 +86,24 @@ public class ChatHandler {
Placeholder.unparsed("target", (target.isEmpty() ? " tried to say: " : " -> " + target + ": ")), Placeholder.unparsed("target", (target.isEmpty() ? " tried to say: " : " -> " + target + ": ")),
Placeholder.unparsed("input", input) Placeholder.unparsed("input", input)
); );
ComponentLike blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, Placeholders); Component blockedNotification = Utility.parseMiniMessage(Config.NOTIFICATIONFORMAT, Placeholders);
serverConnection.getServer().getPlayersConnected().forEach(pl -> { serverConnection.getServer().getPlayersConnected().forEach(pl ->{
if (pl.hasPermission("chat.alert-blocked")) { if (pl.hasPermission("chat.alert-blocked")) {
pl.sendMessage(blockedNotification); pl.sendMessage(blockedNotification);
} }
}); });
player.sendMessage(Utility.parseMiniMessage("<red>The language you used in your message is not allowed, " + player.sendMessage(Utility.parseMiniMessage("<red>The language you used in your message is not allowed, " +
"this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>")); "this constitutes as your only warning. Any further attempts at bypassing the filter will result in staff intervention.</red>"));
} }
public void sendPartyMessage(Party party, Component message, @Nullable List<UUID> ignoredPlayers) { public void sendPartyMessage(Party party, Component message, @Nullable List<UUID> ignoredPlayers)
{
VelocityChat.getPlugin().getProxy().getAllPlayers().stream() VelocityChat.getPlugin().getProxy().getAllPlayers().stream()
.filter(pl -> { .filter(pl -> {
UUID uuid = pl.getUniqueId(); UUID uuid = pl.getUniqueId();
if (ignoredPlayers != null && ignoredPlayers.contains(uuid)) { if (ignoredPlayers != null && ignoredPlayers.contains(uuid))
return false; return false;
}
return party.getPartyUsers().stream().anyMatch(pu -> pu.getUuid().equals(uuid)); return party.getPartyUsers().stream().anyMatch(pu -> pu.getUuid().equals(uuid));
}).forEach(pl -> { }).forEach(pl -> {
pl.sendMessage(message); pl.sendMessage(message);
@ -144,178 +112,81 @@ public class ChatHandler {
}); });
} }
public void sendPartyMessageFromWeb(UUID uuid, Party party, Component senderName, String message) {
Utility.getOrLoadUser(uuid).thenAccept(user -> sendPartyMessageFromWeb(uuid, party, senderName, message, user));
}
public void sendPartyMessageFromWeb(UUID uuid, Party party, Component senderName, String message, User user) {
Optional<ParsedPartyMessage> optionalParsedPartyMessage = getResult(uuid,
message,
null,
null,
senderName,
party,
user.getUsername(),
user,
null
);
if (optionalParsedPartyMessage.isEmpty()) {
log.error("Failed to parse party message: {}", message);
return;
}
ParsedPartyMessage parsedPartyMessage = optionalParsedPartyMessage.get();
chatLogHandler.addChatLog(uuid,
"web",
message,
ChatLogType.PARTY,
null,
null,
parsedPartyMessage.partyMessage(),
false
);
}
public void sendPartyMessage(UUID uuid, String message, Component item, ServerConnection serverConnection) { public void sendPartyMessage(UUID uuid, String message, Component item, ServerConnection serverConnection) {
Utility.getOrLoadUser(uuid).thenAccept(user -> sendPartyMessage(uuid, message, item, serverConnection, user));
}
public void sendPartyMessage(UUID uuid, String message, Component item, ServerConnection serverConnection, User user) {
Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid); Optional<Player> optionalPlayer = VelocityChat.getPlugin().getProxy().getPlayer(uuid);
if (optionalPlayer.isEmpty()) { if (optionalPlayer.isEmpty()) return;
return;
}
Player player = optionalPlayer.get(); Player player = optionalPlayer.get();
ChatUser chatUser = ChatUserManager.getChatUser(uuid); ChatUser user = ChatUserManager.getChatUser(uuid);
Party party = PartyManager.getParty(chatUser.getPartyId()); Party party = PartyManager.getParty(user.getPartyId());
if (party == null) { if (party == null) {
player.sendMessage(Utility.parseMiniMessage(Config.NOT_IN_A_PARTY)); player.sendMessage(Utility.parseMiniMessage(Config.NOT_IN_A_PARTY));
return; return;
} }
ComponentLike senderName = chatUser.getDisplayName(); Component senderName = user.getDisplayName();
Optional<ParsedPartyMessage> optionalParsedPartyMessage = getResult(uuid, TagResolver Placeholders = TagResolver.resolver(
message,
item,
serverConnection,
senderName,
party,
player.getUsername(),
user,
player
);
if (optionalParsedPartyMessage.isEmpty()) {
return; // the message was blocked
}
ParsedPartyMessage parsedPartyMessage = optionalParsedPartyMessage.get();
sendPartyMessage(party, parsedPartyMessage.partyMessage(), chatUser.getIgnoredBy());
chatLogHandler.addChatLog(uuid,
serverConnection.getServer().getServerInfo().getName(),
PlainTextComponentSerializer.plainText().serialize(parsedPartyMessage.partyMessage()),
ChatLogType.PARTY,
String.valueOf(party.getPartyId()),
null,
parsedPartyMessage.partyMessage(),
false
);
ComponentLike spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, parsedPartyMessage.placeholders());
for (Player pl : serverConnection.getServer().getPlayersConnected()) {
if (pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) {
pl.sendMessage(spyMessage);
}
}
ALogger.info(PlainTextComponentSerializer.plainText().serialize(parsedPartyMessage.partyMessage()));
}
private Optional<ParsedPartyMessage> getResult(UUID uuid, String message, @Nullable Component item,
@Nullable ServerConnection serverConnection, ComponentLike senderName,
Party party, String playerName, User user, @Nullable Player player) {
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("sender", senderName), Placeholder.component("sender", senderName),
Placeholder.component("sendername", senderName), Placeholder.component("sendername", senderName),
Placeholder.unparsed("partyname", party.getPartyName()), Placeholder.unparsed("partyname", party.getPartyName()),
Placeholder.component("message", parseMessageContent(user, message)), Placeholder.component("message", parseMessageContent(player, message)),
Placeholder.unparsed("server", Placeholder.unparsed("server", serverConnection.getServer().getServerInfo().getName())
serverConnection != null ? serverConnection.getServer().getServerInfo().getName() : "web"
)
); );
Component partyMessage;
if (item != null) { Component partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, Placeholders)
partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, placeholders).asComponent() .replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(item).build());
.replaceText(TextReplacementConfig.builder().once().matchLiteral("[i]").replacement(item).build());
} else {
partyMessage = Utility.parseMiniMessage(Config.PARTY_FORMAT, placeholders).asComponent();
}
ModifiableString modifiableString = new ModifiableString(partyMessage); ModifiableString modifiableString = new ModifiableString(partyMessage);
if (!RegexManager.filterText(playerName, uuid, modifiableString, "party")) { if (!RegexManager.filterText(player.getUsername(), uuid, modifiableString, "party")) {
if (serverConnection != null && player != null) { sendBlockedNotification("Party Language", player, message, "", serverConnection);
sendBlockedNotification("Party Language", player, message, "", serverConnection); return; // the message was blocked
}
return Optional.empty();
} }
partyMessage = modifiableString.component(); partyMessage = modifiableString.component();
return Optional.of(new ParsedPartyMessage(placeholders, partyMessage));
sendPartyMessage(party, partyMessage, user.getIgnoredBy());
Component spyMessage = Utility.parseMiniMessage(Config.PARTY_SPY, Placeholders);
for(Player pl : serverConnection.getServer().getPlayersConnected()) {
if(pl.hasPermission(Config.SPYPERMISSION) && !party.getPartyUsersUuid().contains(pl.getUniqueId())) {
pl.sendMessage(spyMessage);
}
}
ALogger.info(PlainTextComponentSerializer.plainText().serialize(partyMessage));
} }
public void globalAdminChat(String message) { public void globalAdminChat(String message) {
Component component = GsonComponentSerializer.gson().deserialize(message); Component component = GsonComponentSerializer.gson().deserialize(message);
VelocityChat.getPlugin().getProxy().getAllPlayers() VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.chat.globaladminchat")/*TODO permission*/).forEach(target -> {
.stream() target.sendMessage(component);
.filter(target -> target.hasPermission("command.chat.globaladminchat")) });
.forEach(target -> target.sendMessage(component));
chatLogHandler.addChatLog(
new UUID(0, 0), //No origin uuid for GAC
"GAC", //No server for GAC
PlainTextComponentSerializer.plainText().serialize(component),
ChatLogType.GAC,
null,
null,
component,
false
);
} }
public void globalAdminChat(CommandSource commandSource, String message) { public void globalAdminChat(CommandSource commandSource, String message) {
ComponentLike senderName = Component.text(Config.CONSOLENAME); Component senderName = Component.text(Config.CONSOLENAME);
String serverName = "Altitude"; String serverName = "Altitude";
if (commandSource instanceof Player sender) { if (commandSource instanceof Player) {
ChatUser chatUser = ChatUserManager.getChatUser(sender.getUniqueId()); Player sender = (Player) commandSource;
if (chatUser == null) { ChatUser user = ChatUserManager.getChatUser(sender.getUniqueId());
return; if(user == null) return;
} senderName = user.getDisplayName();
senderName = chatUser.getDisplayName(); serverName = sender.getCurrentServer().isPresent() ? sender.getCurrentServer().get().getServerInfo().getName() : "Altitude";
serverName = sender.getCurrentServer().isPresent() ? sender.getCurrentServer()
.get()
.getServerInfo()
.getName() : "Altitude";
} }
TagResolver Placeholders = TagResolver.resolver( TagResolver Placeholders = TagResolver.resolver(
Placeholder.component("message", parseMessageContent(commandSource, message)), Placeholder.component("message", parseMessageContent(commandSource, message)),
Placeholder.component("sender", senderName), Placeholder.component("sender", senderName),
Placeholder.unparsed("server", serverName) Placeholder.unparsed("server", serverName));
);
ComponentLike component = Utility.parseMiniMessage(Config.GACFORMAT, Placeholders); Component component = Utility.parseMiniMessage(Config.GACFORMAT, Placeholders);
VelocityChat.getPlugin().getProxy().getAllPlayers() VelocityChat.getPlugin().getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.chat.globaladminchat")/*TODO permission*/).forEach(target -> {
.stream() target.sendMessage(component);
.filter(target -> target.hasPermission("command.chat.globaladminchat")) });
.forEach(target -> target.sendMessage(component));
} }
public void sendMail(CommandSource commandSource, String recipient, String message) { public void sendMail(CommandSource commandSource, String recipient, String message) {
UUID uuid = Config.CONSOLEUUID; UUID uuid = Config.CONSOLEUUID;;
String senderName = Config.CONSOLENAME; String senderName = Config.CONSOLENAME;
UUID targetUUID; UUID targetUUID;
if (commandSource instanceof Player player) { if (commandSource instanceof Player player) {
@ -326,8 +197,7 @@ public class ChatHandler {
if (optionalPlayer.isEmpty()) { if (optionalPlayer.isEmpty()) {
targetUUID = ServerHandler.getPlayerUUID(recipient); targetUUID = ServerHandler.getPlayerUUID(recipient);
if (targetUUID == null) { if (targetUUID == null) {
commandSource.sendMessage(Utility.parseMiniMessage( commandSource.sendMessage(Utility.parseMiniMessage("<red>A player with this name hasn't logged in recently.")); // TOOD load from config
"<red>A player with this name hasn't logged in recently.")); // TOOD load from config
return; return;
} }
} else { } else {
@ -335,10 +205,6 @@ public class ChatHandler {
} }
Mail mail = new Mail(targetUUID, uuid, message); Mail mail = new Mail(targetUUID, uuid, message);
ChatUser chatUser = ChatUserManager.getChatUser(targetUUID); ChatUser chatUser = ChatUserManager.getChatUser(targetUUID);
if (chatUser.getIgnoredPlayers().contains(uuid)) {
commandSource.sendMessage(Utility.parseMiniMessage("<red>You cannot mail this player</red>"));
return;
}
chatUser.addMail(mail); chatUser.addMail(mail);
// TODO load from config // TODO load from config
String finalSenderName = senderName; String finalSenderName = senderName;
@ -378,7 +244,7 @@ public class ChatHandler {
} }
private Component parseMails(List<Mail> mails, boolean mark) { private Component parseMails(List<Mail> mails, boolean mark) {
Component component = Utility.parseMiniMessage(Config.mailHeader).asComponent(); Component component = Utility.parseMiniMessage(Config.mailHeader);
for (Mail mail : mails) { for (Mail mail : mails) {
if (mail.isUnRead() && mark) { if (mail.isUnRead() && mark) {
mail.setReadTime(System.currentTimeMillis()); mail.setReadTime(System.currentTimeMillis());
@ -391,17 +257,29 @@ public class ChatHandler {
Placeholder.component("sender", chatUser.getDisplayName()), Placeholder.component("sender", chatUser.getDisplayName()),
Placeholder.component("message", Utility.parseMiniMessage(mail.getMessage())), Placeholder.component("message", Utility.parseMiniMessage(mail.getMessage())),
Placeholder.unparsed("date", date.toString()), Placeholder.unparsed("date", date.toString()),
Placeholder.unparsed("time_ago", Placeholder.unparsed("time_ago", getTimeAgo(Duration.between(date.toInstant(), new Date().toInstant())))
getTimeAgo(Duration.between(date.toInstant(), new Date().toInstant()))
)
); );
ComponentLike mailMessage = Utility.parseMiniMessage(Config.mailBody, Placeholders); Component mailMessage = Utility.parseMiniMessage(Config.mailBody, Placeholders);
component = component.append(Component.newline()).append(mailMessage); component = component.append(Component.newline()).append(mailMessage);
} }
component = component.append(Component.newline()).append(Utility.parseMiniMessage(Config.mailFooter)); component = component.append(Component.newline()).append(Utility.parseMiniMessage(Config.mailFooter));
return component; return component;
} }
public void partyChat(String partyId, UUID uuid, Component message) {
Party party = PartyManager.getParty(Integer.parseInt(partyId));
if (party == null) {
ALogger.warn("Received a non existent party");
return;
}
List<UUID> ignoredPlayers = ChatUserManager.getChatUser(uuid).getIgnoredPlayers();
List<UUID> partyUsersUuid = party.getPartyUsersUuid();
VelocityChat.getPlugin().getProxy().getAllPlayers().stream()
.filter(p -> partyUsersUuid.contains(p.getUniqueId()))
.filter(p -> !ignoredPlayers.contains(p.getUniqueId()))
.forEach(p -> p.sendMessage(message));
}
public void mutePlayer(String uuid, boolean muted) { public void mutePlayer(String uuid, boolean muted) {
ByteArrayDataOutput buf = ByteStreams.newDataOutput(); ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("chatpunishments"); buf.writeUTF("chatpunishments");
@ -411,44 +289,26 @@ public class ChatHandler {
private String getTimeAgo(Duration duration) { private String getTimeAgo(Duration duration) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
if (duration.toDays() != 0) { if (duration.toDays() != 0)
stringBuilder.append(duration.toDays()).append("d "); stringBuilder.append(duration.toDays()).append("d ");
} if (duration.toHoursPart() != 0 || !stringBuilder.isEmpty())
if (duration.toHoursPart() != 0 || !stringBuilder.isEmpty()) {
stringBuilder.append(duration.toHoursPart()).append("h "); stringBuilder.append(duration.toHoursPart()).append("h ");
}
stringBuilder.append(duration.toMinutesPart()).append("m ago"); stringBuilder.append(duration.toMinutesPart()).append("m ago");
return stringBuilder.toString(); return stringBuilder.toString();
} }
private Component parseMessageContent(User user, String rawMessage) { private Component parseMessageContent(CommandSource source, String rawMessage) {
TagResolver.Builder tagResolver = TagResolver.builder(); TagResolver.Builder tagResolver = TagResolver.builder();
Utility.formattingPerms.forEach((perm, pair) -> { Utility.formattingPerms.forEach((perm, pair) -> {
if (Utility.hasPermission(user, perm)) { if (source.hasPermission(perm)) {
tagResolver.resolver(pair.getX()); tagResolver.resolver(pair.getX());
} }
}); });
return getComponent(rawMessage, tagResolver);
}
private Component parseMessageContent(CommandSource commandSource, String rawMessage) {
TagResolver.Builder tagResolver = TagResolver.builder();
Utility.formattingPerms.forEach((perm, pair) -> {
if (commandSource.hasPermission(perm)) {
tagResolver.resolver(pair.getX());
}
});
return getComponent(rawMessage, tagResolver);
}
private static @NonNull Component getComponent(String rawMessage, TagResolver.Builder tagResolver) {
MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build(); MiniMessage miniMessage = MiniMessage.builder().tags(tagResolver.build()).build();
Component component = miniMessage.deserialize(rawMessage); Component component = miniMessage.deserialize(rawMessage);
for (ChatFilter chatFilter : RegexManager.getEmoteFilters()) { for(ChatFilter chatFilter : RegexManager.getEmoteFilters()) {
component = component.replaceText( component = component.replaceText(
TextReplacementConfig.builder() TextReplacementConfig.builder()
.times(Config.EMOTELIMIT) .times(Config.EMOTELIMIT)
@ -457,5 +317,6 @@ public class ChatHandler {
} }
return component; return component;
} }
} }

View File

@ -1,8 +0,0 @@
package com.alttd.velocitychat.handlers;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
public record ParsedPartyMessage(TagResolver placeholders, Component partyMessage) {
}

View File

@ -5,20 +5,24 @@ import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.VelocityChat; import com.alttd.velocitychat.VelocityChat;
import com.alttd.velocitychat.events.GlobalAdminChatEvent; import com.alttd.velocitychat.events.GlobalAdminChatEvent;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.util.ArrayList;
import java.util.List;
// TODO code CLEANUP
public class ChatListener { public class ChatListener {
private final VelocityChat plugin; private VelocityChat plugin;
public ChatListener() { public ChatListener() {
plugin = VelocityChat.getPlugin(); plugin = VelocityChat.getPlugin();
} }
@Subscribe(priority = 0) @Subscribe(order = PostOrder.FIRST)
public void onGlobalStaffChat(GlobalAdminChatEvent event) { public void onGlobalStaffChat(GlobalAdminChatEvent event) {
String senderName = Config.CONSOLENAME; String senderName = Config.CONSOLENAME;
String serverName = "Altitude"; String serverName = "Altitude";
@ -26,20 +30,18 @@ public class ChatListener {
if (commandSource instanceof Player) { if (commandSource instanceof Player) {
Player sender = (Player) event.getSender(); Player sender = (Player) event.getSender();
senderName = sender.getUsername(); senderName = sender.getUsername();
serverName = sender.getCurrentServer().isPresent() serverName = sender.getCurrentServer().isPresent() ? sender.getCurrentServer().get().getServerInfo().getName() : "Altitude";
? sender.getCurrentServer().get().getServerInfo().getName() : "Altitude";
} }
ComponentLike message = Utility.parseMiniMessage(Config.GACFORMAT, Component message = Utility.parseMiniMessage(Config.GACFORMAT,
Placeholder.parsed("sender", senderName), Placeholder.parsed("sender", senderName),
Placeholder.component("message", Utility.parseMiniMessage(event.getMessage())), Placeholder.component("message", Utility.parseMiniMessage(event.getMessage())),
Placeholder.parsed("server", serverName) Placeholder.parsed("server", serverName)
); );
plugin.getProxy().getAllPlayers() plugin.getProxy().getAllPlayers().stream().filter(target -> target.hasPermission("command.chat.globaladminchat")).forEach(target -> {
.stream() target.sendMessage(message);
.filter(target -> target.hasPermission("command.chat.globaladminchat")) });
.forEach(target -> target.sendMessage(message));
} }
} }

View File

@ -1,122 +1,87 @@
package com.alttd.velocitychat.listeners; package com.alttd.velocitychat.listeners;
import com.alttd.chat.config.Config;
import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.ChatUserManager;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.ChatUser;
import com.alttd.chat.objects.Mail; import com.alttd.chat.objects.Mail;
import com.alttd.chat.objects.Party;
import com.alttd.chat.util.Utility; import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.VelocityChat; import com.alttd.velocitychat.VelocityChat;
import com.alttd.velocitychat.commands.vote_to_mute.ActiveVoteToMute; import com.alttd.chat.config.Config;
import com.alttd.velocitychat.data.ServerWrapper; import com.alttd.velocitychat.data.ServerWrapper;
import com.alttd.velocitychat.handlers.ServerHandler; import com.alttd.velocitychat.handlers.ServerHandler;
import com.alttd.chat.managers.PartyManager;
import com.alttd.chat.objects.Party;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.LoginEvent; import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.player.ServerPostConnectEvent; import com.velocitypowered.api.event.player.ServerPostConnectEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.title.Title;
import java.util.HashSet; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class ProxyPlayerListener { public class ProxyPlayerListener {
@Subscribe(priority = 0) @Subscribe(order = PostOrder.FIRST)
public void onPlayerLogin(LoginEvent event) { public void onPlayerLogin(LoginEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
UUID uuid = player.getUniqueId(); UUID uuid = player.getUniqueId();
Party party = PartyManager.getParty(event.getPlayer().getUniqueId()); Party party = PartyManager.getParty(event.getPlayer().getUniqueId());
if (party == null) { if (party == null) return;
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(uuid); ChatUser chatUser = ChatUserManager.getChatUser(uuid);
if (chatUser == null) { if (chatUser == null)
return; return;
}
VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party, VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
Utility.parseMiniMessage(Config.PARTY_MEMBER_LOGGED_ON, Utility.parseMiniMessage(Config.PARTY_MEMBER_LOGGED_ON,
Placeholder.component("player", chatUser.getDisplayName()) Placeholder.component("player", chatUser.getDisplayName())
).asComponent(), ),
chatUser.getIgnoredPlayers()); chatUser.getIgnoredPlayers());
// TODO setup ChatUser on Proxy // TODO setup ChatUser on Proxy
//VelocityChat.getPlugin().getChatHandler().addPlayer(new ChatPlayer(event.getPlayer().getUniqueId())); //VelocityChat.getPlugin().getChatHandler().addPlayer(new ChatPlayer(event.getPlayer().getUniqueId()));
ServerHandler.addPlayerUUID(player.getUsername(), uuid); ServerHandler.addPlayerUUID(player.getUsername(), uuid);
} }
@Subscribe(priority = 4) @Subscribe(order = PostOrder.LAST)
public void afterPlayerLogin(ServerPostConnectEvent event) { public void afterPlayerLogin(ServerPostConnectEvent event) {
if (event.getPreviousServer() != null)
return;
Player player = event.getPlayer(); Player player = event.getPlayer();
RegisteredServer previousServer = event.getPreviousServer();
if (previousServer != null) {
ActiveVoteToMute.removePotentialVoter(player, previousServer);
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isEmpty()) {
return;
}
ActiveVoteToMute.addPotentialVoter(player, currentServer.get());
return;
}
Optional<ServerConnection> currentServer = player.getCurrentServer();
if (currentServer.isEmpty()) {
return;
}
ActiveVoteToMute.addPotentialVoter(player, currentServer.get());
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
List<Mail> unReadMail = chatUser.getUnReadMail(); List<Mail> unReadMail = chatUser.getUnReadMail();
if (unReadMail.isEmpty()) { if (unReadMail.isEmpty())
return; return;
} player.sendMessage(Utility.parseMiniMessage(Config.mailUnread,
VelocityChat plugin = VelocityChat.getPlugin(); Placeholder.unparsed("amount", String.valueOf(unReadMail.size()))
plugin.getProxy().getScheduler().buildTask(plugin, () -> { ));
if (!player.isActive()) {
return;
}
ComponentLike message = Utility.parseMiniMessage(Config.mailUnread,
Placeholder.unparsed("amount", String.valueOf(unReadMail.size())));
player.sendMessage(message);
player.showTitle(Title.title(message.asComponent(), Component.empty()));
}).delay(Config.mailDisplayDelay * 50L, TimeUnit.MILLISECONDS).schedule();
} }
@Subscribe @Subscribe
public void quitEvent(DisconnectEvent event) { public void quitEvent(DisconnectEvent event) {
ActiveVoteToMute.removePotentialVoter(event.getPlayer(), null);
UUID uuid = event.getPlayer().getUniqueId(); UUID uuid = event.getPlayer().getUniqueId();
Party party = PartyManager.getParty(event.getPlayer().getUniqueId()); Party party = PartyManager.getParty(event.getPlayer().getUniqueId());
if (party == null) { if (party == null) return;
return;
}
ChatUser chatUser = ChatUserManager.getChatUser(uuid); ChatUser chatUser = ChatUserManager.getChatUser(uuid);
if (chatUser == null) { if (chatUser == null)
return; return;
}
VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party, VelocityChat.getPlugin().getChatHandler().sendPartyMessage(party,
Utility.parseMiniMessage(Config.PARTY_MEMBER_LOGGED_OFF, Utility.parseMiniMessage(Config.PARTY_MEMBER_LOGGED_OFF,
Placeholder.component("player", chatUser.getDisplayName()) Placeholder.component("player", chatUser.getDisplayName())
).asComponent(), ),
chatUser.getIgnoredPlayers()); chatUser.getIgnoredPlayers());
// TODO setup ChatUser on Proxy // TODO setup ChatUser on Proxy
//VelocityChat.getPlugin().getChatHandler().removePlayer(event.getPlayer().getUniqueId()); //VelocityChat.getPlugin().getChatHandler().removePlayer(event.getPlayer().getUniqueId());
} }
private static final HashSet<UUID> silentJoin = new HashSet<>(); private static final HashSet<UUID> silentJoin = new HashSet<>();
public static void addSilentJoin(UUID uuid) { public static void addSilentJoin(UUID uuid)
{
silentJoin.add(uuid); silentJoin.add(uuid);
} }
@ -133,11 +98,11 @@ public class ProxyPlayerListener {
Placeholder.parsed("player", player.getUsername()), Placeholder.parsed("player", player.getUsername()),
Placeholder.parsed("from_server", previousServer.getServerInfo().getName()), Placeholder.parsed("from_server", previousServer.getServerInfo().getName()),
Placeholder.parsed("to_server", event.getServer().getServerInfo().getName()) Placeholder.parsed("to_server", event.getServer().getServerInfo().getName())
); );
if (silentJoin.remove(uuid)) { if (silentJoin.remove(uuid)) {
ComponentLike message = Utility.parseMiniMessage(Config.SILENT_JOIN_JOINED_FROM, Component message = Utility.parseMiniMessage(Config.SILENT_JOIN_JOINED_FROM,
placeholders); placeholders);
event.getServer().getPlayersConnected().stream() event.getServer().getPlayersConnected().stream()
.filter(player1 -> player1.hasPermission("command.chat.silent-join-notify")) .filter(player1 -> player1.hasPermission("command.chat.silent-join-notify"))
.forEach(player1 -> player1.sendMessage(message)); .forEach(player1 -> player1.sendMessage(message));
@ -145,17 +110,17 @@ public class ProxyPlayerListener {
} }
ServerWrapper wrapper = serverHandler.getWrapper(previousServer.getServerInfo().getName()); ServerWrapper wrapper = serverHandler.getWrapper(previousServer.getServerInfo().getName());
if (wrapper != null) { if(wrapper != null) {
wrapper.sendJoinLeaveMessage(uuid, Utility.parseMiniMessage(Config.SERVERSWTICHMESSAGETO, placeholders)); wrapper.sendJoinLeaveMessage(uuid, Utility.parseMiniMessage(Config.SERVERSWTICHMESSAGETO, placeholders));
} }
wrapper = serverHandler.getWrapper(event.getServer().getServerInfo().getName()); wrapper = serverHandler.getWrapper(event.getServer().getServerInfo().getName());
if (wrapper != null) { if(wrapper != null) {
wrapper.sendJoinLeaveMessage(uuid, Utility.parseMiniMessage(Config.SERVERSWTICHMESSAGEFROM, placeholders)); wrapper.sendJoinLeaveMessage(uuid, Utility.parseMiniMessage(Config.SERVERSWTICHMESSAGEFROM, placeholders));
} }
} else { } else {
if (silentJoin.remove(uuid)) { if (silentJoin.remove(uuid)) {
ComponentLike message = Utility.parseMiniMessage(Config.SILENT_JOIN_JOINED, Component message = Utility.parseMiniMessage(Config.SILENT_JOIN_JOINED,
Placeholder.unparsed("player", player.getUsername())); Placeholder.unparsed("player", player.getUsername()));
event.getServer().getPlayersConnected().stream() event.getServer().getPlayersConnected().stream()
.filter(player1 -> player1.hasPermission("command.chat.silent-join-notify")) .filter(player1 -> player1.hasPermission("command.chat.silent-join-notify"))
.forEach(player1 -> player1.sendMessage(message)); .forEach(player1 -> player1.sendMessage(message));
@ -163,7 +128,7 @@ public class ProxyPlayerListener {
} }
ServerWrapper wrapper = serverHandler.getWrapper(event.getServer().getServerInfo().getName()); ServerWrapper wrapper = serverHandler.getWrapper(event.getServer().getServerInfo().getName());
if (wrapper != null) { if(wrapper != null) {
wrapper.sendJoinLeaveMessage(uuid, Utility.parseMiniMessage(Config.SERVERJOINMESSAGE, Placeholder.unparsed("player", player.getUsername()))); wrapper.sendJoinLeaveMessage(uuid, Utility.parseMiniMessage(Config.SERVERJOINMESSAGE, Placeholder.unparsed("player", player.getUsername())));
} }
} }
@ -176,11 +141,11 @@ public class ProxyPlayerListener {
RegisteredServer registeredServer = event.getPlayer().getCurrentServer().get().getServer(); RegisteredServer registeredServer = event.getPlayer().getCurrentServer().get().getServer();
ServerWrapper wrapper = serverHandler.getWrapper(registeredServer.getServerInfo().getName()); ServerWrapper wrapper = serverHandler.getWrapper(registeredServer.getServerInfo().getName());
if (wrapper != null) { if(wrapper != null) {
wrapper.sendJoinLeaveMessage(event.getPlayer().getUniqueId(), Utility.parseMiniMessage(Config.SERVERLEAVEMESSAGE, wrapper.sendJoinLeaveMessage(event.getPlayer().getUniqueId(), Utility.parseMiniMessage(Config.SERVERLEAVEMESSAGE,
Placeholder.unparsed("player", event.getPlayer().getUsername()), Placeholder.unparsed("player", event.getPlayer().getUsername()),
Placeholder.unparsed("from_server", registeredServer.getServerInfo().getName()) Placeholder.unparsed("from_server", registeredServer.getServerInfo().getName())
)); ));
} }
} }
} }

View File

@ -1,235 +0,0 @@
package com.alttd.velocitychat.chat_web;
import com.alttd.chat.web.handler_class.PunishFromWeb;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class PunishmentCommandBuilderTest {
private PunishFromWeb baseEvent(String type, UUID executorUuid, UUID targetUuid) {
PunishFromWeb event = new PunishFromWeb();
event.setExecutor(executorUuid);
event.setTarget(targetUuid);
event.setType(type);
return event;
}
// ---- BAN ----
@Test
public void testBuildBanCommandWithTime() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("ban", executorUuid, targetUuid);
event.setTime("P7D"); // ISO-8601 duration: 7 days
event.setReason("Griefing");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "ban " + targetUuid + " 7d --sender=ExecutorName --sender-uuid=" + executorUuid + " Griefing";
assertEquals(expected, command);
}
@Test
public void testBuildBanCommandPermanentWhenNoTime() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("ban", executorUuid, targetUuid);
event.setTime(null);
event.setReason("Griefing");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "ban " + targetUuid + " --sender=ExecutorName --sender-uuid=" + executorUuid + " Griefing";
assertEquals(expected, command);
}
@Test
public void testBuildBanCommandPermanentWhenBlankTime() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("ban", executorUuid, targetUuid);
event.setTime(" ");
event.setReason("Griefing");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "ban " + targetUuid + " --sender=ExecutorName --sender-uuid=" + executorUuid + " Griefing";
assertEquals(expected, command);
}
@Test
public void testBuildBanCommandMissingReasonThrows() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("ban", executorUuid, targetUuid);
event.setTime(" ");
event.setReason(" ");
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.buildCommand("ExecutorName", event)
);
}
// ---- MUTE ----
@Test
public void testBuildMuteCommand() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("mute", executorUuid, targetUuid);
event.setTime("PT30M"); // 30 minutes
event.setReason("Spamming");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "mute " + targetUuid + " 30m --sender=ExecutorName --sender-uuid=" + executorUuid + " Spamming";
assertEquals(expected, command);
}
@Test
public void testBuildMuteCommandNoTimeThrows() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("mute", executorUuid, targetUuid);
event.setTime(null);
event.setReason("Spamming");
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.buildCommand("ExecutorName", event)
);
}
@Test
public void testBuildMuteCommandBlankTimeThrows() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("mute", executorUuid, targetUuid);
event.setTime(" ");
event.setReason("Spamming");
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.buildCommand("ExecutorName", event)
);
}
// ---- WARN ----
@Test
public void testBuildWarnCommandAlwaysUses30d() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("warn", executorUuid, targetUuid);
event.setTime(null); // not supplied for warns, should default to 30d anyway
event.setReason("Bad attitude");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "warn " + targetUuid + " 30d --sender=ExecutorName --sender-uuid=" + executorUuid + " Bad attitude";
assertEquals(expected, command);
}
@Test
public void testBuildWarnCommandIgnoresSuppliedTime() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("warn", executorUuid, targetUuid);
event.setTime("P1D"); // should be ignored; warns are always 30d
event.setReason("Bad attitude");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "warn " + targetUuid + " 30d --sender=ExecutorName --sender-uuid=" + executorUuid + " Bad attitude";
assertEquals(expected, command);
}
@Test
public void testBuildWarnCommandNoReasonThrows() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("warn", executorUuid, targetUuid);
event.setTime(null);
event.setReason("");
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.buildCommand("ExecutorName", event)
);
}
// ---- Type validation ----
@Test
public void testInvalidTypeThrows() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("kick", executorUuid, targetUuid);
event.setReason("Bad attitude");
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.buildCommand("ExecutorName", event)
);
}
@Test
public void testTypeIsCaseInsensitive() {
UUID executorUuid = UUID.randomUUID();
UUID targetUuid = UUID.randomUUID();
PunishFromWeb event = baseEvent("BAN", executorUuid, targetUuid);
event.setTime(null);
event.setReason("Griefing");
String command = PunishmentCommandBuilder.buildCommand("ExecutorName", event);
String expected = "ban " + targetUuid + " --sender=ExecutorName --sender-uuid=" + executorUuid + " Griefing";
assertEquals(expected, command);
}
// ---- Duration parsing ----
@Test
public void testParseDurationDaysOnly() {
assertEquals("7d", PunishmentCommandBuilder.parseDuration("P7D"));
}
@Test
public void testParseDurationMinutesOnly() {
assertEquals("45m", PunishmentCommandBuilder.parseDuration("PT45M"));
}
@Test
public void testParseDurationTruncatesToDaysWhenDaysPresent() {
// days take priority over everything smaller, which is dropped entirely
assertEquals("1d", PunishmentCommandBuilder.parseDuration("P1DT2H3M4S"));
}
@Test
public void testParseDurationTruncatesToHoursWhenNoDays() {
// hours take priority over minutes, which is dropped
assertEquals("2h", PunishmentCommandBuilder.parseDuration("PT2H3M"));
}
@Test
public void testParseDurationInvalidFormatThrows() {
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.parseDuration("7d")
);
}
@Test
public void testParseDurationZeroThrows() {
// duration must be positive
assertThrows(IllegalArgumentException.class,
() -> PunishmentCommandBuilder.parseDuration("PT0S")
);
}
@Test
public void testParseDurationSubMinuteThrows() {
// litebans has no second-level precision defaults to 1m
assertEquals("1m", PunishmentCommandBuilder.parseDuration("PT30S"));
}
}

View File

@ -1,66 +0,0 @@
plugins {
java
id("org.openapi.generator") version "7.12.0"
}
group = "com.alttd.chat"
version = "2.0.0-SNAPSHOT"
dependencies {
implementation("io.swagger.core.v3:swagger-annotations:2.2.28")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
implementation("com.google.code.gson:gson:2.12.1")
implementation("io.gsonfire:gson-fire:1.9.0")
implementation("javax.annotation:javax.annotation-api:1.3.2")
implementation("jakarta.annotation:jakarta.annotation-api:2.1.1")
}
sourceSets {
main {
java {
srcDir("${projectDir}/build/generated-sources/client/src/main/java")
}
}
}
tasks.named<org.openapitools.generator.gradle.plugin.tasks.GenerateTask>("openApiGenerate") {
generatorName.set("java")
inputSpec.set("${projectDir}/src/main/resources/chat-api.yml")
configFile.set("${projectDir}/src/main/resources/config_backend.json")
outputDir.set("${projectDir}/build/generated-sources/client")
apiPackage.set("com.alttd.altitudeweb.api")
modelPackage.set("com.alttd.altitudeweb.model")
modelNameSuffix.set("Dto")
additionalProperties.set(
mapOf(
"dateLibrary" to "java8",
"library" to "okhttp-gson"
)
)
generateApiTests.set(false)
generateApiDocumentation.set(false)
generateModelTests.set(false)
generateModelDocumentation.set(false)
generateAliasAsModel.set(true)
}
sourceSets {
main {
java {
srcDir(layout.buildDirectory.dir("generated/openapi/src/main/java"))
}
}
}
tasks.compileJava {
dependsOn("openApiGenerate")
}

View File

@ -1,144 +0,0 @@
openapi: 3.0.3
info:
title: Minecraft Network API
version: 1.0.0
servers:
- url: http://localhost:8080/api
tags:
- name: chat
description: Data for displaying Chat messages to clients
paths:
/chat/send/chat/message:
post:
tags:
- chat
summary: Sends one or more chat messages
operationId: sendChatMessages
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/ChatMessage"
responses:
"202":
description: Accepted
/chat/send/servers/state:
post:
tags:
- chat
summary: Update server states
operationId: updateServerStates
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ServerState"
responses:
"202":
description: Accepted
components:
schemas:
User:
type: object
required:
- uuid
- name
- styledName
# TODO [Stijn] [2026-07-19]: Add who they ignore and who they are ignored by
properties:
uuid:
type: string
format: uuid
name:
type: string
styledName:
type: string
ChatMessage:
type: object
required:
- uuid
- message
- server
- type
- timestamp
- blocked
properties:
uuid:
type: string
format: uuid
message:
type: string
server:
type: string
type:
type: string
enum:
- PUBLIC
- GLOBAL
- PARTY
- GAC
- MSG
- CUSTOM
channel:
type: string
maxLength: 36
receiver:
type: string
maxLength: 36
timestamp:
type: string
format: date-time
blocked:
type: boolean
PrivateMessage:
type: object
required:
- sender
- receiver
- message
- senderServer
- receiverServer
properties:
sender:
type: string
format: uuid
receiver:
type: string
format: uuid
message:
type: string
senderServer:
type: string
receiverServer:
type: string
Server:
type: object
properties:
name:
type: string
players:
type: array
items:
$ref: "#/components/schemas/User"
ServerState:
type: object
properties:
servers:
type: array
items:
$ref: "#/components/schemas/Server"

View File

@ -1,22 +0,0 @@
{
"library": "okhttp-gson",
"hideGenerationTimestamp": true,
"modelPackage": "com.alttd.altitudeweb.model",
"apiPackage": "com.alttd.altitudeweb.api",
"invokerPackage": "com.alttd.altitudeweb.invoker",
"serializableModel": true,
"openApiNullable": false,
"useTags": true,
"generateApis": true,
"generateApiTests": false,
"generateApiDocumentation": false,
"generateModels": true,
"generateModelTests": false,
"generateSupportingFiles": true,
"modelNameSuffix": "Dto",
"generateTests": false,
"dateLibrary": "java8",
"enumUnknownDefaultCase": true,
"disallowAdditionalPropertiesIfNotPresent": false,
"useJakartaEe": true
}