Skip to content

Commit

Permalink
Added Private Messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jun 5, 2024
1 parent b9d476e commit 5d6f308
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 8 deletions.
4 changes: 0 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,9 @@ hangarPublish {

tasks.withType<ShadowJar> {
minimize()
relocate("net.kyori", "com.beanbeanjuice.simpleproxychat.libs.net.kyori")
relocate("net.dv8tion", "com.beanbeanjuice.simpleproxychat.libs.net.dv8tion")
relocate("dev.dejvokep", "com.beanbeanjuice.simpleproxychat.libs.dev.dejvokep")
relocate("org.bstats", "com.beanbeanjuice.simpleproxychat.libs.org.bstats")
relocate("joda-time", "com.beanbeanjuice.simpleproxychat.libs.joda-time")
relocate("org.apache.maven", "com.beanbeanjuice.simpleproxychat.libs.org.apache.maven")
relocate("kotlin", "com.beanbeanjuice.simpleproxychat.kotlin.libs")
archiveBaseName.set(rootProject.name)
archiveClassifier.set("")
archiveVersion.set(version as String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.beanbeanjuice.simpleproxychat.commands.bungee.BungeeChatToggleCommand;
import com.beanbeanjuice.simpleproxychat.commands.bungee.BungeeReloadCommand;
import com.beanbeanjuice.simpleproxychat.commands.bungee.BungeeReplyCommand;
import com.beanbeanjuice.simpleproxychat.commands.bungee.BungeeWhisperCommand;
import com.beanbeanjuice.simpleproxychat.socket.bungee.BungeeCordPluginMessagingListener;
import com.beanbeanjuice.simpleproxychat.utility.Helper;
import com.beanbeanjuice.simpleproxychat.utility.WhisperHandler;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.epoch.EpochHelper;
import com.beanbeanjuice.simpleproxychat.utility.listeners.bungee.BungeeServerListener;
Expand Down Expand Up @@ -34,6 +37,7 @@ public final class SimpleProxyChatBungee extends Plugin {
@Getter private Bot discordBot;
@Getter private Metrics metrics;
@Getter private BungeeServerListener serverListener;
@Getter private WhisperHandler whisperHandler;

@Override
public void onEnable() {
Expand Down Expand Up @@ -164,11 +168,15 @@ private void registerListeners() {

serverListener = new BungeeServerListener(this, chatHandler);
this.getProxy().getPluginManager().registerListener(this, serverListener);

whisperHandler = new WhisperHandler();
}

private void registerCommands() {
this.getProxy().getPluginManager().registerCommand(this, new BungeeReloadCommand(this, config));
this.getProxy().getPluginManager().registerCommand(this, new BungeeChatToggleCommand(this, config));
this.getProxy().getPluginManager().registerCommand(this, new BungeeWhisperCommand(this, config, config.getAsArrayList(ConfigDataKey.WHISPER_ALIASES).toArray(new String[0])));
this.getProxy().getPluginManager().registerCommand(this, new BungeeReplyCommand(this, config, config.getAsArrayList(ConfigDataKey.REPLY_ALIASES).toArray(new String[0])));
}

private void startPluginMessaging() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.beanbeanjuice.simpleproxychat.commands.velocity.VelocityChatToggleCommand;
import com.beanbeanjuice.simpleproxychat.commands.velocity.VelocityReloadCommand;
import com.beanbeanjuice.simpleproxychat.commands.velocity.VelocityReplyCommand;
import com.beanbeanjuice.simpleproxychat.commands.velocity.VelocityWhisperCommand;
import com.beanbeanjuice.simpleproxychat.socket.velocity.VelocityPluginMessagingListener;
import com.beanbeanjuice.simpleproxychat.utility.UpdateChecker;
import com.beanbeanjuice.simpleproxychat.utility.WhisperHandler;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.epoch.EpochHelper;
import com.beanbeanjuice.simpleproxychat.utility.status.ServerStatusManager;
Expand Down Expand Up @@ -42,6 +45,7 @@ public class SimpleProxyChatVelocity {
@Getter private final Config config;
@Getter private final EpochHelper epochHelper;
@Getter private Bot discordBot;
@Getter private WhisperHandler whisperHandler;
private Metrics metrics;
private VelocityServerListener serverListener;

Expand Down Expand Up @@ -197,6 +201,8 @@ private void registerListeners() {

this.proxyServer.getEventManager().register(this, new VelocityPluginMessagingListener(this, serverListener));
this.proxyServer.getChannelRegistrar().register(VelocityPluginMessagingListener.IDENTIFIER);

whisperHandler = new WhisperHandler();
}

private void registerCommands() {
Expand All @@ -212,8 +218,20 @@ private void registerCommands() {
.plugin(this)
.build();

CommandMeta whisperCommand = commandManager.metaBuilder("spc-whisper")
.aliases(config.getAsArrayList(ConfigDataKey.WHISPER_ALIASES).toArray(new String[0]))
.plugin(this)
.build();

CommandMeta replyCommand = commandManager.metaBuilder("spc-reply")
.aliases(config.getAsArrayList(ConfigDataKey.REPLY_ALIASES).toArray(new String[0]))
.plugin(this)
.build();

commandManager.register(reloadCommand, new VelocityReloadCommand(this, config));
commandManager.register(chatToggleCommand, new VelocityChatToggleCommand(this, config));
commandManager.register(whisperCommand, new VelocityWhisperCommand(this, config));
commandManager.register(replyCommand, new VelocityReplyCommand(this, config));
}

@Subscribe(order = PostOrder.LAST)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.beanbeanjuice.simpleproxychat.commands.bungee;

import com.beanbeanjuice.simpleproxychat.SimpleProxyChatBungee;
import com.beanbeanjuice.simpleproxychat.utility.Helper;
import com.beanbeanjuice.simpleproxychat.utility.Tuple;
import com.beanbeanjuice.simpleproxychat.utility.config.Config;
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigDataKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;

import java.util.ArrayList;
import java.util.List;

public class BungeeReplyCommand extends Command {

private final SimpleProxyChatBungee plugin;
private final Config config;

public BungeeReplyCommand(SimpleProxyChatBungee plugin, Config config, String... aliases) {
super("Spc-reply", Permission.USE_WHISPER.getPermissionNode(), aliases);
this.plugin = plugin;;
this.config = config;
}

@Override
public void execute(CommandSender sender, String[] args) {
plugin.getWhisperHandler().getLink(sender.getName()).map((playerName) -> plugin.getProxy().getPlayer(playerName)).ifPresentOrElse(
(receiver) -> {
String message = Helper.translateLegacyCodes(String.join(" ", args));

String senderString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_SEND);
String receiverString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_RECEIVE);

List<Tuple<String, String>> replacements = new ArrayList<>();
replacements.add(Tuple.of("sender", sender.getName()));
replacements.add(Tuple.of("receiver", receiver.getName()));
replacements.add(Tuple.of("message", message));
replacements.add(Tuple.of("plugin-prefix", config.getAsString(ConfigDataKey.PLUGIN_PREFIX)));

senderString = Helper.replaceKeys(senderString, replacements);
receiverString = Helper.replaceKeys(receiverString, replacements);

sender.sendMessage(Helper.convertToBungee(senderString));
receiver.sendMessage(Helper.convertToBungee(receiverString));

plugin.getWhisperHandler().set(sender.getName(), receiver.getName());
},
() -> sender.sendMessage(Helper.convertToBungee(config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_ERROR)))
);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.beanbeanjuice.simpleproxychat.commands.bungee;

import com.beanbeanjuice.simpleproxychat.SimpleProxyChatBungee;
import com.beanbeanjuice.simpleproxychat.utility.Helper;
import com.beanbeanjuice.simpleproxychat.utility.Tuple;
import com.beanbeanjuice.simpleproxychat.utility.config.Config;
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigDataKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class BungeeWhisperCommand extends Command implements TabExecutor {

private final SimpleProxyChatBungee plugin;
private final Config config;

public BungeeWhisperCommand(SimpleProxyChatBungee plugin, Config config, String... aliases) {
super("Spc-whisper", Permission.USE_WHISPER.getPermissionNode(), aliases);
this.plugin = plugin;
this.config = config;
}

@Override
public void execute(CommandSender sender, String[] args) {
if (args.length < 2) {
sender.sendMessage(Helper.convertToBungee(config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_ERROR)));
return;
}

ProxiedPlayer receiver = plugin.getProxy().getPlayer(args[0]);
if (receiver == null) {
sender.sendMessage(Helper.convertToBungee(config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_ERROR)));
return;
}

String message = Helper.translateLegacyCodes(Arrays.stream(args).skip(1).collect(Collectors.joining(" ")));

String senderString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_SEND);
String receiverString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_RECEIVE);

List<Tuple<String, String>> replacements = new ArrayList<>();
replacements.add(Tuple.of("sender", sender.getName()));
replacements.add(Tuple.of("receiver", receiver.getName()));
replacements.add(Tuple.of("message", message));
replacements.add(Tuple.of("plugin-prefix", config.getAsString(ConfigDataKey.PLUGIN_PREFIX)));

senderString = Helper.replaceKeys(senderString, replacements);
receiverString = Helper.replaceKeys(receiverString, replacements);

sender.sendMessage(Helper.convertToBungee(senderString));
receiver.sendMessage(Helper.convertToBungee(receiverString));

plugin.getWhisperHandler().set(sender.getName(), receiver.getName());
}

@Override
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
if (args.length <= 1)
return plugin.getProxy().getPlayers().stream().map(ProxiedPlayer::getName).filter((username) -> {
if (args.length == 1) {
return username.toLowerCase().startsWith(args[0].toLowerCase());
}
return true;
}).toList();

return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.beanbeanjuice.simpleproxychat.commands.velocity;

import com.beanbeanjuice.simpleproxychat.SimpleProxyChatVelocity;
import com.beanbeanjuice.simpleproxychat.utility.Helper;
import com.beanbeanjuice.simpleproxychat.utility.Tuple;
import com.beanbeanjuice.simpleproxychat.utility.config.Config;
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigDataKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;

import java.util.ArrayList;
import java.util.List;

public class VelocityReplyCommand implements SimpleCommand {

private final SimpleProxyChatVelocity plugin;
private final Config config;

public VelocityReplyCommand(SimpleProxyChatVelocity plugin, Config config) {
this.plugin = plugin;
this.config = config;
}

@Override
public void execute(Invocation invocation) {
plugin.getProxyServer().getPlayer(plugin.getWhisperHandler().getLink(((Player) invocation.source()).getUsername()).orElse("")).ifPresentOrElse(
(receiver) -> {
String message = Helper.translateLegacyCodes(String.join(" ", invocation.arguments()));

String senderString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_SEND);
String receiverString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_RECEIVE);

List<Tuple<String, String>> replacements = new ArrayList<>();
replacements.add(Tuple.of("sender", ((Player) invocation.source()).getUsername()));
replacements.add(Tuple.of("receiver", receiver.getUsername()));
replacements.add(Tuple.of("message", message));
replacements.add(Tuple.of("plugin-prefix", config.getAsString(ConfigDataKey.PLUGIN_PREFIX)));

senderString = Helper.replaceKeys(senderString, replacements);
receiverString = Helper.replaceKeys(receiverString, replacements);

invocation.source().sendMessage(Helper.stringToComponent(senderString));
receiver.sendMessage(Helper.stringToComponent(receiverString));

plugin.getWhisperHandler().set(((Player) invocation.source()).getUsername(), receiver.getUsername());
},
() -> invocation.source().sendMessage(Helper.stringToComponent(config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_ERROR)))
);
}

@Override
public boolean hasPermission(Invocation invocation) {
return invocation.source().hasPermission(Permission.USE_WHISPER.getPermissionNode());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.beanbeanjuice.simpleproxychat.commands.velocity;

import com.beanbeanjuice.simpleproxychat.SimpleProxyChatVelocity;
import com.beanbeanjuice.simpleproxychat.utility.Helper;
import com.beanbeanjuice.simpleproxychat.utility.Tuple;
import com.beanbeanjuice.simpleproxychat.utility.config.Config;
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigDataKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class VelocityWhisperCommand implements SimpleCommand {

private final SimpleProxyChatVelocity plugin;
private final Config config;

public VelocityWhisperCommand(SimpleProxyChatVelocity plugin, Config config) {
this.plugin = plugin;
this.config = config;
}

@Override
public void execute(Invocation invocation) {
if (invocation.arguments().length < 2) {
invocation.source().sendMessage(Helper.stringToComponent(config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_ERROR)));
return;
}

plugin.getProxyServer().getPlayer(invocation.arguments()[0]).ifPresentOrElse(
(receiver) -> {
String message = Helper.translateLegacyCodes(Arrays.stream(invocation.arguments()).skip(1).collect(Collectors.joining(" ")));

String senderString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_SEND);
String receiverString = config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_RECEIVE);

List<Tuple<String, String>> replacements = new ArrayList<>();
replacements.add(Tuple.of("sender", ((Player) invocation.source()).getUsername()));
replacements.add(Tuple.of("receiver", receiver.getUsername()));
replacements.add(Tuple.of("message", message));
replacements.add(Tuple.of("plugin-prefix", config.getAsString(ConfigDataKey.PLUGIN_PREFIX)));

senderString = Helper.replaceKeys(senderString, replacements);
receiverString = Helper.replaceKeys(receiverString, replacements);

invocation.source().sendMessage(Helper.stringToComponent(senderString));
receiver.sendMessage(Helper.stringToComponent(receiverString));

plugin.getWhisperHandler().set(((Player) invocation.source()).getUsername(), receiver.getUsername());
},
() -> invocation.source().sendMessage(Helper.stringToComponent(config.getAsString(ConfigDataKey.MINECRAFT_WHISPER_ERROR)))
);
}

@Override
public List<String> suggest(Invocation invocation) {
if (invocation.arguments().length <= 1) return plugin.getProxyServer().getAllPlayers().stream().map((Player::getUsername)).filter((username) -> {
if (invocation.arguments().length == 1) {
return username.toLowerCase().startsWith(invocation.arguments()[0].toLowerCase());
}
return true;
}).toList();
return List.of();
}

@Override
public boolean hasPermission(Invocation invocation) {
return invocation.source().hasPermission(Permission.USE_WHISPER.getPermissionNode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.beanbeanjuice.simpleproxychat.utility;

import java.util.HashMap;
import java.util.Optional;

public class WhisperHandler {

private final HashMap<String, String> lastMessagedPlayer;

public WhisperHandler() {
lastMessagedPlayer = new HashMap<>();
}

public void set(String player1, String player2) {
lastMessagedPlayer.put(player1, player2);
lastMessagedPlayer.put(player2, player1);
}

public Optional<String> getLink(String playerName) {
return Optional.ofNullable(lastMessagedPlayer.get(playerName));
}

}
Loading

0 comments on commit 5d6f308

Please sign in to comment.