3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-10-03 08:51:05 +02:00

Fix NPE for chat messages before join game (#456)

Dieser Commit ist enthalten in:
Gero 2022-06-14 10:39:35 +02:00 committet von GitHub
Ursprung b84cda54c3
Commit 187b79f4e1
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
5 geänderte Dateien mit 44 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -51,7 +51,7 @@ public class BackwardsMappings extends MappingDataBase {
} }
@Override @Override
protected void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) { protected final void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) {
if (diffMappings != null) { if (diffMappings != null) {
JsonObject diffItems = diffMappings.getAsJsonObject("items"); JsonObject diffItems = diffMappings.getAsJsonObject("items");
if (diffItems != null) { if (diffItems != null) {

Datei anzeigen

@ -29,6 +29,7 @@ import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonPrimitive; import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.viaversion.libs.gson.JsonSyntaxException; import com.viaversion.viaversion.libs.gson.JsonSyntaxException;
import com.viaversion.viaversion.util.GsonUtil; import com.viaversion.viaversion.util.GsonUtil;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -58,10 +59,14 @@ public final class VBMappingDataLoader {
return null; return null;
} }
public static @Nullable InputStream getResource(String name) {
return VBMappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viabackwards/data/" + name);
}
public static JsonObject loadData(String name) { public static JsonObject loadData(String name) {
InputStream stream = VBMappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viabackwards/data/" + name); try (InputStream stream = getResource(name)) {
try (InputStreamReader reader = new InputStreamReader(stream)) { if (stream == null) return null;
return GsonUtil.getGson().fromJson(reader, JsonObject.class); return GsonUtil.getGson().fromJson(new InputStreamReader(stream), JsonObject.class);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@ -135,12 +140,12 @@ public final class VBMappingDataLoader {
public static Int2ObjectMap<MappedItem> loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) { public static Int2ObjectMap<MappedItem> loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) {
Int2ObjectMap<MappedItem> itemMapping = new Int2ObjectOpenHashMap<>(diffMapping.size(), 0.99F); Int2ObjectMap<MappedItem> itemMapping = new Int2ObjectOpenHashMap<>(diffMapping.size(), 0.99F);
Object2IntMap<String> newIdenfierMap = MappingDataLoader.indexedObjectToMap(newMapping); Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(newMapping);
Object2IntMap<String> oldIdenfierMap = MappingDataLoader.indexedObjectToMap(oldMapping); Object2IntMap<String> oldIdentifierMap = MappingDataLoader.indexedObjectToMap(oldMapping);
for (Map.Entry<String, JsonElement> entry : diffMapping.entrySet()) { for (Map.Entry<String, JsonElement> entry : diffMapping.entrySet()) {
JsonObject object = entry.getValue().getAsJsonObject(); JsonObject object = entry.getValue().getAsJsonObject();
String mappedIdName = object.getAsJsonPrimitive("id").getAsString(); String mappedIdName = object.getAsJsonPrimitive("id").getAsString();
int mappedId = newIdenfierMap.getInt(mappedIdName); int mappedId = newIdentifierMap.getInt(mappedIdName);
if (mappedId == -1) { if (mappedId == -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( "); ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( ");
@ -148,7 +153,7 @@ public final class VBMappingDataLoader {
continue; continue;
} }
int oldId = oldIdenfierMap.getInt(entry.getKey()); int oldId = oldIdentifierMap.getInt(entry.getKey());
if (oldId == -1) { if (oldId == -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( "); ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( ");
@ -162,8 +167,8 @@ public final class VBMappingDataLoader {
// Look for missing keys // Look for missing keys
if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings()) { if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings()) {
for (Object2IntMap.Entry<String> entry : oldIdenfierMap.object2IntEntrySet()) { for (Object2IntMap.Entry<String> entry : oldIdentifierMap.object2IntEntrySet()) {
if (!newIdenfierMap.containsKey(entry.getKey()) && !itemMapping.containsKey(entry.getIntValue())) { if (!newIdentifierMap.containsKey(entry.getKey()) && !itemMapping.containsKey(entry.getIntValue())) {
ViaBackwards.getPlatform().getLogger().warning("No item mapping for " + entry.getKey() + " :( "); ViaBackwards.getPlatform().getLogger().warning("No item mapping for " + entry.getKey() + " :( ");
} }
} }

Datei anzeigen

@ -17,23 +17,33 @@
*/ */
package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.data; package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.data;
import com.viaversion.viabackwards.api.data.VBMappingDataLoader;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import com.viaversion.viaversion.libs.gson.JsonArray; import com.viaversion.viaversion.libs.gson.JsonArray;
import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.IOException;
public final class BackwardsMappings extends com.viaversion.viabackwards.api.data.BackwardsMappings { public final class BackwardsMappings extends com.viaversion.viabackwards.api.data.BackwardsMappings {
private String[] argumentTypes; private String[] argumentTypes;
private final Int2ObjectMap<CompoundTag> defaultChatTypes = new Int2ObjectOpenHashMap<>();
public BackwardsMappings() { public BackwardsMappings() {
super("1.19", "1.18", Protocol1_19To1_18_2.class, true); super("1.19", "1.18", Protocol1_19To1_18_2.class, true);
} }
@Override @Override
protected void loadExtras(final JsonObject oldMappings, final JsonObject newMappings, @Nullable final JsonObject diffMappings) { protected void loadVBExtras(final JsonObject oldMappings, final JsonObject newMappings) {
super.loadExtras(oldMappings, newMappings, diffMappings);
int i = 0; int i = 0;
final JsonArray types = oldMappings.getAsJsonArray("argumenttypes"); final JsonArray types = oldMappings.getAsJsonArray("argumenttypes");
this.argumentTypes = new String[types.size()]; this.argumentTypes = new String[types.size()];
@ -41,9 +51,24 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat
final String id = element.getAsString(); final String id = element.getAsString();
this.argumentTypes[i++] = id; this.argumentTypes[i++] = id;
} }
try {
ListTag chatTypes = BinaryTagIO.readCompressedInputStream(VBMappingDataLoader.getResource("chat-types-1.19.nbt")).get("values");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
defaultChatTypes.put(idTag.asInt(), chatTypeCompound);
}
} catch (IOException e) {
e.printStackTrace();
}
} }
public @Nullable String argumentType(final int argumentTypeId) { public @Nullable String argumentType(final int argumentTypeId) {
return argumentTypeId >= 0 && argumentTypeId < argumentTypes.length ? argumentTypes[argumentTypeId] : null; return argumentTypeId >= 0 && argumentTypeId < argumentTypes.length ? argumentTypes[argumentTypeId] : null;
} }
public @Nullable CompoundTag chatType(final int id) {
return defaultChatTypes.get(id);
}
} }

Datei anzeigen

@ -17,6 +17,7 @@
*/ */
package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.storage; package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.storage;
import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.Protocol1_18_2To1_19;
import com.viaversion.viaversion.api.connection.StorableObject; import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
@ -41,6 +42,7 @@ public final class DimensionRegistryStorage implements StorableObject {
} }
public @Nullable CompoundTag chatType(final int id) { public @Nullable CompoundTag chatType(final int id) {
if (chatTypes.isEmpty()) return Protocol1_18_2To1_19.MAPPINGS.chatType(id);
return chatTypes.get(id); return chatTypes.get(id);
} }