Skip to content

Commit

Permalink
Allow listening to EvaluationException resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Apr 13, 2024
1 parent 9531d16 commit 6fef4df
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.cyclops.integrateddynamics.api.evaluate;

import com.google.common.collect.Lists;
import net.minecraft.network.chat.MutableComponent;

import java.util.List;

/**
* Exception to indicate a failed evaluation.
* @author rubensworks
Expand All @@ -10,22 +13,48 @@ public class EvaluationException extends Exception {

private final MutableComponent errorMessage;
private boolean retryEvaluation;
private final List<Runnable> resolutionListeners;

public EvaluationException(MutableComponent errorMessage) {
super(errorMessage.toString());
this.errorMessage = errorMessage;
this.retryEvaluation = false;
this.resolutionListeners = Lists.newArrayList();
}

public MutableComponent getErrorMessage() {
return errorMessage;
}

/**
* This should only be set at construction time of this exception.
* @param retryEvaluation If the evaluation may be retried again in the next tick.
*/
public void setRetryEvaluation(boolean retryEvaluation) {
this.retryEvaluation = retryEvaluation;
}

/**
* @return If the evaluation may be retried again in the next tick.
*/
public boolean isRetryEvaluation() {
return retryEvaluation;
}

public void addResolutionListeners(Runnable listener) {
this.resolutionListeners.add(listener);
}

/**
* If evaluators halt operation due to this thrown evaluation,
* invoking this method will cause them to remove the exception and resume operation.
*
* In contrast to {@link #retryEvaluation}, this may be invoked anywhere within the lifetime of evaluation
* exceptions.
*/
public void resolve() {
for (Runnable resolutionListener : Lists.newArrayList(this.resolutionListeners.listIterator())) {
resolutionListener.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ public V getValue() throws EvaluationException {
return (V) value;
} catch (ClassCastException e) {
errored = true;
throw new EvaluationException(Component.translatable(L10NValues.OPERATOR_ERROR_WRONGTYPEOUTPUT,
EvaluationException e2 = new EvaluationException(Component.translatable(L10NValues.OPERATOR_ERROR_WRONGTYPEOUTPUT,
op,
Component.translatable(value.getType().getTranslationKey()),
Component.translatable(op.getOutputType().getTranslationKey())));
e2.addResolutionListeners(this::invalidate);
throw e2;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
state.addGlobalError(e.getErrorMessage());
if (e.isRetryEvaluation()) {
state.setRetryEvaluation(true);
} else {
e.addResolutionListeners(() -> state.onVariableContentsUpdated((P) this, target));
}
}
}
Expand Down Expand Up @@ -190,6 +192,7 @@ protected void onValueChanged(INetwork network, IPartNetwork partNetwork, PartTa
materializedValue = newValue.getType().materialize(newValue);
} catch (EvaluationException e) {
state.addGlobalError(e.getErrorMessage());
e.addResolutionListeners(() -> state.addGlobalError(null)); // TODO: also change here?
}
state.setDisplayValue(materializedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public <P extends IPartType<P, S>, S extends IPartState<P>> void update(INetwork
} catch (EvaluationException e) {
writerState.addError(this, e.getErrorMessage());
writerState.setDeactivated(true);
e.addResolutionListeners(() -> writerState.onVariableContentsUpdated(partTypeWriter, target));
}
} else if(!writerState.isDeactivated()) {
onDeactivate(partTypeWriter, target, writerState);
Expand Down

0 comments on commit 6fef4df

Please sign in to comment.