package com.alttd.chat.nicknames; import com.alttd.chat.ChatPlugin; import com.alttd.chat.config.Config; import com.alttd.chat.database.Queries; import com.alttd.chat.objects.Nick; import com.alttd.chat.util.ALogger; import com.alttd.chat.util.Utility; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.UUID; public class NickUtilities { public static String stringRegen; public static String removeAllColors(String string) { for (final String colorCodes : Config.NICK_ALLOWED_COLOR_CODESLIST) { string = string.replace(colorCodes, ""); } return string.replaceAll("\\{#[A-Fa-f0-9]{6}(<)?(>)?}", ""); } static { NickUtilities.stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}"; } public static void updateCache() { if (!Nicknames.getInstance().nickCacheUpdate.isEmpty()) { Nicknames.getInstance().nickCacheUpdate.forEach(uuid -> { Nick nick = Queries.getNick(uuid); if (nick == null) { Nicknames.getInstance().NickCache.remove(uuid); } else { Nicknames.getInstance().NickCache.put(uuid, nick); } }); } } public static boolean validNick(Player sender, OfflinePlayer target, String nickName) { if (!noBlockedCodes(nickName)) { sender.sendRichMessage(Config.NICK_BLOCKED_COLOR_CODES); return false; } if (!Utility.checkNickBrightEnough(nickName)) { sender.sendRichMessage("At least one color must be brighter than 30 for each color"); return false; } String cleanNick = NickUtilities.removeAllColors(nickName); if (cleanNick.length() < 3 || cleanNick.length() > 16) { sender.sendRichMessage(Config.NICK_INVALID_LENGTH); return false; } 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); return false; } if (cleanNick.equalsIgnoreCase(target.getName())) { return true; } for (Nick nick : Nicknames.getInstance().NickCache.values()) { if (!nick.getUuid().equals(target.getUniqueId()) && ((nick.getCurrentNickNoColor() != null && nick.getCurrentNickNoColor().equalsIgnoreCase(cleanNick)) || (nick.getNewNickNoColor() != null && nick.getNewNickNoColor().equalsIgnoreCase(cleanNick)))) { UUID uuid = nick.getUuid(); UUID uniqueId = target.getUniqueId(); if (uniqueId.equals(uuid)) { ChatPlugin.getInstance().getLogger().info(uuid + " " + uniqueId); } sender.sendRichMessage(Config.NICK_TAKEN); return false; } } return true; } public static boolean noBlockedCodes(final String getNick) { for (final String blockedCodes : Config.NICK_BLOCKED_COLOR_CODESLIST) { if (getNick.contains(blockedCodes)) { return false; } } return true; } public static void bungeeMessageHandled(UUID uniqueId, Player player, String channel) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); // out.writeUTF("Forward"); // So BungeeCord knows to forward it // out.writeUTF("ALL"); out.writeUTF("NickName" + channel); // The channel name to check if this your data ByteArrayOutputStream msgbytes = new ByteArrayOutputStream(); DataOutputStream msgout = new DataOutputStream(msgbytes); try { msgout.writeUTF(uniqueId.toString()); } catch (IOException exception) { ALogger.error("Failed to write UUID to byte array", exception); return; } byte[] bytes = msgbytes.toByteArray(); out.writeShort(bytes.length); out.write(bytes); player.sendPluginMessage(ChatPlugin.getInstance(), Config.MESSAGECHANNEL, out.toByteArray()); } }