Update nicknames to use Chat's plugin message channels.

This commit is contained in:
Len 2022-10-01 20:42:35 +02:00
parent 3d432a5af6
commit a4e4982dc3
6 changed files with 23 additions and 13 deletions

View File

@ -509,6 +509,7 @@ public final class Config {
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 = "<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);
@ -537,5 +538,6 @@ public final class Config {
NICK_ITEM_LORE = getList("nicknames.item-lore", List.of("&bNew nick: %newNick%", "&bOld nick: %oldNick%", "&bLast changed: %lastChanged%", "&aLeft click to Accept &d| &cRight click to Deny")); NICK_ITEM_LORE = getList("nicknames.item-lore", List.of("&bNew nick: %newNick%", "&bOld nick: %oldNick%", "&bLast changed: %lastChanged%", "&aLeft click to Accept &d| &cRight 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", List.of("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f", "&r")); 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"));
NICK_CURRENT = getString("nicknames.messages.nick-current", NICK_CURRENT);
} }
} }

View File

@ -193,8 +193,8 @@ 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();

View File

@ -11,6 +11,8 @@ import com.alttd.chat.objects.Nick;
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.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -104,7 +106,11 @@ public class Nicknames implements CommandExecutor, TabCompleter {
case "current": case "current":
if (hasPermission(sender, "utility.nick.current")) { if (hasPermission(sender, "utility.nick.current")) {
ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId()); ChatUser chatUser = ChatUserManager.getChatUser(player.getUniqueId());
player.sendMessage(Component.text("Current nick: ").append(chatUser.getDisplayName()).append(Component.text(" (" + chatUser.getNickNameString() + ")"))); TagResolver placeholders = TagResolver.resolver(
Placeholder.component("nickname", chatUser.getDisplayName()),
Placeholder.parsed("currentnickname", chatUser.getNickNameString())
);
player.sendMiniMessage(Config.NICK_CURRENT, placeholders);
} }
break; break;
case "help": case "help":
@ -179,9 +185,9 @@ public class Nicknames implements CommandExecutor, TabCompleter {
if (NickCache.containsKey(uniqueId)) { if (NickCache.containsKey(uniqueId)) {
Nick nick = NickCache.get(uniqueId); Nick nick = NickCache.get(uniqueId);
long timeSinceLastChange = new Date().getTime() - nick.getLastChangedDate(); long timeSinceLastChange = new Date().getTime() - nick.getLastChangedDate();
long waitTime = Config.NICK_WAIT_TIME; long waitTime = Config.NICK_WAIT_TIME;
if (timeSinceLastChange > waitTime) { if (timeSinceLastChange > waitTime || player.hasPermission("utility.nick.admin")) {
if (nick.hasRequest()) { if (nick.hasRequest()) {
player.sendMessage(format(Config.NICK_REQUEST_PLACED player.sendMessage(format(Config.NICK_REQUEST_PLACED
.replace("%oldRequestedNick%", nick.getNewNick()) .replace("%oldRequestedNick%", nick.getNewNick())
@ -208,8 +214,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();
@ -268,7 +274,7 @@ public class Nicknames implements CommandExecutor, TabCompleter {
.replace("%player%", target.getName()))); .replace("%player%", target.getName())));
} }
if (target.isOnline()) { if (target.isOnline() && target.getPlayer() != null) {
target.getPlayer().sendMessage(format(Config.NICK_RESET)); target.getPlayer().sendMessage(format(Config.NICK_RESET));
} }
@ -290,7 +296,7 @@ public class Nicknames implements CommandExecutor, TabCompleter {
Nick nick = NickCache.get(target.getUniqueId()); Nick nick = NickCache.get(target.getUniqueId());
nick.setCurrentNick(nickName); nick.setCurrentNick(nickName);
nick.setLastChangedDate(new Date().getTime()); nick.setLastChangedDate(new Date().getTime());
setNick(target.getUniqueId(), nickName);
} else { } else {
NickCache.put(target.getUniqueId(), new Nick(target.getUniqueId(), nickName, new Date().getTime())); NickCache.put(target.getUniqueId(), new Nick(target.getUniqueId(), nickName, new Date().getTime()));
} }

View File

@ -4,6 +4,7 @@ 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.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
@ -90,6 +91,7 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
ByteArrayDataInput in = ByteStreams.newDataInput(message); ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subChannel = in.readUTF(); String subChannel = in.readUTF();
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;

View File

@ -191,8 +191,8 @@ public class NicknamesGui implements Listener {
if (owningPlayer.isOnline() && owningPlayer.getPlayer() != null) { if (owningPlayer.isOnline() && owningPlayer.getPlayer() != null) {
Nicknames.getInstance().setNick(owningPlayer.getUniqueId(), nick.getNewNick()); Nicknames.getInstance().setNick(owningPlayer.getUniqueId(), nick.getNewNick());
owningPlayer.getPlayer().sendMessage(format(Config.NICK_CHANGED // owningPlayer.getPlayer().sendMessage(format(Config.NICK_CHANGED // This message is also send when the plugin message is received
.replace("%nickname%", nick.getNewNick()))); // .replace("%nickname%", nick.getNewNick())));
} }
NickUtilities.bungeeMessageHandled(uniqueId, e.getWhoClicked().getServer().getPlayer(e.getWhoClicked().getName()), "Accepted"); NickUtilities.bungeeMessageHandled(uniqueId, e.getWhoClicked().getServer().getPlayer(e.getWhoClicked().getName()), "Accepted");

View File

@ -69,7 +69,7 @@ public class PluginMessageListener {
} }
// nicknames WIP // nicknames WIP
case "NickNameAccepted" -> { case "NickNameAccepted", "NickNameSet" -> {
try { try {
short len = in.readShort(); short len = in.readShort();
byte[] msgbytes = new byte[len]; byte[] msgbytes = new byte[len];
@ -88,7 +88,7 @@ public class PluginMessageListener {
proxy.getAllServers().forEach(registeredServer -> proxy.getAllServers().forEach(registeredServer ->
registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), event.getData())); registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), event.getData()));
} }
case "NickNameRequest", "NickNameSet", "NickNameDenied" -> { case "NickNameRequest", "NickNameDenied" -> {
ProxyServer proxy = VelocityChat.getPlugin().getProxy(); ProxyServer proxy = VelocityChat.getPlugin().getProxy();
proxy.getAllServers().forEach(registeredServer -> proxy.getAllServers().forEach(registeredServer ->
registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), event.getData())); registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), event.getData()));