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'
//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
{
private static final Lookup LOOKUP = MethodHandles.lookup();
Object2ObjectMap<Class<? extends Event>, Listeners> listeners = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<Class<? extends Event>, Listeners>());
Map<Object, List<EventListener>> instances = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<Object, List<EventListener>>());
Object2ObjectMap<Class<? extends Event>, Listeners> listeners = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
Map<Object, List<EventListener>> instances = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
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)
{
if(instances.containsKey(listener))
{
return;
}
if(instances.containsKey(listener)) return;
getListeners(event).addListener(priority, (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)
{
if(instances.containsKey(obj))
{
return;
}
final List<EventListener> list = new ObjectArrayList<EventListener>();
if(instances.containsKey(obj)) return;
final List<EventListener> list = new ObjectArrayList<>();
try
{
register(obj.getClass().getDeclaredAnnotation(SubscribeEvent.class), obj, list);
findMethods(obj.getClass(), superClasses, new Consumer<Method>() {
@Override
public void accept(Method t)
{
public void accept(Method t) {
try
{
t.setAccessible(true);
SubscribeEvent data = t.getAnnotation(SubscribeEvent.class);
if(data == null)
{
return;
}
if(data == null) return;
Consumer<Event> listener = new MethodListener(LOOKUP.unreflect(t).bindTo(obj));
getListeners(data.event()).addListener(data.priority(), listener);
list.add(new EventListener(data.event(), listener));
}
catch(Exception e)
{
e.printStackTrace();
getListeners(data.value()).addListener(data.priority(), listener);
list.add(new EventListener(data.value(), listener));
}
catch(Exception e) { e.printStackTrace(); }
}
});
findFields(obj.getClass(), superClasses, new Consumer<Field>() {
@Override
public void accept(Field t)
{
public void accept(Field t) {
try
{
t.setAccessible(true);
register(t.getAnnotation(SubscribeEvent.class), t.get(obj), list);
}
catch(Exception e)
{
e.printStackTrace();
}
catch(Exception e) { e.printStackTrace(); }
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
if(list.isEmpty())
{
return;
}
catch(Exception e) { e.printStackTrace(); }
if(list.isEmpty()) return;
instances.put(obj, list);
}
@ -106,8 +83,8 @@ public class EventBus
{
return;
}
getListeners(data.event()).addListener(data.priority(), (Consumer<Event>)obj);
listeners.add(new EventListener(data.event(), (Consumer<Event>)obj));
getListeners(data.value()).addListener(data.priority(), (Consumer<Event>)obj);
listeners.add(new EventListener(data.value(), (Consumer<Event>)obj));
}
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 Class<? extends Event> event();
public Class<? extends Event> value();
}