Attach extra data to the editor payload object to allow for tab completion

This commit is contained in:
Luck 2017-11-11 22:57:25 +00:00
parent 17da2807cf
commit 9a420403de
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 37 additions and 16 deletions

View File

@ -30,6 +30,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.stream.JsonWriter;
import me.lucko.luckperms.common.commands.ArgumentPermissions;
@ -63,10 +64,8 @@ import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
private static final String USER_ID_PATTERN = "user/";
@ -84,22 +83,40 @@ public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
return CommandResult.NO_PERMISSION;
}
JsonObject data = new JsonObject();
Set<NodeModel> nodes = holder.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
data.addProperty("who", id(holder));
data.addProperty("cmdAlias", label);
data.addProperty("uploadedBy", sender.getNameWithLocation());
data.addProperty("time", System.currentTimeMillis());
data.add("nodes", serializePermissions(nodes));
// form the payload data
JsonObject payload = new JsonObject();
payload.addProperty("who", id(holder));
payload.addProperty("whoFriendly", holder.getFriendlyName());
if (holder instanceof User) {
payload.addProperty("whoUuid", ((User) holder).getUuid().toString());
}
payload.addProperty("cmdAlias", label);
payload.addProperty("uploadedBy", sender.getNameWithLocation());
payload.addProperty("uploadedByUuid", sender.getUuid().toString());
payload.addProperty("time", System.currentTimeMillis());
String dataUrl = paste(new GsonBuilder().setPrettyPrinting().create().toJson(data));
// attach the holders permissions
payload.add("nodes", serializePermissions(holder.getEnduringNodes().values().stream().map(NodeModel::fromNode)));
// attach an array of all permissions known to the server, to use for tab completion in the editor
JsonArray knownPermsArray = new JsonArray();
for (String perm : plugin.getPermissionVault().rootAsList()) {
knownPermsArray.add(new JsonPrimitive(perm));
}
payload.add("knownPermissions", knownPermsArray);
// upload the payload data to gist
String dataUrl = paste(new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(payload));
if (dataUrl == null) {
Message.EDITOR_UPLOAD_FAILURE.send(sender);
return CommandResult.STATE_ERROR;
}
// extract the information we need from the gist url
List<String> parts = Splitter.on('/').splitToList(dataUrl);
String id = "?" + parts.get(4) + "/" + parts.get(6);
// form a url for the editor
String url = plugin.getConfiguration().get(ConfigKeys.WEB_EDITOR_URL_PATTERN) + id;
Message.EDITOR_URL.send(sender);
@ -171,10 +188,9 @@ public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
}
}
private static JsonArray serializePermissions(Set<NodeModel> nodes) {
private static JsonArray serializePermissions(Stream<NodeModel> nodes) {
JsonArray arr = new JsonArray();
for (NodeModel node : nodes) {
nodes.forEach(node -> {
JsonObject attributes = new JsonObject();
attributes.addProperty("permission", node.getPermission());
attributes.addProperty("value", node.getValue());
@ -196,8 +212,7 @@ public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
}
arr.add(attributes);
}
});
return arr;
}
}

View File

@ -32,11 +32,13 @@ import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
/**
* Stores a collection of all permissions known to the platform.
@ -98,6 +100,10 @@ public class PermissionVault implements Runnable {
return ImmutableSet.copyOf(knownPermissions);
}
public List<String> rootAsList() {
return rootNode.makeImmutableCopy().getNodeEndings().stream().map(Map.Entry::getValue).collect(Collectors.toList());
}
public int getSize() {
return rootNode.getDeepSize();
}