hollow-cube/multiple-event-node-parents

Signed-off-by: mworzala <mattheworzala@gmail.com>

add `minestom.event.multiple-parents`

(cherry picked from commit 26b79904a6fcbce2459d12ae8165d47dd2c8f1b3)
This commit is contained in:
mworzala 2023-03-18 18:00:33 +02:00
parent 207b658bcd
commit 560b5cb52d
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
2 changed files with 7 additions and 2 deletions

View File

@ -8,4 +8,6 @@ Some of these are pending, some deserve PRs, others are just minor tweaks
* Change `Entity#getInstance` to @UnknownNullability
* Support custom component translator for serverside translation
* **breaking** Replace permission system with a simple user pluggable alternative
* **breaking** Remove tinylog and MinestomTerminal implementation
* **breaking** Remove tinylog and MinestomTerminal implementation
* Add `Tag.Transient`
* Optionally allow multiple parents in event nodes

View File

@ -18,6 +18,8 @@ import java.util.function.BiPredicate;
import java.util.function.Consumer;
non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
private static final boolean ALLOW_MULTIPLE_PARENTS = Boolean.getBoolean("minestom.event.multiple-parents");
static final Object GLOBAL_CHILD_LOCK = new Object();
private final Map<Class, Handle<T>> handleMap = new ConcurrentHashMap<>();
@ -107,7 +109,7 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
public @NotNull EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
synchronized (GLOBAL_CHILD_LOCK) {
final var childImpl = (EventNodeImpl<? extends T>) child;
Check.stateCondition(childImpl.parent != null, "Node already has a parent");
Check.stateCondition(!ALLOW_MULTIPLE_PARENTS && childImpl.parent != null, "Node already has a parent");
Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent");
if (!children.add((EventNodeImpl<T>) childImpl)) return this; // Couldn't add the child (already present?)
childImpl.parent = this;
@ -218,6 +220,7 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
@Override
public @Nullable EventNode<? super T> getParent() {
Check.stateCondition(ALLOW_MULTIPLE_PARENTS, "Cannot use getParent when multiple parents are allowed");
return parent;
}