Update nicknames for Player and CMI.
This commit is contained in:
parent
5b06049265
commit
d90f5ac376
|
|
@ -13,6 +13,7 @@ dependencies {
|
|||
compileOnly("com.gitlab.ruany:LiteBansAPI:0.3.5") // move to proxy
|
||||
compileOnly("org.apache.commons:commons-lang3:3.12.0") // needs an alternative, already removed from upstream api and will be removed in server
|
||||
compileOnly("net.luckperms:api:5.3") // Luckperms
|
||||
compileOnly(files("../libs/CMI.jar"))
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
|
|
|||
|
|
@ -25,77 +25,77 @@ public class NickUtilities
|
|||
{
|
||||
public static String stringRegen;
|
||||
|
||||
// public static String applyColor(String message) {
|
||||
// ChatColor hexColor1 = null;
|
||||
// ChatColor hexColor2;
|
||||
// StringBuilder stringBuilder = new StringBuilder();
|
||||
// message = ChatColor.translateAlternateColorCodes('&', message);
|
||||
// boolean startsWithColor = false;
|
||||
// boolean lastColorMatters = false;
|
||||
//
|
||||
// if (message.matches(".*" + NickUtilities.stringRegen + ".*")) {
|
||||
// String[] split = message.split(NickUtilities.stringRegen);
|
||||
//
|
||||
// ArrayList<String> list = new ArrayList<>();
|
||||
// int nextIndex = 0;
|
||||
// if (message.indexOf("}") <= 11) {
|
||||
// startsWithColor = true;
|
||||
// list.add(message.substring(0, message.indexOf("}") + 1));
|
||||
// }
|
||||
// for (String s : split) {
|
||||
// nextIndex += s.length();
|
||||
// int tmp = message.indexOf("}", nextIndex);
|
||||
// if (tmp < message.length() && tmp>=0) {
|
||||
// list.add(message.substring(nextIndex, tmp + 1));
|
||||
// nextIndex = tmp + 1;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// int i;
|
||||
// boolean firstLoop = true;
|
||||
// if (startsWithColor) {
|
||||
// i = -1;
|
||||
// } else {
|
||||
// i = 0;
|
||||
// stringBuilder.append(split[i]);
|
||||
// }
|
||||
//
|
||||
// for (String s : list) {
|
||||
// boolean lesser = s.contains("<");
|
||||
// boolean bigger = s.contains(">");
|
||||
//
|
||||
// if (bigger && lesser) {
|
||||
// hexColor2 = ChatColor.of(s.substring(1, s.length() - 3));
|
||||
// } else if (bigger || lesser) {
|
||||
// hexColor2 = ChatColor.of(s.substring(1, s.length() - 2));
|
||||
// } else {
|
||||
// hexColor2 = ChatColor.of(s.substring(1, s.length() -1));
|
||||
// }
|
||||
//
|
||||
// if (firstLoop) {
|
||||
// lastColorMatters = bigger;
|
||||
// hexColor1 = hexColor2;
|
||||
// firstLoop = false;
|
||||
// i++;
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// if (lesser && lastColorMatters) {
|
||||
// stringBuilder.append(hexGradient(hexColor1.getColor(), hexColor2.getColor(), split[i]));
|
||||
// } else {
|
||||
// stringBuilder.append(hexColor1).append(split[i]);
|
||||
// }
|
||||
//
|
||||
// hexColor1 = hexColor2;
|
||||
// lastColorMatters = bigger;
|
||||
// i++;
|
||||
// }
|
||||
// if (split.length > i){
|
||||
// stringBuilder.append(hexColor1).append(split[i]);
|
||||
// }
|
||||
// }
|
||||
// return stringBuilder.length()==0 ? message : stringBuilder.toString();
|
||||
// }
|
||||
public static String applyColor(String message) {
|
||||
ChatColor hexColor1 = null;
|
||||
ChatColor hexColor2;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
message = ChatColor.translateAlternateColorCodes('&', message);
|
||||
boolean startsWithColor = false;
|
||||
boolean lastColorMatters = false;
|
||||
|
||||
if (message.matches(".*" + NickUtilities.stringRegen + ".*")) {
|
||||
String[] split = message.split(NickUtilities.stringRegen);
|
||||
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
int nextIndex = 0;
|
||||
if (message.indexOf("}") <= 11) {
|
||||
startsWithColor = true;
|
||||
list.add(message.substring(0, message.indexOf("}") + 1));
|
||||
}
|
||||
for (String s : split) {
|
||||
nextIndex += s.length();
|
||||
int tmp = message.indexOf("}", nextIndex);
|
||||
if (tmp < message.length() && tmp>=0) {
|
||||
list.add(message.substring(nextIndex, tmp + 1));
|
||||
nextIndex = tmp + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int i;
|
||||
boolean firstLoop = true;
|
||||
if (startsWithColor) {
|
||||
i = -1;
|
||||
} else {
|
||||
i = 0;
|
||||
stringBuilder.append(split[i]);
|
||||
}
|
||||
|
||||
for (String s : list) {
|
||||
boolean lesser = s.contains("<");
|
||||
boolean bigger = s.contains(">");
|
||||
|
||||
if (bigger && lesser) {
|
||||
hexColor2 = ChatColor.of(s.substring(1, s.length() - 3));
|
||||
} else if (bigger || lesser) {
|
||||
hexColor2 = ChatColor.of(s.substring(1, s.length() - 2));
|
||||
} else {
|
||||
hexColor2 = ChatColor.of(s.substring(1, s.length() -1));
|
||||
}
|
||||
|
||||
if (firstLoop) {
|
||||
lastColorMatters = bigger;
|
||||
hexColor1 = hexColor2;
|
||||
firstLoop = false;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lesser && lastColorMatters) {
|
||||
stringBuilder.append(hexGradient(hexColor1.getColor(), hexColor2.getColor(), split[i]));
|
||||
} else {
|
||||
stringBuilder.append(hexColor1).append(split[i]);
|
||||
}
|
||||
|
||||
hexColor1 = hexColor2;
|
||||
lastColorMatters = bigger;
|
||||
i++;
|
||||
}
|
||||
if (split.length > i){
|
||||
stringBuilder.append(hexColor1).append(split[i]);
|
||||
}
|
||||
}
|
||||
return stringBuilder.length()==0 ? message : stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static String removeAllColors(String string) {
|
||||
|
||||
|
|
@ -110,28 +110,28 @@ public class NickUtilities
|
|||
NickUtilities.stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}";
|
||||
}
|
||||
|
||||
// public static String hexGradient(Color color1, Color color2, String text){
|
||||
// double r = color1.getRed();
|
||||
// double g = color1.getGreen();
|
||||
// double b = color1.getBlue();
|
||||
//
|
||||
// double rDifference = (color1.getRed() - color2.getRed()) / ((double) text.length() - 1);
|
||||
// double gDifference = (color1.getGreen() - color2.getGreen()) / ((double) text.length() - 1);
|
||||
// double bDifference = (color1.getBlue() - color2.getBlue()) / ((double) text.length() - 1);
|
||||
//
|
||||
// StringBuilder stringBuilder = new StringBuilder();
|
||||
// char[] chars = text.toCharArray();
|
||||
// for (int i = 0; i < text.length(); i++) {
|
||||
// if (i > 0) {
|
||||
// r = r - rDifference;
|
||||
// g = g - gDifference;
|
||||
// b = b - bDifference;
|
||||
// }
|
||||
// stringBuilder.append(ChatColor.of(new Color((int) r, (int) g, (int) b))).append(chars[i]);
|
||||
// }
|
||||
//
|
||||
// return stringBuilder.toString();
|
||||
// }
|
||||
public static String hexGradient(Color color1, Color color2, String text){
|
||||
double r = color1.getRed();
|
||||
double g = color1.getGreen();
|
||||
double b = color1.getBlue();
|
||||
|
||||
double rDifference = (color1.getRed() - color2.getRed()) / ((double) text.length() - 1);
|
||||
double gDifference = (color1.getGreen() - color2.getGreen()) / ((double) text.length() - 1);
|
||||
double bDifference = (color1.getBlue() - color2.getBlue()) / ((double) text.length() - 1);
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
char[] chars = text.toCharArray();
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
if (i > 0) {
|
||||
r = r - rDifference;
|
||||
g = g - gDifference;
|
||||
b = b - bDifference;
|
||||
}
|
||||
stringBuilder.append(ChatColor.of(new Color((int) r, (int) g, (int) b))).append(chars[i]);
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static void updateCache() {
|
||||
if (!Nicknames.getInstance().nickCacheUpdate.isEmpty()){
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.alttd.chat.nicknames;
|
||||
|
||||
import com.Zrips.CMI.CMI;
|
||||
import com.Zrips.CMI.Containers.CMIUser;
|
||||
import com.alttd.chat.ChatAPI;
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.config.Config;
|
||||
|
|
@ -22,6 +24,7 @@ import org.bukkit.command.CommandExecutor;
|
|||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
|
@ -284,7 +287,7 @@ public class Nicknames implements CommandExecutor, TabCompleter {
|
|||
|
||||
} else if (NickUtilities.validNick(sender, target, nickName)) {
|
||||
if (target.isOnline()) {
|
||||
setNick(target.getUniqueId(), nickName);
|
||||
setNick(target.getPlayer(), nickName);
|
||||
} else {
|
||||
NickUtilities.bungeeMessageHandled(target.getUniqueId(), sender, "Set");
|
||||
}
|
||||
|
|
@ -297,7 +300,7 @@ public class Nicknames implements CommandExecutor, TabCompleter {
|
|||
Nick nick = NickCache.get(target.getUniqueId());
|
||||
nick.setCurrentNick(nickName);
|
||||
nick.setLastChangedDate(new Date().getTime());
|
||||
setNick(target.getUniqueId(), nickName);
|
||||
setNick(target.getPlayer(), nickName);
|
||||
} else {
|
||||
NickCache.put(target.getUniqueId(), new Nick(target.getUniqueId(), nickName, new Date().getTime()));
|
||||
}
|
||||
|
|
@ -380,6 +383,8 @@ public class Nicknames implements CommandExecutor, TabCompleter {
|
|||
public void resetNick(final Player player) {
|
||||
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||
user.setDisplayName(player.getName());
|
||||
player.displayName(user.getDisplayName());
|
||||
updateCMIUser(player, null);
|
||||
}
|
||||
|
||||
public String getNick(final Player player) {
|
||||
|
|
@ -387,18 +392,45 @@ public class Nicknames implements CommandExecutor, TabCompleter {
|
|||
return user.getNickNameString();
|
||||
}
|
||||
|
||||
public void setNick(final UUID uuid, final String nickName) {
|
||||
ChatUser user = ChatUserManager.getChatUser(uuid);
|
||||
public void setNick(final Player player, final String nickName) {
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
ChatUser user = ChatUserManager.getChatUser(player.getUniqueId());
|
||||
user.setDisplayName(nickName);
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
player.displayName(user.getDisplayName());
|
||||
player.displayName(user.getDisplayName());
|
||||
updateCMIUser(player, nickName);
|
||||
}
|
||||
|
||||
// public static String format(final String m) {
|
||||
// return NickUtilities.applyColor(m);
|
||||
// }
|
||||
|
||||
public void updateCMIUser(Player player, String nickName) {
|
||||
if (!isCMIEnabled())
|
||||
return;
|
||||
|
||||
CMIUser cmiUser = CMI.getInstance().getPlayerManager().getUser(player);
|
||||
if (nickName == null){
|
||||
cmiUser.setNickName(null, true);
|
||||
} else {
|
||||
cmiUser.setNickName(NickUtilities.applyColor(nickName), true);
|
||||
}
|
||||
cmiUser.updateDisplayName();
|
||||
}
|
||||
|
||||
private Boolean isCMIEnabled = null;
|
||||
private Boolean isCMIEnabled() {
|
||||
if (!(isCMIEnabled == null))
|
||||
return isCMIEnabled;
|
||||
|
||||
Plugin plugin = Bukkit.getPluginManager().getPlugin("CMI");
|
||||
if (plugin != null && plugin.isEnabled())
|
||||
return isCMIEnabled = true;
|
||||
|
||||
return isCMIEnabled = false;
|
||||
}
|
||||
|
||||
public static Nicknames getInstance() {
|
||||
return Nicknames.instance;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package com.alttd.chat.nicknames;
|
||||
|
||||
import com.Zrips.CMI.commands.list.colorlimits;
|
||||
import com.Zrips.CMI.utils.Util;
|
||||
import com.alttd.chat.ChatPlugin;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.Queries;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.Nick;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
|
@ -50,18 +54,18 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
|
|||
|
||||
String nickName = nick.getCurrentNick();
|
||||
String strippedNick = Nicknames.getInstance().getNick(player);
|
||||
// try {
|
||||
// strippedNick = MiniMessage.miniMessage().stripTokens(Nicknames.getInstance().getNick(player));
|
||||
// } catch (NullPointerException ignored) {
|
||||
// }
|
||||
try {
|
||||
strippedNick = MiniMessage.miniMessage().stripTags(Nicknames.getInstance().getNick(player));
|
||||
} catch (NullPointerException ignored) {
|
||||
}
|
||||
// final String strippedNick = CMIChatColor.stripColor(Nicknames.getInstance().getNick(player));
|
||||
|
||||
//final String cmiNick = Util.CMIChatColor.deColorize(Nicknames.getInstance().getNick(player));
|
||||
// final String cmiNick = Util.CMIChatColor.deColorize(Nicknames.getInstance().getNick(player));
|
||||
|
||||
if (nickName == null) {
|
||||
Nicknames.getInstance().resetNick(player);
|
||||
} else if (!nickName.equals(strippedNick)) {
|
||||
Nicknames.getInstance().setNick(player.getUniqueId(), nickName);
|
||||
Nicknames.getInstance().setNick(player, nickName);
|
||||
}
|
||||
|
||||
Nicknames.getInstance().NickCache.put(e.getPlayer().getUniqueId(), nick);
|
||||
|
|
@ -133,7 +137,7 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
|
|||
if (offlinePlayer.isOnline()) {
|
||||
Nick nick = Queries.getNick(playerUUID);
|
||||
if (nick != null && nick.getCurrentNick() != null) {
|
||||
Nicknames.getInstance().setNick(offlinePlayer.getUniqueId(), nick.getCurrentNick());
|
||||
Nicknames.getInstance().setNick(offlinePlayer.getPlayer(), nick.getCurrentNick());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -152,7 +156,7 @@ public class NicknamesEvents implements Listener, PluginMessageListener {
|
|||
Nick nick = Queries.getNick(playerUUID);
|
||||
Player target = Bukkit.getPlayer(playerUUID);
|
||||
if (target != null && nick != null && nick.getCurrentNick() != null) {
|
||||
Nicknames.getInstance().setNick(target.getUniqueId(), nick.getCurrentNick());
|
||||
Nicknames.getInstance().setNick(target, nick.getCurrentNick());
|
||||
target.sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_CHANGED,
|
||||
Placeholder.unparsed("nickname", nick.getCurrentNick())));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,7 +198,7 @@ public class NicknamesGui implements Listener {
|
|||
Placeholder.component("oldnick", Utility.applyColor(nick.getCurrentNick() == null ? clickedItem.getItemMeta().getDisplayName() : nick.getCurrentNick()))));
|
||||
|
||||
if (owningPlayer.isOnline() && owningPlayer.getPlayer() != null) {
|
||||
Nicknames.getInstance().setNick(owningPlayer.getUniqueId(), nick.getNewNick());
|
||||
Nicknames.getInstance().setNick(owningPlayer.getPlayer(), nick.getNewNick());
|
||||
// owningPlayer.getPlayer().sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_CHANGED // This message is also send when the plugin message is received
|
||||
// .replace("%nickname%", nick.getNewNick())));
|
||||
}
|
||||
|
|
@ -254,7 +254,7 @@ public class NicknamesGui implements Listener {
|
|||
}
|
||||
|
||||
if (owningPlayer.isOnline() && owningPlayer.getPlayer() != null) {
|
||||
Nicknames.getInstance().setNick(owningPlayer.getUniqueId(), nick.getCurrentNick() == null ? owningPlayer.getName() : nick.getCurrentNick());
|
||||
Nicknames.getInstance().setNick(owningPlayer.getPlayer(), nick.getCurrentNick() == null ? owningPlayer.getName() : nick.getCurrentNick());
|
||||
owningPlayer.getPlayer().sendMessage(MiniMessage.miniMessage().deserialize(Config.NICK_NOT_CHANGED));
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
libs/CMI.jar
Executable file
BIN
libs/CMI.jar
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user