Skip to content

Commit

Permalink
Add more predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 29, 2023
1 parent 6323c67 commit d748902
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 41 deletions.
44 changes: 38 additions & 6 deletions BUILTIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Checks if player has required operator level.
"key": ""
}
```

Checks and returns value of player's statistic with success value of true
if it's higher than 0. Should be used with other predicates.

Expand All @@ -95,7 +94,7 @@ See https://minecraft.fandom.com/wiki/Statistics#Statistic_types_and_names
}
```

Checks using Vanilla (advancement/loot table) predicates.
Checks using Vanilla (advancement) predicates.

See https://minecraft.fandom.com/wiki/Template:Nbt_inherit/conditions/entity

Expand Down Expand Up @@ -130,13 +129,46 @@ Should be used with others for more specific checks
```json5
{
"type": "placeholder",
// Placeholder value with arguments, without `%`
// Placeholder value with arguments, without `%`, for example "player:displayname"
"placeholder": "...",
// (Optional) Boolean, making it return raw value (if provides string, it won't be formatted). Defaults to false
"raw": false
}
```

Checks if player has specified option set by permission mod.
Should be used with others for more specific checks
(see Comparator predicates).
Returns value of provided placeholder, to be used with other comparators.


## Has (Entity/Player/World/Game Profile) (0.2.0+1.20.1 and newer)
```json5
{
// [X] needs to be replaced with entity, player, world, game_profile
"type": "has_[X]"
}
```

Checks if Entity/Player/World/Game Profile is present.

## Starts With (0.2.0+1.20.1 and newer)
```json5
{
"type": "starts_with",
// A string (text in quotes), a number or predicate definition
"input": "",
"argument": "",
}
```

Checks if input starts with argument (converted to strings)

## Ends With (0.2.0+1.20.1 and newer)
```json5
{
"type": "ends_with",
// A string (text in quotes), a number or predicate definition
"input": "",
"argument": "",
}
```

Checks if input starts with argument (converted to strings)
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dependencies {
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
modLocalRuntime("eu.pb4:polymer-reg-sync-manipulator:0.0.1+1.19")
modCompileOnly("eu.pb4:placeholder-api:2.0.0-pre.1+1.19.2")
modCompileOnly("eu.pb4:placeholder-api:2.1.1+1.20")
modCompileOnly("me.lucko:fabric-permissions-api:0.2-SNAPSHOT")

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20-rc1
yarn_mappings=1.20-rc1+build.2
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.2
loader_version=0.14.21

#Fabric api
fabric_version=0.83.0+1.20
fabric_version=0.83.0+1.20.1

# Mod Properties
mod_version = 0.1.2+1.20
mod_version = 0.2.0+1.20.1
maven_group = eu.pb4
archives_base_name = predicate-api

24 changes: 24 additions & 0 deletions src/main/java/eu/pb4/predicate/api/BuiltinPredicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public static MinecraftPredicate moreOrEqual(Object object, Object object2) {
return new NumberPredicate.MoreEqual(object, object2);
}

public static MinecraftPredicate startsWith(Object input, Object argument) {
return new StringPredicate.StartsWith(input, argument);
}

public static MinecraftPredicate endsWith(Object input, Object argument) {
return new StringPredicate.EndsWith(input, argument);
}

public static MinecraftPredicate operatorLevel(int level) {
return new OperatorPredicate(level);
}
Expand All @@ -65,6 +73,22 @@ public static <T> MinecraftPredicate statistic(StatType<T> type, T key) {
return new StatisticPredicate(type, type.getRegistry().getId(key));
}

public static MinecraftPredicate hasWorld() {
return SimplePredicate.HAS_WORLD;
}

public static MinecraftPredicate hasPlayer() {
return SimplePredicate.HAS_PLAYER;
}

public static MinecraftPredicate hasGameProfile() {
return SimplePredicate.HAS_GAME_PROFILE;
}

public static MinecraftPredicate hasEntity() {
return SimplePredicate.HAS_ENTITY;
}

public static <T> MinecraftPredicate vanillaEntityPredicate(EntityPredicate predicate) {
return new EntityPredicatePredicate(predicate);
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/eu/pb4/predicate/api/MinecraftPredicate.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package eu.pb4.predicate.api;

import com.mojang.serialization.MapCodec;
import eu.pb4.predicate.impl.predicates.generic.UnitPredicate;
import eu.pb4.predicate.impl.predicates.generic.ConstantUnitPredicate;
import eu.pb4.predicate.impl.predicates.generic.SimplePredicate;
import net.minecraft.util.Identifier;

import java.util.function.Function;

public interface MinecraftPredicate {
static MinecraftPredicate unit(Object valueA) {
return new UnitPredicate(valueA);
return new ConstantUnitPredicate(valueA);
}

static MinecraftPredicate simple(Identifier identifier, Function<PredicateContext, PredicateResult<?>> resultFunction) {
return new SimplePredicate(identifier, resultFunction);
}

static MapCodec<MinecraftPredicate> simpleCodec(Identifier identifier, Function<PredicateContext, PredicateResult<?>> resultFunction) {
return simple(identifier, resultFunction).codec();
}

PredicateResult<?> test(PredicateContext context);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/pb4/predicate/api/PredicateRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public final class PredicateRegistry {
private static final Map<Identifier, MapCodec<MinecraftPredicate>> CODECS = new HashMap<>();
private static final Map<MapCodec<MinecraftPredicate>, Identifier> CODEC_IDS = new HashMap<>();
public static final Codec<MinecraftPredicate> CODEC = new MapCodec.MapCodecCodec(new BaseCodec());
public static final Codec<MinecraftPredicate> CODEC = new MapCodec.MapCodecCodec<>(new BaseCodec());

private PredicateRegistry() {}

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/eu/pb4/predicate/impl/PredicatesInit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package eu.pb4.predicate.impl;

import com.mojang.serialization.MapCodec;
import eu.pb4.predicate.api.MinecraftPredicate;
import eu.pb4.predicate.api.PredicateRegistry;
import eu.pb4.predicate.impl.predicates.compat.CompatStatus;
import eu.pb4.predicate.impl.predicates.compat.PermissionOptionPredicate;
import eu.pb4.predicate.impl.predicates.compat.PermissionPredicate;
Expand All @@ -8,12 +11,11 @@
import eu.pb4.predicate.impl.predicates.player.OperatorPredicate;
import eu.pb4.predicate.impl.predicates.player.StatisticPredicate;
import eu.pb4.predicate.impl.predicates.player.EntityPredicatePredicate;
import net.minecraft.util.Identifier;

import static eu.pb4.predicate.api.PredicateRegistry.register;

public class PredicatesInit {
public static void initialize() {

register(EqualityPredicate.ID, EqualityPredicate.CODEC);
register(NegatePredicate.ID, NegatePredicate.CODEC);
register(AnyPredicate.ID, AnyPredicate.CODEC);
Expand All @@ -22,11 +24,18 @@ public static void initialize() {
register(NumberPredicate.LessEqual.ID, NumberPredicate.LessEqual.CODEC);
register(NumberPredicate.MoreThan.ID, NumberPredicate.MoreThan.CODEC);
register(NumberPredicate.MoreEqual.ID, NumberPredicate.MoreEqual.CODEC);
register(StringPredicate.StartsWith.ID, StringPredicate.StartsWith.CODEC);
register(StringPredicate.EndsWith.ID, StringPredicate.EndsWith.CODEC);
register(StringPredicate.Join.ID, StringPredicate.Join.CODEC);

register(OperatorPredicate.ID, OperatorPredicate.CODEC);
register(StatisticPredicate.ID, StatisticPredicate.CODEC);
register(EntityPredicatePredicate.ID, EntityPredicatePredicate.CODEC);

register(SimplePredicate.HAS_ENTITY);
register(SimplePredicate.HAS_PLAYER);
register(SimplePredicate.HAS_WORLD);
register(SimplePredicate.HAS_GAME_PROFILE);

if (CompatStatus.PLACEHOLDER_API) {
register(PlaceholderPredicate.ID, PlaceholderPredicate.CODEC);
Expand All @@ -37,4 +46,12 @@ public static void initialize() {
register(PermissionOptionPredicate.ID, PermissionOptionPredicate.CODEC);
}
}

public static void register(MinecraftPredicate predicate) {
register(predicate.identifier(), predicate.codec());
}

public static <T extends MinecraftPredicate> void register(Identifier identifier, MapCodec<T> codec) {
PredicateRegistry.register(identifier, codec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class GenericObject {
public static final Codec<Object> CODEC = (Codec<Object>) (Object) Codec.either(PredicateRegistry.CODEC, Codec.either(Codec.STRING, Codec.DOUBLE));

public static MinecraftPredicate toPredicate(Object valueA) {
if (valueA instanceof Either<?,?> either) {
if (valueA instanceof Either<?, ?> either) {
if (either.left().isPresent()) {
return toPredicate(either.left().get());
} else {
Expand Down Expand Up @@ -38,4 +38,19 @@ public static double toNumber(Object value, boolean bool) {
}
return bool ? 1 : 0;
}

public static String toString(Object value) {
try {
if (value instanceof Text text) {
return text.getString();
} else if (value instanceof String string) {
return string;
} else {
return value.toString();
}
} catch (Throwable e) {

}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import eu.pb4.predicate.api.PredicateResult;
import net.minecraft.util.Identifier;

public final class UnitPredicate extends AbstractPredicate {
public final class ConstantUnitPredicate extends AbstractPredicate {
private final PredicateResult<Object> value;

public <T extends MinecraftPredicate> UnitPredicate(Object value) {
public <T extends MinecraftPredicate> ConstantUnitPredicate(Object value) {
super(new Identifier("unit"), MapCodec.unit(null));

this.value = PredicateResult.ofNullable(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package eu.pb4.predicate.impl.predicates.generic;

import com.mojang.serialization.MapCodec;
import eu.pb4.predicate.api.AbstractPredicate;
import eu.pb4.predicate.api.MinecraftPredicate;
import eu.pb4.predicate.api.PredicateContext;
import eu.pb4.predicate.api.PredicateResult;
import net.minecraft.util.Identifier;
import org.apache.commons.lang3.mutable.MutableObject;

import java.util.function.Function;
import java.util.function.Predicate;

public final class SimplePredicate extends AbstractPredicate {
public static final MinecraftPredicate HAS_PLAYER = new SimplePredicate(new Identifier("has_player"), PredicateContext::hasPlayer);
public static final MinecraftPredicate HAS_ENTITY = new SimplePredicate(new Identifier("has_entity"), PredicateContext::hasEntity);
public static final MinecraftPredicate HAS_WORLD = new SimplePredicate(new Identifier("has_world"), PredicateContext::hasWorld);
public static final MinecraftPredicate HAS_GAME_PROFILE = new SimplePredicate(new Identifier("has_game_profile"), PredicateContext::hasGameProfile);

private final Function<PredicateContext, PredicateResult<?>> function;

public SimplePredicate(Identifier identifier, Predicate<PredicateContext> function) {
this(identifier, (x) -> PredicateResult.ofBoolean(function.test(x)), new MutableObject<>());
}

public SimplePredicate(Identifier identifier, Function<PredicateContext, PredicateResult<?>> function) {
this(identifier, function, new MutableObject<>());
}

private SimplePredicate(Identifier identifier, Function<PredicateContext, PredicateResult<?>> function, MutableObject<SimplePredicate> self) {
super(identifier, MapCodec.unit(self::getValue));
this.function = function;
}

@Override
public PredicateResult<?> test(PredicateContext context) {
return function.apply(context);
}

}
Loading

0 comments on commit d748902

Please sign in to comment.