1
0
mirror of https://github.com/lucko/LuckPerms.git synced 2025-09-08 21:30:55 +02:00

Context & action log changes

This commit is contained in:
Luck
2019-08-11 22:42:01 +01:00
parent 5b97d01363
commit bafada4f17
93 changed files with 1109 additions and 1040 deletions

View File

@@ -30,6 +30,7 @@ import me.lucko.luckperms.api.LuckPermsProvider;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
@@ -48,50 +49,25 @@ public interface Action extends Comparable<Action> {
}
/**
* Gets the time in unix seconds when the action occurred.
* Gets the time when the action occurred.
*
* @return the timestamp
*/
long getTimestamp();
@NonNull Instant getTimestamp();
/**
* Gets the id of the object which performed the action.
* Gets the source of the action.
*
* <p>This is the players uuid in most cases.</p>
*
* @return the actor id
* @return the source
*/
@NonNull UUID getActor();
@NonNull Source getSource();
/**
* Gets the name describing the actor.
* Gets the target of the action.
*
* @return the name of the actor
* @return the target
*/
@NonNull String getActorName();
/**
* Gets the type of action.
*
* @return the action type
*/
@NonNull Type getType();
/**
* Gets the uuid of the object which was acted upon.
*
* <p>Will only return a value for {@link Type#USER} entries.</p>
*
* @return the uuid of acted object
*/
@NonNull Optional<UUID> getActed();
/**
* Gets the name describing the object which was acted upon
*
* @return the name of the acted object
*/
@NonNull String getActedName();
@NonNull Target getTarget();
/**
* Returns a string describing the action which took place.
@@ -101,71 +77,65 @@ public interface Action extends Comparable<Action> {
*
* @return the action
*/
@NonNull String getAction();
@NonNull String getDescription();
/**
* Represents the type of a {@link Action}.
* Represents the source of an action.
*/
enum Type {
USER('U'), GROUP('G'), TRACK('T');
interface Source {
/**
* Parses a {@link Type} from a string.
* Gets the source unique id.
*
* @param type the string
* @return a type
* @throws IllegalArgumentException if a type could not be parsed
* @return the source unique id
*/
public static @NonNull Type parse(String type) {
try {
return valueOf(type);
} catch (IllegalArgumentException e) {
// ignore
}
try {
return valueOf(type.charAt(0));
} catch (IllegalArgumentException e) {
// ignore
}
throw new IllegalArgumentException("Unknown type: " + type);
}
@NonNull UUID getUniqueId();
/**
* Returns a {@link Type} by its code.
* Gets the source name.
*
* @param code the code - see {@link Type#getCode()}.
* @return a type
* @throws IllegalArgumentException if a type could not be resolved
* @return the source name
*/
public static @NonNull Type valueOf(char code) {
switch (code) {
case 'U':
case 'u':
return USER;
case 'G':
case 'g':
return GROUP;
case 'T':
case 't':
return TRACK;
default:
throw new IllegalArgumentException("Unknown code: " + code);
}
}
@NonNull String getName();
private final char code;
}
Type(char code) {
this.code = code;
}
/**
* Represents the target of an action.
*/
interface Target {
public char getCode() {
return this.code;
/**
* Gets the target unique id.
*
* @return the target unique id
*/
@NonNull Optional<UUID> getUniqueId();
/**
* Gets the target name.
*
* @return the target name
*/
@NonNull String getName();
/**
* Gets the target type.
*
* @return the target type
*/
@NonNull Type getType();
/**
* Represents the type of a {@link Target}.
*/
enum Type {
USER, GROUP, TRACK
}
}
/**
* Builds a LogEntry instance
* Builds an {@link Action} instance
*/
interface Builder {
@@ -176,61 +146,55 @@ public interface Action extends Comparable<Action> {
* @return the builder
* @see Action#getTimestamp()
*/
@NonNull Builder timestamp(long timestamp);
@NonNull Builder timestamp(@NonNull Instant timestamp);
/**
* Sets the actor of the entry.
*
* @param actor the actor
* @return the builder
* @see Action#getActor()
*/
@NonNull Builder actor(@NonNull UUID actor);
@NonNull Builder source(@NonNull UUID actor);
/**
* Sets the actor name of the entry.
*
* @param actorName the actor name
* @return the builder
* @see Action#getActorName()
*/
@NonNull Builder actorName(@NonNull String actorName);
@NonNull Builder sourceName(@NonNull String actorName);
/**
* Sets the type of the entry.
*
* @param type the type
* @return the builder
* @see Action#getType()
*/
@NonNull Builder type(@NonNull Type type);
@NonNull Builder targetType(Action.Target.Type type);
/**
* Sets the acted object for the entry.
*
* @param acted the acted object
* @return the builder
* @see Action#getActed()
*/
@NonNull Builder acted(@Nullable UUID acted);
@NonNull Builder target(@Nullable UUID acted);
/**
* Sets the acted name for the entry.
*
* @param actedName the acted name
* @return the builder
* @see Action#getActedName()
*/
@NonNull Builder actedName(@NonNull String actedName);
@NonNull Builder targetName(@NonNull String actedName);
/**
* Sets the action of the entry.
*
* @param action the action
* @return the builder
* @see Action#getAction()
*/
@NonNull Builder action(@NonNull String action);
@NonNull Builder description(@NonNull String action);
/**
* Creates a {@link Action} instance from the builder.

View File

@@ -0,0 +1,49 @@
/*
* 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.context;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Represents an individual context pair.
*/
public interface Context {
/**
* Gets the context key.
*
* @return the key
*/
@NonNull String getKey();
/**
* Gets the context value
*
* @return the value
*/
@NonNull String getValue();
}

View File

@@ -92,6 +92,6 @@ public interface ContextCalculator<T> {
* @param target the target contextual subject for this operation
* @param consumer the {@link ContextConsumer} to submit contexts to
*/
void giveApplicableContext(@NonNull T target, @NonNull ContextConsumer consumer);
void calculate(@NonNull T target, @NonNull ContextConsumer consumer);
}

View File

@@ -27,8 +27,6 @@ package me.lucko.luckperms.api.context;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Map;
/**
* Functional interface that accepts context key value pairs.
*/
@@ -43,14 +41,23 @@ public interface ContextConsumer {
*/
void accept(@NonNull String key, @NonNull String value);
/**
* Accepts a context pair.
*
* @param context the context
*/
default void accept(@NonNull Context context) {
accept(context.getKey(), context.getValue());
}
/**
* Accepts a context set.
*
* @param contextSet the context set
*/
default void accept(@NonNull ContextSet contextSet) {
for (Map.Entry<String, String> entry : contextSet) {
accept(entry.getKey(), entry.getValue());
for (Context entry : contextSet) {
accept(entry);
}
}

View File

@@ -57,7 +57,7 @@ import java.util.Set;
* {@link MutableContextSet} allows the addition and removal of context keys
* after construction, and {@link ImmutableContextSet} does not.</p>
*/
public interface ContextSet extends Iterable<Map.Entry<String, String>> {
public interface ContextSet extends Iterable<Context> {
/**
* Gets if this {@link ContextSet} is immutable.
@@ -89,7 +89,7 @@ public interface ContextSet extends Iterable<Map.Entry<String, String>> {
@NonNull MutableContextSet mutableCopy();
/**
* Returns a {@link Set} of {@link Map.Entry}s representing the current
* Returns a {@link Set} of {@link Context}s representing the current
* state of this {@link ContextSet}.
*
* <p>The returned set is immutable, and is a copy of the current set.
@@ -97,7 +97,7 @@ public interface ContextSet extends Iterable<Map.Entry<String, String>> {
*
* @return an immutable set
*/
@NonNull Set<Map.Entry<String, String>> toSet();
@NonNull Set<Context> toSet();
/**
* Returns a {@link Map} representing the current state of this
@@ -137,7 +137,7 @@ public interface ContextSet extends Iterable<Map.Entry<String, String>> {
* @return an iterator
*/
@Override
@NonNull Iterator<Map.Entry<String, String>> iterator();
@NonNull Iterator<Context> iterator();
/**
* Returns if the {@link ContextSet} contains at least one value for the
@@ -191,7 +191,7 @@ public interface ContextSet extends Iterable<Map.Entry<String, String>> {
* @return true if the set contains the context pair
* @throws NullPointerException if the key or value is null
*/
default boolean contains(Map.@NonNull Entry<String, String> entry) {
default boolean contains(@NonNull Context entry) {
Objects.requireNonNull(entry, "entry");
return contains(entry.getKey(), entry.getValue());
}
@@ -207,31 +207,7 @@ public interface ContextSet extends Iterable<Map.Entry<String, String>> {
* @param other the other set to check
* @return true if all entries in this set are also in the other set
*/
default boolean isSatisfiedBy(@NonNull ContextSet other) {
if (this == other) {
return true;
}
Objects.requireNonNull(other, "other");
if (this.isEmpty()) {
// this is empty, so is therefore always satisfied.
return true;
} else if (other.isEmpty()) {
// this set isn't empty, but the other one is
return false;
} else if (this.size() > other.size()) {
// this set has more unique entries than the other set, so there's no way this can be satisfied.
return false;
} else {
// neither are empty, we need to compare the individual entries
for (Map.Entry<String, String> context : toSet()) {
if (!other.contains(context)) {
return false;
}
}
return true;
}
}
boolean isSatisfiedBy(@NonNull ContextSet other);
/**
* Returns if the {@link ContextSet} is empty.

View File

@@ -36,8 +36,6 @@ public interface ContextSetFactory {
@NonNull ImmutableContextSet immutableOf(@NonNull String key, @NonNull String value);
@NonNull ImmutableContextSet immutableOf(@NonNull String key1, @NonNull String value1, @NonNull String key2, @NonNull String value2);
@NonNull ImmutableContextSet immutableEmpty();
@NonNull MutableContextSet mutable();

View File

@@ -29,9 +29,7 @@ import me.lucko.luckperms.api.LuckPermsProvider;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* An immutable implementation of {@link ContextSet}.
@@ -47,6 +45,15 @@ public interface ImmutableContextSet extends ContextSet {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().immutableBuilder();
}
/**
* Returns an empty {@link ImmutableContextSet}.
*
* @return an empty ImmutableContextSet
*/
static @NonNull ImmutableContextSet empty() {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().immutableEmpty();
}
/**
* Creates an {@link ImmutableContextSet} from a context pair.
*
@@ -59,58 +66,6 @@ public interface ImmutableContextSet extends ContextSet {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().immutableOf(key, value);
}
/**
* Creates an {@link ImmutableContextSet} from two context pairs.
*
* @param key1 the first key
* @param value1 the first value
* @param key2 the second key
* @param value2 the second value
* @return a new ImmutableContextSet containing the two pairs
* @throws NullPointerException if any of the keys or values are null
*/
static @NonNull ImmutableContextSet of(@NonNull String key1, @NonNull String value1, @NonNull String key2, @NonNull String value2) {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().immutableOf(key1, value1, key2, value2);
}
/**
* Creates an {@link ImmutableContextSet} from an existing {@link Iterable} of {@link Map.Entry}s.
*
* @param iterable the iterable to copy from
* @return a new ImmutableContextSet representing the pairs in the iterable
* @throws NullPointerException if the iterable is null
*/
static @NonNull ImmutableContextSet fromEntries(@NonNull Iterable<? extends Map.Entry<String, String>> iterable) {
Objects.requireNonNull(iterable, "iterable");
Builder builder = builder();
for (Map.Entry<String, String> entry : iterable) {
builder.add(entry);
}
return builder.build();
}
/**
* Creates an new {@link ImmutableContextSet} from an existing {@link Set}.
*
* <p>Only really useful for converting between mutable and immutable types.</p>
*
* @param contextSet the context set to copy from
* @return a new ImmutableContextSet with the same content and the one provided
* @throws NullPointerException if contextSet is null
*/
static @NonNull ImmutableContextSet fromSet(@NonNull ContextSet contextSet) {
return Objects.requireNonNull(contextSet, "contextSet").immutableCopy();
}
/**
* Returns an empty {@link ImmutableContextSet}.
*
* @return an empty ImmutableContextSet
*/
static @NonNull ImmutableContextSet empty() {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().immutableEmpty();
}
/**
* @deprecated Already immutable!
*/
@@ -140,9 +95,9 @@ public interface ImmutableContextSet extends ContextSet {
* @param entry the entry to add
* @return the builder
* @throws NullPointerException if the entry is null
* @see MutableContextSet#add(Map.Entry)
* @see MutableContextSet#add(Context)
*/
default @NonNull Builder add(Map.@NonNull Entry<String, String> entry) {
default @NonNull Builder add(@NonNull Context entry) {
Objects.requireNonNull(entry, "entry");
add(entry.getKey(), entry.getValue());
return this;
@@ -156,8 +111,8 @@ public interface ImmutableContextSet extends ContextSet {
* @throws NullPointerException if iterable is null
* @see MutableContextSet#addAll(Iterable)
*/
default @NonNull Builder addAll(@NonNull Iterable<? extends Map.Entry<String, String>> iterable) {
for (Map.Entry<String, String> e : Objects.requireNonNull(iterable, "iterable")) {
default @NonNull Builder addAll(@NonNull Iterable<Context> iterable) {
for (Context e : Objects.requireNonNull(iterable, "iterable")) {
add(e);
}
return this;

View File

@@ -29,15 +29,22 @@ import me.lucko.luckperms.api.LuckPermsProvider;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* A mutable implementation of {@link ContextSet}.
*/
public interface MutableContextSet extends ContextSet {
/**
* Creates a new empty MutableContextSet.
*
* @return a new MutableContextSet
*/
static @NonNull MutableContextSet create() {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().mutable();
}
/**
* Creates a {@link MutableContextSet} from a context pair.
*
@@ -54,63 +61,6 @@ public interface MutableContextSet extends ContextSet {
return set;
}
/**
* Creates a {@link MutableContextSet} from two context pairs.
*
* @param key1 the first key
* @param value1 the first value
* @param key2 the second key
* @param value2 the second value
* @return a new MutableContextSet containing the two pairs
* @throws NullPointerException if any of the keys or values are null
*/
static @NonNull MutableContextSet of(@NonNull String key1, @NonNull String value1, @NonNull String key2, @NonNull String value2) {
Objects.requireNonNull(key1, "key1");
Objects.requireNonNull(value1, "value1");
Objects.requireNonNull(key2, "key2");
Objects.requireNonNull(value2, "value2");
MutableContextSet set = create();
set.add(key1, value1);
set.add(key2, value2);
return set;
}
/**
* Creates a {@link MutableContextSet} from an existing {@link Iterable} of {@link Map.Entry}s.
*
* @param iterable the iterable to copy from
* @return a new MutableContextSet representing the pairs in the iterable
* @throws NullPointerException if the iterable is null
*/
static @NonNull MutableContextSet fromEntries(@NonNull Iterable<? extends Map.Entry<String, String>> iterable) {
Objects.requireNonNull(iterable, "iterable");
MutableContextSet set = create();
set.addAll(iterable);
return set;
}
/**
* Creates a new {@link MutableContextSet} from an existing {@link Set}.
*
* <p>Only really useful for converting between mutable and immutable types.</p>
*
* @param contextSet the context set to copy from
* @return a new MutableContextSet with the same content and the one provided
* @throws NullPointerException if contextSet is null
*/
static @NonNull MutableContextSet fromSet(@NonNull ContextSet contextSet) {
return contextSet.mutableCopy();
}
/**
* Creates a new empty MutableContextSet.
*
* @return a new MutableContextSet
*/
static @NonNull MutableContextSet create() {
return LuckPermsProvider.get().getContextManager().getContextSetFactory().mutable();
}
/**
* Adds a context to this set.
*
@@ -126,7 +76,7 @@ public interface MutableContextSet extends ContextSet {
* @param entry the entry to add
* @throws NullPointerException if the entry is null
*/
default void add(Map.@NonNull Entry<String, String> entry) {
default void add(@NonNull Context entry) {
Objects.requireNonNull(entry, "entry");
add(entry.getKey(), entry.getValue());
}
@@ -137,8 +87,8 @@ public interface MutableContextSet extends ContextSet {
* @param iterable an iterable of key value context pairs
* @throws NullPointerException if iterable is null
*/
default void addAll(@NonNull Iterable<? extends Map.Entry<String, String>> iterable) {
for (Map.Entry<String, String> e : Objects.requireNonNull(iterable, "iterable")) {
default void addAll(@NonNull Iterable<Context> iterable) {
for (Context e : Objects.requireNonNull(iterable, "iterable")) {
add(e);
}
}

View File

@@ -66,10 +66,10 @@ public interface StaticContextCalculator extends ContextCalculator<Object> {
*
* @param consumer the {@link ContextConsumer} to submit contexts to
*/
void giveApplicableContext(@NonNull ContextConsumer consumer);
void calculate(@NonNull ContextConsumer consumer);
@Override
default void giveApplicableContext(@NonNull Object target, @NonNull ContextConsumer consumer) {
giveApplicableContext(consumer);
default void calculate(@NonNull Object target, @NonNull ContextConsumer consumer) {
calculate(consumer);
}
}