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 Summary

    Modifier and Type
    Method
    Description
    static void
    register(net.minecraft.resources.Identifier id, net.minecraft.network.chat.Component description, net.minecraft.commands.arguments.selector.options.EntitySelectorOptions.Modifier modifier, Predicate<net.minecraft.commands.arguments.selector.EntitySelectorParser> canUse)
    Registers an entity selector option.
    static void
    registerNonRepeatable(net.minecraft.resources.Identifier id, net.minecraft.network.chat.Component description, net.minecraft.commands.arguments.selector.options.EntitySelectorOptions.Modifier modifier)
    Registers an entity selector option.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • register

      public static void register(net.minecraft.resources.Identifier id, net.minecraft.network.chat.Component description, net.minecraft.commands.arguments.selector.options.EntitySelectorOptions.Modifier modifier, Predicate<net.minecraft.commands.arguments.selector.EntitySelectorParser> 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(
      	Identifier.fromNamespaceAndPath("modid", "min_health"),
      	Component.literal("Minimum entity health"),
      	(parser) -> {
      	    final float minHealth = parser.getReader().readFloat();
      
      	    if (minHealth > 0) {
      	        parser.addPredicate((entity) -> entity instanceof LivingEntity livingEntity && livingEntity.getHealth() >= minHealth);
      	    }
      	},
      	(parser) -> true
      );
      

      By default, a selector option can be used multiple times. To make a non-repeatable option, either use FabricEntitySelectorParser to flag the existence of an option and check it inside canUse, or use registerNonRepeatable(Identifier, Component, EntitySelectorOptions.Modifier) instead of this method.

      Parameters:
      id - the ID of the option
      description - the description of the option
      modifier - the modifier 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(net.minecraft.resources.Identifier id, net.minecraft.network.chat.Component description, net.minecraft.commands.arguments.selector.options.EntitySelectorOptions.Modifier modifier)
      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
      modifier - the modifier for the entity option that reads and sets the predicate