Skip to content

Commit

Permalink
completley server-side, better message configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
OGBlackDiamond committed Aug 13, 2024
1 parent 44fa33f commit 406b4d0
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/home/caden/.sdkman/candidates/java/21.0.4-oracle
java.home=/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64
jvm.arguments=
offline.mode=false
override.workspace.settings=true
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions ProxyMessagesPaper/bin/main/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: ProxyMessages
version: 1.1.0
main: dev.ogblackdiamond.proxymessages.ProxyMessages
description: Paper plugin to interface with Velocity
author: BlackDiamond
api-version: '1.21'
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.ogblackdiamond.proxymessages;

import dev.ogblackdiamond.proxymessages.listener.MessageListener;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -14,15 +12,11 @@
*/
public class ProxyMessages extends JavaPlugin implements Listener {

MessageListener listener;

@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);

listener = new MessageListener(this);

Bukkit.getLogger().info("Thank you for using ProxyMessages");
Bukkit.getLogger().info("[ProxyMessages] Thank you for using ProxyMessages");
}


Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion ProxyMessagesPaper/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ProxyMessages
version: 1.1.0
version: 2.0.0
main: dev.ogblackdiamond.proxymessages.ProxyMessages
description: Paper plugin to interface with Velocity
author: BlackDiamond
Expand Down
2 changes: 1 addition & 1 deletion ProxyMessagesVelocity/.factorypath
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/net.kyori/adventure-text-minimessage/4.17.0/696817a0f9700483bcd9dbd5cc62f33ac83b12f9/adventure-text-minimessage-4.17.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/net.kyori/examination-string/1.3.0/6f34afef5c54ccce4996bc321abf77518b55b4bd/examination-string-1.3.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/com.velocitypowered/velocity-api/3.3.0-SNAPSHOT/2eafa6ed07f1c637f763c988a8afbfbe897d3b94/velocity-api-3.3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/net.kyori/ansi/1.0.3/6f779bed836ddfa9d9719d2e08ad4524023343e6/ansi-1.0.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/net.kyori/adventure-text-serializer-legacy/4.17.0/1f5271e54a4feb6dada26873297337f4b9f2f99a/adventure-text-serializer-legacy-4.17.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/org.spongepowered/configurate-yaml/4.1.2/f726180c21ec387be5b8a2e04d916443c4046207/configurate-yaml-4.1.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/com.typesafe/config/1.4.1/19058a07624a87f90d129af7cd9c68bee94535a9/config-1.4.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/com.velocitypowered/velocity-api/3.3.0-SNAPSHOT/f2df936a9ad6818ff27597e43a399b4e3f5de65e/velocity-api-3.3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/net.kyori/adventure-text-logger-slf4j/4.17.0/ebc3d59bdce591f1e1daed8b5b9ebd13f231c946/adventure-text-logger-slf4j-4.17.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/org.spongepowered/configurate-gson/4.1.2/3e5c7a0ea73e95ce6139fa72f1b6d36eb531ab81/configurate-gson-4.1.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/caden/.gradle/caches/modules-2/files-2.1/net.kyori/adventure-key/4.17.0/799f1b5fedb920a7b71916e9b71123ff94d07acf/adventure-key-4.17.0.jar" enabled="true" runInBatchMode="false"/>
Expand Down
1 change: 1 addition & 0 deletions ProxyMessagesVelocity/bin/default/velocity-plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"proxymessages","name":"Proxy Messages","version":"2.0.0","description":"A message system for servers to interact over a proxy.","authors":["BlackDiamond"],"dependencies":[],"main":"dev.ogblackdiamond.proxymessages.ProxyMessages"}
27 changes: 27 additions & 0 deletions ProxyMessagesVelocity/bin/main/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# enables a network-wide join message for when a player joins the network
global-network-join: true

# enables a network-wide leave message for when a player leaves the network
global-network-leave: true

# enables a network-wide message that notifies players when a player switches between servers when true
global-network-switch: true

# options from this list will be randomly chosen to be displayed when a player joins the network
# {player} will be replaced by the player name
join-message-options:
- "{player} joined the network"

# options from this list will be randomly chosen to be displayed when a player leaves the network
# {player} will be replaced by the player name
leave-message-options:
- "{player} left the network"

# options from this list will be randomly chosen to be displayed when a player changes between two servers on the network
# {player} will be replaced by the player name
# {prev} will be replaced by the server the player is connecting from
# {cur} will be replaced by the server the player is connecting to
switch-message-options:
- "{player} left {prev} and joined {cur}"


Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer;

import dev.ogblackdiamond.proxymessages.util.MessageUtil;
import net.kyori.adventure.text.Component;

import java.util.List;
import java.util.Random;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand All @@ -26,7 +31,7 @@
/**
* Main class for ProxyMessages.
*/
@Plugin(id = "proxymessages", name = "Proxy Messages", version = "1.1.0",
@Plugin(id = "proxymessages", name = "Proxy Messages", version = "2.0.0",
description = "A message system for servers to interact over a proxy.",
authors = {"BlackDiamond"})
public class ProxyMessages {
Expand All @@ -37,13 +42,20 @@ public class ProxyMessages {
@DataDirectory
private final Path dataDirectory;

private MessageUtil messageUtil;

private boolean globalJoin;

private boolean globalLeave;

private boolean globalSwitch;

private List<String> joinMessageOptions;

private List <String> leaveMessageOptions;

private List<String> switchMessageOptions;

/**
* Constructor, initializes the logger and the proxy server.
*/
Expand All @@ -53,6 +65,8 @@ public ProxyMessages(ProxyServer server, Logger logger, @DataDirectory Path data
this.logger = logger;
this.dataDirectory = dataDirectory;

messageUtil = new MessageUtil();

logger.info("Thank you for using ProxyMessages");
}

Expand All @@ -76,7 +90,10 @@ public void onProxyInitialization(ProxyInitializeEvent event) throws IOException
globalLeave = root.node("global-network-leave").getBoolean();

globalSwitch = root.node("global-network-switch").getBoolean();


joinMessageOptions = root.node("join-message-options").getList(String.class);
leaveMessageOptions = root.node("leave-message-options").getList(String.class);
switchMessageOptions = root.node("switch-message-options").getList(String.class);
}

/**
Expand All @@ -90,20 +107,26 @@ public void onPlayerConnect(ServerPostConnectEvent event) {
if (event.getPreviousServer() == null && !globalJoin) return;

Player player = event.getPlayer();

boolean previousServerNull = event.getPreviousServer() == null;

ByteArrayDataOutput out = ByteStreams.newDataOutput();

out.writeUTF(event.getPreviousServer() == null ? "join" : "switch");
out.writeUTF(player.getUsername());

String message;

if (event.getPreviousServer() != null) {
out.writeUTF(event.getPreviousServer().getServerInfo().getName());
out.writeUTF(player.getCurrentServer().get().getServerInfo().getName());
message = switchMessageOptions.get(new Random().nextInt(switchMessageOptions.size()));
} else {
message = joinMessageOptions.get(new Random().nextInt(switchMessageOptions.size()));
}

sendMessage(out.toByteArray());

sendMessage(
messageUtil.compileFormattedMessage(
event.getPreviousServer() == null ? "join" : "switch",
player.getUsername(),
previousServerNull ? "" : event.getPreviousServer().getServerInfo().getName(),
previousServerNull ? "" : player.getCurrentServer().get().getServerInfo().getName(),
message
)
);
}

/**
Expand All @@ -116,23 +139,23 @@ public void onPlayerDisconnect(DisconnectEvent event) {

Player player = event.getPlayer();

ByteArrayDataOutput out = ByteStreams.newDataOutput();

out.writeUTF("quit");
out.writeUTF(player.getUsername());

sendMessage(out.toByteArray());
sendMessage(
messageUtil.compileFormattedMessage(
"quit",
player.getUsername(),
"",
"",
leaveMessageOptions.get(new Random().nextInt(switchMessageOptions.size()))
)
);

}


private void sendMessage(byte[] message) {
private void sendMessage(Component message) {
for (RegisteredServer srvr : server.getAllServers()) {
if (!srvr.getPlayersConnected().isEmpty()) {
srvr.sendPluginMessage(
MinecraftChannelIdentifier.from("proxymessages:main"),
message
);
srvr.sendMessage(message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dev.ogblackdiamond.proxymessages.util;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;

public class MessageUtil {

public Component compileFormattedMessage(String type, String playerName, String previousServer, String newServer, String chosenMessage) {

TextComponent.Builder finalMessage = Component.text().color(NamedTextColor.YELLOW);

int messageLength = chosenMessage.length();
int playerNameLength = "{player}".length();
int previousServerNameLength = "{prev}".length();
int newServerNameLength = "{cur}".length();



// builds the final component, interpolating the correct strings when need
switch (type) {

case "join":
case "leave": {

for (int i = 0; i < messageLength; i++) {

boolean atLength = i + playerNameLength > messageLength;

if (!atLength && chosenMessage.substring(i, i + playerNameLength).equals("{player}")) {
finalMessage.append(Component.text(playerName).decoration(TextDecoration.BOLD, true));
i += playerNameLength - 1;
} else {
finalMessage.append(Component.text(chosenMessage.substring(i, i+1)).decoration(TextDecoration.BOLD, false));
}
}
break;
}

case "switch": {

for (int i = 0; i < messageLength; i++) {

boolean playerAtLength = i + playerNameLength > messageLength;
boolean previousServerLength = i + previousServerNameLength > messageLength;
boolean newServerLength = i + newServerNameLength > messageLength;

if (!playerAtLength && chosenMessage.substring(i, i + playerNameLength).equals("{player}")) {
finalMessage.append(Component.text(playerName).decoration(TextDecoration.BOLD, true));
i += playerNameLength - 1;
} else if (!previousServerLength && chosenMessage.substring(i, i + previousServerNameLength).equals("{prev}")) {
finalMessage.append(Component.text(previousServer).decoration(TextDecoration.BOLD, true));
i += previousServerNameLength - 1;
} else if (!newServerLength && chosenMessage.substring(i, i + newServerNameLength).equals("{cur}")) {
finalMessage.append(Component.text(newServer).decoration(TextDecoration.BOLD, true));
i += newServerNameLength - 1;
} else {
finalMessage.append(Component.text(chosenMessage.substring(i, i+1)).decoration(TextDecoration.BOLD, false));
}
}
break;
}

default: {
return Component.text("[ProxyMessages] Invalid type passed! Cannot render message.");
}

}

return finalMessage.build();
}
}
Loading

0 comments on commit 406b4d0

Please sign in to comment.