Skip to content

Commit

Permalink
Fix variables dependent on offset aspects not always updating
Browse files Browse the repository at this point in the history
Closes #1416
  • Loading branch information
rubensworks committed Nov 9, 2024
1 parent a133f99 commit 03f5aa7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.part.PartTarget;

import java.util.function.Supplier;

/**
* An element that can be used inside parts to access a specific aspect of something to read.
* @author rubensworks
Expand All @@ -15,7 +17,17 @@ public interface IAspectRead<V extends IValue, T extends IValueType<V>> extends
* @param target The target for this aspect.
* @return The variable pointing to the given target.
*/
public IAspectVariable<V> createNewVariable(PartTarget target);
@Deprecated // TODO: rm in next major
public default IAspectVariable<V> createNewVariable(PartTarget target) {
return this.createNewVariable(() -> target);
}

/**
* Creates a new variable for this aspect.
* @param targetSupplier The target supplier for this aspect.
* @return The variable pointing to the given target.
*/
public IAspectVariable<V> createNewVariable(Supplier<PartTarget> targetSupplier);

/**
* @return The update type on which this aspect should invalidate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.cyclops.integrateddynamics.api.part.aspect.property.IAspectProperties;
import org.cyclops.integrateddynamics.core.helper.L10NValues;

import java.util.function.Supplier;

/**
* Variable for a specific aspect from a part that calculates its target value only maximum once per ticking interval.
* No calculations will be done if the value of this variable is not called.
Expand All @@ -25,19 +27,28 @@
public abstract class LazyAspectVariable<V extends IValue> extends VariableAdapter<V> implements IAspectVariable<V> {

@Getter private final IValueType<V> type;
@Getter private final PartTarget target;
private final Supplier<PartTarget> targetSupplier;
@Getter private final IAspectRead<V, ?> aspect;
@NonNull private V value;
private IAspectProperties cachedProperties = null;

private boolean isGettingValue = false;

public LazyAspectVariable(IValueType<V> type, PartTarget target, IAspectRead<V, ?> aspect) {
public LazyAspectVariable(IValueType<V> type, Supplier<PartTarget> targetSupplier, IAspectRead<V, ?> aspect) {
this.type = type;
this.target = target;
this.targetSupplier = targetSupplier;
this.aspect = aspect;
}

@Deprecated // TODO: rm in next major
public LazyAspectVariable(IValueType<V> type, PartTarget target, IAspectRead<V, ?> aspect) {
this(type, () -> target, aspect);
}

public PartTarget getTarget() {
return targetSupplier.get();
}

@Override
public void invalidate() {
if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public void setVariable(IAspect aspect, IAspectVariable variable) {

@Override
public void resetVariables() {
this.aspectVariables.clear();
for (IAspectVariable variable : this.aspectVariables.values()) {
variable.invalidate();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public <V extends IValue, T extends IValueType<V>> IAspectVariable<V> getVariabl
throw new IllegalArgumentException(String.format("Tried to get the variable for the aspect %s that did not exist within the " +
"part type %s.", aspect.getUniqueName(), this));
}
variable = aspect.createNewVariable(target);
variable = aspect.createNewVariable(() -> getTarget(target.getCenter(), partState));
partState.setVariable(aspect, variable);
}
return variable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.cyclops.integrateddynamics.part.aspect.AspectBase;
import org.cyclops.integrateddynamics.part.aspect.Aspects;

import java.util.function.Supplier;

/**
* Base class for read aspects.
* @author rubensworks
Expand Down Expand Up @@ -73,11 +75,11 @@ protected void registerModelResourceLocation() {
protected abstract V getValue(PartTarget target, IAspectProperties properties) throws EvaluationException;

@Override
public IAspectVariable<V> createNewVariable(final PartTarget target) {
public IAspectVariable<V> createNewVariable(Supplier<PartTarget> target) {
return new LazyAspectVariable<V>(getValueType(), target, this) {
@Override
public V getValueLazy() throws EvaluationException {
return AspectReadBase.this.getValue(target, getAspectProperties());
return AspectReadBase.this.getValue(getTarget(), getAspectProperties());
}
};
}
Expand Down

0 comments on commit 03f5aa7

Please sign in to comment.