Implement support for futures & other changes in the Sponge Permissions API
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?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</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 API -->
|
||||
<dependency>
|
||||
<groupId>org.spongepowered</groupId>
|
||||
<artifactId>spongeapi</artifactId>
|
||||
<version>7.0.0-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</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>
|
||||
<!-- guava -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>21.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||
import me.lucko.luckperms.common.utils.ImmutableCollectors;
|
||||
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Utility class for converting between Sponge and LuckPerms context and tristate classes
|
||||
*/
|
||||
@UtilityClass
|
||||
public class CompatibilityUtil {
|
||||
private static final LoadingCache<Set<Context>, ImmutableContextSet> SPONGE_TO_LP_CACHE = Caffeine.newBuilder()
|
||||
.build(ImmutableContextSet::fromEntries);
|
||||
|
||||
private static final LoadingCache<ImmutableContextSet, ImmutableSet<Context>> LP_TO_SPONGE_CACHE = Caffeine.newBuilder()
|
||||
.build(set -> set.toSet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(ImmutableCollectors.toImmutableSet()));
|
||||
|
||||
public static ImmutableContextSet convertContexts(@NonNull Set<Context> contexts) {
|
||||
return SPONGE_TO_LP_CACHE.get(ImmutableSet.copyOf(contexts));
|
||||
}
|
||||
|
||||
public static ImmutableSet<Context> convertContexts(@NonNull ContextSet contexts) {
|
||||
return LP_TO_SPONGE_CACHE.get(contexts.makeImmutable());
|
||||
}
|
||||
|
||||
public static Tristate convertTristate(me.lucko.luckperms.api.Tristate tristate) {
|
||||
switch (tristate) {
|
||||
case TRUE:
|
||||
return Tristate.TRUE;
|
||||
case FALSE:
|
||||
return Tristate.FALSE;
|
||||
default:
|
||||
return Tristate.UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
public static me.lucko.luckperms.api.Tristate convertTristate(Tristate tristate) {
|
||||
switch (tristate) {
|
||||
case TRUE:
|
||||
return me.lucko.luckperms.api.Tristate.TRUE;
|
||||
case FALSE:
|
||||
return me.lucko.luckperms.api.Tristate.FALSE;
|
||||
default:
|
||||
return me.lucko.luckperms.api.Tristate.UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import org.spongepowered.api.plugin.PluginContainer;
|
||||
import org.spongepowered.api.service.permission.PermissionDescription;
|
||||
import org.spongepowered.api.text.Text;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface LPPermissionDescription {
|
||||
|
||||
PermissionDescription sponge();
|
||||
|
||||
LPPermissionService getService();
|
||||
|
||||
String getId();
|
||||
|
||||
Optional<Text> getDescription();
|
||||
|
||||
Optional<PluginContainer> getOwner();
|
||||
|
||||
CompletableFuture<Map<SubjectReference, Boolean>> findAssignedSubjects(String collectionIdentifier);
|
||||
|
||||
Map<LPSubject, Boolean> getAssignedSubjects(String collectionIdentifier);
|
||||
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
|
||||
import org.spongepowered.api.plugin.PluginContainer;
|
||||
import org.spongepowered.api.service.context.ContextCalculator;
|
||||
import org.spongepowered.api.service.permission.PermissionService;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.text.Text;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* LuckPerms model for the Sponge PermissionService
|
||||
*/
|
||||
public interface LPPermissionService {
|
||||
|
||||
LuckPermsPlugin getPlugin();
|
||||
|
||||
PermissionService sponge();
|
||||
|
||||
LPSubjectCollection getUserSubjects();
|
||||
|
||||
LPSubjectCollection getGroupSubjects();
|
||||
|
||||
LPSubjectCollection getDefaultSubjects();
|
||||
|
||||
default LPSubjectData getDefaultData() {
|
||||
return getDefaults().getSubjectData();
|
||||
}
|
||||
|
||||
LPSubject getDefaults();
|
||||
|
||||
Predicate<String> getIdentifierValidityPredicate();
|
||||
|
||||
LPSubjectCollection getCollection(String identifier);
|
||||
|
||||
ImmutableMap<String, LPSubjectCollection> getLoadedCollections();
|
||||
|
||||
SubjectReference newSubjectReference(String collectionIdentifier, String subjectIdentifier);
|
||||
|
||||
LPPermissionDescription registerPermissionDescription(String id, Text description, PluginContainer owner);
|
||||
|
||||
Optional<LPPermissionDescription> getDescription(String permission);
|
||||
|
||||
ImmutableCollection<LPPermissionDescription> getDescriptions();
|
||||
|
||||
void registerContextCalculator(ContextCalculator<Subject> calculator);
|
||||
|
||||
// utils
|
||||
ImmutableList<SubjectReference> sortSubjects(Collection<SubjectReference> s);
|
||||
|
||||
Contexts calculateContexts(ImmutableContextSet contextSet);
|
||||
|
||||
void invalidatePermissionCaches();
|
||||
|
||||
void invalidateParentCaches();
|
||||
|
||||
void invalidateOptionCaches();
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* LuckPerms model for the Sponge Subject
|
||||
*/
|
||||
public interface LPSubject {
|
||||
|
||||
Subject sponge();
|
||||
|
||||
LPPermissionService getService();
|
||||
|
||||
String getIdentifier();
|
||||
|
||||
default SubjectReference toReference() {
|
||||
return getService().newSubjectReference(getParentCollection().getIdentifier(), getIdentifier());
|
||||
}
|
||||
|
||||
default LPSubjectData getDefaultData() {
|
||||
return getDefaults().getSubjectData();
|
||||
}
|
||||
|
||||
default LPSubject getDefaults() {
|
||||
return getService().getDefaultSubjects().loadSubject(getIdentifier()).join();
|
||||
}
|
||||
|
||||
default Optional<String> getFriendlyIdentifier() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
default Optional<CommandSource> getCommandSource() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
LPSubjectCollection getParentCollection();
|
||||
|
||||
LPSubjectData getSubjectData();
|
||||
|
||||
LPSubjectData getTransientSubjectData();
|
||||
|
||||
Tristate getPermissionValue(ImmutableContextSet contexts, String permission);
|
||||
|
||||
boolean isChildOf(ImmutableContextSet contexts, SubjectReference parent);
|
||||
|
||||
ImmutableList<SubjectReference> getParents(ImmutableContextSet contexts);
|
||||
|
||||
Optional<String> getOption(ImmutableContextSet contexts, String key);
|
||||
|
||||
ImmutableContextSet getActiveContextSet();
|
||||
|
||||
default void performCleanup() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||
|
||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* LuckPerms model for the Sponge SubjectCollection
|
||||
*/
|
||||
public interface LPSubjectCollection {
|
||||
|
||||
SubjectCollection sponge();
|
||||
|
||||
LPPermissionService getService();
|
||||
|
||||
String getIdentifier();
|
||||
|
||||
default SubjectReference newSubjectReference(String identifier) {
|
||||
return getService().newSubjectReference(getIdentifier(), identifier);
|
||||
}
|
||||
|
||||
Predicate<String> getIdentifierValidityPredicate();
|
||||
|
||||
CompletableFuture<LPSubject> loadSubject(String identifier);
|
||||
|
||||
Optional<LPSubject> getSubject(String identifier);
|
||||
|
||||
CompletableFuture<Boolean> hasRegistered(String identifier);
|
||||
|
||||
CompletableFuture<ImmutableCollection<LPSubject>> loadSubjects(Set<String> identifiers);
|
||||
|
||||
ImmutableCollection<LPSubject> getLoadedSubjects();
|
||||
|
||||
CompletableFuture<ImmutableSet<String>> getAllIdentifiers();
|
||||
|
||||
CompletableFuture<ImmutableMap<SubjectReference, Boolean>> getAllWithPermission(String permission);
|
||||
|
||||
CompletableFuture<ImmutableMap<SubjectReference, Boolean>> getAllWithPermission(ImmutableContextSet contexts, String permission);
|
||||
|
||||
ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(String permission);
|
||||
|
||||
ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(ImmutableContextSet contexts, String permission);
|
||||
|
||||
LPSubject getDefaults();
|
||||
|
||||
void suggestUnload(String identifier);
|
||||
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* LuckPerms model for the Sponge SubjectData
|
||||
*/
|
||||
public interface LPSubjectData {
|
||||
|
||||
LPSubject getParentSubject();
|
||||
|
||||
/* permissions */
|
||||
|
||||
ImmutableMap<ImmutableContextSet, ImmutableMap<String, Boolean>> getAllPermissions();
|
||||
|
||||
default ImmutableMap<String, Boolean> getPermissions(ImmutableContextSet contexts) {
|
||||
return ImmutableMap.copyOf(getAllPermissions().getOrDefault(contexts, ImmutableMap.of()));
|
||||
}
|
||||
|
||||
CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate value);
|
||||
|
||||
CompletableFuture<Boolean> clearPermissions();
|
||||
|
||||
CompletableFuture<Boolean> clearPermissions(ImmutableContextSet contexts);
|
||||
|
||||
/* parents */
|
||||
|
||||
ImmutableMap<ImmutableContextSet, ImmutableList<SubjectReference>> getAllParents();
|
||||
|
||||
default ImmutableList<SubjectReference> getParents(ImmutableContextSet contexts) {
|
||||
return ImmutableList.copyOf(getAllParents().getOrDefault(contexts, ImmutableList.of()));
|
||||
}
|
||||
|
||||
CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, SubjectReference parent);
|
||||
|
||||
CompletableFuture<Boolean> removeParent(ImmutableContextSet contexts, SubjectReference parent);
|
||||
|
||||
CompletableFuture<Boolean> clearParents();
|
||||
|
||||
CompletableFuture<Boolean> clearParents(ImmutableContextSet contexts);
|
||||
|
||||
/* options */
|
||||
|
||||
ImmutableMap<ImmutableContextSet, ImmutableMap<String, String>> getAllOptions();
|
||||
|
||||
default ImmutableMap<String, String> getOptions(ImmutableContextSet contexts) {
|
||||
return ImmutableMap.copyOf(getAllOptions().getOrDefault(contexts, ImmutableMap.of()));
|
||||
}
|
||||
|
||||
CompletableFuture<Boolean> setOption(ImmutableContextSet contexts, String key, String value);
|
||||
|
||||
CompletableFuture<Boolean> unsetOption(ImmutableContextSet contexts, String key);
|
||||
|
||||
CompletableFuture<Boolean> clearOptions();
|
||||
|
||||
CompletableFuture<Boolean> clearOptions(ImmutableContextSet contexts);
|
||||
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* 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.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ToString(of = {"collectionIdentifier", "subjectIdentifier"})
|
||||
@EqualsAndHashCode(of = {"collectionIdentifier", "subjectIdentifier"})
|
||||
@RequiredArgsConstructor(staticName = "of")
|
||||
public final class SubjectReference implements org.spongepowered.api.service.permission.SubjectReference {
|
||||
|
||||
@Deprecated
|
||||
public static SubjectReference deserialize(LPPermissionService service, String s) {
|
||||
List<String> parts = Splitter.on('/').limit(2).splitToList(s);
|
||||
return of(service, parts.get(0), parts.get(1));
|
||||
}
|
||||
|
||||
public static SubjectReference of(LPPermissionService service, Subject subject) {
|
||||
return of(service, subject.getContainingCollection().getIdentifier(), subject.getIdentifier());
|
||||
}
|
||||
|
||||
public static SubjectReference cast(LPPermissionService service, org.spongepowered.api.service.permission.SubjectReference reference) {
|
||||
if (reference instanceof SubjectReference) {
|
||||
return ((SubjectReference) reference);
|
||||
} else {
|
||||
return of(service, reference.getCollectionIdentifier(), reference.getSubjectIdentifier());
|
||||
}
|
||||
}
|
||||
|
||||
private final LPPermissionService service;
|
||||
|
||||
@Getter
|
||||
private final String collectionIdentifier;
|
||||
|
||||
@Getter
|
||||
private final String subjectIdentifier;
|
||||
|
||||
private long lastLookup = 0L;
|
||||
private WeakReference<LPSubject> cache = null;
|
||||
|
||||
private synchronized LPSubject resolveDirectly() {
|
||||
long sinceLast = System.currentTimeMillis() - lastLookup;
|
||||
|
||||
// try the cache
|
||||
if (sinceLast < TimeUnit.SECONDS.toMillis(10)) {
|
||||
if (cache != null) {
|
||||
LPSubject s = cache.get();
|
||||
if (s != null) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LPSubject s = service.getCollection(collectionIdentifier).loadSubject(subjectIdentifier).join();
|
||||
lastLookup = System.currentTimeMillis();
|
||||
cache = new WeakReference<>(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
public CompletableFuture<LPSubject> resolveLp() {
|
||||
long sinceLast = System.currentTimeMillis() - lastLookup;
|
||||
|
||||
// try the cache
|
||||
if (sinceLast < TimeUnit.SECONDS.toMillis(10)) {
|
||||
if (cache != null) {
|
||||
LPSubject s = cache.get();
|
||||
if (s != null) {
|
||||
return CompletableFuture.completedFuture(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CompletableFuture.supplyAsync(this::resolveDirectly);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Subject> resolve() {
|
||||
long sinceLast = System.currentTimeMillis() - lastLookup;
|
||||
|
||||
// try the cache
|
||||
if (sinceLast < TimeUnit.SECONDS.toMillis(10)) {
|
||||
if (cache != null) {
|
||||
LPSubject s = cache.get();
|
||||
if (s != null) {
|
||||
return CompletableFuture.completedFuture(s.sponge());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CompletableFuture.supplyAsync(() -> resolveDirectly().sponge());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user