Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Allow event node handle to be garbage collected (Minestom#1835)
Browse files Browse the repository at this point in the history
(cherry picked from commit 79ce957)
  • Loading branch information
iam4722202468 authored and mworzala committed Apr 29, 2023
1 parent a744063 commit b7690d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/java/net/minestom/server/event/EventNodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.ref.WeakReference;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -445,22 +446,24 @@ void invalidate() {
final var mappedNodeCache = node.registeredMappedNode;
if (mappedNodeCache.isEmpty()) return null;
Set<EventFilter<E, ?>> filters = new HashSet<>(mappedNodeCache.size());
Map<Object, Handle<E>> handlers = new WeakHashMap<>(mappedNodeCache.size());
Map<Object, WeakReference<Handle<E>>> handlers = new WeakHashMap<>(mappedNodeCache.size());

// Retrieve all filters used to retrieve potential handlers
for (var mappedEntry : mappedNodeCache.entrySet()) {
final EventNodeImpl<E> mappedNode = mappedEntry.getValue();
final Handle<E> handle = (Handle<E>) mappedNode.getHandle(eventType);
if (!handle.hasListener()) continue; // Implicit update
filters.add(mappedNode.filter);
handlers.put(mappedEntry.getKey(), handle);
handlers.put(mappedEntry.getKey(), new WeakReference<>(handle));
}
// If at least one mapped node listen to this handle type,
// loop through them and forward to mapped node if there is a match
if (filters.isEmpty()) return null;
final EventFilter<E, ?>[] filterList = filters.toArray(EventFilter[]::new);
final BiConsumer<EventFilter<E, ?>, E> mapper = (filter, event) -> {
final Object handler = filter.castHandler(event);
final Handle<E> handle = handlers.get(handler);
final WeakReference<Handle<E>> handleRef = handlers.get(handler);
final Handle<E> handle = handleRef != null ? handleRef.get() : null;
if (handle != null) handle.call(event);
};
// Specialize the consumer depending on the number of filters to avoid looping
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package net.minestom.server.instance;

import net.minestom.server.coordinate.Pos;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.player.PlayerMoveEvent;
import net.minestom.server.event.player.PlayerTickEvent;
import net.minestom.server.world.DimensionType;
import net.minestom.testing.Env;
import net.minestom.testing.EnvTest;
import org.junit.jupiter.api.Test;

import java.lang.ref.WeakReference;
import java.util.UUID;

import static net.minestom.testing.TestUtils.waitUntilCleared;

Expand Down Expand Up @@ -80,4 +83,23 @@ public void chunkGC(Env env) {
chunk = null;
waitUntilCleared(ref);
}

@Test
public void testGCWithEventsLambda(Env env) {
var ref = new WeakReference<>(new InstanceContainer(UUID.randomUUID(), DimensionType.OVERWORLD));
env.process().instance().registerInstance(ref.get());

tmp(ref.get());

ref.get().tick(0);
env.process().instance().unregisterInstance(ref.get());

waitUntilCleared(ref);
}

private void tmp(InstanceContainer instanceContainer) {
instanceContainer.eventNode().addListener(InstanceTickEvent.class, (e) -> {
var uuid = instanceContainer.getUniqueId();
});
}
}

0 comments on commit b7690d1

Please sign in to comment.