diff --git a/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java b/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java index 519079624..7bbf5ba62 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java @@ -21,10 +21,14 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.Writer; +import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.Path; +import java.security.MessageDigest; import java.util.Collection; +import java.util.HexFormat; import java.util.Map; +import java.util.Objects; public class JsonIO { @HideFromJS @@ -179,9 +183,18 @@ public static byte[] getJsonHashBytes(JsonElement json) { } catch (IOException ex) { ex.printStackTrace(); var h = json.hashCode(); - return new byte[]{(byte) (h >> 24), (byte) (h >> 16), (byte) (h >> 8), (byte) (h >> 0)}; + return new byte[]{(byte) (h >> 24), (byte) (h >> 16), (byte) (h >> 8), (byte) h}; } return baos.toByteArray(); } + + public static String getJsonHashString(JsonElement json) { + try { + var messageDigest = Objects.requireNonNull(MessageDigest.getInstance("MD5")); + return new BigInteger(HexFormat.of().formatHex(messageDigest.digest(JsonIO.getJsonHashBytes(json))), 16).toString(36); + } catch (Exception ex) { + return "%08x".formatted(json.hashCode()); + } + } } \ No newline at end of file diff --git a/common/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java b/common/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java index d4ed75183..09857eebc 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java @@ -74,9 +74,7 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; -import java.math.BigInteger; import java.nio.file.Path; -import java.security.MessageDigest; import java.time.Duration; import java.time.temporal.TemporalAmount; import java.util.ArrayList; @@ -85,10 +83,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.HexFormat; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Random; import java.util.Set; @@ -836,13 +832,7 @@ public static String getUniqueId(T input, Codec codec) { } private static String getUniqueId(T input, Function toJson) { - try { - var messageDigest = Objects.requireNonNull(MessageDigest.getInstance("MD5")); - var json = toJson.apply(input); - return new BigInteger(HexFormat.of().formatHex(messageDigest.digest(JsonIO.getJsonHashBytes(json))), 16).toString(36); - } catch (Exception ex) { - throw new RuntimeException("MD5 not supported", ex); - } + return JsonIO.getJsonHashString(toJson.apply(input)); } public static String stripEventName(String s) {