mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-09-09 12:38:01 +02:00
Remove default assignments
For real this time
This commit is contained in:
parent
7a31edea09
commit
8b970c21fc
@ -25,11 +25,9 @@
|
||||
|
||||
package me.lucko.luckperms.common.config;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import me.lucko.luckperms.common.command.utils.ArgumentParser;
|
||||
import me.lucko.luckperms.common.defaultassignments.AssignmentRule;
|
||||
import me.lucko.luckperms.common.graph.TraversalAlgorithm;
|
||||
import me.lucko.luckperms.common.metastacking.SimpleMetaStackDefinition;
|
||||
import me.lucko.luckperms.common.metastacking.StandardStackElements;
|
||||
@ -423,21 +421,6 @@ public final class ConfigKeys {
|
||||
*/
|
||||
public static final ConfigKey<Map<String, String>> GROUP_NAME_REWRITES = mapKey("group-name-rewrite");
|
||||
|
||||
/**
|
||||
* The default assignments being applied by the plugin
|
||||
*/
|
||||
public static final ConfigKey<List<AssignmentRule>> DEFAULT_ASSIGNMENTS = customKey(c -> {
|
||||
return c.getKeys("default-assignments", ImmutableList.of()).stream().map(name -> {
|
||||
String hasTrue = c.getString("default-assignments." + name + ".if.has-true", null);
|
||||
String hasFalse = c.getString("default-assignments." + name + ".if.has-false", null);
|
||||
String lacks = c.getString("default-assignments." + name + ".if.lacks", null);
|
||||
List<String> give = ImmutableList.copyOf(c.getStringList("default-assignments." + name + ".give", ImmutableList.of()));
|
||||
List<String> take = ImmutableList.copyOf(c.getStringList("default-assignments." + name + ".take", ImmutableList.of()));
|
||||
String pg = c.getString("default-assignments." + name + ".set-primary-group", null);
|
||||
return new AssignmentRule(hasTrue, hasFalse, lacks, give, take, pg);
|
||||
}).collect(ImmutableCollectors.toList());
|
||||
});
|
||||
|
||||
/**
|
||||
* The database settings, username, password, etc for use by any database
|
||||
*/
|
||||
|
@ -1,157 +0,0 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.defaultassignments;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import me.lucko.luckperms.common.model.PermissionHolder;
|
||||
import me.lucko.luckperms.common.node.factory.NodeFactory;
|
||||
import me.lucko.luckperms.common.util.Scripting;
|
||||
|
||||
import net.luckperms.api.model.DataType;
|
||||
import net.luckperms.api.node.Node;
|
||||
import net.luckperms.api.node.NodeEqualityPredicate;
|
||||
import net.luckperms.api.node.Tristate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
|
||||
public class AssignmentExpression {
|
||||
|
||||
public static AssignmentExpression compile(String expression) {
|
||||
if (expression == null) {
|
||||
return null;
|
||||
}
|
||||
return new AssignmentExpression(expression);
|
||||
}
|
||||
|
||||
private final List<Token> expression;
|
||||
|
||||
private AssignmentExpression(String expression) {
|
||||
this.expression = generateExpression(expression);
|
||||
}
|
||||
|
||||
public boolean eval(PermissionHolder holder, Tristate tristate) throws IllegalArgumentException {
|
||||
ScriptEngine engine = Scripting.getScriptEngine();
|
||||
if (engine == null) {
|
||||
throw new NullPointerException("script engine");
|
||||
}
|
||||
|
||||
Predicate<Node> checker = node -> holder.hasPermission(DataType.NORMAL, node, NodeEqualityPredicate.IGNORE_VALUE_OR_IF_TEMPORARY) == tristate;
|
||||
|
||||
String exp = this.expression.stream().map(t -> t.forExpression(checker)).collect(Collectors.joining())
|
||||
.replace("&", "&&").replace("|", "||");
|
||||
|
||||
try {
|
||||
String result = engine.eval(exp).toString();
|
||||
|
||||
if (!result.equals("true") && !result.equals("false")) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
return Boolean.parseBoolean(result);
|
||||
|
||||
} catch (Throwable t) {
|
||||
throw new IllegalArgumentException(exp, t);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Token> generateExpression(String input) {
|
||||
ImmutableList.Builder<Token> exp = ImmutableList.builder();
|
||||
|
||||
while (true) {
|
||||
int start = input.indexOf("<");
|
||||
int end = input.indexOf(">");
|
||||
if (start == -1 || end == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (start != 0) {
|
||||
Token before = new StringToken(input.substring(0, start));
|
||||
exp.add(before);
|
||||
}
|
||||
|
||||
String match = input.substring(start, end + 1);
|
||||
String matchContent = match.substring(1, match.length() - 1);
|
||||
|
||||
Token permission = new PermissionToken(matchContent);
|
||||
exp.add(permission);
|
||||
|
||||
input = input.substring(end + 1);
|
||||
}
|
||||
|
||||
if (!input.isEmpty()) {
|
||||
exp.add(new StringToken(input));
|
||||
}
|
||||
|
||||
return exp.build();
|
||||
}
|
||||
|
||||
private interface Token {
|
||||
String forExpression(Predicate<Node> checker);
|
||||
}
|
||||
|
||||
private static final class StringToken implements Token {
|
||||
private final String string;
|
||||
|
||||
private StringToken(String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String forExpression(Predicate<Node> checker) {
|
||||
return this.string;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.string;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PermissionToken implements Token {
|
||||
private final String permission;
|
||||
private final Node node;
|
||||
|
||||
private PermissionToken(String permission) {
|
||||
this.permission = permission;
|
||||
this.node = NodeFactory.make(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String forExpression(Predicate<Node> checker) {
|
||||
return Boolean.toString(checker.test(this.node));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<" + this.permission + ">";
|
||||
}
|
||||
}
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
/*
|
||||
* This file is part of LuckPerms, licensed under the MIT License.
|
||||
*
|
||||
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.defaultassignments;
|
||||
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.node.factory.NodeFactory;
|
||||
import me.lucko.luckperms.common.util.ImmutableCollectors;
|
||||
|
||||
import net.luckperms.api.model.DataType;
|
||||
import net.luckperms.api.node.Node;
|
||||
import net.luckperms.api.node.Tristate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AssignmentRule {
|
||||
private final AssignmentExpression hasTrueExpression;
|
||||
private final AssignmentExpression hasFalseExpression;
|
||||
private final AssignmentExpression lacksExpression;
|
||||
|
||||
private final List<Node> toGive;
|
||||
private final List<Node> toTake;
|
||||
private final String setPrimaryGroup;
|
||||
|
||||
public AssignmentRule(String hasTrueExpression, String hasFalseExpression, String lacksExpression, List<String> toGive, List<String> toTake, String setPrimaryGroup) {
|
||||
this.hasTrueExpression = AssignmentExpression.compile(hasTrueExpression);
|
||||
this.hasFalseExpression = AssignmentExpression.compile(hasFalseExpression);
|
||||
this.lacksExpression = AssignmentExpression.compile(lacksExpression);
|
||||
this.toGive = toGive.stream().map(s -> NodeFactory.make(s, true)).collect(ImmutableCollectors.toList());
|
||||
this.toTake = toTake.stream().map(s -> NodeFactory.make(s, true)).collect(ImmutableCollectors.toList());
|
||||
this.setPrimaryGroup = setPrimaryGroup;
|
||||
}
|
||||
|
||||
public boolean apply(User user) {
|
||||
if (this.hasTrueExpression != null) {
|
||||
try {
|
||||
boolean b = this.hasTrueExpression.eval(user, Tristate.TRUE);
|
||||
if (!b) {
|
||||
// The holder does not meet this requirement
|
||||
return false;
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Couldn't parse
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.hasFalseExpression != null) {
|
||||
try {
|
||||
boolean b = this.hasFalseExpression.eval(user, Tristate.FALSE);
|
||||
if (!b) {
|
||||
// The holder does not meet this requirement
|
||||
return false;
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Couldn't parse
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.lacksExpression != null) {
|
||||
try {
|
||||
boolean b = this.lacksExpression.eval(user, Tristate.UNDEFINED);
|
||||
if (!b) {
|
||||
// The holder does not meet this requirement
|
||||
return false;
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Couldn't parse
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// The holder meets all of the requirements of this rule.
|
||||
for (Node n : this.toTake) {
|
||||
user.unsetPermission(DataType.NORMAL, n);
|
||||
}
|
||||
|
||||
for (Node n : this.toGive) {
|
||||
user.setPermission(DataType.NORMAL, n, true);
|
||||
}
|
||||
|
||||
if (this.setPrimaryGroup != null) {
|
||||
user.getPrimaryGroup().setStoredValue(this.setPrimaryGroup);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public AssignmentExpression getHasTrueExpression() {
|
||||
return this.hasTrueExpression;
|
||||
}
|
||||
|
||||
public AssignmentExpression getHasFalseExpression() {
|
||||
return this.hasFalseExpression;
|
||||
}
|
||||
|
||||
public AssignmentExpression getLacksExpression() {
|
||||
return this.lacksExpression;
|
||||
}
|
||||
|
||||
public List<Node> getToGive() {
|
||||
return this.toGive;
|
||||
}
|
||||
|
||||
public List<Node> getToTake() {
|
||||
return this.toTake;
|
||||
}
|
||||
|
||||
public String getSetPrimaryGroup() {
|
||||
return this.setPrimaryGroup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AssignmentRule(" +
|
||||
"hasTrueExpression=" + this.getHasTrueExpression() + ", " +
|
||||
"hasFalseExpression=" + this.getHasFalseExpression() + ", " +
|
||||
"lacksExpression=" + this.getLacksExpression() + ", " +
|
||||
"toGive=" + this.getToGive() + ", " +
|
||||
"toTake=" + this.getToTake() + ", " +
|
||||
"setPrimaryGroup=" + this.getSetPrimaryGroup() + ")";
|
||||
}
|
||||
}
|
@ -25,8 +25,6 @@
|
||||
|
||||
package me.lucko.luckperms.common.plugin.util;
|
||||
|
||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||
import me.lucko.luckperms.common.defaultassignments.AssignmentRule;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
|
||||
@ -106,19 +104,6 @@ public abstract class AbstractConnectionListener {
|
||||
throw new NullPointerException("User is null");
|
||||
}
|
||||
|
||||
// Setup defaults for the user
|
||||
boolean saveRequired = false;
|
||||
for (AssignmentRule rule : this.plugin.getConfiguration().get(ConfigKeys.DEFAULT_ASSIGNMENTS)) {
|
||||
if (rule.apply(user)) {
|
||||
saveRequired = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If they were given a default, persist the new assignments back to the storage.
|
||||
if (saveRequired) {
|
||||
this.plugin.getStorage().saveUser(user).join();
|
||||
}
|
||||
|
||||
final long time = System.currentTimeMillis() - startTime;
|
||||
if (time >= 1000) {
|
||||
this.plugin.getLogger().warn("Processing login for " + username + " took " + time + "ms.");
|
||||
|
Loading…
Reference in New Issue
Block a user