mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
implement verbose expression matching
This commit is contained in:
parent
f89fb87c92
commit
0c1a2e82bf
@ -53,6 +53,7 @@ import me.lucko.luckperms.common.contexts.ServerCalculator;
|
||||
import me.lucko.luckperms.common.core.UuidCache;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
import me.lucko.luckperms.common.data.Importer;
|
||||
import me.lucko.luckperms.common.debug.DebugHandler;
|
||||
import me.lucko.luckperms.common.dependencies.DependencyManager;
|
||||
import me.lucko.luckperms.common.locale.LocaleManager;
|
||||
import me.lucko.luckperms.common.locale.NoopLocaleManager;
|
||||
@ -72,7 +73,6 @@ import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
|
||||
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
|
||||
import me.lucko.luckperms.common.tasks.UpdateTask;
|
||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||
import me.lucko.luckperms.common.utils.DebugHandler;
|
||||
import me.lucko.luckperms.common.utils.LoggerImpl;
|
||||
import me.lucko.luckperms.common.utils.PermissionCache;
|
||||
|
||||
|
@ -44,6 +44,7 @@ import me.lucko.luckperms.common.contexts.ServerCalculator;
|
||||
import me.lucko.luckperms.common.core.UuidCache;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
import me.lucko.luckperms.common.data.Importer;
|
||||
import me.lucko.luckperms.common.debug.DebugHandler;
|
||||
import me.lucko.luckperms.common.dependencies.DependencyManager;
|
||||
import me.lucko.luckperms.common.locale.LocaleManager;
|
||||
import me.lucko.luckperms.common.locale.NoopLocaleManager;
|
||||
@ -63,7 +64,6 @@ import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
|
||||
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
|
||||
import me.lucko.luckperms.common.tasks.UpdateTask;
|
||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||
import me.lucko.luckperms.common.utils.DebugHandler;
|
||||
import me.lucko.luckperms.common.utils.LoggerImpl;
|
||||
import me.lucko.luckperms.common.utils.PermissionCache;
|
||||
|
||||
|
@ -37,6 +37,7 @@ import me.lucko.luckperms.common.contexts.ContextManager;
|
||||
import me.lucko.luckperms.common.core.UuidCache;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
import me.lucko.luckperms.common.data.Importer;
|
||||
import me.lucko.luckperms.common.debug.DebugHandler;
|
||||
import me.lucko.luckperms.common.locale.LocaleManager;
|
||||
import me.lucko.luckperms.common.managers.GroupManager;
|
||||
import me.lucko.luckperms.common.managers.TrackManager;
|
||||
@ -44,7 +45,6 @@ import me.lucko.luckperms.common.managers.UserManager;
|
||||
import me.lucko.luckperms.common.messaging.AbstractMessagingService;
|
||||
import me.lucko.luckperms.common.storage.Storage;
|
||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||
import me.lucko.luckperms.common.utils.DebugHandler;
|
||||
import me.lucko.luckperms.common.utils.PermissionCache;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -22,8 +22,6 @@
|
||||
|
||||
package me.lucko.luckperms.common.commands.misc;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import me.lucko.luckperms.common.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.commands.Arg;
|
||||
import me.lucko.luckperms.common.commands.CommandResult;
|
||||
@ -31,6 +29,7 @@ import me.lucko.luckperms.common.commands.SingleCommand;
|
||||
import me.lucko.luckperms.common.commands.sender.Sender;
|
||||
import me.lucko.luckperms.common.constants.Message;
|
||||
import me.lucko.luckperms.common.constants.Permission;
|
||||
import me.lucko.luckperms.common.debug.DebugListener;
|
||||
import me.lucko.luckperms.common.utils.Predicates;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -39,10 +38,10 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class VerboseCommand extends SingleCommand {
|
||||
public VerboseCommand() {
|
||||
super("Verbose", "Enable verbose permission check output", "/%s verbose <true|false> [filters...]", Permission.VERBOSE, Predicates.is(0),
|
||||
super("Verbose", "Manage verbose permission checking", "/%s verbose <true|false> [filter]", Permission.VERBOSE, Predicates.is(0),
|
||||
Arg.list(
|
||||
Arg.create("true|false|record|paste", true, "whether to enable/disable logging, or start/stop recording"),
|
||||
Arg.create("filters...", false, "the name of the user / start of the node to filter by")
|
||||
Arg.create("filter", false, "the filter to match entries against")
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -56,60 +55,56 @@ public class VerboseCommand extends SingleCommand {
|
||||
|
||||
String mode = args.get(0).toLowerCase();
|
||||
|
||||
if (mode.equals("on") || mode.equals("true")) {
|
||||
if (mode.equals("on") || mode.equals("true") || mode.equals("record")) {
|
||||
List<String> filters = new ArrayList<>();
|
||||
if (args.size() != 1) {
|
||||
filters.addAll(args.subList(1, args.size()));
|
||||
}
|
||||
|
||||
String filter = filters.isEmpty() ? "" : filters.stream().collect(Collectors.joining(" "));
|
||||
boolean notify = !mode.equals("record");
|
||||
|
||||
plugin.getDebugHandler().register(sender, filters);
|
||||
if (!filters.isEmpty()) {
|
||||
Message.VERBOSE_ON_QUERY.send(sender, filters.stream().collect(Collectors.joining("&7, &f")));
|
||||
plugin.getDebugHandler().register(sender, filter, notify);
|
||||
|
||||
if (notify) {
|
||||
if (!filter.equals("")) {
|
||||
Message.VERBOSE_ON_QUERY.send(sender, filter);
|
||||
} else {
|
||||
Message.VERBOSE_ON.send(sender);
|
||||
}
|
||||
} else {
|
||||
Message.VERBOSE_ON.send(sender);
|
||||
if (!filter.equals("")) {
|
||||
Message.VERBOSE_RECORDING_ON_QUERY.send(sender, filter);
|
||||
} else {
|
||||
Message.VERBOSE_RECORDING_ON.send(sender);
|
||||
}
|
||||
}
|
||||
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
|
||||
if (mode.equals("off") || mode.equals("false")) {
|
||||
plugin.getDebugHandler().unregister(sender.getUuid());
|
||||
Message.VERBOSE_OFF.send(sender);
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
if (mode.equals("off") || mode.equals("false") || mode.equals("paste")) {
|
||||
DebugListener listener = plugin.getDebugHandler().unregister(sender.getUuid());
|
||||
|
||||
if (mode.equals("record")) {
|
||||
List<String> filters = new ArrayList<>();
|
||||
if (args.size() != 1) {
|
||||
filters.addAll(args.subList(1, args.size()));
|
||||
}
|
||||
if (mode.equals("paste")) {
|
||||
if (listener == null) {
|
||||
Message.VERBOSE_OFF.send(sender);
|
||||
} else {
|
||||
String url = listener.uploadPastedData();
|
||||
if (url == null) {
|
||||
url = "null";
|
||||
}
|
||||
|
||||
plugin.getDebugHandler().setPastedFilters(ImmutableList.copyOf(filters));
|
||||
plugin.getDebugHandler().setPasted(true);
|
||||
|
||||
if (!filters.isEmpty()) {
|
||||
Message.VERBOSE_RECORDING_ON_QUERY.send(sender, filters.stream().collect(Collectors.joining("&7, &f")));
|
||||
Message.VERBOSE_RECORDING_URL.send(sender, url);
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
} else {
|
||||
Message.VERBOSE_RECORDING_ON.send(sender);
|
||||
Message.VERBOSE_OFF.send(sender);
|
||||
}
|
||||
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
|
||||
if (mode.equals("paste")) {
|
||||
plugin.getDebugHandler().setPasted(false);
|
||||
Message.VERBOSE_RECORDING_UPLOAD_START.send(sender);
|
||||
|
||||
String url = plugin.getDebugHandler().uploadPastedData();
|
||||
if (url == null) {
|
||||
url = "null";
|
||||
}
|
||||
|
||||
Message.VERBOSE_RECORDING_URL.send(sender, url);
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
|
||||
sendUsage(sender, label);
|
||||
return CommandResult.INVALID_ARGS;
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.debug;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class CheckData {
|
||||
|
||||
private final String checked;
|
||||
private final String node;
|
||||
private final Tristate value;
|
||||
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.debug;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.common.commands.sender.Sender;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
public class DebugHandler implements Runnable {
|
||||
private final String pluginVersion;
|
||||
|
||||
private final Map<UUID, DebugListener> listeners;
|
||||
private final Queue<CheckData> queue;
|
||||
|
||||
@Setter
|
||||
private boolean shutdown = false;
|
||||
|
||||
public DebugHandler(Executor executor, String pluginVersion) {
|
||||
this.pluginVersion = "v" + pluginVersion;
|
||||
listeners = new ConcurrentHashMap<>();
|
||||
queue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
executor.execute(this);
|
||||
}
|
||||
|
||||
public void offer(String checked, String node, Tristate value) {
|
||||
queue.offer(new CheckData(checked, node, value));
|
||||
}
|
||||
|
||||
public void register(Sender sender, String filter, boolean notify) {
|
||||
listeners.put(sender.getUuid(), new DebugListener(pluginVersion, sender, filter, notify));
|
||||
}
|
||||
|
||||
public DebugListener unregister(UUID uuid) {
|
||||
flush();
|
||||
return listeners.remove(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
flush();
|
||||
|
||||
if (shutdown) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void flush() {
|
||||
for (CheckData e; (e = queue.poll()) != null; ) {
|
||||
for (DebugListener listener : listeners.values()) {
|
||||
listener.acceptData(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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.debug;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import me.lucko.luckperms.common.commands.sender.Sender;
|
||||
import me.lucko.luckperms.common.constants.Message;
|
||||
import me.lucko.luckperms.common.utils.PasteUtils;
|
||||
import me.lucko.luckperms.common.utils.Scripting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class DebugListener {
|
||||
private final String pluginVersion;
|
||||
private final Sender holder;
|
||||
private final String filter;
|
||||
private final boolean notify;
|
||||
|
||||
private List<CheckData> results = new ArrayList<>();
|
||||
|
||||
public void acceptData(CheckData data) {
|
||||
if (!matches(data, filter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (results.size() < 500) {
|
||||
results.add(data);
|
||||
}
|
||||
|
||||
if (notify) {
|
||||
Message.LOG.send(holder, "&7Checking &a" + data.getChecked() + "&7 for: &a" + data.getNode() + " &f(&7" + data.getValue().toString() + "&f)");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean matches(CheckData data, String filter) {
|
||||
if (filter.equals("")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ScriptEngine engine = Scripting.getScriptEngine();
|
||||
if (engine == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
StringTokenizer tokenizer = new StringTokenizer(filter, " |&()", true);
|
||||
StringBuilder expression = new StringBuilder();
|
||||
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
String token = tokenizer.nextToken();
|
||||
if (!isDelim(token)) {
|
||||
boolean b = data.getChecked().equalsIgnoreCase(token) ||
|
||||
data.getNode().toLowerCase().startsWith(token.toLowerCase()) ||
|
||||
data.getValue().name().equalsIgnoreCase(token);
|
||||
|
||||
token = "" + b;
|
||||
}
|
||||
|
||||
expression.append(token);
|
||||
}
|
||||
|
||||
try {
|
||||
String exp = expression.toString().replace("&", "&&").replace("|", "||");
|
||||
String result = engine.eval(exp).toString();
|
||||
if (!result.equals("true") && !result.equals("false")) {
|
||||
throw new IllegalArgumentException(exp + " - " + result);
|
||||
}
|
||||
|
||||
return Boolean.parseBoolean(result);
|
||||
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isDelim(String token) {
|
||||
return token.equals(" ") || token.equals("|") || token.equals("&") || token.equals("(") || token.equals(")");
|
||||
}
|
||||
|
||||
public String uploadPastedData() {
|
||||
ImmutableList.Builder<String> output = ImmutableList.<String>builder()
|
||||
.add("#### This file was automatically generated by [LuckPerms](https://github.com/lucko/LuckPerms) " + pluginVersion)
|
||||
.add("")
|
||||
.add("Format: `<checked>` `<permission>` `<value>`")
|
||||
.add("")
|
||||
.add("Size: " + results.size())
|
||||
.add("");
|
||||
|
||||
if (results.size() >= 500) {
|
||||
output.add("**WARN:** Result set exceeded max size of 500. Output was trimmed.");
|
||||
output.add("");
|
||||
}
|
||||
|
||||
List<String> ret = results.stream()
|
||||
.map(c -> "`" + c.getChecked() + "` - " + c.getNode() + " - **" + c.getValue().toString() + "** ")
|
||||
.collect(Collectors.toList());
|
||||
|
||||
output.add("___")
|
||||
.add("")
|
||||
.addAll(ret)
|
||||
.build();
|
||||
|
||||
results.clear();
|
||||
return PasteUtils.paste(output.build().stream().collect(Collectors.joining("\n")));
|
||||
}
|
||||
|
||||
}
|
@ -25,23 +25,23 @@ package me.lucko.luckperms.common.defaults;
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.common.core.NodeFactory;
|
||||
import me.lucko.luckperms.common.core.model.PermissionHolder;
|
||||
import me.lucko.luckperms.common.utils.Scripting;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
public class LogicParser {
|
||||
private static final ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager(null).getEngineByName("nashorn");
|
||||
|
||||
public static boolean parse(String s, PermissionHolder holder, Tristate tristate) throws IllegalArgumentException {
|
||||
try {
|
||||
ScriptEngine engine = Scripting.getScriptEngine();
|
||||
|
||||
String expression = generateExpression(s, s1 -> holder.hasPermission(NodeFactory.fromSerialisedNode(s1, true)) == tristate);
|
||||
if (SCRIPT_ENGINE == null) {
|
||||
if (engine == null) {
|
||||
throw new NullPointerException("script engine");
|
||||
}
|
||||
String result = SCRIPT_ENGINE.eval(expression).toString();
|
||||
String result = engine.eval(expression).toString();
|
||||
|
||||
if (!result.equals("true") && !result.equals("false")) {
|
||||
throw new IllegalArgumentException();
|
||||
|
@ -1,152 +0,0 @@
|
||||
/*
|
||||
* 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.utils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.common.commands.sender.Sender;
|
||||
import me.lucko.luckperms.common.constants.Message;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DebugHandler {
|
||||
private final String pluginVersion;
|
||||
|
||||
@Setter
|
||||
private boolean pasted = false;
|
||||
|
||||
@Setter
|
||||
private List<String> pastedFilters = ImmutableList.of();
|
||||
|
||||
private final List<String> pastedList = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
private final Map<Receiver, List<String>> listeners;
|
||||
private final Queue<Data> queue;
|
||||
|
||||
@Setter
|
||||
private boolean shutdown = false;
|
||||
|
||||
public DebugHandler(Executor executor, String pluginVersion) {
|
||||
this.pluginVersion = "v" + pluginVersion;
|
||||
listeners = new ConcurrentHashMap<>();
|
||||
queue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
executor.execute(() -> {
|
||||
while (true) {
|
||||
for (Data e; (e = queue.poll()) != null; ) {
|
||||
handleOutput(e.getChecked(), e.getNode(), e.getValue());
|
||||
}
|
||||
|
||||
if (shutdown) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleOutput(String checked, String node, Tristate value) {
|
||||
paste:
|
||||
if (pasted) {
|
||||
for (String filter : pastedFilters) {
|
||||
if (node.toLowerCase().startsWith(filter.toLowerCase())) continue;
|
||||
if (checked.equalsIgnoreCase(filter)) continue;
|
||||
break paste;
|
||||
}
|
||||
|
||||
pastedList.add("`" + checked + "` - " + node + " - **" + value.toString() + "** ");
|
||||
}
|
||||
|
||||
all:
|
||||
for (Map.Entry<Receiver, List<String>> e : listeners.entrySet()) {
|
||||
for (String filter : e.getValue()) {
|
||||
if (node.toLowerCase().startsWith(filter.toLowerCase())) continue;
|
||||
if (checked.equalsIgnoreCase(filter)) continue;
|
||||
continue all;
|
||||
}
|
||||
|
||||
Message.LOG.send(e.getKey().getSender(), "&7Checking &a" + checked + "&7 for: &a" + node + " &f(&7" + value.toString() + "&f)");
|
||||
}
|
||||
}
|
||||
|
||||
public void offer(String checked, String node, Tristate value) {
|
||||
queue.offer(new Data(checked, node, value));
|
||||
}
|
||||
|
||||
public void register(Sender sender, List<String> filters) {
|
||||
listeners.put(new Receiver(sender.getUuid(), sender), ImmutableList.copyOf(filters));
|
||||
}
|
||||
|
||||
public void unregister(UUID uuid) {
|
||||
listeners.remove(new Receiver(uuid, null));
|
||||
}
|
||||
|
||||
public String uploadPastedData() {
|
||||
List<String> output = ImmutableList.<String>builder()
|
||||
.add("#### This file was automatically generated by [LuckPerms](https://github.com/lucko/LuckPerms) " + pluginVersion)
|
||||
.add("")
|
||||
.add("Format: `<checked>` `<permission>` `<value>`")
|
||||
.add("")
|
||||
.add("___")
|
||||
.add("")
|
||||
.addAll(pastedList)
|
||||
.build();
|
||||
|
||||
pastedList.clear();
|
||||
return PasteUtils.paste(output.stream().collect(Collectors.joining("\n")));
|
||||
}
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode(of = "uuid")
|
||||
@AllArgsConstructor
|
||||
private static final class Receiver {
|
||||
private final UUID uuid;
|
||||
private final Sender sender;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
private static final class Data {
|
||||
private final String checked;
|
||||
private final String node;
|
||||
private final Tristate value;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.utils;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
@UtilityClass
|
||||
public class Scripting {
|
||||
private static ScriptEngine SCRIPT_ENGINE = null;
|
||||
|
||||
// Lazily load
|
||||
public static synchronized ScriptEngine getScriptEngine() {
|
||||
if (SCRIPT_ENGINE == null) {
|
||||
SCRIPT_ENGINE = new ScriptEngineManager(null).getEngineByName("nashorn");
|
||||
}
|
||||
return SCRIPT_ENGINE;
|
||||
}
|
||||
|
||||
}
|
@ -44,6 +44,7 @@ import me.lucko.luckperms.common.contexts.ServerCalculator;
|
||||
import me.lucko.luckperms.common.core.UuidCache;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
import me.lucko.luckperms.common.data.Importer;
|
||||
import me.lucko.luckperms.common.debug.DebugHandler;
|
||||
import me.lucko.luckperms.common.dependencies.DependencyManager;
|
||||
import me.lucko.luckperms.common.locale.LocaleManager;
|
||||
import me.lucko.luckperms.common.locale.NoopLocaleManager;
|
||||
@ -59,7 +60,6 @@ import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
|
||||
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
|
||||
import me.lucko.luckperms.common.tasks.UpdateTask;
|
||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||
import me.lucko.luckperms.common.utils.DebugHandler;
|
||||
import me.lucko.luckperms.common.utils.LoggerImpl;
|
||||
import me.lucko.luckperms.common.utils.PermissionCache;
|
||||
import me.lucko.luckperms.sponge.commands.SpongeMainCommand;
|
||||
|
Loading…
Reference in New Issue
Block a user