Improve message sending

This commit is contained in:
libraryaddict
2020-07-03 17:36:29 +12:00
parent 3ced8ae9e4
commit e74df537bb
26 changed files with 109 additions and 97 deletions

View File

@@ -2148,11 +2148,33 @@ public class DisguiseUtilities {
return Strings.isEmpty(player.getPlayerListName()) ? player.getName() : player.getPlayerListName();
}
public static void sendMessage(CommandSender sender, String message) {
if (!NmsVersion.v1_16.isSupported()) {
if (!message.isEmpty()) {
sender.sendMessage(message);
}
} else {
BaseComponent[] components = getColoredChat(message);
if (components.length > 0) {
sender.spigot().sendMessage(components);
}
}
}
public static void sendMessage(CommandSender sender, LibsMsg msg, Object... args) {
if (!NmsVersion.v1_16.isSupported()) {
sender.sendMessage(msg.get(args));
String message = msg.get(args);
if (!message.isEmpty()) {
sender.sendMessage(message);
}
} else {
sender.spigot().sendMessage(msg.getChat(args));
BaseComponent[] components = msg.getChat(args);
if (components.length > 0) {
sender.spigot().sendMessage(components);
}
}
}
@@ -2219,6 +2241,10 @@ public class DisguiseUtilities {
* Modification of TextComponent.fromLegacyText
*/
public static BaseComponent[] getColoredChat(String message) {
if (message.isEmpty()) {
return new BaseComponent[0];
}
ArrayList<BaseComponent> components = new ArrayList();
StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent();

View File

@@ -125,11 +125,7 @@ public class DisguiseListener implements Listener {
if (disguises.length > 0) {
DisguiseAPI.undisguiseToAll(player);
String blown = LibsMsg.BLOWN_DISGUISE.get();
if (blown.length() > 0) {
player.sendMessage(blown);
}
DisguiseUtilities.sendMessage(player, LibsMsg.BLOWN_DISGUISE);
}
}
@@ -202,22 +198,14 @@ public class DisguiseListener implements Listener {
if (disguises.length > 0) {
event.setCancelled(true);
String cantAttack = LibsMsg.CANT_ATTACK_DISGUISED.get();
if (cantAttack.length() > 0) {
attacker.sendMessage(cantAttack);
}
DisguiseUtilities.sendMessage(attacker, LibsMsg.CANT_ATTACK_DISGUISED);
} else if (DisguiseConfig.getPvPTimer() > 0 && attacker.hasMetadata("LastDisguise")) {
long lastDisguised = attacker.getMetadata("LastDisguise").get(0).asLong();
if (lastDisguised + DisguiseConfig.getPvPTimer() * 1000 > System.currentTimeMillis()) {
event.setCancelled(true);
String cantAttack = LibsMsg.CANT_ATTACK_DISGUISED_RECENTLY.get();
if (cantAttack.length() > 0) {
attacker.sendMessage(cantAttack);
}
DisguiseUtilities.sendMessage(attacker, LibsMsg.CANT_ATTACK_DISGUISED_RECENTLY);
}
}
}
@@ -331,9 +319,9 @@ public class DisguiseListener implements Listener {
if (DisguiseUtilities.isOlderThan(requiredProtocolLib, version)) {
p.sendMessage(ChatColor.RED + "Update your ProtocolLib! You are running " + version +
" but the minimum version you should be on is " + requiredProtocolLib + "!");
p.sendMessage(ChatColor.RED + "https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/target" +
"/ProtocolLib" +
".jar");
p.sendMessage(
ChatColor.RED + "https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/target" +
"/ProtocolLib" + ".jar");
p.sendMessage(ChatColor.RED + "Use /ld updateprotocollib - To update to the latest development build");
}
}
@@ -563,11 +551,7 @@ public class DisguiseListener implements Listener {
disguise.removeDisguise();
}
String msg = LibsMsg.SWITCH_WORLD_DISGUISE_REMOVED.get();
if (msg.length() > 0) {
event.getPlayer().sendMessage(msg);
}
DisguiseUtilities.sendMessage(event.getPlayer(), LibsMsg.SWITCH_WORLD_DISGUISE_REMOVED);
}
}
@@ -662,11 +646,7 @@ public class DisguiseListener implements Listener {
disguise.removeDisguise();
}
String msg = LibsMsg.SWITCH_WORLD_DISGUISE_REMOVED.get();
if (msg.length() > 0) {
event.getPlayer().sendMessage(msg);
}
DisguiseUtilities.sendMessage(event.getPlayer(), LibsMsg.SWITCH_WORLD_DISGUISE_REMOVED);
}
} else {
// Stupid hack to fix worldswitch invisibility bug & paper packet bug

View File

@@ -372,16 +372,20 @@ public enum LibsMsg {
}
public String get(Object... strings) {
if (StringUtils.countMatches(getRaw(), "%s") != strings.length) {
int matches = StringUtils.countMatches(getRaw(), "%s");
if (matches != strings.length) {
DisguiseUtilities.getLogger().severe("Mismatch in messages, incorrect parameters supplied for " + name() +
". Please inform plugin author.");
". Please inform plugin author if not using translations.");
}
if (strings.length == 0) {
return TranslateType.MESSAGES.get(getRaw());
String trans = TranslateType.MESSAGES.get(getRaw());
if (trans.isEmpty() || strings.length == 0) {
return trans;
}
return String.format(TranslateType.MESSAGES.get(getRaw()), strings);
return String.format(trans, strings);
}
public String toString() {