Implement default assignment system (#25)

This commit is contained in:
Luck
2016-10-16 13:58:57 +01:00
Unverified
parent 02a75fc32a
commit 13fbc7dd39
12 changed files with 578 additions and 54 deletions
@@ -22,14 +22,18 @@
package me.lucko.luckperms.common.config;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import lombok.AccessLevel;
import lombok.Getter;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.constants.Patterns;
import me.lucko.luckperms.common.defaults.Rule;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
@@ -65,6 +69,7 @@ public abstract class AbstractConfiguration<T extends LuckPermsPlugin> implement
private boolean vaultIgnoreWorld;
private Map<String, String> worldRewrites;
private Map<String, String> groupNameRewrites;
private List<Rule> defaultAssignments;
private DatastoreConfiguration databaseValues;
private String storageMethod;
private boolean splitStorage;
@@ -80,6 +85,8 @@ public abstract class AbstractConfiguration<T extends LuckPermsPlugin> implement
protected abstract String getString(String path, String def);
protected abstract int getInt(String path, int def);
protected abstract boolean getBoolean(String path, boolean def);
protected abstract List<String> getList(String path, List<String> def);
protected abstract List<String> getObjectList(String path, List<String> def);
protected abstract Map<String, String> getMap(String path, Map<String, String> def);
public void load(String defaultServerName, boolean defaultIncludeGlobal, String defaultStorage) {
@@ -105,6 +112,19 @@ public abstract class AbstractConfiguration<T extends LuckPermsPlugin> implement
vaultIgnoreWorld = getBoolean("vault-ignore-world", false);
worldRewrites = ImmutableMap.copyOf(getMap("world-rewrite", Collections.emptyMap()));
groupNameRewrites = ImmutableMap.copyOf(getMap("group-name-rewrite", Collections.emptyMap()));
ImmutableList.Builder<Rule> defs = ImmutableList.builder();
List<String> ruleNames = getObjectList("default-assignments", new ArrayList<>());
for (String ruleName : ruleNames) {
String hasTrue = getString("default-assignments." + ruleName + ".if.has-true", null);
String hasFalse = getString("default-assignments." + ruleName + ".if.has-false", null);
String lacks = getString("default-assignments." + ruleName + ".if.lacks", null);
List<String> give = getList("default-assignments." + ruleName + ".give", new ArrayList<>());
List<String> take = getList("default-assignments." + ruleName + ".take", new ArrayList<>());
defs.add(new Rule(hasTrue, hasFalse, lacks, give, take));
}
defaultAssignments = defs.build();
databaseValues = new DatastoreConfiguration(
getString("data.address", null),
getString("data.database", null),
@@ -22,8 +22,10 @@
package me.lucko.luckperms.common.config;
import me.lucko.luckperms.common.defaults.Rule;
import me.lucko.luckperms.common.storage.DatastoreConfiguration;
import java.util.List;
import java.util.Map;
public interface LPConfiguration {
@@ -80,6 +82,8 @@ public interface LPConfiguration {
Map<String, String> getGroupNameRewrites();
List<Rule> getDefaultAssignments();
DatastoreConfiguration getDatabaseValues();
String getStorageMethod();
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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.defaults;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.core.Node;
import me.lucko.luckperms.common.core.PermissionHolder;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.function.Function;
import java.util.regex.Pattern;
public class LogicParser {
private static final ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
public static boolean parse(String s, PermissionHolder holder, Tristate tristate) throws IllegalArgumentException {
try {
String expression = generateExpression(s, s1 -> holder.hasPermission(Node.fromSerialisedNode(s1, true)) == tristate);
String result = SCRIPT_ENGINE.eval(expression).toString();
if (!result.equals("true") && !result.equals("false")) {
throw new IllegalArgumentException();
}
return Boolean.parseBoolean(result);
} catch (Throwable t) {
throw new IllegalArgumentException(s, t);
}
}
private static String generateExpression(String input, Function<String, Boolean> checker) {
while (true) {
int i = input.indexOf("<");
int i2 = input.indexOf(">");
if (i == -1 || i2 == -1) {
break;
}
String match = input.substring(i, i2 + 1);
String matchContent = match.substring(1, match.length() - 1);
String matchReplacement = ("" + checker.apply(matchContent)).toLowerCase();
input = input.replaceFirst(Pattern.quote(match), matchReplacement);
}
return input.replace("&", "&&").replace("|", "||");
}
}
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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.defaults;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.common.core.Node;
import me.lucko.luckperms.common.core.PermissionHolder;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException;
import java.util.List;
@Getter
@ToString
@AllArgsConstructor
public class Rule {
private final String hasTrueExpression;
private final String hasFalseExpression;
private final String lacksExpression;
private final List<String> toGive;
private final List<String> toTake;
public boolean apply(PermissionHolder holder) {
if (hasTrueExpression != null) {
try {
boolean b = LogicParser.parse(hasTrueExpression, holder, Tristate.TRUE);
if (!b) {
// The holder does not meet this requirement
return false;
}
} catch (IllegalArgumentException e) {
// Couldn't parse
e.printStackTrace();
return false;
}
}
if (hasFalseExpression != null) {
try {
boolean b = LogicParser.parse(hasFalseExpression, holder, Tristate.FALSE);
if (!b) {
// The holder does not meet this requirement
return false;
}
} catch (IllegalArgumentException e) {
// Couldn't parse
e.printStackTrace();
return false;
}
}
if (lacksExpression != null) {
try {
boolean b = LogicParser.parse(lacksExpression, holder, 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 (String s : toTake) {
try {
holder.unsetPermission(Node.fromSerialisedNode(s, true));
} catch (ObjectLacksException ignored) {}
}
for (String s : toGive) {
try {
holder.setPermission(Node.fromSerialisedNode(s, true));
} catch (ObjectAlreadyHasException ignored) {}
}
return true;
}
}
@@ -27,6 +27,7 @@ import me.lucko.luckperms.api.data.Callback;
import me.lucko.luckperms.api.event.events.UserFirstLoginEvent;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.core.UuidCache;
import me.lucko.luckperms.common.defaults.Rule;
import me.lucko.luckperms.common.users.User;
import java.util.UUID;
@@ -64,6 +65,19 @@ public class AbstractListener {
if (user == null) {
plugin.getLog().warn("Failed to load user: " + username);
} else {
// Setup defaults for the user
boolean save = false;
for (Rule rule : plugin.getConfiguration().getDefaultAssignments()) {
if (rule.apply(user)) {
save = true;
}
}
// If they were given a default, persist the new assignments back to the storage.
if (save) {
plugin.getDatastore().saveUser(user);
}
user.setupData(false); // Pretty nasty calculation call. Sets up the caching system so data is ready when the user joins.
}