mirror of
https://github.com/ViaVersion/ViaBackwards.git
synced 2025-01-22 21:51:33 +01:00
Fix show_entity and show_item in 1.16->1.15
This commit is contained in:
parent
4b0e352849
commit
8b4733258b
@ -0,0 +1,59 @@
|
|||||||
|
package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.chat;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
import us.myles.viaversion.libs.gson.JsonElement;
|
||||||
|
import us.myles.viaversion.libs.gson.JsonObject;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility class to serialize a JsonObject with Minecraft's CompoundTag serialization
|
||||||
|
*/
|
||||||
|
public class TagSerializer {
|
||||||
|
|
||||||
|
private static final Pattern PLAIN_TEXT = Pattern.compile("[A-Za-z0-9._+-]+");
|
||||||
|
|
||||||
|
public static String toString(JsonObject object) {
|
||||||
|
StringBuilder builder = new StringBuilder("{");
|
||||||
|
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
|
||||||
|
Preconditions.checkArgument(entry.getValue().isJsonPrimitive());
|
||||||
|
if (builder.length() != 1) {
|
||||||
|
builder.append(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
String escapedText = escape(entry.getValue().getAsString());
|
||||||
|
builder.append(entry.getKey()).append(':').append(escapedText);
|
||||||
|
}
|
||||||
|
return builder.append('}').toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String escape(String s) {
|
||||||
|
if (PLAIN_TEXT.matcher(s).matches()) return s;
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder(" ");
|
||||||
|
char currentQuote = '\0';
|
||||||
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
|
char c = s.charAt(i);
|
||||||
|
if (c == '\\') {
|
||||||
|
builder.append('\\');
|
||||||
|
} else if (c == '\"' || c == '\'') {
|
||||||
|
if (currentQuote == '\0') {
|
||||||
|
currentQuote = ((c == '\"') ? '\'' : '\"');
|
||||||
|
}
|
||||||
|
if (currentQuote == c) {
|
||||||
|
builder.append('\\');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.append(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentQuote == '\0') {
|
||||||
|
currentQuote = '\"';
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.setCharAt(0, currentQuote);
|
||||||
|
builder.append(currentQuote);
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -68,22 +68,26 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter {
|
|||||||
JsonElement contentsElement = hoverEvent.remove("contents");
|
JsonElement contentsElement = hoverEvent.remove("contents");
|
||||||
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
|
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
|
||||||
if (contentsElement != null) {
|
if (contentsElement != null) {
|
||||||
if (action.equals("show_text")) {
|
|
||||||
// show_text as chat component
|
// show_text as chat component
|
||||||
|
// show_entity and show_item serialized as nbt
|
||||||
|
if (action.equals("show_text")) {
|
||||||
processTranslate(contentsElement);
|
processTranslate(contentsElement);
|
||||||
hoverEvent.add("value", contentsElement);
|
hoverEvent.add("value", contentsElement);
|
||||||
} else if (action.equals("show_item")) {
|
} else if (action.equals("show_item")) {
|
||||||
JsonObject item = contentsElement.getAsJsonObject();
|
JsonObject item = contentsElement.getAsJsonObject();
|
||||||
JsonElement count = item.remove("count");
|
JsonElement count = item.remove("count");
|
||||||
if (count != null) {
|
item.addProperty("Count", count != null ? count.getAsByte() : 1);
|
||||||
item.addProperty("Count", count.getAsByte());
|
|
||||||
|
hoverEvent.addProperty("value", TagSerializer.toString(item));
|
||||||
|
} else if (action.equals("show_entity")) {
|
||||||
|
JsonObject entity = contentsElement.getAsJsonObject();
|
||||||
|
if (entity.has("name")) {
|
||||||
|
entity.addProperty("name", entity.getAsJsonObject("name").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
hoverEvent.addProperty("value", contentsElement.toString());
|
JsonObject hoverObject = new JsonObject();
|
||||||
} else {
|
hoverObject.addProperty("text", TagSerializer.toString(entity));
|
||||||
//TODO escape/fix?
|
hoverEvent.add("value", hoverObject);
|
||||||
// the server sends the json as a string
|
|
||||||
hoverEvent.addProperty("value", contentsElement.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user