Skip to content

Commit

Permalink
Allow custom variable facades to be defined in advancement codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jun 15, 2024
1 parent c643aa2 commit 6b91b36
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;

/**
Expand Down Expand Up @@ -34,4 +35,17 @@ public interface IVariableFacadeHandler<F extends IVariableFacade> {
*/
public void setVariableFacade(CompoundTag tagCompound, F variableFacade);

/**
* If the given facade is created by this handler.
* @param variableFacade A facade.
* @return If it is an instance.
*/
public boolean isInstance(IVariableFacade variableFacade);

/**
* If the given variable is created by this handler.
* @param variable A variable.
* @return If it is an instance.
*/
public boolean isInstance(IVariable<?> variable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import org.cyclops.integrateddynamics.Reference;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.api.item.IDelayVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandler;
import org.cyclops.integrateddynamics.core.item.DelayVariableFacade;

Expand Down Expand Up @@ -44,4 +46,14 @@ public IDelayVariableFacade getVariableFacade(ValueDeseralizationContext valueDe
public void setVariableFacade(CompoundTag tag, IDelayVariableFacade variableFacade) {
tag.putInt("partId", variableFacade.getProxyId());
}

@Override
public boolean isInstance(IVariableFacade variableFacade) {
return variableFacade instanceof IDelayVariableFacade;
}

@Override
public boolean isInstance(IVariable<?> variable) {
return variable instanceof IVariable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import org.cyclops.integrateddynamics.Reference;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.api.item.IProxyVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandler;
import org.cyclops.integrateddynamics.core.item.ProxyVariableFacade;

Expand Down Expand Up @@ -44,4 +46,14 @@ public IProxyVariableFacade getVariableFacade(ValueDeseralizationContext valueDe
public void setVariableFacade(CompoundTag tag, IProxyVariableFacade variableFacade) {
tag.putInt("partId", variableFacade.getProxyId());
}

@Override
public boolean isInstance(IVariableFacade variableFacade) {
return variableFacade instanceof IProxyVariableFacade;
}

@Override
public boolean isInstance(IVariable<?> variable) {
return variable instanceof IVariable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.api.item.IOperatorVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
import org.cyclops.integrateddynamics.core.evaluate.expression.LazyExpression;
import org.cyclops.integrateddynamics.core.helper.L10NValues;
Expand Down Expand Up @@ -187,6 +188,16 @@ public void setVariableFacade(CompoundTag tag, IOperatorVariableFacade variableF
tag.putIntArray("variableIds", variableFacade.getVariableIds());
}

@Override
public boolean isInstance(IVariableFacade variableFacade) {
return variableFacade instanceof IOperatorVariableFacade;
}

@Override
public boolean isInstance(IVariable<?> variable) {
return variable instanceof IVariable;
}

@Override
public Map<String, IOperator> getGlobalInteractOperators() {
return globalInteractOperators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueTypeCategory;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueTypeRegistry;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.api.item.IValueTypeVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
import org.cyclops.integrateddynamics.core.item.ValueTypeVariableFacade;

Expand Down Expand Up @@ -124,6 +126,16 @@ public void setVariableFacade(CompoundTag tag, IValueTypeVariableFacade variable
tag.put("value", ValueHelpers.serializeRaw(variableFacade.getValue()));
}

@Override
public boolean isInstance(IVariableFacade variableFacade) {
return variableFacade instanceof IValueTypeVariableFacade;
}

@Override
public boolean isInstance(IVariable<?> variable) {
return variable instanceof IVariable;
}

public static class ValueTypeVariableFacadePredicate extends VariableFacadePredicate<IValueTypeVariableFacade> {

private final Optional<IValueType> valueType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.cyclops.integrateddynamics.core.evaluate.variable;

import org.cyclops.integrateddynamics.api.advancement.criterion.VariableFacadePredicate;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandler;

/**
* @author rubensworks
*/
public class VariableFacadePredicateTyped extends VariableFacadePredicate<IVariableFacade> {
private final IVariableFacadeHandler<?> handler;

public VariableFacadePredicateTyped(IVariableFacadeHandler<?> handler) {
super(IVariableFacade.class);
this.handler = handler;
}

public IVariableFacadeHandler<?> getHandler() {
return handler;
}

@Override
protected boolean testTyped(IVariableFacade variableFacade) {
return this.handler.isInstance(variableFacade);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.cyclops.integrateddynamics.core.evaluate.variable;

import org.cyclops.integrateddynamics.api.advancement.criterion.ValuePredicate;
import org.cyclops.integrateddynamics.api.advancement.criterion.VariablePredicate;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandler;

import java.util.Optional;

/**
* @author rubensworks
*/
public class VariablePredicateTyped extends VariablePredicate<IVariable<?>> {
private final IVariableFacadeHandler<?> handler;

public VariablePredicateTyped(IVariableFacadeHandler<?> handler, Optional<IValueType> valueType, Optional<ValuePredicate> valuePredicate) {
super((Class) IVariable.class, valueType, valuePredicate);
this.handler = handler;
}

public IVariableFacadeHandler<?> getHandler() {
return handler;
}

@Override
protected boolean testTyped(IVariable<?> variable) {
return this.handler.isInstance(variable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import org.cyclops.integrateddynamics.IntegratedDynamics;
import org.cyclops.integrateddynamics.api.advancement.criterion.ValuePredicate;
import org.cyclops.integrateddynamics.api.advancement.criterion.VariableFacadePredicate;
import org.cyclops.integrateddynamics.api.advancement.criterion.VariablePredicate;
Expand All @@ -27,6 +28,7 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
import org.cyclops.integrateddynamics.api.part.IPartType;
import org.cyclops.integrateddynamics.api.part.aspect.IAspect;
import org.cyclops.integrateddynamics.core.evaluate.operator.OperatorRegistry;
Expand All @@ -38,6 +40,8 @@
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeOperator;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeRegistry;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
import org.cyclops.integrateddynamics.core.evaluate.variable.VariableFacadePredicateTyped;
import org.cyclops.integrateddynamics.core.evaluate.variable.VariablePredicateTyped;
import org.cyclops.integrateddynamics.core.part.PartTypes;
import org.cyclops.integrateddynamics.core.part.aspect.AspectRegistry;
import org.cyclops.integrateddynamics.part.aspect.Aspects;
Expand Down Expand Up @@ -180,6 +184,19 @@ public static Codec<VariablePredicate> getVariableCodec() { // This is a functio
ExtraCodecs.strictOptionalField(VALUE, "value").forGetter(VariablePredicate::getValuePredicate)
)
.apply(builder, (type, valueType, valuePredicate) -> new VariablePredicate<>(IVariable.class, valueType, valuePredicate))
),
RecordCodecBuilder.<VariablePredicateTyped>create(
builder -> builder.group(
ExtraCodecs.validate(
Codec.STRING,
type -> IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class).getHandler(new ResourceLocation(type)) == null ?
DataResult.error(() -> "Variable facade predicate is expected to have as 'type' one of: " + String.join(", ", IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class).getHandlerNames())) :
DataResult.success(type)
).fieldOf("type").forGetter(p -> p.getHandler().getUniqueName().toString()),
ExtraCodecs.strictOptionalField(VALUE_TYPE, "value_type").forGetter(VariablePredicate::getValueType),
ExtraCodecs.strictOptionalField(VALUE, "value").forGetter(VariablePredicate::getValuePredicate)
)
.apply(builder, (type, valueType, valuePredicate) -> new VariablePredicateTyped(IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class).getHandler(new ResourceLocation(type)), valueType, valuePredicate))
)
));
}
Expand All @@ -201,6 +218,17 @@ public static Codec<VariablePredicate> getVariableCodec() { // This is a functio
)
.apply(builder, (type, valueType, value) -> new ValueTypeRegistry.ValueTypeVariableFacadePredicate(valueType, value))
),
RecordCodecBuilder.<VariableFacadePredicateTyped>create(
builder -> builder.group(
ExtraCodecs.validate(
Codec.STRING,
type -> IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class).getHandler(new ResourceLocation(type)) == null ?
DataResult.error(() -> "Variable facade predicate is expected to have as 'type' one of: " + String.join(", ", IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class).getHandlerNames())) :
DataResult.success(type)
).fieldOf("type").forGetter(p -> p.getHandler().getUniqueName().toString())
)
.apply(builder, (type) -> new VariableFacadePredicateTyped(IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class).getHandler(new ResourceLocation(type))))
),
Codec.unit(new VariableFacadePredicate<>(IVariableFacade.class))
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.cyclops.integrateddynamics.api.advancement.criterion.VariableFacadePredicate;
import org.cyclops.integrateddynamics.api.advancement.criterion.VariablePredicate;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
import org.cyclops.integrateddynamics.api.item.IAspectVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
import org.cyclops.integrateddynamics.api.part.IPartType;
import org.cyclops.integrateddynamics.api.part.aspect.IAspect;
Expand Down Expand Up @@ -191,6 +193,16 @@ public void setVariableFacade(CompoundTag tag, IAspectVariableFacade variableFac
tag.putString("aspectName", variableFacade.getAspect().getUniqueName().toString());
}

@Override
public boolean isInstance(IVariableFacade variableFacade) {
return variableFacade instanceof IAspectVariableFacade;
}

@Override
public boolean isInstance(IVariable<?> variable) {
return variable instanceof IAspectVariable;
}

public static class AspectVariablePredicate extends VariablePredicate<IAspectVariable> {

private final Optional<IAspect> aspect;
Expand Down

0 comments on commit 6b91b36

Please sign in to comment.