|
| 1 | +package org.cyclops.cyclopscore.nbt.path.parse; |
| 2 | + |
| 3 | +import net.minecraft.nbt.ByteTag; |
| 4 | +import net.minecraft.nbt.Tag; |
| 5 | +import org.cyclops.cyclopscore.nbt.path.INbtPathExpression; |
| 6 | +import org.cyclops.cyclopscore.nbt.path.NbtParseException; |
| 7 | +import org.cyclops.cyclopscore.nbt.path.NbtPath; |
| 8 | +import org.cyclops.cyclopscore.nbt.path.NbtPathExpressionMatches; |
| 9 | + |
| 10 | +import javax.annotation.Nullable; |
| 11 | +import java.util.function.Supplier; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author rubensworks |
| 18 | + */ |
| 19 | +public abstract class NbtPathExpressionParseHandlerBooleanLogicalAdapter implements INbtPathExpressionParseHandler { |
| 20 | + |
| 21 | + private final Pattern regex; |
| 22 | + |
| 23 | + protected NbtPathExpressionParseHandlerBooleanLogicalAdapter(String relation) { |
| 24 | + this.regex = Pattern.compile("^ *" + relation + " *"); |
| 25 | + } |
| 26 | + |
| 27 | + protected abstract boolean getLogicalValue(boolean left, Supplier<Boolean> right); |
| 28 | + |
| 29 | + /** |
| 30 | + * Determine if a tag is truthy. |
| 31 | + * ByteTag with value 1 is true, 0 is false. |
| 32 | + * Any other non-null tag is considered true. |
| 33 | + * This follows the same logic as {@link org.cyclops.cyclopscore.nbt.path.INbtPathExpression#test(Tag)}. |
| 34 | + * |
| 35 | + * @param tag The tag to check |
| 36 | + * @return true if the tag is truthy, false otherwise |
| 37 | + */ |
| 38 | + public static boolean isTruthy(Tag tag) { |
| 39 | + if (tag == null) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + if (tag.getId() == Tag.TAG_BYTE) { |
| 43 | + return ((ByteTag) tag).getAsByte() == (byte) 1; |
| 44 | + } |
| 45 | + // Non-null non-ByteTags are truthy |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Find the end position of an expression, stopping at logical operators or closing parenthesis. |
| 51 | + * This method is shared by logical operator handlers to identify expression boundaries. |
| 52 | + * |
| 53 | + * @param expression The full expression string |
| 54 | + * @param start The starting position to search from |
| 55 | + * @return The position where the expression ends |
| 56 | + */ |
| 57 | + public static int findExpressionEnd(String expression, int start) { |
| 58 | + int depth = 0; |
| 59 | + for (int i = start; i < expression.length(); i++) { |
| 60 | + char c = expression.charAt(i); |
| 61 | + |
| 62 | + if (c == '(') { |
| 63 | + depth++; |
| 64 | + } else if (c == ')') { |
| 65 | + if (depth == 0) { |
| 66 | + return i; |
| 67 | + } |
| 68 | + depth--; |
| 69 | + } else if (depth == 0) { |
| 70 | + // Check for logical operators at top level |
| 71 | + if (i + 1 < expression.length()) { |
| 72 | + String twoChar = expression.substring(i, i + 2); |
| 73 | + if (twoChar.equals("&&") || twoChar.equals("||")) { |
| 74 | + return i; |
| 75 | + } |
| 76 | + } |
| 77 | + // Check for NOT operator (but not != which is handled differently) |
| 78 | + if (c == '!' && (i + 1 >= expression.length() || expression.charAt(i + 1) != '=')) { |
| 79 | + return i; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return expression.length(); |
| 84 | + } |
| 85 | + |
| 86 | + @Nullable |
| 87 | + @Override |
| 88 | + public HandleResult handlePrefixOf(String nbtPathExpression, int pos) { |
| 89 | + Matcher matcher = this.regex |
| 90 | + .matcher(nbtPathExpression) |
| 91 | + .region(pos, nbtPathExpression.length()); |
| 92 | + if (!matcher.find()) { |
| 93 | + return HandleResult.INVALID; |
| 94 | + } |
| 95 | + |
| 96 | + // Parse the right-hand side expression |
| 97 | + int rightPos = pos + matcher.group().length(); |
| 98 | + if (rightPos >= nbtPathExpression.length()) { |
| 99 | + return HandleResult.INVALID; |
| 100 | + } |
| 101 | + |
| 102 | + // Find the end of the right expression |
| 103 | + int endPos = findExpressionEnd(nbtPathExpression, rightPos); |
| 104 | + if (endPos == rightPos) { |
| 105 | + return HandleResult.INVALID; |
| 106 | + } |
| 107 | + |
| 108 | + String rightExpressionString = nbtPathExpression.substring(rightPos, endPos); |
| 109 | + try { |
| 110 | + INbtPathExpression rightExpression = NbtPath.parse(rightExpressionString.trim()); |
| 111 | + return new HandleResult(new Expression(rightExpression, this), |
| 112 | + matcher.group().length() + rightExpressionString.length()); |
| 113 | + } catch (NbtParseException e) { |
| 114 | + return HandleResult.INVALID; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static class Expression implements INbtPathExpression { |
| 119 | + |
| 120 | + protected final INbtPathExpression expression; |
| 121 | + protected final NbtPathExpressionParseHandlerBooleanLogicalAdapter handler; |
| 122 | + |
| 123 | + public Expression(INbtPathExpression expression, NbtPathExpressionParseHandlerBooleanLogicalAdapter handler) { |
| 124 | + this.expression = expression; |
| 125 | + this.handler = handler; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public NbtPathExpressionMatches matchContexts(Stream<NbtPathExpressionExecutionContext> executionContexts) { |
| 130 | + return new NbtPathExpressionMatches(executionContexts |
| 131 | + .map(executionContext -> { |
| 132 | + Tag currentTag = executionContext.getCurrentTag(); |
| 133 | + |
| 134 | + // The left side is the current tag (should be a boolean result from previous expression) |
| 135 | + boolean leftValue = isTruthy(currentTag); |
| 136 | + |
| 137 | + // Evaluate the right expression against the root context |
| 138 | + // This ensures both sides of the expression are evaluated against the same base context |
| 139 | + Tag rootTag = executionContext.getRootContext().getCurrentTag(); |
| 140 | + |
| 141 | + // AND operation |
| 142 | + boolean result = handler.getLogicalValue(leftValue, () -> expression.test(rootTag)); |
| 143 | + |
| 144 | + return new NbtPathExpressionExecutionContext(ByteTag.valueOf(result), executionContext); |
| 145 | + }) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments