Implement support for futures & other changes in the Sponge Permissions API

This commit is contained in:
Luck
2017-07-16 14:10:56 +01:00
Unverified
parent 7a6c0ab154
commit b41dea9ee1
42 changed files with 1474 additions and 171 deletions
+69
View File
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>luckperms</artifactId>
<groupId>me.lucko.luckperms</groupId>
<version>3.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>luckperms-sponge-service-api7</artifactId>
<packaging>jar</packaging>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Sponge Model -->
<dependency>
<groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-sponge-service</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- LuckPerms Common -->
<dependency>
<groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- LuckPerms API -->
<dependency>
<groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- Sponge API -->
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>7.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,78 @@
/*
* 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.sponge.service.proxy.api7;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.sponge.service.model.LPPermissionDescription;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectReference;
import org.spongepowered.api.text.Text;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor
public class PermissionDescription7Proxy implements PermissionDescription {
private final LPPermissionService service;
private final LPPermissionDescription handle;
@Override
public String getId() {
return handle.getId();
}
@Override
public Optional<Text> getDescription() {
return handle.getDescription();
}
@Override
public Optional<PluginContainer> getOwner() {
return handle.getOwner();
}
@Override
public Map<Subject, Boolean> getAssignedSubjects(String s) {
return handle.getAssignedSubjects(s).entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
e -> new Subject7Proxy(service, e.getKey().toReference()),
Map.Entry::getValue
));
}
@SuppressWarnings("unchecked")
@Override
public CompletableFuture<Map<SubjectReference, Boolean>> findAssignedSubjects(String s) {
return (CompletableFuture) handle.findAssignedSubjects(s);
}
}
@@ -0,0 +1,135 @@
/*
* 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.sponge.service.proxy.api7;
import lombok.RequiredArgsConstructor;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.sponge.service.model.LPPermissionDescription;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.service.context.ContextCalculator;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection;
import org.spongepowered.api.service.permission.SubjectReference;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
@RequiredArgsConstructor
public class PermissionService7Proxy implements PermissionService {
private final LPPermissionService handle;
@Override
public SubjectCollection getUserSubjects() {
return handle.getUserSubjects().sponge();
}
@Override
public SubjectCollection getGroupSubjects() {
return handle.getGroupSubjects().sponge();
}
@Override
public Subject getDefaults() {
return handle.getDefaults().sponge();
}
@Override
public Predicate<String> getIdentifierValidityPredicate() {
return handle.getIdentifierValidityPredicate();
}
@Override
public CompletableFuture<SubjectCollection> loadCollection(String s) {
return CompletableFuture.completedFuture(handle.getCollection(s).sponge());
}
@Override
public Optional<SubjectCollection> getCollection(String s) {
return Optional.ofNullable(handle.getLoadedCollections().get(s.toLowerCase())).map(LPSubjectCollection::sponge);
}
@Override
public CompletableFuture<Boolean> hasCollection(String s) {
return CompletableFuture.completedFuture(handle.getLoadedCollections().containsKey(s.toLowerCase()));
}
@Override
public Map<String, SubjectCollection> getLoadedCollections() {
return handle.getLoadedCollections().entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
Map.Entry::getKey,
e -> e.getValue().sponge()
));
}
@Override
public CompletableFuture<Set<String>> getAllIdentifiers() {
return CompletableFuture.completedFuture(ImmutableSet.copyOf(handle.getLoadedCollections().keySet()));
}
@Override
public SubjectReference newSubjectReference(String s, String s1) {
return handle.newSubjectReference(s, s1);
}
@Override
public PermissionDescription.Builder newDescriptionBuilder(Object o) {
Optional<PluginContainer> container = Sponge.getGame().getPluginManager().fromInstance(o);
if (!container.isPresent()) {
throw new IllegalArgumentException("Couldn't find a plugin container for " + o.getClass().getSimpleName());
}
return new SimpleDescription7Builder(handle, container.get());
}
@Override
public Optional<PermissionDescription> getDescription(String s) {
return handle.getDescription(s).map(LPPermissionDescription::sponge);
}
@Override
public Collection<PermissionDescription> getDescriptions() {
return handle.getDescriptions().stream().map(LPPermissionDescription::sponge).collect(ImmutableCollectors.toImmutableSet());
}
@Override
public void registerContextCalculator(ContextCalculator<Subject> contextCalculator) {
handle.registerContextCalculator(contextCalculator);
}
}
@@ -0,0 +1,100 @@
/*
* 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.sponge.service.proxy.api7;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.sponge.service.model.LPPermissionDescription;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.text.Text;
import java.util.HashMap;
import java.util.Map;
@ToString(of = {"container", "roles", "id", "description"})
@EqualsAndHashCode(of = {"container", "roles", "id", "description"})
@RequiredArgsConstructor
public final class SimpleDescription7Builder implements PermissionDescription.Builder {
private final LPPermissionService service;
private final PluginContainer container;
private final Map<String, Tristate> roles = new HashMap<>();
private String id = null;
private Text description = null;
@Override
public PermissionDescription.Builder id(@NonNull String s) {
id = s;
return this;
}
@Override
public PermissionDescription.Builder description(Text text) {
description = text;
return this;
}
@Override
public PermissionDescription.Builder assign(@NonNull String s, boolean b) {
roles.put(s, Tristate.fromBoolean(b));
return this;
}
@Override
public PermissionDescription register() throws IllegalStateException {
if (id == null) {
throw new IllegalStateException("id cannot be null");
}
LPPermissionDescription d = service.registerPermissionDescription(id, description, container);
// Set role-templates
LPSubjectCollection subjects = service.getCollection(PermissionService.SUBJECTS_ROLE_TEMPLATE);
for (Map.Entry<String, Tristate> assignment : roles.entrySet()) {
LPSubject subject = subjects.loadSubject(assignment.getKey()).join();
subject.getTransientSubjectData().setPermission(ContextSet.empty(), id, assignment.getValue());
}
service.getPlugin().getPermissionVault().offer(id);
// null stuff so this instance can be reused
roles.clear();
id = null;
description = null;
return d.sponge();
}
}
@@ -0,0 +1,160 @@
/*
* 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.sponge.service.proxy.api7;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.sponge.service.model.CompatibilityUtil;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.SubjectReference;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection;
import org.spongepowered.api.service.permission.SubjectData;
import org.spongepowered.api.util.Tristate;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
@SuppressWarnings("unchecked")
@RequiredArgsConstructor
public class Subject7Proxy implements Subject {
private final LPPermissionService service;
private final SubjectReference ref;
private CompletableFuture<LPSubject> getHandle() {
return ref.resolveLp();
}
@Override
public Optional<CommandSource> getCommandSource() {
return getHandle().thenApply(LPSubject::getCommandSource).join();
}
@Override
public SubjectCollection getContainingCollection() {
return service.getCollection(ref.getCollectionIdentifier()).sponge();
}
@Override
public org.spongepowered.api.service.permission.SubjectReference asSubjectReference() {
return ref;
}
@Override
public boolean isSubjectDataPersisted() {
return true;
}
@Override
public SubjectData getSubjectData() {
return new SubjectData7Proxy(service, ref, true);
}
@Override
public SubjectData getTransientSubjectData() {
return new SubjectData7Proxy(service, ref, false);
}
@Override
public boolean hasPermission(Set<Context> contexts, String permission) {
return getHandle().thenApply(handle -> {
return handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission).asBoolean();
}).join();
}
@Override
public boolean hasPermission(String permission) {
return getHandle().thenApply(handle -> {
return handle.getPermissionValue(ImmutableContextSet.empty(), permission).asBoolean();
}).join();
}
@Override
public Tristate getPermissionValue(Set<Context> contexts, String permission) {
return getHandle().thenApply(handle -> {
return CompatibilityUtil.convertTristate(handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission));
}).join();
}
@Override
public boolean isChildOf(org.spongepowered.api.service.permission.SubjectReference parent) {
return getHandle().thenApply(handle -> {
return handle.isChildOf(ImmutableContextSet.empty(), SubjectReference.cast(service, parent));
}).join();
}
@Override
public boolean isChildOf(Set<Context> contexts, org.spongepowered.api.service.permission.SubjectReference parent) {
return getHandle().thenApply(handle -> {
return handle.isChildOf(CompatibilityUtil.convertContexts(contexts), SubjectReference.cast(service, parent));
}).join();
}
@Override
public List<org.spongepowered.api.service.permission.SubjectReference> getParents() {
return (List) getHandle().thenApply(handle -> {
return handle.getParents(ImmutableContextSet.empty());
}).join();
}
@Override
public List<org.spongepowered.api.service.permission.SubjectReference> getParents(Set<Context> contexts) {
return (List) getHandle().thenApply(handle -> {
return handle.getParents(CompatibilityUtil.convertContexts(contexts));
}).join();
}
@Override
public Optional<String> getOption(Set<Context> contexts, String key) {
return getHandle().thenApply(handle -> {
return handle.getOption(CompatibilityUtil.convertContexts(contexts), key);
}).join();
}
@Override
public Optional<String> getOption(String key) {
return getHandle().thenApply(handle -> {
return handle.getOption(ImmutableContextSet.empty(), key);
}).join();
}
@Override
public String getIdentifier() {
return ref.getSubjectIdentifier();
}
@Override
public Set<Context> getActiveContexts() {
return getHandle().thenApply(handle -> CompatibilityUtil.convertContexts(handle.getActiveContextSet())).join();
}
}
@@ -0,0 +1,135 @@
/*
* 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.sponge.service.proxy.api7;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.sponge.service.model.CompatibilityUtil;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.SubjectCollection;
import org.spongepowered.api.service.permission.SubjectReference;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
@SuppressWarnings("unchecked")
@RequiredArgsConstructor
public class SubjectCollection7Proxy implements SubjectCollection {
private final LPSubjectCollection handle;
@Override
public String getIdentifier() {
return handle.getIdentifier();
}
@Override
public Predicate<String> getIdentifierValidityPredicate() {
return handle.getIdentifierValidityPredicate();
}
@Override
public CompletableFuture<Subject> loadSubject(String s) {
return handle.loadSubject(s).thenApply(LPSubject::sponge);
}
@Override
public Optional<Subject> getSubject(String s) {
return handle.getSubject(s).map(LPSubject::sponge);
}
@Override
public CompletableFuture<Boolean> hasSubject(String s) {
return handle.hasRegistered(s);
}
@Override
public CompletableFuture<Map<String, Subject>> loadSubjects(Set<String> set) {
return handle.loadSubjects(set).thenApply(subs -> subs.stream().collect(ImmutableCollectors.toImmutableMap(LPSubject::getIdentifier, LPSubject::sponge)));
}
@Override
public Collection<Subject> getLoadedSubjects() {
return handle.getLoadedSubjects().stream().map(LPSubject::sponge).collect(ImmutableCollectors.toImmutableSet());
}
@Override
public CompletableFuture<Set<String>> getAllIdentifiers() {
return (CompletableFuture) handle.getAllIdentifiers();
}
@Override
public SubjectReference newSubjectReference(String s) {
return handle.newSubjectReference(s);
}
@Override
public CompletableFuture<Map<SubjectReference, Boolean>> getAllWithPermission(String s) {
return (CompletableFuture) handle.getAllWithPermission(s);
}
@Override
public CompletableFuture<Map<SubjectReference, Boolean>> getAllWithPermission(Set<Context> set, String s) {
return (CompletableFuture) handle.getAllWithPermission(CompatibilityUtil.convertContexts(set), s);
}
@Override
public Map<Subject, Boolean> getLoadedWithPermission(String s) {
return handle.getLoadedWithPermission(s).entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
sub -> sub.getKey().sponge(),
Map.Entry::getValue
));
}
@Override
public Map<Subject, Boolean> getLoadedWithPermission(Set<Context> set, String s) {
return handle.getLoadedWithPermission(CompatibilityUtil.convertContexts(set), s).entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
sub -> sub.getKey().sponge(),
Map.Entry::getValue
));
}
@Override
public Subject getDefaults() {
return handle.getDefaults().sponge();
}
@Override
public void suggestUnload(String s) {
handle.suggestUnload(s);
}
}
@@ -0,0 +1,169 @@
/*
* 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.sponge.service.proxy.api7;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.sponge.service.model.CompatibilityUtil;
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectData;
import me.lucko.luckperms.sponge.service.model.SubjectReference;
import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.SubjectData;
import org.spongepowered.api.util.Tristate;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
/**
* Proxies a LuckPerms Subject to implement {@link SubjectData}.
*/
@SuppressWarnings("unchecked")
@RequiredArgsConstructor
public class SubjectData7Proxy implements SubjectData {
private final LPPermissionService service;
private final SubjectReference ref;
private final boolean enduring;
private CompletableFuture<LPSubjectData> getHandle() {
return enduring ? ref.resolveLp().thenApply(LPSubject::getSubjectData) : ref.resolveLp().thenApply(LPSubject::getTransientSubjectData);
}
@Override
public Map<Set<Context>, Map<String, Boolean>> getAllPermissions() {
return (Map) getHandle().thenApply(handle -> {
return handle.getAllPermissions().entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
e -> CompatibilityUtil.convertContexts(e.getKey()),
Map.Entry::getValue
));
}).join();
}
@Override
public Map<String, Boolean> getPermissions(Set<Context> contexts) {
return getHandle().thenApply(handle -> handle.getPermissions(CompatibilityUtil.convertContexts(contexts))).join();
}
@Override
public CompletableFuture<Boolean> setPermission(Set<Context> contexts, String permission, Tristate value) {
return getHandle().thenCompose(handle -> {
return handle.setPermission(
CompatibilityUtil.convertContexts(contexts),
permission,
CompatibilityUtil.convertTristate(value)
);
});
}
@Override
public CompletableFuture<Boolean> clearPermissions() {
return getHandle().thenCompose(LPSubjectData::clearPermissions);
}
@Override
public CompletableFuture<Boolean> clearPermissions(Set<Context> contexts) {
return getHandle().thenCompose(handle -> handle.clearPermissions(CompatibilityUtil.convertContexts(contexts)));
}
@Override
public Map<Set<Context>, List<org.spongepowered.api.service.permission.SubjectReference>> getAllParents() {
return (Map) getHandle().thenApply(handle -> {
return handle.getAllParents().entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
e -> CompatibilityUtil.convertContexts(e.getKey()),
Map.Entry::getValue
));
}).join();
}
@Override
public List<org.spongepowered.api.service.permission.SubjectReference> getParents(Set<Context> contexts) {
return (List) getHandle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts))).join();
}
@Override
public CompletableFuture<Boolean> addParent(Set<Context> contexts, org.spongepowered.api.service.permission.SubjectReference ref) {
return getHandle().thenCompose(handle -> handle.addParent(CompatibilityUtil.convertContexts(contexts), SubjectReference.cast(service, ref)));
}
@Override
public CompletableFuture<Boolean> removeParent(Set<Context> contexts, org.spongepowered.api.service.permission.SubjectReference ref) {
return getHandle().thenCompose(handle -> handle.removeParent(CompatibilityUtil.convertContexts(contexts), SubjectReference.cast(service, ref)));
}
@Override
public CompletableFuture<Boolean> clearParents() {
return getHandle().thenCompose(LPSubjectData::clearParents);
}
@Override
public CompletableFuture<Boolean> clearParents(Set<Context> contexts) {
return getHandle().thenCompose(handle -> handle.clearParents(CompatibilityUtil.convertContexts(contexts)));
}
@Override
public Map<Set<Context>, Map<String, String>> getAllOptions() {
return (Map) getHandle().thenApply(handle -> {
return handle.getAllOptions().entrySet().stream()
.collect(ImmutableCollectors.toImmutableMap(
e -> CompatibilityUtil.convertContexts(e.getKey()),
Map.Entry::getValue
));
}).join();
}
@Override
public Map<String, String> getOptions(Set<Context> contexts) {
return getHandle().thenApply(handle -> handle.getOptions(CompatibilityUtil.convertContexts(contexts))).join();
}
@Override
public CompletableFuture<Boolean> setOption(Set<Context> contexts, String key, @Nullable String value) {
if (value == null) {
return getHandle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key));
} else {
return getHandle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value));
}
}
@Override
public CompletableFuture<Boolean> clearOptions() {
return getHandle().thenCompose(LPSubjectData::clearOptions);
}
@Override
public CompletableFuture<Boolean> clearOptions(Set<Context> contexts) {
return getHandle().thenCompose(handle -> handle.clearOptions(CompatibilityUtil.convertContexts(contexts)));
}
}