Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public interface CapabilityRegisteredCallback<C> {
void onCapabilityRegistered(Capability<C> capability);

static <C> Event<CapabilityRegisteredCallback<C>> event(Class<C> type) {
return CapabilityRegisteredCallbackInternal.getOrCreateEvent(type);
return CapabilityRegisteredCallbackInternal.getOrCreateEvent(type.getName());
}

static <C> Event<CapabilityRegisteredCallback<C>> event(String className) {
return CapabilityRegisteredCallbackInternal.getOrCreateEvent(className);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not confident we should allow arbitrary strings, but i guess in the long run it's really not that big of a deal

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package net.patchworkmc.impl.capability;

import java.util.IdentityHashMap;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
Expand All @@ -33,10 +33,10 @@
public class CapabilityRegisteredCallbackInternal {
private static final Logger LOGGER = LogManager.getLogger(CapabilityRegisteredCallback.class);

private static final Map<Class<?>, Event<?>> CALLBACKS = new IdentityHashMap<>();
private static final Map<String, Event<?>> CALLBACKS = new HashMap<>();

@SuppressWarnings("unchecked")
public static <C> Event<CapabilityRegisteredCallback<C>> getOrCreateEvent(Class<C> type) {
public static <C> Event<CapabilityRegisteredCallback<C>> getOrCreateEvent(String type) {
return (Event<CapabilityRegisteredCallback<C>>) CALLBACKS.computeIfAbsent(type, $ -> EventFactory.createArrayBacked(CapabilityRegisteredCallback.class, capability -> { }, callbacks -> capability -> {
boolean error = false;
Throwable throwable = new Throwable();
Expand All @@ -51,7 +51,7 @@ public static <C> Event<CapabilityRegisteredCallback<C>> getOrCreateEvent(Class<
}

if (error) {
LOGGER.error("An uncaught exception was thrown while processing a CapabilityRegisteredCallback<{}>", type.getName(), throwable);
LOGGER.error("An uncaught exception was thrown while processing a CapabilityRegisteredCallback<{}>", type, throwable);
}
}));
}
Expand Down