Skip to content

Commit

Permalink
Mechanism registration improvements (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
tal5 committed Nov 17, 2022
1 parent 9ca3456 commit 304f939
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Expand Up @@ -99,22 +99,42 @@ public static <T extends Property, R extends ObjectTag> void registerTagInternal
}, isStatic, variants);
}

@FunctionalInterface
public interface PropertyMechanism<T extends Property> {
void run(T prop, Mechanism mechanism);
}

@FunctionalInterface
public interface PropertyMechanismWithParam<T extends Property, P extends ObjectTag> {
void run(T prop, Mechanism mechanism, P param);
}

public static <T extends Property, P extends ObjectTag> void registerMechanism(Class<T> propType, Class<P> paramType, String name, PropertyMechanismWithParam<T, P> runner) {
public static <T extends Property> void registerMechanism(Class<T> propType, String name, PropertyMechanism<T> runner, String... deprecatedVariants) {
final PropertyParser.PropertyGetter getter = PropertyParser.currentlyRegisteringProperty;
ObjectTagProcessor<?> tagProcessor = PropertyParser.currentlyRegisteringObjectType.tagProcessor;
tagProcessor.registerMechanism(name, true, paramType, (object, mechanism, param) -> {
tagProcessor.registerMechanism(name, true, (object, mechanism) -> {
Property prop = getter.get(object);
if (prop == null) {
mechanism.echoError("Property '" + DebugInternals.getClassNameOpti(propType) + "' does not describe the input object.");
return;
}
runner.run((T) prop, mechanism, param);
});
runner.run((T) prop, mechanism);
}, deprecatedVariants);
}

public static <T extends Property, P extends ObjectTag> void registerMechanism(Class<T> propType, Class<P> paramType, String name, PropertyMechanismWithParam<T, P> runner, String... deprecatedVariants) {
registerMechanism(propType, name, (object, mechanism) -> {
if (mechanism.value == null) {
mechanism.echoError("Error: mechanism '" + name + "' must have input of type '" + DebugInternals.getClassNameOpti(paramType) + "', but none was given.");
return;
}
P input = mechanism.value.asType(paramType, mechanism.context);
if (input == null) {
mechanism.echoError("Error: mechanism '" + name + "' must have input of type '" + DebugInternals.getClassNameOpti(paramType) + "', but value '" + mechanism.value + "' cannot be converted to the required type.");
return;
}
runner.run(object, mechanism, input);
}, deprecatedVariants);
}

public static Class currentlyRegisteringPropertyClass;
Expand Down
Expand Up @@ -208,16 +208,26 @@ public final void processMechanism(T object, Mechanism mechanism) {
mechData.runner.run(object, mechanism);
}

public void registerMechanism(String name, boolean allowProperty, Mechanism.GenericMechRunnerInterface<T> runner) {
public void registerMechanism(String name, boolean allowProperty, Mechanism.GenericMechRunnerInterface<T> runner, String... deprecatedVariants) {
MechanismData<T> data = new MechanismData<>();
data.allowProperty = allowProperty;
data.name = name;
data.runner = runner;
PropertyParser.allMechanismsEver.add(name);
registeredMechanisms.put(name, data);
for (String variant : deprecatedVariants) {
MechanismData<T> variantData = new MechanismData<>();
variantData.allowProperty = allowProperty;
variantData.name = variant;
variantData.runner = (object, mechanism) -> {
mechanism.echoError("Using deprecated form of mechanism '" + name + "': '" + variant + "'.");
runner.run(object, mechanism);
};
registeredMechanisms.put(variant, variantData);
}
}

public <P extends ObjectTag> void registerMechanism(String name, boolean allowProperty, Class<P> paramType, Mechanism.ObjectInputMechRunnerInterface<T, P> runner) {
public <P extends ObjectTag> void registerMechanism(String name, boolean allowProperty, Class<P> paramType, Mechanism.ObjectInputMechRunnerInterface<T, P> runner, String... deprecatedVariants) {
registerMechanism(name, allowProperty, (object, mechanism) -> {
if (mechanism.value == null) {
mechanism.echoError("Error: mechanism '" + name + "' must have input of type '" + DebugInternals.getClassNameOpti(paramType) + "', but none was given.");
Expand All @@ -229,6 +239,6 @@ public <P extends ObjectTag> void registerMechanism(String name, boolean allowPr
return;
}
runner.run(object, mechanism, input);
});
}, deprecatedVariants);
}
}

0 comments on commit 304f939

Please sign in to comment.