Search in sources :

Example 1 with KeyEventListener

use of org.lanternpowered.server.data.key.KeyEventListener in project LanternServer by LanternPowered.

the class LanternEventManager method post.

@SuppressWarnings({ "unchecked", "ConstantConditions" })
@Override
public boolean post(Event event) {
    checkNotNull(event, "event");
    final Class<? extends Event> eventClass = event.getClass();
    final EventType<? extends Event> eventType;
    if (event instanceof GenericEvent) {
        eventType = new EventType(eventClass, checkNotNull(((GenericEvent) event).getGenericType()));
    } else {
        eventType = new EventType(eventClass, null);
    }
    final List<RegisteredListener<?>> listeners = this.listenersCache.get(eventType);
    // Special case
    if (event instanceof AbstractValueChangeEvent) {
        final AbstractValueChangeEvent event1 = (AbstractValueChangeEvent) event;
        final TempDataEventData temp = new TempDataEventData();
        temp.lastResult = event1.getEndResult();
        return post(event, listeners, listener -> {
            if (listener.getHandler() instanceof KeyEventListener) {
                final KeyEventListener keyEventListener = (KeyEventListener) listener.getHandler();
                if (keyEventListener.getDataHolderPredicate().test(event1.getTargetHolder())) {
                    final DataTransactionResult newResult = event1.getEndResult();
                    // We need the keys, only regenerate if changed
                    if (temp.keys == null || temp.lastResult != newResult) {
                        // Ignore rejected data, nothing changed for those keys
                        if (temp.baseKeys == null) {
                            temp.baseKeys = new HashSet<>();
                            final DataTransactionResult original = event1.getOriginalChanges();
                            original.getSuccessfulData().forEach(value -> temp.baseKeys.add(value.getKey()));
                            original.getReplacedData().forEach(value -> temp.baseKeys.add(value.getKey()));
                        }
                        if (event1.getOriginalChanges() == newResult) {
                            temp.keys = temp.baseKeys;
                        } else {
                            temp.keys = new HashSet<>();
                            temp.keys.addAll(temp.baseKeys);
                            newResult.getSuccessfulData().forEach(value -> temp.keys.add(value.getKey()));
                            newResult.getReplacedData().forEach(value -> temp.keys.add(value.getKey()));
                        }
                    }
                    if (temp.keys.contains(keyEventListener.getKey())) {
                        listener.handle(event);
                    }
                }
            }
        });
    }
    return post(event, listeners);
}
Also used : GenericEvent(org.spongepowered.api.event.GenericEvent) AbstractValueChangeEvent(org.spongepowered.api.event.impl.AbstractValueChangeEvent) KeyEventListener(org.lanternpowered.server.data.key.KeyEventListener) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Aggregations

KeyEventListener (org.lanternpowered.server.data.key.KeyEventListener)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 GenericEvent (org.spongepowered.api.event.GenericEvent)1 AbstractValueChangeEvent (org.spongepowered.api.event.impl.AbstractValueChangeEvent)1