Search in sources :

Example 1 with PipeEventActionActivate

use of buildcraft.api.transport.pipe.PipeEventActionActivate in project BuildCraft by BuildCraft.

the class GateLogic method resolveActions.

public void resolveActions() {
    int groupCount = 0;
    int groupActive = 0;
    boolean[] prevTriggers = Arrays.copyOf(triggerOn, triggerOn.length);
    boolean[] prevActions = Arrays.copyOf(actionOn, actionOn.length);
    Arrays.fill(triggerOn, false);
    Arrays.fill(actionOn, false);
    activeActions.clear();
    EnumSet<EnumDyeColor> previousBroadcasts = EnumSet.copyOf(wireBroadcasts);
    wireBroadcasts.clear();
    for (int triggerIndex = 0; triggerIndex < statements.length; triggerIndex++) {
        StatementPair pair = statements[triggerIndex];
        TriggerWrapper trigger = pair.trigger.get();
        groupCount++;
        if (trigger != null) {
            IStatementParameter[] params = new IStatementParameter[pair.trigger.getParamCount()];
            for (int p = 0; p < pair.trigger.getParamCount(); p++) {
                params[p] = pair.trigger.getParamRef(p).get();
            }
            if (trigger.isTriggerActive(this, params)) {
                groupActive++;
                triggerOn[triggerIndex] = true;
            }
        }
        if (connections.length == triggerIndex || !connections[triggerIndex]) {
            boolean allActionsActive;
            if (variant.logic == EnumGateLogic.AND) {
                allActionsActive = groupActive == groupCount;
            } else {
                allActionsActive = groupActive > 0;
            }
            for (int i = groupCount - 1; i >= 0; i--) {
                int actionIndex = triggerIndex - i;
                StatementPair fullAction = statements[actionIndex];
                ActionWrapper action = fullAction.action.get();
                actionOn[actionIndex] = allActionsActive;
                if (action != null) {
                    if (allActionsActive) {
                        StatementSlot slot = new StatementSlot();
                        slot.statement = action.delegate;
                        slot.parameters = fullAction.action.getParameters().clone();
                        slot.part = action.sourcePart;
                        activeActions.add(slot);
                        action.actionActivate(this, slot.parameters);
                        PipeEvent evt = new PipeEventActionActivate(getPipeHolder(), action.getDelegate(), slot.parameters, action.sourcePart);
                        getPipeHolder().fireEvent(evt);
                    } else {
                        action.actionDeactivated(this, fullAction.action.getParameters());
                    }
                }
            }
            groupActive = 0;
            groupCount = 0;
        }
    }
    if (!previousBroadcasts.equals(wireBroadcasts)) {
        IWireManager wires = getPipeHolder().getWireManager();
        EnumSet<EnumDyeColor> turnedOff = EnumSet.copyOf(previousBroadcasts);
        turnedOff.removeAll(wireBroadcasts);
        // FIXME: add call to "wires.stopEmittingColour(turnedOff)"
        EnumSet<EnumDyeColor> turnedOn = EnumSet.copyOf(wireBroadcasts);
        turnedOn.removeAll(previousBroadcasts);
        if (!getPipeHolder().getPipeWorld().isRemote) {
            WorldSavedDataWireSystems.get(getPipeHolder().getPipeWorld()).gatesChanged = true;
        }
    }
    if (!Arrays.equals(prevTriggers, triggerOn) || !Arrays.equals(prevActions, actionOn)) {
        sendResolveData();
    }
}
Also used : PipeEventActionActivate(buildcraft.api.transport.pipe.PipeEventActionActivate) IStatementParameter(buildcraft.api.statements.IStatementParameter) EnumDyeColor(net.minecraft.item.EnumDyeColor) IWireManager(buildcraft.api.transport.IWireManager) StatementSlot(buildcraft.api.statements.StatementSlot) PipeEvent(buildcraft.api.transport.pipe.PipeEvent)

Aggregations

IStatementParameter (buildcraft.api.statements.IStatementParameter)1 StatementSlot (buildcraft.api.statements.StatementSlot)1 IWireManager (buildcraft.api.transport.IWireManager)1 PipeEvent (buildcraft.api.transport.pipe.PipeEvent)1 PipeEventActionActivate (buildcraft.api.transport.pipe.PipeEventActionActivate)1 EnumDyeColor (net.minecraft.item.EnumDyeColor)1