Small Improvements

This commit is contained in:
Speiger 2021-11-06 23:46:36 +01:00
parent 8257209337
commit df67199fdd
3 changed files with 19 additions and 42 deletions

View File

@ -66,5 +66,5 @@ dependencies {
compile 'com.google.code.gson:gson:2.8.6' compile 'com.google.code.gson:gson:2.8.6'
//Primitive Collections //Primitive Collections
compile 'de.speiger:Primitive-Collections:0.4.4' compile 'de.speiger:Primitive-Collections:0.4.5'
} }

View File

@ -18,8 +18,8 @@ import speiger.src.collections.objects.utils.maps.Object2ObjectMaps;
public class EventBus public class EventBus
{ {
private static final Lookup LOOKUP = MethodHandles.lookup(); private static final Lookup LOOKUP = MethodHandles.lookup();
Object2ObjectMap<Class<? extends Event>, Listeners> listeners = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<Class<? extends Event>, Listeners>()); Object2ObjectMap<Class<? extends Event>, Listeners> listeners = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
Map<Object, List<EventListener>> instances = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<Object, List<EventListener>>()); Map<Object, List<EventListener>> instances = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
public <T extends Event> void register(Class<T> event, Consumer<T> listener) public <T extends Event> void register(Class<T> event, Consumer<T> listener)
{ {
@ -28,10 +28,7 @@ public class EventBus
public <T extends Event> void register(Class<T> event, EventPriority priority, Consumer<T> listener) public <T extends Event> void register(Class<T> event, EventPriority priority, Consumer<T> listener)
{ {
if(instances.containsKey(listener)) if(instances.containsKey(listener)) return;
{
return;
}
getListeners(event).addListener(priority, (Consumer<Event>)listener); getListeners(event).addListener(priority, (Consumer<Event>)listener);
instances.put(listener, ObjectLists.singleton(new EventListener(event, (Consumer<Event>)listener))); instances.put(listener, ObjectLists.singleton(new EventListener(event, (Consumer<Event>)listener)));
} }
@ -43,60 +40,40 @@ public class EventBus
public void register(Object obj, boolean superClasses) public void register(Object obj, boolean superClasses)
{ {
if(instances.containsKey(obj)) if(instances.containsKey(obj)) return;
{ final List<EventListener> list = new ObjectArrayList<>();
return;
}
final List<EventListener> list = new ObjectArrayList<EventListener>();
try try
{ {
register(obj.getClass().getDeclaredAnnotation(SubscribeEvent.class), obj, list); register(obj.getClass().getDeclaredAnnotation(SubscribeEvent.class), obj, list);
findMethods(obj.getClass(), superClasses, new Consumer<Method>() { findMethods(obj.getClass(), superClasses, new Consumer<Method>() {
@Override @Override
public void accept(Method t) public void accept(Method t) {
{
try try
{ {
t.setAccessible(true); t.setAccessible(true);
SubscribeEvent data = t.getAnnotation(SubscribeEvent.class); SubscribeEvent data = t.getAnnotation(SubscribeEvent.class);
if(data == null) if(data == null) return;
{
return;
}
Consumer<Event> listener = new MethodListener(LOOKUP.unreflect(t).bindTo(obj)); Consumer<Event> listener = new MethodListener(LOOKUP.unreflect(t).bindTo(obj));
getListeners(data.event()).addListener(data.priority(), listener); getListeners(data.value()).addListener(data.priority(), listener);
list.add(new EventListener(data.event(), listener)); list.add(new EventListener(data.value(), listener));
}
catch(Exception e)
{
e.printStackTrace();
} }
catch(Exception e) { e.printStackTrace(); }
} }
}); });
findFields(obj.getClass(), superClasses, new Consumer<Field>() { findFields(obj.getClass(), superClasses, new Consumer<Field>() {
@Override @Override
public void accept(Field t) public void accept(Field t) {
{
try try
{ {
t.setAccessible(true); t.setAccessible(true);
register(t.getAnnotation(SubscribeEvent.class), t.get(obj), list); register(t.getAnnotation(SubscribeEvent.class), t.get(obj), list);
} }
catch(Exception e) catch(Exception e) { e.printStackTrace(); }
{
e.printStackTrace();
}
} }
}); });
} }
catch(Exception e) catch(Exception e) { e.printStackTrace(); }
{ if(list.isEmpty()) return;
e.printStackTrace();
}
if(list.isEmpty())
{
return;
}
instances.put(obj, list); instances.put(obj, list);
} }
@ -106,8 +83,8 @@ public class EventBus
{ {
return; return;
} }
getListeners(data.event()).addListener(data.priority(), (Consumer<Event>)obj); getListeners(data.value()).addListener(data.priority(), (Consumer<Event>)obj);
listeners.add(new EventListener(data.event(), (Consumer<Event>)obj)); listeners.add(new EventListener(data.value(), (Consumer<Event>)obj));
} }
private void findFields(Class<?> clz, boolean superClasses, Consumer<Field> fields) private void findFields(Class<?> clz, boolean superClasses, Consumer<Field> fields)

View File

@ -14,5 +14,5 @@ public @interface SubscribeEvent
{ {
public EventPriority priority() default EventPriority.MEDIUM; public EventPriority priority() default EventPriority.MEDIUM;
public Class<? extends Event> event(); public Class<? extends Event> value();
} }