Skip to content

Selector.RANDOM_SELECTION crashes on empty behavior list #93

@kangarko

Description

@kangarko

Companion to #92. With executing now reset in SimpleBehaviorController.clear(), the immediate crash is gone, but Selector.RANDOM_SELECTION still throws IllegalArgumentException: bound must be positive if getBehaviors() is empty when getNextBehavior runs:

https://github.com/CitizensDev/CitizensAPI/blob/master/src/main/java/net/citizensnpcs/api/ai/tree/Selector.java#L109-L110

private static final Function<List<Behavior>, Behavior> RANDOM_SELECTION = behaviors -> behaviors
        .get(RANDOM.nextInt(behaviors.size()));

Random.nextInt(0) blows up. Selector.run() already handles a null return cleanly:

if ((executing = getNextBehavior()) == null)
    return BehaviorStatus.FAILURE;

So an isEmpty() short-circuit would defang any future bug of this shape:

private static final Function<List<Behavior>, Behavior> RANDOM_SELECTION = behaviors -> behaviors.isEmpty()
        ? null
        : behaviors.get(RANDOM.nextInt(behaviors.size()));

Same idea for any other selectionFunction that indexes by Random.nextInt(size()).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions