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

Commit

Permalink
Move BooleanExpression to Commander.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed May 5, 2024
1 parent 13333de commit 0339325
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import me.melontini.commander.impl.expression.EvalUtils;
import com.mojang.serialization.DataResult;
import me.melontini.dark_matter.api.data.codecs.ExtraCodecs;
import net.minecraft.loot.context.LootContext;
import org.jetbrains.annotations.Contract;
Expand All @@ -18,7 +18,7 @@
*/
public interface Arithmetica extends ToDoubleFunction<LootContext> {

Codec<Arithmetica> CODEC = ExtraCodecs.either(Codec.DOUBLE, Codec.STRING).comapFlatMap(EvalUtils::parseEither, Arithmetica::toSource);
Codec<Arithmetica> CODEC = ExtraCodecs.either(Codec.DOUBLE, Codec.STRING).comapFlatMap((either) -> either.map(b -> DataResult.success(constant(b)), s -> Expression.parse(s).map(Arithmetica::of)), Arithmetica::toSource);

default long asLong(LootContext context) {
return (long) this.applyAsDouble(context);
Expand Down Expand Up @@ -51,12 +51,16 @@ public Either<Double, String> toSource() {
public double applyAsDouble(LootContext context) {
return d;
}

@Override
public String toString() {
return "Arithmetica{double=" + d + "}";
}
};
}

@Contract("_, _ -> new")
static @NotNull Arithmetica of(ToDoubleFunction<LootContext> function, String expression) {
Either<Double, String> either = Either.right(expression);
static @NotNull Arithmetica of(Expression expression) {
Either<Double, String> either = Either.right(expression.original());
return new Arithmetica() {
@Override
public Either<Double, String> toSource() {
Expand All @@ -65,7 +69,12 @@ public Either<Double, String> toSource() {

@Override
public double applyAsDouble(LootContext context) {
return function.applyAsDouble(context);
return expression.apply(context).getAsDecimal().doubleValue();
}

@Override
public String toString() {
return "Arithmetica{expression=" + expression + "}";
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.melontini.commander.api.expression;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import me.melontini.dark_matter.api.data.codecs.ExtraCodecs;
import net.minecraft.loot.context.LootContext;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
* A simple {@code context -> boolean} functions, which is encoded as either a boolean or an expression.
* <p>Can be used as a substitute for {@link Codec#BOOL} if {@link LootContext} is available</p>
*
* @see Expression
*/
public interface BooleanExpression {

Codec<BooleanExpression> CODEC = ExtraCodecs.either(Codec.BOOL, Codec.STRING).comapFlatMap((either) -> either.map(b -> DataResult.success(constant(b)), s -> Expression.parse(s).map(BooleanExpression::of)), BooleanExpression::toSource);

boolean asBoolean(LootContext context);

Either<Boolean, String> toSource();

@Contract("_ -> new")
static @NotNull BooleanExpression constant(boolean b) {
Either<Boolean, String> either = Either.left(b);
return new BooleanExpression() {
@Override
public Either<Boolean, String> toSource() {
return either;
}

@Override
public boolean asBoolean(LootContext context) {
return b;
}

@Override
public String toString() {
return "BooleanExpression{boolean=" + b + "}";
}
};
}

@Contract("_ -> new")
static @NotNull BooleanExpression of(Expression expression) {
Either<Boolean, String> either = Either.right(expression.original());
return new BooleanExpression() {
@Override
public Either<Boolean, String> toSource() {
return either;
}

@Override
public boolean asBoolean(LootContext context) {
return expression.eval(context).getAsBoolean();
}

@Override
public String toString() {
return "BooleanExpression{expression=" + expression.original() + "}";
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import com.ezylang.evalex.functions.trigonometric.*;
import com.ezylang.evalex.parser.ParseException;
import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.DataResult;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import me.melontini.commander.api.expression.Arithmetica;
import me.melontini.commander.impl.event.data.types.ExtractionTypes;
import me.melontini.commander.impl.util.functions.*;
import net.minecraft.loot.context.LootContext;
Expand Down Expand Up @@ -133,10 +131,6 @@ public static EvaluationValue evaluate(LootContext context, Expression exp) {
}
}

public static DataResult<Arithmetica> parseEither(Either<Double, String> either) {
return either.map(d -> DataResult.success(Arithmetica.constant(d)), string -> parseExpression(string).map(exp -> Arithmetica.of(context -> evaluate(context, exp).getNumberValue().doubleValue(), string)));
}

public static DataResult<Expression> parseExpression(String expression) {
try {
Expression exp = new Expression(expression.replace(":", "__idcl__"), CONFIGURATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public static void downloadMappings() {

log.info("Downloading {}...", Objects.requireNonNull(mappings.getFileName()).toString());
Files.writeString(parent.resolve("LICENSE.txt"), """
(c) 2020 Microsoft Corporation.
These mappings are provided "as-is" and you bear the risk of using them.
You may copy and use the mappings for development purposes, but you may not redistribute the mappings complete and unmodified.
Microsoft makes no warranties, express or implied, with respect to the mappings provided here.
Use and modification of this document or the source code (in any form) of Minecraft: Java Edition is governed by the Minecraft End User License Agreement available at https://account.mojang.com/documents/minecraft_eula.
(c) 2020 Microsoft Corporation.
These mappings are provided "as-is" and you bear the risk of using them.
You may copy and use the mappings for development purposes, but you may not redistribute the mappings complete and unmodified.
Microsoft makes no warranties, express or implied, with respect to the mappings provided here.
Use and modification of this document or the source code (in any form) of Minecraft: Java Edition is governed by the Minecraft End User License Agreement available at https://account.mojang.com/documents/minecraft_eula.
""");
try (var defStream = new DeflaterInputStream(stream)) {
Files.write(mappings, defStream.readAllBytes());
Expand Down

0 comments on commit 0339325

Please sign in to comment.