Fix MetaValueSelector NPE

This commit is contained in:
Luck 2021-12-31 17:50:42 +00:00
parent d319d8dc52
commit acdc259771
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
8 changed files with 22 additions and 17 deletions

View File

@ -23,7 +23,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
package me.lucko.luckperms.common.cacheddata.result; package net.luckperms.api.cacheddata;
import net.luckperms.api.node.Node; import net.luckperms.api.node.Node;
@ -33,6 +33,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* Represents the result of a cached data lookup * Represents the result of a cached data lookup
* *
* @param <T> the result type * @param <T> the result type
* @since 5.4
*/ */
public interface Result<T, N extends Node> { public interface Result<T, N extends Node> {

View File

@ -25,6 +25,7 @@
package net.luckperms.api.query.meta; package net.luckperms.api.query.meta;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.node.types.MetaNode; import net.luckperms.api.node.types.MetaNode;
import net.luckperms.api.query.OptionKey; import net.luckperms.api.query.OptionKey;
@ -54,6 +55,6 @@ public interface MetaValueSelector {
* @param values the values, in the order in which they were accumulated. * @param values the values, in the order in which they were accumulated.
* @return the selected value * @return the selected value
*/ */
@NonNull MetaNode selectValue(@NonNull String key, @NonNull List<MetaNode> values); @NonNull Result<String, MetaNode> selectValue(@NonNull String key, @NonNull List<? extends Result<String, MetaNode>> values);
} }

View File

@ -25,6 +25,7 @@
package me.lucko.luckperms.common.cacheddata.result; package me.lucko.luckperms.common.cacheddata.result;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.node.Node; import net.luckperms.api.node.Node;
import net.luckperms.api.node.types.ChatMetaNode; import net.luckperms.api.node.types.ChatMetaNode;
import net.luckperms.api.node.types.MetaNode; import net.luckperms.api.node.types.MetaNode;

View File

@ -28,6 +28,7 @@ package me.lucko.luckperms.common.cacheddata.result;
import me.lucko.luckperms.common.calculator.PermissionCalculator; import me.lucko.luckperms.common.calculator.PermissionCalculator;
import me.lucko.luckperms.common.calculator.processor.PermissionProcessor; import me.lucko.luckperms.common.calculator.processor.PermissionProcessor;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.node.Node; import net.luckperms.api.node.Node;
import net.luckperms.api.util.Tristate; import net.luckperms.api.util.Tristate;

View File

@ -40,6 +40,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.verbose.event.CheckOrigin; import me.lucko.luckperms.common.verbose.event.CheckOrigin;
import net.luckperms.api.cacheddata.CachedMetaData; import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.metastacking.MetaStackDefinition; import net.luckperms.api.metastacking.MetaStackDefinition;
import net.luckperms.api.node.types.ChatMetaNode; import net.luckperms.api.node.types.ChatMetaNode;
import net.luckperms.api.node.types.MetaNode; import net.luckperms.api.node.types.MetaNode;
@ -92,12 +93,12 @@ public class MetaCache extends UsageTracked implements CachedMetaData {
continue; continue;
} }
MetaNode selected = metaValueSelector.selectValue(e.getKey(), Lists.transform(e.getValue(), StringResult::node)); Result<String, MetaNode> selected = metaValueSelector.selectValue(e.getKey(), e.getValue());
if (selected == null) { if (selected == null) {
throw new NullPointerException(metaValueSelector + " returned null"); throw new NullPointerException(metaValueSelector + " returned null");
} }
builder.put(e.getKey(), StringResult.of(selected)); builder.put(e.getKey(), (StringResult<MetaNode>) selected);
} }
this.flattenedMeta = builder.build(); this.flattenedMeta = builder.build();
this.meta = new LowerCaseMetaMap(meta); this.meta = new LowerCaseMetaMap(meta);

View File

@ -25,6 +25,7 @@
package me.lucko.luckperms.common.cacheddata.type; package me.lucko.luckperms.common.cacheddata.type;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.node.types.MetaNode; import net.luckperms.api.node.types.MetaNode;
import net.luckperms.api.query.meta.MetaValueSelector; import net.luckperms.api.query.meta.MetaValueSelector;
@ -44,7 +45,7 @@ public class SimpleMetaValueSelector implements MetaValueSelector {
} }
@Override @Override
public @NonNull MetaNode selectValue(@NonNull String key, @NonNull List<MetaNode> values) { public @NonNull Result<String, MetaNode> selectValue(@NonNull String key, @NonNull List<? extends Result<String, MetaNode>> values) {
switch (values.size()) { switch (values.size()) {
case 0: case 0:
throw new IllegalArgumentException("values is empty"); throw new IllegalArgumentException("values is empty");
@ -58,7 +59,7 @@ public class SimpleMetaValueSelector implements MetaValueSelector {
public enum Strategy { public enum Strategy {
INHERITANCE { INHERITANCE {
@Override @Override
public MetaNode select(List<MetaNode> values) { public Result<String, MetaNode> select(List<? extends Result<String, MetaNode>> values) {
return values.get(0); return values.get(0);
} }
}, },
@ -66,7 +67,7 @@ public class SimpleMetaValueSelector implements MetaValueSelector {
private final DoubleSelectionPredicate selection = (value, current) -> value > current; private final DoubleSelectionPredicate selection = (value, current) -> value > current;
@Override @Override
public MetaNode select(List<MetaNode> values) { public Result<String, MetaNode> select(List<? extends Result<String, MetaNode>> values) {
return selectNumber(values, this.selection); return selectNumber(values, this.selection);
} }
}, },
@ -74,12 +75,12 @@ public class SimpleMetaValueSelector implements MetaValueSelector {
private final DoubleSelectionPredicate selection = (value, current) -> value < current; private final DoubleSelectionPredicate selection = (value, current) -> value < current;
@Override @Override
public MetaNode select(List<MetaNode> values) { public Result<String, MetaNode> select(List<? extends Result<String, MetaNode>> values) {
return selectNumber(values, this.selection); return selectNumber(values, this.selection);
} }
}; };
public abstract MetaNode select(List<MetaNode> values); public abstract Result<String, MetaNode> select(List<? extends Result<String, MetaNode>> values);
public static Strategy parse(String s) { public static Strategy parse(String s) {
try { try {
@ -95,16 +96,15 @@ public class SimpleMetaValueSelector implements MetaValueSelector {
boolean shouldSelect(double value, double current); boolean shouldSelect(double value, double current);
} }
private static MetaNode selectNumber(List<MetaNode> values, DoubleSelectionPredicate selection) { private static Result<String, MetaNode> selectNumber(List<? extends Result<String, MetaNode>> values, DoubleSelectionPredicate selection) {
double current = 0; double current = 0;
MetaNode selected = null; Result<String, MetaNode> selected = null;
for (MetaNode node : values) { for (Result<String, MetaNode> result : values) {
String value = node.getMetaValue();
try { try {
double parse = Double.parseDouble(value); double parse = Double.parseDouble(result.result());
if (selected == null || selection.shouldSelect(parse, current)) { if (selected == null || selection.shouldSelect(parse, current)) {
selected = node; selected = result;
current = parse; current = parse;
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {

View File

@ -27,7 +27,6 @@ package me.lucko.luckperms.common.verbose;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import me.lucko.luckperms.common.cacheddata.result.Result;
import me.lucko.luckperms.common.cacheddata.result.TristateResult; import me.lucko.luckperms.common.cacheddata.result.TristateResult;
import me.lucko.luckperms.common.http.AbstractHttpClient; import me.lucko.luckperms.common.http.AbstractHttpClient;
import me.lucko.luckperms.common.http.BytebinClient; import me.lucko.luckperms.common.http.BytebinClient;
@ -47,6 +46,7 @@ import me.lucko.luckperms.common.verbose.event.VerboseEvent;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.node.Node; import net.luckperms.api.node.Node;
import net.luckperms.api.node.types.MetaNode; import net.luckperms.api.node.types.MetaNode;
import net.luckperms.api.query.QueryMode; import net.luckperms.api.query.QueryMode;

View File

@ -27,13 +27,13 @@ package me.lucko.luckperms.common.verbose.event;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import me.lucko.luckperms.common.cacheddata.result.Result;
import me.lucko.luckperms.common.util.StackTracePrinter; import me.lucko.luckperms.common.util.StackTracePrinter;
import me.lucko.luckperms.common.util.gson.JArray; import me.lucko.luckperms.common.util.gson.JArray;
import me.lucko.luckperms.common.util.gson.JObject; import me.lucko.luckperms.common.util.gson.JObject;
import me.lucko.luckperms.common.verbose.VerboseCheckTarget; import me.lucko.luckperms.common.verbose.VerboseCheckTarget;
import me.lucko.luckperms.common.verbose.expression.BooleanExpressionCompiler.VariableEvaluator; import me.lucko.luckperms.common.verbose.expression.BooleanExpressionCompiler.VariableEvaluator;
import net.luckperms.api.cacheddata.Result;
import net.luckperms.api.context.Context; import net.luckperms.api.context.Context;
import net.luckperms.api.query.QueryMode; import net.luckperms.api.query.QueryMode;
import net.luckperms.api.query.QueryOptions; import net.luckperms.api.query.QueryOptions;