|
| 1 | +package ch.njol.skript.lang; |
| 2 | + |
| 3 | +import ch.njol.skript.classes.ClassInfo; |
| 4 | +import ch.njol.skript.lang.SkriptParser.ExprInfo; |
| 5 | +import ch.njol.util.StringUtils; |
| 6 | +import org.jetbrains.annotations.Nullable; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * Utility class for {@link DefaultExpression}. |
| 12 | + */ |
| 13 | +final class DefaultExpressionUtils { |
| 14 | + |
| 15 | + /** |
| 16 | + * Check if {@code expr} is valid with the settings from {@code exprInfo}. |
| 17 | + * |
| 18 | + * @param expr The {@link DefaultExpression} to check. |
| 19 | + * @param exprInfo The {@link ExprInfo} to check {@code expr} against its settings. |
| 20 | + * @param index The index of the {@link ClassInfo} in {@code exprInfo} used to grab {@code expr}. |
| 21 | + * @return {@link DefaultExpressionError} if it's not valid, otherwise {@code null}. |
| 22 | + */ |
| 23 | + static @Nullable DefaultExpressionError isValid(DefaultExpression<?> expr, ExprInfo exprInfo, int index) { |
| 24 | + if (expr == null) { |
| 25 | + return DefaultExpressionError.NOT_FOUND; |
| 26 | + } else if (!(expr instanceof Literal<?>) && (exprInfo.flagMask & SkriptParser.PARSE_EXPRESSIONS) == 0) { |
| 27 | + return DefaultExpressionError.NOT_LITERAL; |
| 28 | + } else if (expr instanceof Literal<?> && (exprInfo.flagMask & SkriptParser.PARSE_LITERALS) == 0) { |
| 29 | + return DefaultExpressionError.LITERAL; |
| 30 | + } else if (!exprInfo.isPlural[index] && !expr.isSingle()) { |
| 31 | + return DefaultExpressionError.NOT_SINGLE; |
| 32 | + } else if (exprInfo.time != 0 && !expr.setTime(exprInfo.time)) { |
| 33 | + return DefaultExpressionError.TIME_STATE; |
| 34 | + } |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + enum DefaultExpressionError { |
| 39 | + /** |
| 40 | + * Error type for when a {@link DefaultExpression} can not be found for a {@link Class}. |
| 41 | + */ |
| 42 | + NOT_FOUND { |
| 43 | + @Override |
| 44 | + public String getError(List<String> codeNames, String pattern) { |
| 45 | + StringBuilder builder = new StringBuilder(); |
| 46 | + String combinedComma = getCombinedComma(codeNames); |
| 47 | + String combinedSlash = StringUtils.join(codeNames, "/"); |
| 48 | + builder.append(plurality(codeNames, "The class '", "The classes '")); |
| 49 | + builder.append(combinedComma) |
| 50 | + .append("'") |
| 51 | + .append(plurality(codeNames, " does ", " do ")) |
| 52 | + .append("not provide a default expression. Either allow null (with %-") |
| 53 | + .append(combinedSlash) |
| 54 | + .append("%) or make it mandatory [pattern: ") |
| 55 | + .append(pattern) |
| 56 | + .append("]"); |
| 57 | + return builder.toString(); |
| 58 | + } |
| 59 | + }, |
| 60 | + |
| 61 | + /** |
| 62 | + * Error type for when the {@link DefaultExpression} for a {@link Class} is not a {@link Literal} |
| 63 | + * and the pattern only accepts {@link Literal}s. |
| 64 | + */ |
| 65 | + NOT_LITERAL { |
| 66 | + @Override |
| 67 | + public String getError(List<String> codeNames, String pattern) { |
| 68 | + StringBuilder builder = new StringBuilder(); |
| 69 | + builder.append(defaultExpression(codeNames, " is not a literal. ", " are not literals. ")) |
| 70 | + .append("Either allow null (with %-*") |
| 71 | + .append(StringUtils.join(codeNames, "/")) |
| 72 | + .append("%) or make it mandatory [pattern: ") |
| 73 | + .append(pattern) |
| 74 | + .append("]"); |
| 75 | + return builder.toString(); |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + /** |
| 80 | + * Error type for when the {@link DefaultExpression} for a {@link Class} is a {@link Literal} |
| 81 | + * and the pattern does not accept {@link Literal}s. |
| 82 | + */ |
| 83 | + LITERAL { |
| 84 | + @Override |
| 85 | + public String getError(List<String> codeNames, String pattern) { |
| 86 | + StringBuilder builder = new StringBuilder(); |
| 87 | + builder.append(defaultExpression(codeNames, " is a literal. ", " are literals. ")) |
| 88 | + .append("Either allow null (with %-~") |
| 89 | + .append(StringUtils.join(codeNames, "/")) |
| 90 | + .append("%) or make it mandatory [pattern: ") |
| 91 | + .append(pattern) |
| 92 | + .append("]"); |
| 93 | + return builder.toString(); |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * Error type for when the {@link DefaultExpression} for a {@link Class} is plural |
| 99 | + * but the pattern only accepts single. |
| 100 | + */ |
| 101 | + NOT_SINGLE { |
| 102 | + @Override |
| 103 | + public String getError(List<String> codeNames, String pattern) { |
| 104 | + StringBuilder builder = new StringBuilder(); |
| 105 | + builder.append(defaultExpression(codeNames, " is not a single-element expression. ", " are not single-element expressions. ")) |
| 106 | + .append("Change your pattern to allow multiple elements or make the expression mandatory [pattern: ") |
| 107 | + .append(pattern) |
| 108 | + .append("]"); |
| 109 | + return builder.toString(); |
| 110 | + } |
| 111 | + }, |
| 112 | + |
| 113 | + /** |
| 114 | + * Error type for when the {@link DefaultExpression} for a {@link Class} does not accept time states |
| 115 | + * but the pattern infers it. |
| 116 | + */ |
| 117 | + TIME_STATE { |
| 118 | + @Override |
| 119 | + public String getError(List<String> codeNames, String pattern) { |
| 120 | + StringBuilder builder = new StringBuilder(defaultExpression(codeNames, " does ", " do ")); |
| 121 | + builder.append("not have distinct time states. [pattern: ") |
| 122 | + .append(pattern) |
| 123 | + .append("]"); |
| 124 | + return builder.toString(); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns an error message for the given type. |
| 130 | + * |
| 131 | + * @param codeNames The codeNames of {@link ClassInfo}s to include in the error message. |
| 132 | + * @param pattern The pattern to include in the error message. |
| 133 | + * @return error message. |
| 134 | + */ |
| 135 | + public abstract String getError(List<String> codeNames, String pattern); |
| 136 | + |
| 137 | + /** |
| 138 | + * Utility method for constructing error messages in the format of |
| 139 | + * <code> |
| 140 | + * The default expression(s) of (codenames) (single/plural) |
| 141 | + * single -> The default expression of item type is |
| 142 | + * plural -> the default expressions of item type and entity are |
| 143 | + * </code> |
| 144 | + * |
| 145 | + * @param codeNames The list of codenames to be included in the error message. |
| 146 | + * @param single The string to be formatted at the end if there is only one codename. |
| 147 | + * @param plural The string to be formatted at the end if there is more than one codename. |
| 148 | + * @return The formatted error message. |
| 149 | + */ |
| 150 | + private static String defaultExpression(List<String> codeNames, String single, String plural) { |
| 151 | + StringBuilder builder = new StringBuilder(); |
| 152 | + String combinedComma = getCombinedComma(codeNames); |
| 153 | + builder.append("The default ") |
| 154 | + .append(plurality(codeNames, "expression ", "expressions ")) |
| 155 | + .append("of '") |
| 156 | + .append(combinedComma) |
| 157 | + .append("'") |
| 158 | + .append(plurality(codeNames, single, plural)); |
| 159 | + return builder.toString(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Utility method for grabbing {@code single} if {@code codeNames} is singular, otherwise {@code plural}. |
| 164 | + * |
| 165 | + * @param codeNames The list of codenames to be checked. |
| 166 | + * @param single The string to be used if there is only one codename. |
| 167 | + * @param plural The string to be used if there is more than one codename. |
| 168 | + * @return {@code single} or {@code plural}. |
| 169 | + */ |
| 170 | + private static String plurality(List<String> codeNames, String single, String plural) { |
| 171 | + return codeNames.size() > 1 ? plural : single; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Utility method for combining {@code codeNames} into one string following this format. |
| 176 | + * <p> |
| 177 | + * 1: x |
| 178 | + * 2: x and y |
| 179 | + * 3 or more: x, y, and z |
| 180 | + * </p> |
| 181 | + * @param codeNames {@link List} of codenames to combine. |
| 182 | + * @return The combined string. |
| 183 | + */ |
| 184 | + private static String getCombinedComma(List<String> codeNames) { |
| 185 | + assert !codeNames.isEmpty(); |
| 186 | + if (codeNames.size() == 1) { |
| 187 | + return codeNames.get(0); |
| 188 | + } else if (codeNames.size() == 2) { |
| 189 | + return StringUtils.join(codeNames, " and "); |
| 190 | + } else { |
| 191 | + return StringUtils.join(codeNames, ", ", ", and "); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments