Fix invalid json nbt chat messages, #712

This commit is contained in:
Myles 2017-07-31 15:24:25 +01:00
parent 4d58c717cd
commit a64c21da8b
8 changed files with 95 additions and 0 deletions

View File

@ -157,6 +157,11 @@ public class BukkitConfigAPI extends Config implements ViaVersionConfig {
return getBoolean("force-json-transform", false);
}
@Override
public boolean is1_12NBTArrayFix() {
return getBoolean("chat-nbt-fix", true);
}
@Override
public List<Integer> getBlockedProtocols() {
return getIntegerList("block-protocols");

View File

@ -191,6 +191,11 @@ public class BungeeConfigAPI extends Config implements ViaVersionConfig {
return getBoolean("force-json-transform", false);
}
@Override
public boolean is1_12NBTArrayFix() {
return getBoolean("chat-nbt-fix", true);
}
@Override
public List<Integer> getBlockedProtocols() {
return getIntegerList("block-protocols");

View File

@ -203,6 +203,13 @@ public interface ViaVersionConfig {
*/
boolean isForceJsonTransform();
/**
* Should we fix nbt array's in json chat messages for 1.12 clients
*
* @return True if enabled
*/
boolean is1_12NBTArrayFix();
/**
* Get the blocked protocols
*

View File

@ -0,0 +1,42 @@
package us.myles.ViaVersion.protocols.protocol1_12to1_11_1;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import us.myles.ViaVersion.api.data.UserConnection;
import java.util.regex.Pattern;
public class ChatItemRewriter {
private static Pattern indexRemoval = Pattern.compile("\\d+:(?=([^\"\\\\]*(\\\\.|\"([^\"\\\\]*\\\\.)*[^\"\\\\]*\"))*[^\"]*$)");
// Taken from https://stackoverflow.com/questions/6462578/alternative-to-regex-match-all-instances-not-inside-quotes
public static void toClient(JsonElement element, UserConnection user) {
if (element instanceof JsonObject) {
JsonObject obj = (JsonObject) element;
if (obj.has("hoverEvent")) {
if (obj.get("hoverEvent") instanceof JsonObject) {
JsonObject hoverEvent = (JsonObject) obj.get("hoverEvent");
if (hoverEvent.has("action") && hoverEvent.has("value")) {
String type = hoverEvent.get("action").getAsString();
if (type.equals("show_item") || type.equals("show_entity")) {
String value = hoverEvent.get("value").getAsString();
value = indexRemoval.matcher(value).replaceAll("");
hoverEvent.addProperty("value", value);
}
}
}
} else {
if (obj.has("extra")) {
toClient(obj.get("extra"), user);
}
}
}
if (element instanceof JsonArray) {
JsonArray array = (JsonArray) element;
for (JsonElement value : array) {
toClient(value, user);
}
}
}
}

View File

@ -4,7 +4,10 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.google.common.base.Optional;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_12Types;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
@ -19,6 +22,7 @@ import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.packets.InventoryPacke
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage.EntityTracker;
import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
public class Protocol1_12To1_11_1 extends Protocol {
@ -85,6 +89,29 @@ public class Protocol1_12To1_11_1 extends Protocol {
}
});
// Chat message packet
registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING, Protocol1_9TO1_8.FIX_JSON); // 0 - Chat Message (json)
map(Type.BYTE); // 1 - Chat Positon
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
if (!Via.getConfig().is1_12NBTArrayFix()) return;
try {
JsonElement obj = new JsonParser().parse(wrapper.get(Type.STRING, 0));
ChatItemRewriter.toClient(obj, wrapper.user());
wrapper.set(Type.STRING, 0, obj.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
// Chunk Data
registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() {
@Override
@ -128,6 +155,7 @@ public class Protocol1_12To1_11_1 extends Protocol {
});
}
});
// Join Packet
registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() {
@Override

View File

@ -24,6 +24,7 @@ public abstract class Config implements ConfigurationProvider {
return new Yaml(new YamlConstructor(), new Representer(), options);
}
};
private CommentStore commentStore = new CommentStore('.', 2);
private final File configFile;
private ConcurrentSkipListMap<String, Object> config;

View File

@ -82,6 +82,8 @@ hologram-y: -0.96
# Should we disable piston animation for 1.11/1.11.1 clients?
# In some cases when firing lots of pistons it crashes them.
piston-animation-patch: false
# Should we fix nbt for 1.12 and above clients in chat messages (causes invalid item)
chat-nbt-fix: true
#
#----------------------------------------------------------#
# 1.9 & 1.10 CLIENTS ON 1.8 SERVERS OPTIONS #

View File

@ -164,6 +164,11 @@ public class SpongeConfigAPI extends Config implements ViaVersionConfig {
return getBoolean("force-json-transform", false);
}
@Override
public boolean is1_12NBTArrayFix() {
return getBoolean("chat-nbt-fix", true);
}
@Override
public List<Integer> getBlockedProtocols() {
return getIntegerList("block-protocols");