Fix adventure stuff

This commit is contained in:
libraryaddict 2021-06-15 11:04:37 +12:00
parent 381ab971b6
commit fffea76e8d
5 changed files with 17 additions and 151 deletions

View File

@ -73,9 +73,7 @@
<minimizeJar>true</minimizeJar> <minimizeJar>true</minimizeJar>
<artifactSet> <artifactSet>
<includes> <includes>
<include>net.kyori:adventure-text-minimessage</include> <include>net.kyori:*</include>
<include>net.kyori:adventure-text-serializer-gson</include>
<include>net.kyori:adventure-api</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations> <relocations>
@ -100,7 +98,7 @@
<asm.version>9.0</asm.version> <asm.version>9.0</asm.version>
<lombok.version>1.18.16</lombok.version> <lombok.version>1.18.16</lombok.version>
<protocollib.version>master-SNAPSHOT</protocollib.version> <protocollib.version>master-SNAPSHOT</protocollib.version>
<spigot.version>[1.16,]</spigot.version> <spigot.version>[1.16,1.16.5]</spigot.version>
<junit.version>4.13.1</junit.version> <junit.version>4.13.1</junit.version>
<paper-api.version>[1.16,]</paper-api.version> <paper-api.version>[1.16,]</paper-api.version>
<bungeecord-chat.version>1.12-SNAPSHOT</bungeecord-chat.version> <bungeecord-chat.version>1.12-SNAPSHOT</bungeecord-chat.version>

View File

@ -569,13 +569,8 @@ public class FlagWatcher {
} }
if (NmsVersion.v1_13.isSupported()) { if (NmsVersion.v1_13.isSupported()) {
Optional<WrappedChatComponent> optional; Optional<WrappedChatComponent> optional =
Optional.of(WrappedChatComponent.fromJson(DisguiseUtilities.serialize(DisguiseUtilities.getAdventureChat(name))));
if (DisguiseUtilities.hasAdventureTextSupport()) {
optional = Optional.of(AdventureComponentConverter.fromComponent(DisguiseUtilities.getAdventureChat(name)));
} else {
optional = Optional.of(WrappedChatComponent.fromJson(ComponentSerializer.toString(DisguiseUtilities.getColoredChat(name))));
}
setData(MetaIndex.ENTITY_CUSTOM_NAME, optional); setData(MetaIndex.ENTITY_CUSTOM_NAME, optional);
} else { } else {

View File

@ -39,7 +39,6 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.chat.ComponentSerializer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -219,29 +218,8 @@ public class DisguiseUtilities {
} }
} }
/** public static String serialize(Component component) {
* This should return true every single time, except for when ProtocolLib isn't updated for AdventureComponentConverter return GsonComponentSerializer.gson().serialize(component);
*
* The reason being is that we shade the adventure text library in the plugin
*/
public static boolean hasAdventureTextSupport() {
if (adventureTextSupport == null) {
try {
adventureTextSupport = true;
// Force a test for support by actually trying to use it all
BaseComponent[] test1 = ComponentSerializer.parse(GsonComponentSerializer.gson().serialize(getAdventureChat("<green>test")));
WrappedChatComponent test2 = AdventureComponentConverter.fromComponent(DisguiseUtilities.getAdventureChat("<green>test"));
if (test1 == null || test1.length == 0 || test2 == null) {
adventureTextSupport = false;
}
} catch (Throwable ex) {
adventureTextSupport = false;
}
}
return adventureTextSupport;
} }
public static void doSkinUUIDWarning(CommandSender sender) { public static void doSkinUUIDWarning(CommandSender sender) {
@ -2549,110 +2527,12 @@ public class DisguiseUtilities {
return MiniMessage.get().parse(message); return MiniMessage.get().parse(message);
} }
/**
* Modification of TextComponent.fromLegacyText
*/
public static BaseComponent[] getColoredChat(String message) { public static BaseComponent[] getColoredChat(String message) {
if (message.isEmpty()) { if (message.isEmpty()) {
return new BaseComponent[0]; return new BaseComponent[0];
} }
if (hasAdventureTextSupport()) { return ComponentSerializer.parse(serialize(getAdventureChat(message)));
return ComponentSerializer.parse(GsonComponentSerializer.gson().serialize(getAdventureChat(message)));
}
ArrayList<BaseComponent> components = new ArrayList();
StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent();
Matcher matcher = urlMatcher.matcher(message);
for (int i = 0; i < message.length(); ++i) {
char c = message.charAt(i);
TextComponent old;
if (c == ChatColor.COLOR_CHAR && i + 1 >= message.length()) {
break;
}
if (c == ChatColor.COLOR_CHAR || (NmsVersion.v1_16.isSupported() && c == '<' && i + 9 < message.length() &&
Pattern.matches("<#[0-9a-fA-F]{6}>", message.substring(i, i + 9)))) {
i++;
net.md_5.bungee.api.ChatColor format;
if (c != ChatColor.COLOR_CHAR || (message.length() - i >= 7 && Pattern.matches("#[0-9a-fA-F]{6}", message.substring(i, i + 7)))) {
format = net.md_5.bungee.api.ChatColor.of(message.substring(i, i + 7));
i += c == '<' ? 7 : 8;
} else {
c = message.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = (char) (c + 32);
}
format = net.md_5.bungee.api.ChatColor.getByChar(c);
}
if (format != null) {
if (builder.length() > 0) {
old = component;
component = new TextComponent(component);
old.setText(builder.toString());
builder = new StringBuilder();
components.add(old);
}
if (format == net.md_5.bungee.api.ChatColor.BOLD) {
component.setBold(true);
} else if (format == net.md_5.bungee.api.ChatColor.ITALIC) {
component.setItalic(true);
} else if (format == net.md_5.bungee.api.ChatColor.UNDERLINE) {
component.setUnderlined(true);
} else if (format == net.md_5.bungee.api.ChatColor.STRIKETHROUGH) {
component.setStrikethrough(true);
} else if (format == net.md_5.bungee.api.ChatColor.MAGIC) {
component.setObfuscated(true);
} else if (format == net.md_5.bungee.api.ChatColor.RESET) {
component = new TextComponent();
component.setColor(net.md_5.bungee.api.ChatColor.WHITE);
} else {
component = new TextComponent();
component.setColor(format);
}
}
} else {
int pos = message.indexOf(32, i);
if (pos == -1) {
pos = message.length();
}
if (matcher.region(i, pos).find()) {
if (builder.length() > 0) {
old = component;
component = new TextComponent(component);
old.setText(unquoteHex(builder.toString()));
builder = new StringBuilder();
components.add(old);
}
old = component;
component = new TextComponent(component);
String urlString = message.substring(i, pos);
component.setText(unquoteHex(urlString));
component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, urlString.startsWith("http") ? urlString : "http://" + urlString));
components.add(component);
i += pos - i - 1;
component = old;
} else {
builder.append(c);
}
}
}
component.setText(unquoteHex(builder.toString()));
components.add(component);
return components.toArray(new BaseComponent[0]);
} }
public static void sendProtocolLibUpdateMessage(CommandSender p, String version, String requiredProtocolLib) { public static void sendProtocolLibUpdateMessage(CommandSender p, String version, String requiredProtocolLib) {

View File

@ -297,12 +297,9 @@ public enum LibsMsg {
SELF_DISGUISE_HIDDEN("<green>Self disguise hidden as it's too tall.."); SELF_DISGUISE_HIDDEN("<green>Self disguise hidden as it's too tall..");
private final String string; private final String string;
private final String useString;
LibsMsg(String string) { LibsMsg(String string) {
this.string = string; this.string = string;
useString = DisguiseUtilities.hasAdventureTextSupport() ? string : getVanillaFormat();
} }
public String getVanillaFormat() { public String getVanillaFormat() {
@ -319,10 +316,6 @@ public enum LibsMsg {
return string; return string;
} }
public String getStringToUse() {
return useString;
}
public BaseComponent[] getChat(Object... strings) { public BaseComponent[] getChat(Object... strings) {
String string = get(strings); String string = get(strings);

View File

@ -138,19 +138,19 @@ public enum TranslateType {
save(null, message, comment); save(null, message, comment);
} }
public void save(LibsMsg orig, String message, String comment) { public void save(LibsMsg orig, String rawMessage, String comment) {
toDeDupe.put(StringEscapeUtils.escapeJava(message.replace("§", "&")), false); toDeDupe.put(StringEscapeUtils.escapeJava(rawMessage.replace("§", "&")), false);
if (translated.containsKey(message)) { if (translated.containsKey(rawMessage)) {
return; return;
} }
String value = message; String value = rawMessage;
if (orig != null) { if (orig != null) {
String vanilla = orig.getVanillaFormat(); String vanilla = orig.getVanillaFormat();
if (translated.containsKey(vanilla) && !vanilla.equals(message) && !translated.get(vanilla).equals(vanilla)) { if (translated.containsKey(vanilla) && !vanilla.equals(rawMessage) && !translated.get(vanilla).equals(vanilla)) {
value = translated.get(vanilla); value = translated.get(vanilla);
for (ChatColor color : ChatColor.values()) { for (ChatColor color : ChatColor.values()) {
@ -159,7 +159,7 @@ public enum TranslateType {
} }
} }
translated.put(message, value); translated.put(rawMessage, value);
try { try {
boolean exists = getFile().exists(); boolean exists = getFile().exists();
@ -185,10 +185,10 @@ public enum TranslateType {
} }
} }
String sanitizedKey = StringEscapeUtils.escapeJava(message.replace("§", "&")); String sanitizedKey = StringEscapeUtils.escapeJava(rawMessage.replace("§", "&"));
String sanitized = StringEscapeUtils.escapeJava(value.replace("§", "&")); String sanitizedValue = StringEscapeUtils.escapeJava(value.replace("§", "&"));
writer.write("\n" + (comment != null ? "# " + comment + "\n" : "") + "\"" + sanitizedKey + "\": \"" + sanitized + "\"\n"); writer.write("\n" + (comment != null ? "# " + comment + "\n" : "") + "\"" + sanitizedKey + "\": \"" + sanitizedValue + "\"\n");
written++; written++;
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -274,7 +274,7 @@ public enum TranslateType {
} }
public String get(LibsMsg msg) { public String get(LibsMsg msg) {
return get(msg.getStringToUse()); return get(msg.getRaw());
} }
public String get(String msg) { public String get(String msg) {