mirror of
https://github.com/lucko/LuckPerms.git
synced 2025-09-08 21:30:55 +02:00
Add node equality predicates, and provide way to determine hasPermission behaviour using them (#782)
This commit is contained in:
@@ -318,39 +318,81 @@ public interface Node extends Map.Entry<String, Boolean> {
|
||||
Map.Entry<Integer, String> getSuffix() throws IllegalStateException;
|
||||
|
||||
/**
|
||||
* Checks if this Node is equal to another node
|
||||
* Returns if this Node is equal to another node
|
||||
*
|
||||
* @param obj the other node
|
||||
* @return true if this node is equal to the other provided
|
||||
* @see #equalsIgnoringValue(Node) for a less strict implementation of this method
|
||||
* @see StandardNodeEquality#EXACT
|
||||
*/
|
||||
@Override
|
||||
boolean equals(Object obj);
|
||||
|
||||
/**
|
||||
* Similar to {@link Node#equals(Object)}, except doesn't take note of the value
|
||||
* Returns if this Node is equal to another node as defined by the given
|
||||
* {@link StandardNodeEquality} predicate.
|
||||
*
|
||||
* @param other the other node
|
||||
* @param equalityPredicate the predicate
|
||||
* @return true if this node is considered equal
|
||||
* @since 4.1
|
||||
*/
|
||||
boolean standardEquals(Node other, StandardNodeEquality equalityPredicate);
|
||||
|
||||
/**
|
||||
* Returns if this Node is equal to another node as defined by the given
|
||||
* {@link NodeEqualityPredicate}.
|
||||
*
|
||||
* @param other the other node
|
||||
* @param equalityPredicate the predicate
|
||||
* @return true if this node is considered equal
|
||||
* @since 4.1
|
||||
*/
|
||||
default boolean equals(Node other, NodeEqualityPredicate equalityPredicate) {
|
||||
return equalityPredicate.areEqual(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to {@link Node#equals(Object)}, except doesn't take note of the
|
||||
* value.
|
||||
*
|
||||
* @param other the other node
|
||||
* @return true if the two nodes are almost equal
|
||||
* @deprecated in favour of {@link #equals(Node, NodeEqualityPredicate)}
|
||||
* @see StandardNodeEquality#IGNORE_VALUE
|
||||
*/
|
||||
boolean equalsIgnoringValue(@Nonnull Node other);
|
||||
@Deprecated
|
||||
default boolean equalsIgnoringValue(@Nonnull Node other) {
|
||||
return equals(other, StandardNodeEquality.IGNORE_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to {@link Node#equals(Object)}, except doesn't take note of the expiry time or value
|
||||
* Similar to {@link Node#equals(Object)}, except doesn't take note of the
|
||||
* expiry time or value.
|
||||
*
|
||||
* @param other the other node
|
||||
* @return true if the two nodes are almost equal
|
||||
* @deprecated in favour of {@link #equals(Node, NodeEqualityPredicate)}
|
||||
* @see StandardNodeEquality#IGNORE_EXPIRY_TIME_AND_VALUE
|
||||
*/
|
||||
boolean almostEquals(@Nonnull Node other);
|
||||
@Deprecated
|
||||
default boolean almostEquals(@Nonnull Node other) {
|
||||
return equals(other, StandardNodeEquality.IGNORE_EXPIRY_TIME_AND_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to {@link Node#equals(Object)}, except doesn't take note of the value or if the node is temporary
|
||||
* Similar to {@link Node#equals(Object)}, except doesn't take note of the
|
||||
* value or if the node is temporary.
|
||||
*
|
||||
* @param other the other node
|
||||
* @return true if the two nodes are almost equal
|
||||
* @since 2.8
|
||||
* @deprecated in favour of {@link #equals(Node, NodeEqualityPredicate)}
|
||||
* @see StandardNodeEquality#IGNORE_VALUE_OR_IF_TEMPORARY
|
||||
*/
|
||||
boolean equalsIgnoringValueOrTemp(@Nonnull Node other);
|
||||
@Deprecated
|
||||
default boolean equalsIgnoringValueOrTemp(@Nonnull Node other) {
|
||||
return equals(other, StandardNodeEquality.IGNORE_VALUE_OR_IF_TEMPORARY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a Node instance
|
||||
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.api;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* A rule for determining if two nodes are equal.
|
||||
*
|
||||
* <p>Generally, individual instances of this interface should fulfil the same
|
||||
* requirements as the {@link Object#equals(Object)} contract.</p>
|
||||
*
|
||||
* <p>Some standard implementations are provided by
|
||||
* {@link StandardNodeEquality}.</p>
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface NodeEqualityPredicate {
|
||||
|
||||
/**
|
||||
* Returns if the two nodes are equal
|
||||
*
|
||||
* @param o1 the first node
|
||||
* @param o2 the second node
|
||||
* @return true if equal
|
||||
*/
|
||||
boolean areEqual(@Nonnull Node o1, @Nonnull Node o2);
|
||||
|
||||
}
|
@@ -271,6 +271,42 @@ public interface PermissionHolder {
|
||||
*/
|
||||
void auditTemporaryPermissions();
|
||||
|
||||
/**
|
||||
* Checks to see if the object has a certain permission
|
||||
*
|
||||
* @param node the node to check for
|
||||
* @param equalityPredicate how to determine if a node matches
|
||||
* @return a Tristate for the holders permission status for the node
|
||||
* @throws NullPointerException if the node is null
|
||||
* @since 4.1
|
||||
*/
|
||||
@Nonnull
|
||||
Tristate hasPermission(@Nonnull Node node, @Nonnull NodeEqualityPredicate equalityPredicate);
|
||||
|
||||
/**
|
||||
* Checks to see if the object has a certain permission
|
||||
*
|
||||
* @param node the node to check for
|
||||
* @param equalityPredicate how to determine if a node matches
|
||||
* @return a Tristate for the holders permission status for the node
|
||||
* @throws NullPointerException if the node is null
|
||||
* @since 4.1
|
||||
*/
|
||||
@Nonnull
|
||||
Tristate hasTransientPermission(@Nonnull Node node, @Nonnull NodeEqualityPredicate equalityPredicate);
|
||||
|
||||
/**
|
||||
* Checks to see if the object inherits a certain permission
|
||||
*
|
||||
* @param node the node to check for
|
||||
* @param equalityPredicate how to determine if a node matches
|
||||
* @return a Tristate for the holders inheritance status for the node
|
||||
* @throws NullPointerException if the node is null
|
||||
* @since 4.1
|
||||
*/
|
||||
@Nonnull
|
||||
Tristate inheritsPermission(@Nonnull Node node, @Nonnull NodeEqualityPredicate equalityPredicate);
|
||||
|
||||
/**
|
||||
* Checks to see if the object has a certain permission
|
||||
*
|
||||
|
@@ -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.api;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Standard {@link NodeEqualityPredicate}s.
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
public enum StandardNodeEquality implements NodeEqualityPredicate {
|
||||
|
||||
/**
|
||||
* Represents an exact match.
|
||||
*
|
||||
* <p>All attributes of the nodes must match for them to be considered
|
||||
* equal.</p>
|
||||
*/
|
||||
EXACT,
|
||||
|
||||
/**
|
||||
* All attributes must match, except for
|
||||
* {@link Node#getValuePrimitive() value}, which is ignored.
|
||||
*/
|
||||
IGNORE_VALUE,
|
||||
|
||||
/**
|
||||
* All attributes must match, except for the
|
||||
* {@link Node#getExpiry() expiry time}, which is ignored.
|
||||
*
|
||||
* <p>Note that with this setting, whether a node is temporary or not is
|
||||
* still considered.</p>
|
||||
*/
|
||||
IGNORE_EXPIRY_TIME,
|
||||
|
||||
/**
|
||||
* All attributes must match, except for
|
||||
* {@link Node#getValuePrimitive() value} and the
|
||||
* {@link Node#getExpiry() expiry time}, which are ignored.
|
||||
*/
|
||||
IGNORE_EXPIRY_TIME_AND_VALUE,
|
||||
|
||||
/**
|
||||
* All attributes must match, except for
|
||||
* {@link Node#getValuePrimitive() value} and the if the node is
|
||||
* {@link Node#isTemporary() temporary}, which are ignored.
|
||||
*/
|
||||
IGNORE_VALUE_OR_IF_TEMPORARY;
|
||||
|
||||
@Override
|
||||
public boolean areEqual(@Nonnull Node o1, @Nonnull Node o2) {
|
||||
return o1.standardEquals(o2, this);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user