Skip to content

Error with spawning villagers #174

Closed
@TheBentoBox

Description

@TheBentoBox

The spawn a villager effect will regularly give internal errors. This appears to be due to Skript trying to assign a random profession to villagers upon spawn; Zombies now also use that same profession enum in Spigot due to 1.9 zombie villagers having varying professions like non-zombie villagers. Because of this, there are two "villager professions" that are actually zombie types. Weird, I know, but that's how Spigot's done it. If it attempts to randomly assign one of the two zombie types as the villager's profession, it throws the error and stops execution. This will even occur with just the single simple inline effect command of spawn a villager.

I believe the issue is occurring in the VillagerData.java file, in the spawn function, here:

@Override
@Nullable
public Villager spawn(final Location loc) {
    final Villager v = super.spawn(loc);
    if (v == null)
        return null;
    if (profession == null)
        v.setProfession(CollectionUtils.getRandom(Profession.values()));
    return v;
}

Suggested fix is to generate a randomized profession, and continue re-randomizing while the randomly generated profession is one meant to be assigned to zombies only, which is available via the profession.isZombie() function.

@Override
@Nullable
public Villager spawn(final Location loc) {
    final Villager v = super.spawn(loc);
    if (v == null)
        return null;
    if (profession == null)
    {
        Profession newProf;
        do {
            newProf = CollectionUtils.getRandom(Profession.values());
        } while (newProf.isZombie());
        v.setProfession(newProf);
    }
    return v;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue that needs to be fixed. Alternatively, a PR fixing an issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions