Search in sources :

Example 1 with ObserverAction

use of com.abilityapi.sequenceapi.action.type.observe.ObserverAction in project guardian by ichorpowered.

the class GuardianSequenceBuilder method build.

@Override
public final SequenceBlueprint<Event> build(final SequenceContext buildContext) {
    return new SequenceBlueprint<Event>() {

        @Override
        public final Sequence<Event> create(final Event rootEvent, final SequenceContext createContext) {
            final SequenceContext modifiedContext = SequenceContext.from(createContext).custom(CommonContextKeys.TRIGGER_CLASS, this.getTrigger()).custom(CommonContextKeys.TRIGGER_INSTANCE, rootEvent).merge(buildContext).build();
            final GuardianCaptureRegistry captureRegistry = new GuardianCaptureRegistry(modifiedContext.get(CommonContextKeys.ENTITY_ENTRY));
            GuardianSequenceBuilder.this.captures.forEach(capture -> captureRegistry.put(modifiedContext.getId(), capture.getClass(), capture));
            return new GuardianSequence(rootEvent, modifiedContext, this, captureRegistry, GuardianSequenceBuilder.this.actions);
        }

        @Override
        public final Class<? extends Event> getTrigger() {
            if (GuardianSequenceBuilder.this.actions.isEmpty())
                throw new NoSuchElementException("Sequence could not be established without an initial observer action.");
            if (GuardianSequenceBuilder.this.actions.get(0) instanceof ObserverAction) {
                return ((ObserverAction<Event>) GuardianSequenceBuilder.this.actions.get(0)).getEventClass();
            } else
                throw new ClassCastException("Sequence could not be established without an initial observer action.");
        }

        @Override
        public SequenceContext getContext() {
            return buildContext;
        }

        @Override
        public int hashCode() {
            return Objects.hash(buildContext.getOwner(), buildContext.getRoot(), getTrigger());
        }

        @Override
        public boolean equals(final Object other) {
            if (this == other)
                return true;
            if (other == null || !(other instanceof SequenceBlueprint<?>))
                return false;
            final SequenceBlueprint<?> that = (SequenceBlueprint<?>) other;
            return Objects.equals(buildContext.getOwner(), that.getContext().getOwner()) && Objects.equals(buildContext.getRoot(), that.getContext().getRoot()) && Objects.equals(getTrigger(), that.getTrigger());
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this).add("owner", buildContext.getOwner()).add("root", buildContext.getRoot()).add("trigger", getTrigger()).toString();
        }
    };
}
Also used : Event(org.spongepowered.api.event.Event) NoSuchElementException(java.util.NoSuchElementException) ObserverAction(com.abilityapi.sequenceapi.action.type.observe.ObserverAction) SequenceBlueprint(com.abilityapi.sequenceapi.SequenceBlueprint) SequenceContext(com.abilityapi.sequenceapi.SequenceContext) GuardianCaptureRegistry(com.ichorpowered.guardian.sequence.capture.GuardianCaptureRegistry)

Aggregations

SequenceBlueprint (com.abilityapi.sequenceapi.SequenceBlueprint)1 SequenceContext (com.abilityapi.sequenceapi.SequenceContext)1 ObserverAction (com.abilityapi.sequenceapi.action.type.observe.ObserverAction)1 GuardianCaptureRegistry (com.ichorpowered.guardian.sequence.capture.GuardianCaptureRegistry)1 NoSuchElementException (java.util.NoSuchElementException)1 Event (org.spongepowered.api.event.Event)1