Cleanup of mcstructs branch (#3604)

This commit is contained in:
RK_01 2023-12-27 19:37:48 +01:00 committed by GitHub
parent ca4b6fc765
commit 28a0813676
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 60 deletions

View File

@ -20,6 +20,9 @@ dependencies {
api(libs.flareFastutil) api(libs.flareFastutil)
api(libs.vianbt) api(libs.vianbt)
api(libs.gson) api(libs.gson)
implementation(rootProject.libs.text) {
exclude("com.google.code.gson", "gson")
}
compileOnlyApi(libs.snakeYaml) compileOnlyApi(libs.snakeYaml)
compileOnlyApi(libs.netty) compileOnlyApi(libs.netty)

View File

@ -25,7 +25,7 @@ package com.viaversion.viaversion.api.minecraft.signature.model.chain.v1_19_1;
import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature;
import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage; import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage;
import com.viaversion.viaversion.api.minecraft.signature.util.DataConsumer; import com.viaversion.viaversion.api.minecraft.signature.util.DataConsumer;
import com.viaversion.viaversion.util.GsonUtil; import net.lenni0451.mcstructs.text.utils.JsonUtils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -62,7 +62,7 @@ public class MessageBody {
dataOutputStream.write(this.content.plain().getBytes(StandardCharsets.UTF_8)); dataOutputStream.write(this.content.plain().getBytes(StandardCharsets.UTF_8));
dataOutputStream.write(HASH_SEPARATOR_BYTE); dataOutputStream.write(HASH_SEPARATOR_BYTE);
if (this.content.isDecorated()) { if (this.content.isDecorated()) {
dataOutputStream.write(GsonUtil.toSortedString(this.content.decorated(), null).getBytes(StandardCharsets.UTF_8)); dataOutputStream.write(JsonUtils.toSortedString(this.content.decorated(), null).getBytes(StandardCharsets.UTF_8));
} }
for (PlayerMessageSignature lastSeenMessage : this.lastSeenMessages) { for (PlayerMessageSignature lastSeenMessage : this.lastSeenMessages) {

View File

@ -25,7 +25,7 @@ package com.viaversion.viaversion.api.minecraft.signature.storage;
import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage; import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage;
import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata; import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata;
import com.viaversion.viaversion.util.GsonUtil; import net.lenni0451.mcstructs.text.utils.JsonUtils;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
@ -48,7 +48,7 @@ public class ChatSession1_19_0 extends ChatSession {
buffer.putLong(metadata.sender().getMostSignificantBits()).putLong(metadata.sender().getLeastSignificantBits()); buffer.putLong(metadata.sender().getMostSignificantBits()).putLong(metadata.sender().getLeastSignificantBits());
buffer.putLong(metadata.timestamp().getEpochSecond()); buffer.putLong(metadata.timestamp().getEpochSecond());
signer.accept(data); signer.accept(data);
signer.accept(GsonUtil.toSortedString(content.decorated(), null).getBytes(StandardCharsets.UTF_8)); signer.accept(JsonUtils.toSortedString(content.decorated(), null).getBytes(StandardCharsets.UTF_8));
}); });
} }

View File

@ -22,12 +22,8 @@
*/ */
package com.viaversion.viaversion.util; package com.viaversion.viaversion.util;
import com.google.gson.*; import com.google.gson.Gson;
import org.checkerframework.checker.nullness.qual.Nullable; import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public final class GsonUtil { public final class GsonUtil {
private static final Gson GSON = new GsonBuilder().create(); private static final Gson GSON = new GsonBuilder().create();
@ -41,52 +37,4 @@ public final class GsonUtil {
return GSON; return GSON;
} }
/**
* Convert a json element to a sorted string.<br>
* If the {@code comparator} is null, {@link Comparator#naturalOrder()} will be used.
*
* @param element The element to convert
* @param comparator The comparator to use
* @return The sorted string
*/
public static String toSortedString(@Nullable final JsonElement element, @Nullable final Comparator<String> comparator) {
if (element == null) {
return null;
} else if (comparator != null) {
return sort(element, comparator).toString();
} else {
return sort(element, Comparator.naturalOrder()).toString();
}
}
/**
* Sort a json element.
*
* @param element The element to sort
* @param comparator The comparator to use
* @return The sorted element
*/
public static JsonElement sort(@Nullable final JsonElement element, final Comparator<String> comparator) {
if (element == null) {
return null;
} else if (element.isJsonArray()) {
final JsonArray array = element.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
array.set(i, sort(array.get(i), comparator));
}
return array;
} else if (element.isJsonObject()) {
final JsonObject object = element.getAsJsonObject();
final JsonObject sorted = new JsonObject();
final List<String> keys = new ArrayList<>(object.keySet());
keys.sort(comparator);
for (String key : keys) {
sorted.add(key, sort(object.get(key), comparator));
}
return sorted;
} else {
return element;
}
}
} }

View File

@ -22,7 +22,6 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import java.util.logging.Level;
import net.lenni0451.mcstructs.snbt.SNbtSerializer; import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.text.ATextComponent; import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style; import net.lenni0451.mcstructs.text.Style;
@ -33,6 +32,8 @@ import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.logging.Level;
/** /**
* Component conversion utility, trying to divert most calls to the component library to this class instead for easy replacement. * Component conversion utility, trying to divert most calls to the component library to this class instead for easy replacement.
*/ */
@ -105,7 +106,7 @@ public final class ComponentUtil {
} }
public static String jsonToLegacy(final String value) { public static String jsonToLegacy(final String value) {
return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString(); return TextComponentSerializer.V1_12.deserializeReader(value).asLegacyFormatString();
} }
public static String jsonToLegacy(final JsonElement value) { public static String jsonToLegacy(final JsonElement value) {