Skip to content

Conditions Registry

Redheris edited this page Nov 3, 2026 · 1 revision

Registry

PreventBuild doesn't have all its conditions hardcoded and uses registry system to associate a parsing keyword with a condition factory returning a new object of some implementation of the condition class. It also designed such way that other mods can register new conditions using the mod's API.

What you should know

  • Any condition class implements Condition interface;
  • ConditionFactory is a functional interface taking RegistryAccess and Object value and returning a new Condition object;
  • ConditionRegistry stores pairs condition_keyword:ConditionFactory
    • You can register several keywords creating an instance of the same condition class. For example, any coordinate condition;

Built-in conditions

  • Some conditions use Identifiers and Tags for values. There's IdentifierTagWrapper class to work with such values
  • Condition values are provided as raw Objects, and you have to cast them to your required types. Built-in solutions are ConditionValueValidator and ConditionValueExtractor. At least make sure of what exceptions they are throwing;
  • Every built-in condition extends ConditionBase abstract class;
    • It provides default behavior for every category so you shouldn't implement all checks if you don't need some of them. For example, Player's coordinate conditions only override checkDefault() method because they don't need other context;
  • All built-in conditions are registered in Conditions class.

Example

I believe you should be able to add the mod as Gradle dependency using Modrinth's maven. Open the needed version download page and read the Developer information block.

Your class

import dev.redheris.preventbuild.conditions.ConditionBase;
import dev.redheris.preventbuild.conditions.ConditionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

public class SwimmingCondition extends ConditionBase {
    // Some specified state
    private final boolean isSwimming;

    public SwimmingCondition(boolean isSwimming) {
        this.isSwimming = isSwimming;
    }

    // Depending on whether the player is swimming, an action
	// will be canceled regardless of what category it was used in
    @Override
    public ConditionResult checkDefault(Player player, Level level) {
        return player.isSwimming() == isSwimming ? ConditionResult.CANCEL : ConditionResult.PASS;
    }
}

Registering your keyword

import dev.redheris.preventbuild.api.ConditionFactory;
import dev.redheris.preventbuild.api.ConditionRegistry;
import com.example.examplemod.conditions.SwimmingCondition;

import static dev.redheris.preventbuild.conditions.util.ConditionValueValidator.requireBoolean;

public class ExampleModClient implements ClientModInitializer {
    
    @Override
    public void onInitializeClient() {
        // Registering creating a new SwimmingCondition condition
        // with the "isSwimming" keyword
        ConditionRegistry.register(
                "isSwimming",
                (regAccess, value) -> new SwimmingCondition(requireBoolean(value))
        );
    }
}

Now you should be able to use your "isSwimming" condition in your condition configs. If you use keyword will be unknown, the process of updating the configs will be failed with a corresponding exception message.

Clone this wiki locally