This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move BooleanExpression to Commander.
- Loading branch information
Showing
4 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/me/melontini/commander/api/expression/BooleanExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() + "}"; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters