Skip to content

Commit

Permalink
feat(critereon): health predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Jan 25, 2025
1 parent ed8a37a commit 50c1433
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ public record AttributePredicate(

private static final Marker MARKER = MarkerFactory.getMarker("AttributePredicate");

public static MapCodec<AttributePredicate> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
Attribute.CODEC.fieldOf("attribute").forGetter(o -> o.attribute),
EnumCodec.byString(ValueType.class, e -> e.name).fieldOf("type").forGetter(o -> o.type),
MinMaxBounds.Doubles.CODEC.fieldOf("value").forGetter(o -> o.value)
).apply(instance, AttributePredicate::new));
public static MapCodec<AttributePredicate> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Attribute.CODEC.fieldOf("attribute").forGetter(o -> o.attribute),
EnumCodec.byString(ValueType.class, e -> e.name).fieldOf("type").forGetter(o -> o.type),
MinMaxBounds.Doubles.CODEC.fieldOf("value").forGetter(o -> o.value)
).apply(instance, AttributePredicate::new));

@Override
public MapCodec<AttributePredicate> codec() { return CODEC; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

/**
* @since 7.0.0
*/
public record FrozenPredicate(MinMaxBounds.Ints ticks, MinMaxBounds.Doubles percent) implements EntitySubPredicate {

public static final MapCodec<FrozenPredicate> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
MinMaxBounds.Ints.CODEC.optionalFieldOf("ticks", MinMaxBounds.Ints.ANY).forGetter(o -> o.ticks),
MinMaxBounds.Doubles.CODEC.optionalFieldOf("percent", MinMaxBounds.Doubles.ANY).forGetter(o -> o.percent)
).apply(instance, FrozenPredicate::new)
);
MinMaxBounds.Ints.CODEC.optionalFieldOf("ticks", MinMaxBounds.Ints.ANY).forGetter(o -> o.ticks),
MinMaxBounds.Doubles.CODEC.optionalFieldOf("percent", MinMaxBounds.Doubles.ANY).forGetter(o -> o.percent)
).apply(instance, FrozenPredicate::new));

@Override
public MapCodec<? extends EntitySubPredicate> codec() { return CODEC; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2025 AUIOC.ORG
*
* This file is part of ArnicaLib, a mod made for Minecraft.
*
* ArnicaLib is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.auioc.mcmod.arnicalib.game.critereon;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancements.critereon.EntitySubPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

/**
* @since 7.0.1
*/
public record HealthPredicate(MinMaxBounds.Doubles current, MinMaxBounds.Doubles percent) implements EntitySubPredicate {

public static MapCodec<HealthPredicate> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
MinMaxBounds.Doubles.CODEC.optionalFieldOf("current", MinMaxBounds.Doubles.ANY).forGetter(o -> o.current),
MinMaxBounds.Doubles.CODEC.optionalFieldOf("percent", MinMaxBounds.Doubles.ANY).forGetter(o -> o.percent)
).apply(instance, HealthPredicate::new));

@Override
public MapCodec<HealthPredicate> codec() { return CODEC; }

@Override
public boolean matches(Entity entity, ServerLevel level, @Nullable Vec3 position) {
if (entity instanceof LivingEntity living) {
if (!current.matches(living.getHealth())) {
return false;
}
if (!percent.matches(living.getHealth() / living.getMaxHealth())) {
return false;
}
return true;
}
return false;
}


// ============================================================================================================== //

public static HealthPredicate current(MinMaxBounds.Doubles value) {
return new HealthPredicate(value, MinMaxBounds.Doubles.ANY);
}

public static HealthPredicate percent(MinMaxBounds.Doubles value) {
return new HealthPredicate(MinMaxBounds.Doubles.ANY, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.auioc.mcmod.arnicalib.ArnicaLib;
import org.auioc.mcmod.arnicalib.game.critereon.AttributePredicate;
import org.auioc.mcmod.arnicalib.game.critereon.FrozenPredicate;
import org.auioc.mcmod.arnicalib.game.critereon.HealthPredicate;

public class AHEntitySubPredicates {

Expand All @@ -36,4 +37,6 @@ public class AHEntitySubPredicates {

public static final DeferredHolder<MapCodec<? extends EntitySubPredicate>, MapCodec<AttributePredicate>> ATTRIBUTE = CODECS.register("attribute", () -> AttributePredicate.CODEC);

public static final DeferredHolder<MapCodec<? extends EntitySubPredicate>, MapCodec<HealthPredicate>> HEALTH = CODECS.register("health", () -> HealthPredicate.CODEC);

}

0 comments on commit 50c1433

Please sign in to comment.