Skip to content

Commit

Permalink
Add script context to errors during translation
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Apr 8, 2024
1 parent a1ec7fb commit 49ded95
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cyclops.integratedscripting.api.evaluate.translation;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;

/**
Expand All @@ -8,6 +10,10 @@
*/
public interface IEvaluationExceptionFactory {

public EvaluationException createError(String message);
public default EvaluationException createError(String message) {
return this.createError(Component.literal(message));
}

public EvaluationException createError(MutableComponent message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public static String getPathExtension(Path path) {
}

public static IEvaluationExceptionFactory getDummyEvaluationExceptionFactory() {
return message -> new EvaluationException(Component.literal(message));
return EvaluationException::new;
}

public static IEvaluationExceptionFactory getEvaluationExceptionFactory(int disk, Path path,String member) {
public static IEvaluationExceptionFactory getEvaluationExceptionFactory(int disk, Path path, String member) {
return message -> new EvaluationException(Component.translatable("script.integratedscripting.error.script_exec", member, path.toString(), disk, message));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public <V extends IValue> IValueTranslator getValueTypeTranslator(IValueType<V>
public <V extends IValue> Value translateToGraal(Context context, V value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
IValueTranslator translator = getValueTypeTranslator(value.getType());
if (translator == null) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal", value.getType()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal", value.getType()));
}
return translator.translateToGraal(context, value, exceptionFactory);
}
Expand All @@ -70,7 +70,7 @@ public IValueTranslator getScriptValueTranslator(Value scriptValue) {
public <V extends IValue> V translateFromGraal(Context context, Value value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
IValueTranslator translator = getScriptValueTranslator(value);
if (translator == null) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unknown_from_graal", value));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unknown_from_graal", value));
}
return (V) translator.translateFromGraal(context, value, exceptionFactory);
}
Expand All @@ -79,11 +79,11 @@ public <V extends IValue> V translateFromGraal(Context context, Value value, IEv
public <V extends IValue> Tag translateToNbt(Context context, V value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
IValueTranslator translator = getValueTypeTranslator(value.getType());
if (translator == null) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal_nbt", value.getType()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal_nbt", value.getType()));
}
if (translator.canTranslateNbt()) {
return translator.translateToNbt(context, value, exceptionFactory);
}
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unmatched", value.getType()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unmatched", value.getType()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.SneakyThrows;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
import org.cyclops.integrateddynamics.api.evaluate.operator.IOperator;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.core.evaluate.operator.CurriedOperator;
Expand Down Expand Up @@ -86,6 +85,6 @@ public boolean hasMember(String key) {
@SneakyThrows
@Override
public void putMember(String key, Value value) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.proxyobject_putMember", key, valueType.getTypeName()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.proxyobject_putMember", key, valueType.getTypeName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean canTranslateNbt() {
@Override
public Value translateToGraal(Context context, ValueTypeList.ValueList value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
if (value.getRawValue().isInfinite()) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
}

// The following should work according to the docs, but doesn't. So we fallback to a sub-optimal approach.
Expand All @@ -67,7 +67,7 @@ public ValueTypeList.ValueList translateFromGraal(Context context, Value value,
@Override
public Tag translateToNbt(Context context, ValueTypeList.ValueList value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
if (value.getRawValue().isInfinite()) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
}

ListTag tag = new ListTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Value translateTag(Context context, Tag tag, IEvaluationExceptionFactory
case Tag.TAG_LONG_ARRAY -> {
return context.asValue(((LongArrayTag) tag).getAsLongArray());
}
default -> throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unknown", tag.getType().getPrettyName()));
default -> throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unknown", tag.getType().getPrettyName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ public V translateFromGraal(Context context, Value value, IEvaluationExceptionFa

@Override
public Tag translateToNbt(Context context, V value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unsupported_translateToNbt", Component.translatable(value.getType().getTranslationKey()), value.getType().toCompactString(value)));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unsupported_translateToNbt", Component.translatable(value.getType().getTranslationKey()), value.getType().toCompactString(value)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ValueTypeOperator.ValueOperator translateFromGraal(Context context, Value

@Override
public Tag translateToNbt(Context context, ValueTypeOperator.ValueOperator value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unsupported_translateToNbt", Component.translatable(value.getType().getTranslationKey()), value.getType().toCompactString(value)));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unsupported_translateToNbt", Component.translatable(value.getType().getTranslationKey()), value.getType().toCompactString(value)));
}

public static class GraalOperator extends OperatorBase {
Expand Down

0 comments on commit 49ded95

Please sign in to comment.