Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
Implement the public interface on evalx classes using mixins.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed May 11, 2024
1 parent 7e15781 commit a76823c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 87 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ Other Changes:

Dev Changes:

* Added `is(Type)` to expression result.
* Added `is(Type)Value` to expression result.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import me.melontini.commander.impl.expression.EvalUtils;
import me.melontini.commander.impl.expression.ExpressionImpl;
import net.minecraft.loot.context.LootContext;

import java.math.BigDecimal;
Expand All @@ -16,7 +15,7 @@ public interface Expression extends Function<LootContext, Expression.Result> {
Codec<Expression> CODEC = Codec.STRING.comapFlatMap(Expression::parse, Expression::original);

static DataResult<Expression> parse(String expression) {
return EvalUtils.parseExpression(expression).map(ExpressionImpl::new);
return (DataResult<Expression>) (Object) EvalUtils.parseExpression(expression);
}

@Override
Expand All @@ -34,10 +33,10 @@ interface Result {
Instant getAsInstant();
Duration getAsDuration();

boolean isDecimal();
boolean isBoolean();
boolean isString();
boolean isInstant();
boolean isDuration();
boolean isDecimalValue();
boolean isBooleanValue();
boolean isStringValue();
boolean isInstantValue();
boolean isDurationValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public CommandType type() {
public NbtElement asElement(EventContext context) {
var r = expression.apply(context.lootContext());

if (r.isDecimal()) return NbtDouble.of(r.getAsDecimal().doubleValue());
if (r.isString()) return NbtString.of(r.getAsString());
if (r.isDecimalValue()) return NbtDouble.of(r.getAsDecimal().doubleValue());
if (r.isStringValue()) return NbtString.of(r.getAsString());

throw new IllegalStateException("Persistent data must be a number or a string!");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package me.melontini.commander.impl.mixin.evalex;

import com.ezylang.evalex.data.EvaluationValue;
import me.melontini.commander.api.expression.Expression;
import me.melontini.commander.impl.expression.EvalUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;

@Mixin(value = EvaluationValue.class, remap = false)
public class EvaluationValueMixin {
public abstract class EvaluationValueMixin implements Expression.Result {

@Shadow public abstract BigDecimal getNumberValue();
@Shadow public abstract Boolean getBooleanValue();
@Shadow public abstract String getStringValue();
@Shadow public abstract Instant getDateTimeValue();
@Shadow public abstract Duration getDurationValue();
@Shadow public abstract boolean isNumberValue();
@Shadow public abstract boolean isDateTimeValue();

/**
* @author melontini
Expand All @@ -25,4 +39,39 @@ public static EvaluationValue booleanValue(Boolean value) {
public static EvaluationValue nullValue() {
return EvalUtils.NULL;
}

@Override
public BigDecimal getAsDecimal() {
return getNumberValue();
}

@Override
public boolean getAsBoolean() {
return getBooleanValue();
}

@Override
public String getAsString() {
return getStringValue();
}

@Override
public Instant getAsInstant() {
return getDateTimeValue();
}

@Override
public Duration getAsDuration() {
return getDurationValue();
}

@Override
public boolean isDecimalValue() {
return isNumberValue();
}

@Override
public boolean isInstantValue() {
return isDateTimeValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.data.EvaluationValue;
import me.melontini.commander.impl.expression.EvalUtils;
import net.minecraft.loot.context.LootContext;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = Expression.class, remap = false)
public class ExpressionMixin {
public abstract class ExpressionMixin implements me.melontini.commander.api.expression.Expression {

@Shadow @Final private ExpressionConfiguration configuration;

@Shadow public abstract String getExpressionString();

/**
* @author melontini
* @reason avoid double object construction
Expand All @@ -21,4 +25,14 @@ public class ExpressionMixin {
public EvaluationValue convertValue(Object value) {
return configuration.getEvaluationValueConverter().convertObject(value, configuration);
}

@Override
public Result eval(LootContext context) {
return (Result) (Object) EvalUtils.evaluate(context, (Expression) (Object) this);
}

@Override
public String original() {
return getExpressionString();
}
}

0 comments on commit a76823c

Please sign in to comment.