Class EntitySelectorOptionRegistry

java.lang.Object
net.fabricmc.fabric.api.command.v2.EntitySelectorOptionRegistry

public final class EntitySelectorOptionRegistry extends Object
Contains a function to register an entity selector option.
  • Method Details

    • register

      public static void register(Identifier id, Text description, EntitySelectorOptions.SelectorHandler handler, Predicate<EntitySelectorReader> canUse)
      Registers an entity selector option. The added option is available under the underscore separated ID.

      Here's an example of a custom entity selector option. The option is registered under example_min_health and can be used like @e[example_min_health=5].

      
       EntitySelectorOptionRegistry.register(
       	new Identifier("example", "min_health"),
       	Text.literal("Minimum entity health"),
       	(reader) -> {
       	    final float minHealth = reader.getReader().readFloat();
      
       	    if (minHealth > 0) {
       	        reader.setPredicate((entity) -> entity instanceof LivingEntity livingEntity && livingEntity.getHealth() >= minHealth);
       	    }
       	},
       	(reader) -> true
       );
       

      By default, a selector option can be used multiple times. To make a non-repeatable option, either use FabricEntitySelectorReader to flag the existence of an option and check it inside canUse, or use registerNonRepeatable(net.minecraft.util.Identifier, net.minecraft.text.Text, net.minecraft.command.EntitySelectorOptions.SelectorHandler) instead of this method.

      Parameters:
      id - the ID of the option
      description - the description of the option
      handler - the handler for the entity option that reads and sets the predicate
      canUse - the predicate that checks whether the option is syntactically valid
    • registerNonRepeatable

      public static void registerNonRepeatable(Identifier id, Text description, EntitySelectorOptions.SelectorHandler handler)
      Registers an entity selector option. The added option is available under the underscore separated ID. The added option cannot be used multiple times within a single selector.
      Parameters:
      id - the ID of the option
      description - the description of the option
      handler - the handler for the entity option that reads and sets the predicate