From 1f292d96a874628679288791966a7d66585e03a7 Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 7 Sep 2023 16:36:17 +0200 Subject: Add check for a JSON element before attempting to match a string This has previously caused massive performance issues, especially in packs that frequently match non text elements. (see https://spark.lucko.me/R6fJJ3zXOJ and the massive amounts of the trace that is taken up by JsonSyntaxExceptions and MalformedJsonExceptions that trace back to this line) This patch first does a sanity check on the text element to see if it ever could be a JSON element. --- .../citresewn/defaults/cit/conditions/ConditionNBT.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java index a64f141..a21ccbc 100644 --- a/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java +++ b/defaults/src/main/java/shcm/shsupercm/fabric/citresewn/defaults/cit/conditions/ConditionNBT.java @@ -19,6 +19,8 @@ public class ConditionNBT extends CITCondition { public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(ConditionNBT.class, ConditionNBT::new, "nbt"); + private static final Pattern jsonBeginPattern = Pattern.compile("^[ \t\n]*\\{"); + protected String[] path; protected StringMatcher matchString = null; @@ -123,9 +125,12 @@ public class ConditionNBT extends CITCondition { private boolean testValue(NbtElement element) { try { - if (element instanceof NbtString nbtString) //noinspection ConstantConditions - return matchString.matches(nbtString.asString()) || matchString.matches(Text.Serializer.fromJson(nbtString.asString()).getString()); - else if (element instanceof NbtInt nbtInt && matchInteger != null) + if (element instanceof NbtString nbtString) { //noinspection ConstantConditions + var asString = nbtString.asString(); + return matchString.matches(asString) || + (jsonBeginPattern.matcher(asString).matches() + && matchString.matches(Text.Serializer.fromJson(asString).getString())); + } else if (element instanceof NbtInt nbtInt && matchInteger != null) return nbtInt.equals(matchInteger); else if (element instanceof NbtByte nbtByte && matchByte != null) return nbtByte.equals(matchByte); -- cgit