All node should have a name

This commit is contained in:
TheMode 2021-06-04 04:10:13 +02:00
parent d81df24132
commit c4d521cde5
6 changed files with 35 additions and 35 deletions

View File

@ -125,7 +125,7 @@ public final class MinecraftServer {
private static ExtensionManager extensionManager;
private static final EventNode<Event> GLOBAL_EVENT_NODE = EventNode.all();
private static final EventNode<Event> GLOBAL_EVENT_NODE = EventNode.all("global");
private static final GlobalEventHandler GLOBAL_EVENT_HANDLER = new GlobalEventHandler();
private static UpdateManager updateManager;

View File

@ -118,7 +118,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
private long lastAbsoluteSynchronizationTime;
// Events
private final EventNode<EntityEvent> eventNode = EventNode.type(EventFilter.ENTITY);
private final EventNode<EntityEvent> eventNode;
protected Metadata metadata = new Metadata(this);
protected EntityMeta entityMeta;
@ -156,6 +156,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
Entity.ENTITY_BY_ID.put(id, this);
Entity.ENTITY_BY_UUID.put(uuid, this);
this.eventNode = EventNode.type("entity-" + uuid, EventFilter.ENTITY);
MinecraftServer.getGlobalEventNode().map(this, eventNode);
}

View File

@ -18,48 +18,54 @@ import java.util.function.Predicate;
public class EventNode<T extends Event> {
public static EventNode<Event> all() {
return type(EventFilter.ALL);
public static EventNode<Event> all(@NotNull String name) {
return type(name, EventFilter.ALL);
}
public static <E extends Event, V> EventNode<E> type(@NotNull EventFilter<E, V> filter,
public static <E extends Event, V> EventNode<E> type(@NotNull String name,
@NotNull EventFilter<E, V> filter,
@NotNull BiPredicate<E, V> predicate) {
return new EventNode<>(filter, (e, o) -> predicate.test(e, (V) o));
return new EventNode<>(name, filter, (e, o) -> predicate.test(e, (V) o));
}
public static <E extends Event, V> EventNode<E> type(@NotNull EventFilter<E, V> filter) {
return type(filter, (e, v) -> true);
public static <E extends Event, V> EventNode<E> type(@NotNull String name,
@NotNull EventFilter<E, V> filter) {
return type(name, filter, (e, v) -> true);
}
public static <E extends Event, V> EventNode<E> event(@NotNull EventFilter<E, V> filter,
public static <E extends Event, V> EventNode<E> event(@NotNull String name,
@NotNull EventFilter<E, V> filter,
@NotNull Predicate<E> predicate) {
return type(filter, (e, h) -> predicate.test(e));
return type(name, filter, (e, h) -> predicate.test(e));
}
public static <E extends Event, V> EventNode<E> value(@NotNull EventFilter<E, V> filter,
public static <E extends Event, V> EventNode<E> value(@NotNull String name,
@NotNull EventFilter<E, V> filter,
@NotNull Predicate<V> predicate) {
return type(filter, (e, h) -> predicate.test(h));
return type(name, filter, (e, h) -> predicate.test(h));
}
public static <E extends Event> EventNode<E> tag(@NotNull EventFilter<E, ? extends TagReadable> filter,
public static <E extends Event> EventNode<E> tag(@NotNull String name,
@NotNull EventFilter<E, ? extends TagReadable> filter,
@NotNull Tag<?> tag) {
return type(filter, (e, h) -> h.hasTag(tag));
return type(name, filter, (e, h) -> h.hasTag(tag));
}
public static <E extends Event, V> EventNode<E> tag(@NotNull EventFilter<E, ? extends TagReadable> filter,
public static <E extends Event, V> EventNode<E> tag(@NotNull String name,
@NotNull EventFilter<E, ? extends TagReadable> filter,
@NotNull Tag<V> tag,
@NotNull Predicate<@Nullable V> consumer) {
return type(filter, (e, h) -> consumer.test(h.getTag(tag)));
return type(name, filter, (e, h) -> consumer.test(h.getTag(tag)));
}
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
private final Map<Object, EventNode<T>> redirectionMap = new ConcurrentHashMap<>();
private final Set<EventNode<T>> children = new CopyOnWriteArraySet<>();
protected final String name;
protected final EventFilter<T, ?> filter;
protected final BiPredicate<T, Object> predicate;
protected final Class<T> eventType;
private volatile String name = "unknown";
// Tree data
private static final Object GLOBAL_CHILD_LOCK = new Object();
@ -67,7 +73,8 @@ public class EventNode<T extends Event> {
private final Object lock = new Object();
private final Object2IntMap<Class<? extends T>> childEventMap = new Object2IntOpenHashMap<>();
protected EventNode(EventFilter<T, ?> filter, BiPredicate<T, Object> predicate) {
protected EventNode(String name, EventFilter<T, ?> filter, BiPredicate<T, Object> predicate) {
this.name = name;
this.filter = filter;
this.predicate = predicate;
this.eventType = filter.getEventType();
@ -230,11 +237,6 @@ public class EventNode<T extends Event> {
return name;
}
public EventNode<T> setName(@NotNull String name) {
this.name = name;
return this;
}
private void increaseListenerCount(Class<? extends T> eventClass, int count) {
final int current = childEventMap.getInt(eventClass);
final int result = current + count;

View File

@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
*/
public final class GlobalEventHandler implements EventHandler<Event> {
private final EventNode<Event> node = EventNode.all();
private final EventNode<Event> node = EventNode.all("global-handler");
{
MinecraftServer.getGlobalEventNode().addChild(node);

View File

@ -80,7 +80,7 @@ public abstract class Instance implements BlockModifier, Tickable, EventHandler<
// Field for tick events
private long lastTickAge = System.currentTimeMillis();
private final EventNode<InstanceEvent> eventNode = EventNode.type(EventFilter.INSTANCE);
private final EventNode<InstanceEvent> eventNode;
// Entities present in this instance
protected final Set<Entity> entities = ConcurrentHashMap.newKeySet();
@ -119,6 +119,8 @@ public abstract class Instance implements BlockModifier, Tickable, EventHandler<
this.dimensionType = dimensionType;
this.worldBorder = new WorldBorder(this);
this.eventNode = EventNode.type("instance-" + uniqueId, EventFilter.INSTANCE);
MinecraftServer.getGlobalEventNode().map(this, eventNode);
}

View File

@ -138,25 +138,23 @@ public class PlayerInit {
// EVENT REGISTERING
var empty = EventNode.all()
.setName("empty")
var empty = EventNode.all("empty-demo")
.addListener(PlayerMoveEvent.class, (event) -> {
});
var node = EventNode.type(EventFilter.PLAYER)
.setName("node")
var node = EventNode.type("test1", EventFilter.PLAYER)
.addListener(EventListener.builder(PlayerTickEvent.class)
.handler(playerTickEvent -> System.out.println("Player tick!"))
.expirationCount(50)
.build());
var conditional = EventNode.value(EventFilter.PLAYER, Player::isCreative)
var conditional = EventNode.value("test2", EventFilter.PLAYER, Player::isCreative)
.addListener(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved"));
var tagNode = EventNode.tag(EventFilter.ITEM, Tag.String("tag"));
var tagNode = EventNode.tag("test-tag", EventFilter.ITEM, Tag.String("tag"));
node.addChild(conditional);
node.addChild(EventNode.value(EventFilter.PLAYER, player -> player.getUsername().equals("TheMode911"))
node.addChild(EventNode.value("test-builder", EventFilter.PLAYER, player -> player.getUsername().equals("TheMode911"))
.addListener(PlayerMoveEvent.class, event -> System.out.println("move!"))
.addListener(PlayerTickEvent.class, event -> System.out.println("tick!")));
@ -184,9 +182,6 @@ public class PlayerInit {
if (source instanceof Player) {
((Player) source).sendMessage("You attacked something!");
}
empty.map(source, EventNode.type(EventFilter.ENTITY)
.addListener(PlayerMoveEvent.class, (e) ->
System.out.println("Test movement")));
});
globalEventHandler.addEventCallback(PlayerDeathEvent.class, event -> {