Mirror von
https://github.com/ViaVersion/ViaBackwards.git
synchronisiert 2024-11-17 13:30:14 +01:00
Improve 1.13->1.12 translatable handling
Dieser Commit ist enthalten in:
Ursprung
efded25594
Commit
587a5e487f
@ -5,7 +5,7 @@ plugins {
|
||||
|
||||
allprojects {
|
||||
group = "com.viaversion"
|
||||
version = "4.6.1"
|
||||
version = "4.6.2-SNAPSHOT"
|
||||
description = "Allow older clients to join newer server versions."
|
||||
}
|
||||
|
||||
|
@ -35,19 +35,43 @@ import com.viaversion.viaversion.api.Via;
|
||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||
import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types;
|
||||
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
|
||||
import com.viaversion.viaversion.libs.gson.JsonElement;
|
||||
import com.viaversion.viaversion.libs.gson.JsonObject;
|
||||
import com.viaversion.viaversion.libs.gson.JsonParser;
|
||||
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
|
||||
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
|
||||
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
|
||||
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public class Protocol1_12_2To1_13 extends BackwardsProtocol<ClientboundPackets1_13, ClientboundPackets1_12_1, ServerboundPackets1_13, ServerboundPackets1_12_1> {
|
||||
|
||||
public static final BackwardsMappings MAPPINGS = new BackwardsMappings();
|
||||
private final EntityPackets1_13 entityRewriter = new EntityPackets1_13(this);
|
||||
private final BlockItemPackets1_13 blockItemPackets = new BlockItemPackets1_13(this);
|
||||
private final TranslatableRewriter<ClientboundPackets1_13> translatableRewriter = new TranslatableRewriter<ClientboundPackets1_13>(this) {
|
||||
@Override
|
||||
protected void handleTranslate(JsonObject root, String translate) {
|
||||
String newTranslate = mappedTranslationKey(translate);
|
||||
if (newTranslate != null || (newTranslate = getMappingData().getTranslateMappings().get(translate)) != null) {
|
||||
root.addProperty("translate", newTranslate);
|
||||
}
|
||||
}
|
||||
};
|
||||
private final TranslatableRewriter<ClientboundPackets1_13> translatableToLegacyRewriter = new TranslatableRewriter<ClientboundPackets1_13>(this) {
|
||||
@Override
|
||||
protected void handleTranslate(JsonObject root, String translate) {
|
||||
String newTranslate = mappedTranslationKey(translate);
|
||||
if (newTranslate != null || (newTranslate = getMappingData().getTranslateMappings().get(translate)) != null) {
|
||||
root.addProperty("translate", Protocol1_13To1_12_2.MAPPINGS.getMojangTranslation().getOrDefault(newTranslate, newTranslate));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public Protocol1_12_2To1_13() {
|
||||
super(ClientboundPackets1_13.class, ClientboundPackets1_12_1.class, ServerboundPackets1_13.class, ServerboundPackets1_12_1.class);
|
||||
@ -61,15 +85,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol<ClientboundPackets1_
|
||||
Via.getManager().getProviders().register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider());
|
||||
});
|
||||
|
||||
TranslatableRewriter<ClientboundPackets1_13> translatableRewriter = new TranslatableRewriter<ClientboundPackets1_13>(this) {
|
||||
@Override
|
||||
protected void handleTranslate(JsonObject root, String translate) {
|
||||
String newTranslate = mappedTranslationKey(translate);
|
||||
if (newTranslate != null || (newTranslate = getMappingData().getTranslateMappings().get(translate)) != null) {
|
||||
root.addProperty("translate", newTranslate);
|
||||
}
|
||||
}
|
||||
};
|
||||
translatableRewriter.registerPing();
|
||||
translatableRewriter.registerBossBar(ClientboundPackets1_13.BOSSBAR);
|
||||
translatableRewriter.registerComponentPacket(ClientboundPackets1_13.CHAT_MESSAGE);
|
||||
@ -126,4 +141,39 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol<ClientboundPackets1_
|
||||
public BlockItemPackets1_13 getItemRewriter() {
|
||||
return blockItemPackets;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TranslatableRewriter<ClientboundPackets1_13> getTranslatableRewriter() {
|
||||
return translatableRewriter;
|
||||
}
|
||||
|
||||
public String jsonToLegacy(String value) {
|
||||
if (value.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
return jsonToLegacy(JsonParser.parseString(value));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String jsonToLegacy(@Nullable JsonElement value) {
|
||||
if (value == null || value.isJsonNull()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
translatableToLegacyRewriter.processText(value);
|
||||
|
||||
try {
|
||||
Component component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(value);
|
||||
return LegacyComponentSerializer.legacySection().serialize(component);
|
||||
} catch (Exception e) {
|
||||
ViaBackwards.getPlatform().getLogger().warning("Error converting json text to legacy: " + value);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
@ -550,7 +550,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit
|
||||
StringTag name = display.get("Name");
|
||||
if (name != null) {
|
||||
display.put(extraNbtTag + "|Name", new StringTag(name.getValue()));
|
||||
name.setValue(ChatRewriter.jsonToLegacyText(name.getValue()));
|
||||
name.setValue(protocol.jsonToLegacy(name.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,9 +37,9 @@ import com.viaversion.viaversion.api.type.Type;
|
||||
import com.viaversion.viaversion.api.type.types.Particle;
|
||||
import com.viaversion.viaversion.api.type.types.version.Types1_12;
|
||||
import com.viaversion.viaversion.api.type.types.version.Types1_13;
|
||||
import com.viaversion.viaversion.libs.gson.JsonElement;
|
||||
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
|
||||
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
|
||||
import java.util.Optional;
|
||||
|
||||
@ -277,37 +277,22 @@ public class EntityPackets1_13 extends LegacyEntityRewriter<ClientboundPackets1_
|
||||
// Rewrite Meta types
|
||||
filter().handler((event, meta) -> {
|
||||
int typeId = meta.metaType().typeId();
|
||||
|
||||
// Rewrite optional chat to string
|
||||
if (typeId == 5) {
|
||||
// Json -> Legacy is done below
|
||||
meta.setTypeAndValue(MetaType1_12.String, meta.getValue() != null ? meta.getValue().toString() : "");
|
||||
}
|
||||
|
||||
// Rewrite items
|
||||
else if (typeId == 6) {
|
||||
if (typeId == 4) {
|
||||
JsonElement element = meta.value();
|
||||
protocol.getTranslatableRewriter().processText(element);
|
||||
meta.setMetaType(MetaType1_12.Chat);
|
||||
} else if (typeId == 5) {
|
||||
// Rewrite optional chat to string
|
||||
JsonElement element = meta.value();
|
||||
meta.setTypeAndValue(MetaType1_12.String, protocol.jsonToLegacy(element));
|
||||
} else if (typeId == 6) {
|
||||
Item item = (Item) meta.getValue();
|
||||
meta.setTypeAndValue(MetaType1_12.Slot, protocol.getItemRewriter().handleItemToClient(item));
|
||||
}
|
||||
|
||||
// Discontinue particles
|
||||
else if (typeId == 15) {
|
||||
} else if (typeId == 15) {
|
||||
// Discontinue particles
|
||||
event.cancel();
|
||||
}
|
||||
|
||||
// Rewrite to 1.12 ids
|
||||
else if (typeId > 5) {
|
||||
meta.setMetaType(MetaType1_12.byId(
|
||||
typeId - 1
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
// Rewrite Custom Name from Chat to String
|
||||
filter().filterFamily(Entity1_13Types.EntityType.ENTITY).index(2).handler((event, meta) -> {
|
||||
String value = meta.getValue().toString();
|
||||
if (!value.isEmpty()) {
|
||||
meta.setValue(ChatRewriter.jsonToLegacyText(value));
|
||||
} else {
|
||||
meta.setMetaType(MetaType1_12.byId(typeId > 5 ? typeId - 1 : typeId));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -35,7 +35,6 @@ import com.viaversion.viaversion.api.type.Type;
|
||||
import com.viaversion.viaversion.libs.gson.JsonElement;
|
||||
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
|
||||
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
|
||||
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
|
||||
@ -202,13 +201,13 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
|
||||
handler(wrapper -> {
|
||||
byte mode = wrapper.get(Type.BYTE, 0);
|
||||
if (mode == 0 || mode == 2) {
|
||||
String value = wrapper.read(Type.COMPONENT).toString();
|
||||
value = ChatRewriter.jsonToLegacyText(value);
|
||||
if (value.length() > 32) {
|
||||
value = value.substring(0, 32);
|
||||
JsonElement value = wrapper.read(Type.COMPONENT);
|
||||
String legacyValue = protocol.jsonToLegacy(value);
|
||||
if (legacyValue.length() > 32) {
|
||||
legacyValue = legacyValue.substring(0, 32);
|
||||
}
|
||||
|
||||
wrapper.write(Type.STRING, value);
|
||||
wrapper.write(Type.STRING, legacyValue);
|
||||
int type = wrapper.read(Type.VAR_INT);
|
||||
wrapper.write(Type.STRING, type == 1 ? "hearts" : "integer");
|
||||
}
|
||||
@ -225,7 +224,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
|
||||
byte action = wrapper.get(Type.BYTE, 0);
|
||||
if (action == 0 || action == 2) {
|
||||
JsonElement displayName = wrapper.read(Type.COMPONENT);
|
||||
String legacyTextDisplayName = displayName == null || displayName.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(displayName.toString());
|
||||
String legacyTextDisplayName = protocol.jsonToLegacy(displayName);
|
||||
legacyTextDisplayName = ChatUtil.removeUnusedColor(legacyTextDisplayName, 'f');
|
||||
if (legacyTextDisplayName.length() > 32) {
|
||||
legacyTextDisplayName = legacyTextDisplayName.substring(0, 32);
|
||||
@ -244,7 +243,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
|
||||
JsonElement prefixComponent = wrapper.read(Type.COMPONENT);
|
||||
JsonElement suffixComponent = wrapper.read(Type.COMPONENT);
|
||||
|
||||
String prefix = prefixComponent == null || prefixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(prefixComponent.toString());
|
||||
String prefix = protocol.jsonToLegacy(prefixComponent);
|
||||
if (ViaBackwards.getConfig().addTeamColorTo1_13Prefix()) {
|
||||
prefix += "§" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r");
|
||||
}
|
||||
@ -253,7 +252,7 @@ public class PlayerPacket1_13 extends RewriterBase<Protocol1_12_2To1_13> {
|
||||
if (prefix.length() > 16) prefix = prefix.substring(0, 16);
|
||||
if (prefix.endsWith("§")) prefix = prefix.substring(0, prefix.length() - 1);
|
||||
|
||||
String suffix = suffixComponent == null || suffixComponent.isJsonNull() ? "" : ChatRewriter.jsonToLegacyText(suffixComponent.toString());
|
||||
String suffix = protocol.jsonToLegacy(suffixComponent);
|
||||
suffix = ChatUtil.removeUnusedColor(suffix, '\0'); // Don't remove white coloring
|
||||
if (suffix.length() > 16) suffix = suffix.substring(0, 16);
|
||||
if (suffix.endsWith("§")) suffix = suffix.substring(0, suffix.length() - 1);
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren