Skip to content

Commit

Permalink
move event methods to BaseHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 15, 2024
1 parent 102879e commit 2782bfd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 53 deletions.
50 changes: 41 additions & 9 deletions ui/src/main/java/me/hsgamer/hscore/ui/BaseHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,66 @@ public void update() {
}

@Override
public void stop() {
clearAllEventConsumer();
removeAllDisplay();
}

/**
* Add an event consumer
*
* @param eventClass the class of the event
* @param eventConsumer the consumer
* @param <T> the type of the event
*/
public <T> void addEventConsumer(@NotNull Class<T> eventClass, @NotNull Consumer<T> eventConsumer) {
classListMap
.computeIfAbsent(eventClass, clazz -> new ArrayList<>())
.add(o -> eventConsumer.accept(eventClass.cast(o)));
}

@Override
public void stop() {
clearAllEventConsumer();
removeAllDisplay();
}

@Override
/**
* Clear the event consumer
*
* @param eventClass the class of the event
*/
public void clearEventConsumer(@NotNull Class<?> eventClass) {
classListMap.remove(eventClass);
}

@Override
/**
* Clear all event consumers
*/
public void clearAllEventConsumer() {
classListMap.clear();
}

@Override
/**
* Handle the event
*
* @param eventClass the class of the event
* @param event the event
*/
public void handleEvent(@NotNull Class<?> eventClass, @NotNull Object event) {
if (!classListMap.containsKey(eventClass)) return;
if (!eventClass.isInstance(event)) {
throw new IllegalArgumentException("The event is not an instance of " + eventClass.getName());
}
classListMap.get(eventClass).forEach(consumer -> consumer.accept(event));
}

@Override
public <E> void handleEvent(@NotNull E event) {
Set<Class<?>> eventClassSet = new HashSet<>();
Queue<Class<?>> eventClassQueue = new LinkedList<>();
eventClassQueue.add(event.getClass());
while (true) {
Class<?> currentClass = eventClassQueue.poll();
if (currentClass == null) break;
if (!eventClassSet.add(currentClass)) continue;
handleEvent(currentClass, event);
Optional.ofNullable(currentClass.getSuperclass()).ifPresent(eventClassQueue::add);
eventClassQueue.addAll(Arrays.asList(currentClass.getInterfaces()));
}
}
}
47 changes: 3 additions & 44 deletions ui/src/main/java/me/hsgamer/hscore/ui/Holder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import me.hsgamer.hscore.ui.property.Updatable;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Consumer;
import java.util.Optional;
import java.util.UUID;

/**
* The holder for all displays
Expand Down Expand Up @@ -39,58 +39,17 @@ public interface Holder<D extends Display> extends Initializable, Updatable {
*/
Optional<@NotNull D> getDisplay(@NotNull UUID uuid);

/**
* Add an event consumer
*
* @param eventClass the event class
* @param eventConsumer the event consumer
* @param <T> the type of the event
*/
<T> void addEventConsumer(@NotNull Class<T> eventClass, @NotNull Consumer<T> eventConsumer);

/**
* Clear all event consumers
*
* @param eventClass the event class
*/
void clearEventConsumer(@NotNull Class<?> eventClass);

/**
* Clear all event consumers of all events
*/
void clearAllEventConsumer();

/**
* Remove all displays
*/
void removeAllDisplay();

/**
* Handle the event
*
* @param eventClass the event class
* @param event the event
*/
void handleEvent(@NotNull Class<?> eventClass, @NotNull Object event);

/**
* Handle the event
*
* @param event the event
*/
default <E> void handleEvent(@NotNull E event) {
Set<Class<?>> eventClassSet = new HashSet<>();
Queue<Class<?>> eventClassQueue = new LinkedList<>();
eventClassQueue.add(event.getClass());
while (true) {
Class<?> currentClass = eventClassQueue.poll();
if (currentClass == null) break;
if (!eventClassSet.add(currentClass)) continue;
handleEvent(currentClass, event);
Optional.ofNullable(currentClass.getSuperclass()).ifPresent(eventClassQueue::add);
eventClassQueue.addAll(Arrays.asList(currentClass.getInterfaces()));
}
}
<E> void handleEvent(@NotNull E event);

/**
* Update the display for the unique id
Expand Down

0 comments on commit 2782bfd

Please sign in to comment.