From 30d7768299046711b990df3eb45c52276e265d38 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 24 Mar 2020 10:03:34 +0000 Subject: [PATCH] Fix parsing contexts from commands with server/world = global (#2093) --- .../common/command/utils/ArgumentParser.java | 25 ++++++++++--------- .../contextset/AbstractContextSet.java | 9 +++++-- .../common/node/AbstractNodeBuilder.java | 4 +-- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java b/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java index 2e79eea46..82f8eb2c1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java +++ b/common/src/main/java/me/lucko/luckperms/common/command/utils/ArgumentParser.java @@ -30,6 +30,7 @@ import me.lucko.luckperms.common.command.abstraction.Command; import me.lucko.luckperms.common.command.abstraction.CommandException; import me.lucko.luckperms.common.command.abstraction.GenericChildCommand; import me.lucko.luckperms.common.commands.user.UserParentCommand; +import me.lucko.luckperms.common.context.contextset.AbstractContextSet; import me.lucko.luckperms.common.context.contextset.ImmutableContextSetImpl; import me.lucko.luckperms.common.context.contextset.MutableContextSetImpl; import me.lucko.luckperms.common.locale.message.Message; @@ -175,26 +176,26 @@ public class ArgumentParser { private static MutableContextSet parseContext(int fromIndex, List args) { MutableContextSet contextSet = new MutableContextSetImpl(); - List pairs = args.subList(fromIndex, args.size()); - for (int i = 0; i < pairs.size(); i++) { - String pair = pairs.get(i); - int index = pair.indexOf('='); + List entries = args.subList(fromIndex, args.size()); + for (int i = 0; i < entries.size(); i++) { + String entry = entries.get(i); + int sep = entry.indexOf('='); String key; String value; - if (index != -1) { - key = pair.substring(0, index); - value = pair.substring(index + 1); + if (sep != -1) { + key = entry.substring(0, sep); + value = entry.substring(sep + 1); } else { key = i == 1 ? DefaultContextKeys.WORLD_KEY : DefaultContextKeys.SERVER_KEY; - value = pair; + value = entry; } - if (key.equals("") || key.trim().isEmpty()) { - continue; - } - if (value.equals("") || value.trim().isEmpty()) { + if (AbstractContextSet.stringIsEmpty(key) || + AbstractContextSet.stringIsEmpty(value) || + // TODO reconsider a better place to insert / avoid this special case + AbstractContextSet.shouldIgnoreEntry(key, value)) { continue; } diff --git a/common/src/main/java/me/lucko/luckperms/common/context/contextset/AbstractContextSet.java b/common/src/main/java/me/lucko/luckperms/common/context/contextset/AbstractContextSet.java index e3f4e006e..2794fb012 100644 --- a/common/src/main/java/me/lucko/luckperms/common/context/contextset/AbstractContextSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/context/contextset/AbstractContextSet.java @@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.SetMultimap; import net.luckperms.api.context.ContextSet; +import net.luckperms.api.context.DefaultContextKeys; import org.checkerframework.checker.nullness.qual.NonNull; @@ -36,7 +37,7 @@ import java.util.Collection; import java.util.Objects; import java.util.Set; -abstract class AbstractContextSet implements ContextSet { +public abstract class AbstractContextSet implements ContextSet { protected abstract SetMultimap backing(); @@ -89,7 +90,7 @@ abstract class AbstractContextSet implements ContextSet { return value.toLowerCase(); } - private static boolean stringIsEmpty(String s) { + public static boolean stringIsEmpty(String s) { if (s.isEmpty()) { return true; } @@ -101,4 +102,8 @@ abstract class AbstractContextSet implements ContextSet { return true; } + public static boolean shouldIgnoreEntry(String key, String value) { + return (key.equalsIgnoreCase(DefaultContextKeys.SERVER_KEY) || key.equalsIgnoreCase(DefaultContextKeys.WORLD_KEY)) && value.equalsIgnoreCase("global"); + } + } diff --git a/common/src/main/java/me/lucko/luckperms/common/node/AbstractNodeBuilder.java b/common/src/main/java/me/lucko/luckperms/common/node/AbstractNodeBuilder.java index 8ae2f63df..b29650fe8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/node/AbstractNodeBuilder.java +++ b/common/src/main/java/me/lucko/luckperms/common/node/AbstractNodeBuilder.java @@ -25,10 +25,10 @@ package me.lucko.luckperms.common.node; +import me.lucko.luckperms.common.context.contextset.AbstractContextSet; import me.lucko.luckperms.common.context.contextset.ImmutableContextSetImpl; import net.luckperms.api.context.ContextSet; -import net.luckperms.api.context.DefaultContextKeys; import net.luckperms.api.context.ImmutableContextSet; import net.luckperms.api.node.NodeBuilder; import net.luckperms.api.node.ScopedNode; @@ -119,7 +119,7 @@ public abstract class AbstractNodeBuilder, B extends @Override public @NonNull B withContext(@NonNull String key, @NonNull String value) { // TODO reconsider a better place to insert / avoid this special case - if ((key.equalsIgnoreCase(DefaultContextKeys.SERVER_KEY) || key.equalsIgnoreCase(DefaultContextKeys.WORLD_KEY)) && value.equalsIgnoreCase("global")) { + if (AbstractContextSet.shouldIgnoreEntry(key, value)) { return (B) this; } this.context.add(key, value);