跳到主要内容

24自定义村民类型

自定义村民

新建一个自定义村民的类:

public class ModVillagers {
public static final RegistryKey<PointOfInterestType> SOUND_POI_KEY = poiKey("soundpoi");
public static final PointOfInterestType SOUND_POI = registerPoi("soundpoi", ModBlocks.SOUND_BLOCK);

public static final VillagerProfession SOUND_MASTER = registerProfession("sound_master", SOUND_POI_KEY);


private static VillagerProfession registerProfession(String name, RegistryKey<PointOfInterestType> type) {
return Registry.register(Registries.VILLAGER_PROFESSION, new Identifier(TutorialMod.MOD_ID, name),
new VillagerProfession(name, entry -> entry.matchesKey(type), entry -> entry.matchesKey(type),
ImmutableSet.of(), ImmutableSet.of(), SoundEvents.ENTITY_VILLAGER_WORK_SHEPHERD));
}

private static PointOfInterestType registerPoi(String name, Block block) {
return PointOfInterestHelper.register(new Identifier(TutorialMod.MOD_ID, name), 1, 1, block);
}

private static RegistryKey<PointOfInterestType> poiKey(String name) {
return RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, new Identifier(TutorialMod.MOD_ID, name));
}

public static void registerVillagers() {
TutorialMod.LOGGER.info("Registering Villagers " + TutorialMod.MOD_ID);
}
}

在启动类TutorialMod中注册:

ModVillagers.registerVillagers();

添加交易逻辑ModCustomTrades中:

TradeOfferHelper.registerVillagerOffers(ModVillagers.SOUND_MASTER, 1,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(ModItems.CORN, 32),
new ItemStack(ModBlocks.SOUND_BLOCK, 2),
6, 12, 0.075f));
});

TradeOfferHelper.registerVillagerOffers(ModVillagers.SOUND_MASTER, 2,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(ModItems.RUBY_SWORD, 1),
new ItemStack(ModItems.RUBY_HELMET, 1),
2, 12, 0.075f));
});

新建一个生成器:

public class ModPoiTagProvider extends TagProvider<PointOfInterestType> {
public ModPoiTagProvider(DataOutput output,
CompletableFuture<RegistryWrapper.WrapperLookup> registryLookupFuture) {
super(output, RegistryKeys.POINT_OF_INTEREST_TYPE, registryLookupFuture);
}

@Override
protected void configure(RegistryWrapper.WrapperLookup lookup) {
this.getOrCreateTagBuilder(PointOfInterestTypeTags.ACQUIRABLE_JOB_SITE)
.addOptional(new Identifier(TutorialMod.MOD_ID, "soundpoi"));
}
}

注册生成器TutorialModDataGenerator中:

pack.addProvider(ModPoiTagProvider::new);

效果

image-20240825094842615