Messages: Version fixes, fallback to plain.

- Properly parse NMS version number to support 1.10
- Fallback to plain messages if sending a fancy message breaks (we try again after restart/reload)
This commit is contained in:
Thijs Wiefferink 2016-06-12 16:48:47 +02:00
parent c2bcc6008b
commit ec57b922b8
2 changed files with 25 additions and 6 deletions

View File

@ -47,10 +47,19 @@ public class FancyMessageSender {
Class<?> chatSerializerClazz;
String version = Reflection.getVersion();
double majorVersion = Double.parseDouble(version.replace('_', '.').substring(1, 4));
int lesserVersion = Integer.parseInt(version.substring(6, 7));
if(version == null || version.length() < 2) {
throw new RuntimeException("Could not get NMS version, found: "+version);
}
version = version.substring(1, version.length()-1); // Strip v and the . at the end
String[] parts = version.split("_");
if(parts.length < 3) {
throw new RuntimeException("Not enough parts in the version, found: "+version);
}
int majorVersion = Integer.parseInt(parts[0]);
int minorVersion = Integer.parseInt(parts[1]);
int revision = Integer.parseInt(parts[2].substring(1));
if(majorVersion < 1.8 || (majorVersion == 1.8 && lesserVersion == 1)) {
if((majorVersion <= 1 && minorVersion < 8) || (majorVersion == 1 && minorVersion == 8 && revision == 1)) {
chatSerializerClazz = Reflection.getNMSClass("ChatSerializer");
} else {
chatSerializerClazz = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");

View File

@ -19,6 +19,7 @@ public class Message {
public static final String LANGUAGEVARIABLE = "lang:";
public static final String CHATLANGUAGEVARIABLE = "prefix";
public static final int REPLACEMENTLIMIT = 50;
private static boolean fancyWorks = true;
private List<String> message;
private Object[] replacements;
@ -158,9 +159,18 @@ public class Message {
}
executeReplacements();
if(target instanceof Player) {
if(AreaShop.getInstance().getConfig().getBoolean("useFancyMessages")) {
FancyMessageSender.sendJSON((Player)target, FancyMessageFormat.convertToJSON(message));
} else {
boolean sendPlain = true;
if(AreaShop.getInstance().getConfig().getBoolean("useFancyMessages") && fancyWorks) {
try {
boolean result = FancyMessageSender.sendJSON((Player)target, FancyMessageFormat.convertToJSON(message));
sendPlain = !result;
fancyWorks = result;
} catch(Exception e) {
fancyWorks = false;
AreaShop.getInstance().getLogger().warning("Sending fancy message did not work, falling back to plain messages. Message key: "+key);
}
}
if(sendPlain) { // Fancy messages disabled or broken
((Player)target).sendMessage(FancyMessageFormat.convertToConsole(message));
}
} else {