Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Permissions API #229

Draft
wants to merge 59 commits into
base: dev/v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
11e0492
start permissions api
MrIvanPlays Nov 22, 2022
2dba238
more foundation
MrIvanPlays Nov 22, 2022
6fde59f
allNodesWithContext
MrIvanPlays Nov 22, 2022
3cea14e
add weight
MrIvanPlays Nov 22, 2022
5856160
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 23, 2022
4ca5ce8
progress
MrIvanPlays Nov 23, 2022
a12c1b3
bring some consistency in NodeFactory
MrIvanPlays Nov 23, 2022
2978084
retrieveHeldContexts
MrIvanPlays Nov 23, 2022
94defc8
simplify space check
MrIvanPlays Nov 24, 2022
4cbbc25
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 27, 2022
73f0695
Merge branch 'new/permissions-api' of https://github.com/ArcanePlugin…
MrIvanPlays Nov 27, 2022
e698aeb
add removeNode
MrIvanPlays Nov 27, 2022
4afceec
allNodesKeys
MrIvanPlays Nov 27, 2022
c62b477
parentNodeHolder
MrIvanPlays Nov 27, 2022
bb411e9
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 27, 2022
9aee46c
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 28, 2022
7779ac5
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 28, 2022
5b399c6
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 28, 2022
4082e78
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 29, 2022
4abc1ed
add missing annotation so that github actions builds
MrIvanPlays Nov 29, 2022
5fcbdd7
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 29, 2022
16eb615
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 29, 2022
06b8cc2
NodeHolderGroup
MrIvanPlays Nov 29, 2022
49f8120
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Nov 30, 2022
c102ce0
require context to be specified for retrieve and insertOrModify
MrIvanPlays Nov 30, 2022
ddef41d
also for removeNode
MrIvanPlays Nov 30, 2022
213884c
remove hasNode(Node<?>)
MrIvanPlays Nov 30, 2022
4d47ce8
group methods
MrIvanPlays Nov 30, 2022
46e594a
do not hardcode contexts and types
MrIvanPlays Nov 30, 2022
a966915
fix node factory
MrIvanPlays Nov 30, 2022
5b13b61
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Dec 1, 2022
cee38b7
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Dec 5, 2022
3726e34
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Dec 11, 2022
067f29b
remove ContextSet#registerContext and document it
MrIvanPlays Dec 11, 2022
2d79a88
remove "hasNode"
MrIvanPlays Dec 11, 2022
adb9c9a
make ContextSet iterable
MrIvanPlays Dec 11, 2022
98269eb
specify nullability
MrIvanPlays Dec 11, 2022
3891a02
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Dec 26, 2022
3a7eb23
basic events
MrIvanPlays Dec 26, 2022
68e90f3
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Jan 7, 2023
eb81eae
use namespacedkey here too
MrIvanPlays Jan 7, 2023
dac47d7
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Feb 2, 2023
f37c380
some small changes
MrIvanPlays Feb 2, 2023
235db89
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Feb 16, 2023
82bb05e
change weight to primitive value
MrIvanPlays Feb 16, 2023
7159a0f
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Apr 5, 2023
789878c
Update with dev/2.0.0
MrIvanPlays Apr 5, 2023
f633b43
rework nodes
MrIvanPlays Apr 9, 2023
2c870d2
some changes
MrIvanPlays Apr 10, 2023
49ce899
forgot annos, whoops
MrIvanPlays Apr 10, 2023
46ef940
Merge branch 'dev/2.0.0' of https://github.com/ArcanePlugins/Treasury…
MrIvanPlays Apr 26, 2023
4bd2284
Transform NodeEvent
MrIvanPlays Apr 26, 2023
afe4506
Update constructors of delegating events
MrIvanPlays Apr 26, 2023
3deb463
Merge branch 'dev/v2' of https://github.com/ArcanePlugins/Treasury in…
MrIvanPlays Jul 2, 2023
8817874
Merge branch 'dev/v2' of https://github.com/ArcanePlugins/Treasury in…
MrIvanPlays Jul 6, 2023
c0540a4
Write some javadocs
MrIvanPlays Jul 6, 2023
e235362
NotNull
MrIvanPlays Jul 6, 2023
516f31d
Merge branch 'dev/v2' of https://github.com/ArcanePlugins/Treasury in…
MrIvanPlays Jul 11, 2023
9c2d517
Document Node.Builder
MrIvanPlays Jul 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is/was part of Treasury. To read more information about Treasury such as its licensing, see <https://github.com/ArcanePlugins/Treasury>.
*/

package me.lokka30.treasury.api.permission;

import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import me.lokka30.treasury.api.common.NamespacedKey;
import me.lokka30.treasury.api.permission.node.holder.NodeHolderGroup;
import me.lokka30.treasury.api.permission.node.holder.NodeHolderPlayer;
import org.jetbrains.annotations.NotNull;

public interface PermissionsProvider {

@NotNull CompletableFuture<NodeHolderPlayer> retrievePlayerNodeHolder(@NotNull UUID uuid);

@NotNull CompletableFuture<Collection<UUID>> allPlayerNodeHolderIds();

@NotNull CompletableFuture<NodeHolderGroup> retrieveOrCreateGroup(@NotNull NamespacedKey groupId);

@NotNull CompletableFuture<Collection<NamespacedKey>> allGroupNodeHolderIds();

@NotNull NodeHolderGroup defaultGroup();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is/was part of Treasury. To read more information about Treasury such as its licensing, see <https://github.com/ArcanePlugins/Treasury>.
*/

package me.lokka30.treasury.api.permission.context;

import java.util.Objects;
import java.util.function.BiPredicate;
import me.lokka30.treasury.api.common.NamespacedKey;
import me.lokka30.treasury.api.permission.node.holder.NodeHolder;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
* Represents a context, in which a specific thing, a {@link me.lokka30.treasury.api.permission.node.Node} for example, applies.
*
* @author <a href="[email protected]">Ivan Pekov</a>
*/
public interface Context {

/**
* Returns the global context. If only this context is applied to a
* {@link me.lokka30.treasury.api.permission.node.Node} for example, that node will always
* apply without any other interference.
*/
Context GLOBAL = Context.of(NamespacedKey.of("treasury", "global"), ($, $1) -> true);

/**
* Creates a new context.
*
* @param key context key
* @param condition context condition
* @return new context
*/
@Contract(value = "_, _ -> new", pure = true)
@NotNull
static Context of(
@NotNull NamespacedKey key, @NotNull BiPredicate<NodeHolder, String> condition
) {
Objects.requireNonNull(key, "key");
Objects.requireNonNull(condition, "condition");
return new Context() {
@Override
public @NotNull NamespacedKey getKey() {
return key;
}

@Override
public @NotNull BiPredicate<NodeHolder, String> getCondition() {
return condition;
}
};
}

/**
* Returns the {@link NamespacedKey key} of this context.
*
* @return key
*/
@NotNull NamespacedKey getKey();

/**
* Returns the condition under which this context will apply.
*
* @return condition
*/
@NotNull BiPredicate<NodeHolder, String> getCondition();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* This file is/was part of Treasury. To read more information about Treasury such as its licensing, see <https://github.com/ArcanePlugins/Treasury>.
*/

package me.lokka30.treasury.api.permission.context;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import me.lokka30.treasury.api.common.NamespacedKey;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
* Represents a set of {@link Context contexts}, which can be applied for a
* {@link me.lokka30.treasury.api.permission.node.Node}.
* <p>Class is immutable.
*
* @author <a href="mailto:[email protected]">Ivan Pekov</a>
*/
public final class ContextSet implements Iterable<Context> {

private static final ContextSet GLOBAL = new ContextSet(Collections.singletonMap(
Context.GLOBAL.getKey(),
Context.GLOBAL
));
private static final ContextSet EMPTY = new ContextSet(Collections.emptyMap());

/**
* Returns the empty context set constant.
*
* @return empty context set
*/
@NotNull
public static ContextSet empty() {
return EMPTY;
}

/**
* Returns the context set constant, which has only 1 context, and that is the
* {@link Context#GLOBAL global} context.
*
* @return global context set
*/
@NotNull
public static ContextSet global() {
return GLOBAL;
}

/**
* Creates a new context set of the specified {@link Context contexts}
*
* @param contexts contexts the created context set will contain
* @return new context set
*/
@NotNull
public static ContextSet of(@NotNull Context @NotNull ... contexts) {
Objects.requireNonNull(contexts, "contexts");
if (contexts.length == 0) {
return empty();
}
Map<NamespacedKey, Context> map = new ConcurrentHashMap<>();
for (Context context : contexts) {
map.put(context.getKey(), context);
}
return new ContextSet(map);
}

private Map<NamespacedKey, Context> contexts;

@Contract(pure = true)
private ContextSet(Map<NamespacedKey, Context> contexts) {
this.contexts = contexts;
}

/**
* Returns a {@link Optional}, which will be fulfilled with a {@link Context} if this context
* set holds a context, identified by the specified {@code contextKey}.
*
* @param contextKey context key
* @return context if such is found, empty optional otherwise
*/
@NotNull
public Optional<Context> getContext(@NotNull NamespacedKey contextKey) {
Objects.requireNonNull(contextKey, "contextKey");
return Optional.ofNullable(this.contexts.get(contextKey));
}

/**
* Returns whether a {@link Context} is held under the specified {@code contextKey} in this
* context set.
*
* @param contextKey context key
* @return true if context is held, false otherwise
*/
public boolean hasContext(@NotNull NamespacedKey contextKey) {
Objects.requireNonNull(contextKey, "contextKey");
return contexts.containsKey(contextKey);
}

/**
* Returns this context set as a mutable {@link Set}. Modifying the returned set will not
* affect the context set object this method is called from.
*
* @return set copy representation of this context set
*/
@NotNull
public Set<Context> asSet() {
if (this.contexts.isEmpty()) {
return Collections.emptySet();
}
Set<Context> ret = ConcurrentHashMap.newKeySet();
ret.addAll(this.contexts.values());
return ret;
}

/**
* {@inheritDoc}
*/
@NotNull
@Override
public Iterator<Context> iterator() {
Iterator<Context> handle = this.contexts.values().iterator();
// Iterator#remove default impl throws a UnsupportedOperationException which suits our needs
// we don't return this.contexts.values().iterator() directly as it is likely to have a
// remove() impl
return new Iterator<Context>() {
@Override
public boolean hasNext() {
return handle.hasNext();
}

// NotNull contract because per Iterator javadoc, Iterator#next throws
// NoSuchElementException if !hasNext() = true
@NotNull
@Override
public Context next() {
return handle.next();
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is/was part of Treasury. To read more information about Treasury such as its licensing, see <https://github.com/ArcanePlugins/Treasury>.
*/

package me.lokka30.treasury.api.permission.events;

import me.lokka30.treasury.api.common.Cause;
import me.lokka30.treasury.api.permission.node.Node;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class NodeEvent {

private Result result;

public NodeEvent(@NotNull Result initialResult) {
this.result = initialResult;
}

@NotNull
public Node getNode() {
return result.getNode();
}

@NotNull
public Cause<?> getCause() {
return result.getCause();
}

@NotNull
public Result asResult() {
return this.result;
}

public void withNewResult(@NotNull Result result) {
this.result = result;
}

public static class Result {

@NotNull
@Contract(value = "_, _ -> new", pure = true)
public static Result of(@NotNull Node node, @NotNull Cause<?> cause) {
return new Result(node, cause);
}

private final Node node;
private final Cause<?> cause;

private Result(@NotNull Node node, @NotNull Cause<?> cause) {
this.node = node;
this.cause = cause;
}

@NotNull
public Node getNode() {
return this.node;
}

@NotNull
public Cause<?> getCause() {
return this.cause;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file is/was part of Treasury. To read more information about Treasury such as its licensing, see <https://github.com/ArcanePlugins/Treasury>.
*/

package me.lokka30.treasury.api.permission.events;

import me.lokka30.treasury.api.permission.node.holder.NodeHolder;
import org.jetbrains.annotations.NotNull;

public class NodeRemoveEvent extends NodeEvent {

private final NodeHolder nodeHolder;

public NodeRemoveEvent(
@NotNull NodeHolder nodeHolder, @NotNull NodeEvent.Result initialResult
) {
super(initialResult);
this.nodeHolder = nodeHolder;
}

@NotNull
public NodeHolder getNodeHolder() {
return this.nodeHolder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file is/was part of Treasury. To read more information about Treasury such as its licensing, see <https://github.com/ArcanePlugins/Treasury>.
*/

package me.lokka30.treasury.api.permission.events;

import me.lokka30.treasury.api.permission.node.holder.NodeHolder;
import org.jetbrains.annotations.NotNull;

public class NodeUpdateEvent extends NodeEvent {

private final NodeHolder nodeHolder;

public NodeUpdateEvent(
@NotNull NodeHolder nodeHolder, @NotNull NodeEvent.Result initialResult
) {
super(initialResult);
this.nodeHolder = nodeHolder;
}

@NotNull
public NodeHolder getNodeHolder() {
return nodeHolder;
}

}
Loading