Skip to content

Commit

Permalink
Merge pull request #12 from unilock/ServerConnectedEvent
Browse files Browse the repository at this point in the history
Allow using server name as a variable in join messages, take 2
  • Loading branch information
confor authored Oct 4, 2023
2 parents ce9d03d + 07db7fd commit 020c432
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ I was disappointed with the current options so I made my own plugin. I hope its
- **Global chat**: your player's voice is echoed across your whole velocity network
- **Log to console**: the chat can be sent to your console (configurable, can be disabled)
- **Configurable format**: chat format can be changed or formatted using MiniMessage tags (or translated!)
- **Global join/quit messages**: let your whole network know when a player joins or quits the velocity proxy (goes well with [HidePlayerJoinQuit](https://github.com/OskarZyg/HidePlayerJoinQuit/releases) or [SilentJoin](https://www.spigotmc.org/resources/silentjoin.34967/))
- **Global join/leave messages**: let your whole network know when a player joins or leaves the velocity proxy (goes well with [HidePlayerJoinQuit](https://github.com/OskarZyg/HidePlayerJoinQuit/releases) or [SilentJoin](https://www.spigotmc.org/resources/silentjoin.34967/))
- **Server switch messages**

## Installation
Expand Down
17 changes: 6 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
plugins {
id 'java'
id 'eclipse'
id 'org.jetbrains.gradle.plugin.idea-ext' version '1.0.1'
id 'com.github.johnrengelman.shadow' version '7.0.0'
id 'org.jetbrains.gradle.plugin.idea-ext' version '1.1.7'
id 'com.github.johnrengelman.shadow' version '8.1.1'
}

group = 'me.confor.velocity'
version = '0.1.0'

repositories {
mavenCentral()
/*maven {
name = 'velocitypowered-repo'
url = 'https://nexus.velocitypowered.com/repository/maven-public/'
}*/
maven {
name = 'papermc-repo'
url = 'https://repo.papermc.io/repository/maven-public/'
}
}

dependencies {
compileOnly 'com.velocitypowered:velocity-api:3.1.2-SNAPSHOT'
annotationProcessor 'com.velocitypowered:velocity-api:3.1.2-SNAPSHOT'
compileOnly 'com.velocitypowered:velocity-api:3.2.0-SNAPSHOT'
annotationProcessor 'com.velocitypowered:velocity-api:3.2.0-SNAPSHOT'

// templating dependency
implementation 'net.kyori:adventure-api:4.11.0-SNAPSHOT'
implementation 'net.kyori:adventure-text-minimessage:4.11.0-SNAPSHOT'
implementation 'net.kyori:adventure-api:4.14.0'
implementation 'net.kyori:adventure-text-minimessage:4.14.0'
}

def targetJavaVersion = 17
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
12 changes: 8 additions & 4 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/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.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,10 +80,10 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
Expand Down Expand Up @@ -143,12 +143,16 @@ fi
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down
1 change: 1 addition & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand Down
24 changes: 17 additions & 7 deletions src/main/java/me/confor/velocity/chat/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.regex.Pattern;

public class Config {
static final long CONFIG_VERSION = 4;
static final long CONFIG_VERSION = 6;

Path dataDir;
Toml toml;
Expand All @@ -33,14 +33,19 @@ public class Config {
public TextReplacementConfig urlReplacement;

public boolean JOIN_ENABLE;
public boolean JOIN_PASSTHROUGH;
public String JOIN_FORMAT;

public boolean QUIT_ENABLE;
public String QUIT_FORMAT;
public boolean LEAVE_ENABLE;
public boolean LEAVE_PASSTHROUGH;
public String LEAVE_FORMAT;

public boolean SWITCH_ENABLE;
public String SWITCH_FORMAT;

public boolean DISCONNECT_ENABLE;
public String DISCONNECT_FORMAT;

@Inject
public Config(@DataDirectory Path dataDir) {
this.dataDir = dataDir;
Expand Down Expand Up @@ -91,12 +96,17 @@ public void loadConfigs() {
this.URLS_PATTERN = this.toml.getString("urls.pattern", "https?:\\/\\/\\S+");

this.JOIN_ENABLE = this.toml.getBoolean("join.enable", false);
this.JOIN_FORMAT = this.toml.getString("join.format", "<yellow><player> joined the game</yellow>");
this.JOIN_PASSTHROUGH = this.toml.getBoolean("join.passthrough", false);
this.JOIN_FORMAT = this.toml.getString("join.format", "<yellow><player> joined <server></yellow>");

this.QUIT_ENABLE = this.toml.getBoolean("quit.enable", true);
this.QUIT_FORMAT = this.toml.getString("quit.format", "<yellow><player> disconnected from <server></yellow>");
this.LEAVE_ENABLE = this.toml.getBoolean("leave.enable", false);
this.LEAVE_PASSTHROUGH = this.toml.getBoolean("leave.passthrough", false);
this.LEAVE_FORMAT = this.toml.getString("leave.format", "<yellow><player> left <server></yellow>");

this.SWITCH_ENABLE = this.toml.getBoolean("switch.enable", true);
this.SWITCH_FORMAT = this.toml.getString("switch.format", "<yellow><player> connected to <server></yellow>");
this.SWITCH_FORMAT = this.toml.getString("switch.format", "<yellow><player> switched from <previous_server> to <server></yellow>");

this.DISCONNECT_ENABLE = this.toml.getBoolean("disconnect.enable", true);
this.DISCONNECT_FORMAT = this.toml.getString("disconnect.format", "<yellow><player> was disconnected</yellow>");
}
}
94 changes: 49 additions & 45 deletions src/main/java/me/confor/velocity/chat/modules/GlobalChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent.ChatResult;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.proxy.ProxyReloadEvent;
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
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;
Expand Down Expand Up @@ -65,74 +64,79 @@ public void onPlayerChat(PlayerChatEvent event) {
if (config.URLS_CLICKABLE)
msg = msg.replaceText(config.urlReplacement);

if (config.GLOBAL_CHAT_PASSTHROUGH)
sendMessage(msg,currentServer.get().getServer());
else
if (!config.GLOBAL_CHAT_PASSTHROUGH) {
sendMessage(msg, currentServer.get().getServer());
} else {
sendMessage(msg);
event.setResult(PlayerChatEvent.ChatResult.denied());
}

if (config.GLOBAL_CHAT_TO_CONSOLE)
this.logger.info("GLOBAL: <{}> {}", player, message);

if (!config.GLOBAL_CHAT_PASSTHROUGH)
event.setResult(ChatResult.denied());
}

@Subscribe
public void onConnect(LoginEvent event) {
if (!config.JOIN_ENABLE)
return;
public void onConnect(ServerConnectedEvent event) {
RegisteredServer currentServer = event.getServer();
Optional<RegisteredServer> previousServer = event.getPreviousServer();

String player = event.getPlayer().getUsername();
String server = currentServer.getServerInfo().getName();

Component msg = parseMessage(config.JOIN_FORMAT, List.of(
new ChatTemplate("player", player, false)
));
if (previousServer.isPresent()) {
if (!config.SWITCH_ENABLE)
return;

sendMessage(msg);
}

@Subscribe
public void onDisconnect(DisconnectEvent event) {
if (!config.QUIT_ENABLE)
return;

Optional<ServerConnection> currentServer = event.getPlayer().getCurrentServer();
if (currentServer.isEmpty())
return;
Component msg = parseMessage(config.SWITCH_FORMAT, List.of(
new ChatTemplate("player", player, false),
new ChatTemplate("server", server, false),
new ChatTemplate("previous_server", previousServer.get().getServerInfo().getName(), false)
));

String player = event.getPlayer().getUsername();
String server = currentServer.get().getServerInfo().getName();
sendMessage(msg);
} else {
if (!config.JOIN_ENABLE)
return;

Component msg = parseMessage(config.QUIT_FORMAT, List.of(
Component msg = parseMessage(config.JOIN_FORMAT, List.of(
new ChatTemplate("player", player, false),
new ChatTemplate("server", server, false)
));
));

if (config.GLOBAL_CHAT_PASSTHROUGH)
sendMessage(msg,currentServer.get().getServer());
else
sendMessage(msg);
if (!config.JOIN_PASSTHROUGH)
sendMessage(msg, currentServer);
else
sendMessage(msg);
}
}

@Subscribe
public void onServerConnect(ServerPostConnectEvent event) {
if (!config.SWITCH_ENABLE)
public void onDisconnect(DisconnectEvent event) {
if (!config.LEAVE_ENABLE)
return;

String player = event.getPlayer().getUsername();
Optional<ServerConnection> currentServer = event.getPlayer().getCurrentServer();
if (currentServer.isEmpty())

if (currentServer.isEmpty() && config.DISCONNECT_ENABLE) {
Component msg = parseMessage(config.DISCONNECT_FORMAT, List.of(
new ChatTemplate("player", player, false)
));

sendMessage(msg);

return;
}

String player = event.getPlayer().getUsername();
String server = currentServer.get().getServerInfo().getName();

Component msg = parseMessage(config.SWITCH_FORMAT, List.of(
Component msg = parseMessage(config.LEAVE_FORMAT, List.of(
new ChatTemplate("player", player, false),
new ChatTemplate("server", server, false)
));

if (config.GLOBAL_CHAT_PASSTHROUGH)
sendMessage(msg,currentServer.get().getServer());
if (!config.LEAVE_PASSTHROUGH)
sendMessage(msg, currentServer.get().getServer());
else
sendMessage(msg);
}
Expand All @@ -151,13 +155,13 @@ private Component parseMessage(String input, List<ChatTemplate> templates) {
}

private void sendMessage(Component msg) {
for (RegisteredServer server : this.server.getAllServers())
server.sendMessage(msg);
for (Player player : this.server.getAllPlayers())
player.sendMessage(msg);
}

private void sendMessage(Component msg, RegisteredServer currentServer) {
private void sendMessage(Component msg, RegisteredServer excludedServer) {
for (RegisteredServer server : this.server.getAllServers())
if (server != currentServer) {
if (server != excludedServer) {
server.sendMessage(msg);
}
}
Expand Down
54 changes: 36 additions & 18 deletions src/main/resources/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config_version = 4
config_version = 6

[chat]
# enable global chat?
Expand All @@ -7,35 +7,53 @@ enable = true
# should the chat messages be echoed to velocity's console?
log_to_console = false

# should chat messages be passed through to the minecraft server?
# if enabled, it will show duplicate messages in the senders chat
passthrough = true

# should chat messages be parsed through minimessage?
# player will be able to use styles such as <rainbow>hello</rainbow> in their messages
# should global chat messages (with the formatting below) be sent to all players, regardless of minecraft server?
# if enabled, the original chat message will be formatted as below, and sent to all players.
# however, this will prevent the target server from receiving the unmodified message, which may break a few mods (see: Botania's "Corporea Index")
# if disabled, the target server will only see the original message.
# players on other minecraft servers will always only see the global message.
# NOTE: enabling this will kick players on >=1.19.1 servers when a chat message is sent.
# a plugin can be used to solve this - i.e. https://modrinth.com/plugin/unsignedvelocity
passthrough = false

# should global chat messages be parsed through minimessage?
# players will be able to use styles such as <rainbow>hello</rainbow> in their messages
parse_player_messages = false

# global chat format, you can use minimessage tags here.
# global chat format, you can use minimessage tags here
format = "<<player>> <message>"

[urls]
# should urls typed in chat be clickable?
# should urls in global chat be clickable?
clickable = true
# the regular expression used to determine whether something is a url
# test using https://regex101.com/ set to Java 8 and only the "g" (global) flag
# test using https://regex101.com/ - set to Java 8 and enable only the "g" (global) flag
pattern = "https?:\\/\\/\\S+"

[join]
# send a chat message when a player connects to velocity? Useful for fabric servers which dont already have a Join message.
enable = false
format = "<yellow><player> joined the game</yellow>"
# send a chat message when a player joins the proxy?
enable = true
# send that chat message to the server the player is joining?
# useful for servers that have the default join message disabled (via mod / plugin)
passthrough = false
format = "<yellow><player> joined <server></yellow>"

[quit]
# send a chat message when a player disconnects?
[leave]
# send a chat message when a player leaves the proxy?
enable = true
format = "<yellow><player> disconnected from <server></yellow>"
# send that chat message to the server the player is leaving?
# useful for servers that have the default leave message disabled (via mod / plugin)
passthrough = false
format = "<yellow><player> left <server></yellow>"

[switch]
# send a chat message when a player connects to a minecraft server?
# send a chat message when a player switches to a different server?
# (e.g. via "/server <server>")
enable = true
format = "<yellow><player> switched from <previous_server> to <server></yellow>"

[disconnect]
# send a chat message when a player is forcibly disconnected from the proxy?
# (e.g. due to timing out, having an incompatible client, or some other error)
enable = true
format = "<yellow><player> connected to <server></yellow>"
format = "<yellow><player> was disconnected</yellow>"

0 comments on commit 020c432

Please sign in to comment.