Updated Developer API (markdown)

Luck 2017-09-04 21:31:32 +01:00
parent 5cb4fa6c78
commit e715704b42

@ -216,14 +216,13 @@ The CompletionStage API can be used to easily interact with the plugins Storage
```java ```java
LuckPermsApi api = null; // See above for how to get the API instance. LuckPermsApi api = null; // See above for how to get the API instance.
// load the user in from storage. we can specify "null" for their username, // load the user in from storage.
// since it's unknown to us. api.getStorage().loadUser(uuid).thenComposeAsync(success -> {
api.getStorage().loadUser(uuid, "null").thenComposeAsync(success -> {
// loading the user failed, return straight away // loading the user failed, return straight away
if (!success) { if (!success) {
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
} }
// get the user instance, they're now loaded in memory. // get the user instance, they're now loaded in memory.
User user = api.getUser(uuid); User user = api.getUser(uuid);
@ -231,13 +230,8 @@ api.getStorage().loadUser(uuid, "null").thenComposeAsync(success -> {
Node node = api.getNodeFactory().newBuilder(permission).setValue(true).build(); Node node = api.getNodeFactory().newBuilder(permission).setValue(true).build();
// Set the permission, and return true if the user didn't already have it set. // Set the permission, and return true if the user didn't already have it set.
try { DataMutateResult result = user.setPermissionUnchecked(node);
user.setPermission(node); if (result.asBoolean()) {
// now we've set the permission, but still need to save the user data
// back to the storage.
// first save the user
return api.getStorage().saveUser(user) return api.getStorage().saveUser(user)
.thenCompose(b -> { .thenCompose(b -> {
// then cleanup their user instance so we don't create // then cleanup their user instance so we don't create
@ -245,14 +239,48 @@ api.getStorage().loadUser(uuid, "null").thenComposeAsync(success -> {
api.cleanupUser(user); api.cleanupUser(user);
return CompletableFuture.completedFuture(b); return CompletableFuture.completedFuture(b);
}); });
} else {
} catch (ObjectAlreadyHasException e) {
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
} }
}, api.getStorage().getAsyncExecutor()); }, api.getStorage().getAsyncExecutor());
``` ```
### Checking a permission for an offline user
```java
public static boolean hasPermission(LuckPermsApi api, UUID uuid, String permission) {
// load the user in from storage.
return api.getStorage().loadUser(uuid).thenApplyAsync(success -> {
// loading the user failed, return straight away
if (!success) {
return false;
}
// get the user instance, they're now loaded in memory.
User user = api.getUser(uuid);
if (user == null) {
return false;
}
// Now get the users "Contexts". This is basically just data about the players current state.
// see more here: https://github.com/lucko/LuckPerms/wiki/Command-Usage#what-is-context
Contexts contexts = api.getContextForUser(user).orElse(null);
// the getContextForUser method will return null for offline users, so we have to specify
// it ourselves. we can just use the global context.
if (contexts == null) {
contexts = Contexts.global();
}
UserData data = user.getCachedData();
PermissionData permissionData = data.getPermissionData(contexts);
// run a permission check, and return the result
return permissionData.getPermissionValue(permission).asBoolean();
}, api.getStorage().getAsyncExecutor()).join();
}
```
### Getting a players prefix ### Getting a players prefix
LuckPerms has a (somewhat complex) caching system which is used for super fast permission / meta lookups. These classes are exposed in the API, and should be used where possible. LuckPerms has a (somewhat complex) caching system which is used for super fast permission / meta lookups. These classes are exposed in the API, and should be used where possible.