tutorial:villager_activities
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Last revisionBoth sides next revision | ||
villager_activities [2021/09/27 02:29] – Fix title skullition | tutorial:villager_activities [2023/09/13 20:30] – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Activities ====== | ||
- | This is summary of how to add stuff for villagers to do. [[https:// | ||
- | An activity is a set of tasks. | ||
- | First register the activity: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | public static final Activity PRAY = Activity.register(" | ||
- | </ | ||
- | |||
- | |||
- | |||
- | Then create a task: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | public class FindTempleTask extends Task< | ||
- | public FindTempleTask() { | ||
- | super(ImmutableMap.of(ReligiousVillagersMod.MOSQUE_POINT, | ||
- | } | ||
- | |||
- | protected boolean shouldRun(ServerWorld world, VillagerEntity entity) { | ||
- | return world.getPointOfInterestStorage() | ||
- | .getNearestPosition( | ||
- | ReligiousVillagersMod.BELIEVER.getCompletionCondition(), | ||
- | entity.getBlockPos(), | ||
- | 48, | ||
- | PointOfInterestStorage.OccupationStatus.ANY | ||
- | ).isPresent(); | ||
- | } | ||
- | |||
- | protected void run(ServerWorld world, VillagerEntity entity, long time) { | ||
- | ReligiousVillagersMod.LOGGER.info(" | ||
- | |||
- | world.getPointOfInterestStorage() | ||
- | .getPositions( | ||
- | ReligiousVillagersMod.BELIEVER.getCompletionCondition(), | ||
- | (blockPos) -> { | ||
- | Path path = entity | ||
- | .getNavigation() | ||
- | .findPathTo(blockPos, | ||
- | return (path != null && path.reachesTarget()); | ||
- | }, | ||
- | entity.getBlockPos(), | ||
- | 48, | ||
- | PointOfInterestStorage.OccupationStatus.ANY | ||
- | ) | ||
- | .findAny() | ||
- | .ifPresent(blockPos -> { | ||
- | GlobalPos globalPos = GlobalPos.create(world.getRegistryKey(), | ||
- | entity.getBrain().remember(ReligiousVillagersMod.MOSQUE_POINT, | ||
- | }); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | This task just finds a ReligiousVillagersMod.MOSQUE_POINT: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | public static final MemoryModuleType< | ||
- | " | ||
- | GlobalPos.CODEC | ||
- | ); | ||
- | </ | ||
- | |||
- | The mosque point is a point of interest (like work benches, or a home): | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | static { | ||
- | | ||
- | " | ||
- | ); | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | private static void addPointsOfInterest() { | ||
- | VillagerEntityAccessor.setPointsOfInterest( | ||
- | new ImmutableMap.Builder< | ||
- | .putAll(VillagerEntity.POINTS_OF_INTEREST) | ||
- | .put(MOSQUE_POINT, | ||
- | .build()); | ||
- | } | ||
- | </ | ||
- | |||
- | The position is saved in a memory module: | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | VillagerEntityAccessor.setMemoryModules(new ImmutableList.Builder< | ||
- | .addAll(VillagerEntityAccessor.getMemoryModules()).add(MOSQUE_POINT).build()); | ||
- | </ | ||
- | |||
- | Once you have a task, you can create a set of tasks (aka Activity): | ||
- | |||
- | <code java [enable_line_numbers=" | ||
- | public static ImmutableList< | ||
- | return ImmutableList.of( | ||
- | Pair.of(1, | ||
- | Pair.of(2, | ||
- | MOSQUE_POINT, | ||
- | )), | ||
- | Pair.of(3, | ||
- | Pair.of(3, | ||
- | Pair.of(99, | ||
- | ); | ||
- | } | ||
- | </ | ||
- | |||
- | Then, you need to add the activity to their brain: | ||
- | |||
- | |||
- | <code java [enable_line_numbers=" | ||
- | @Mixin(VillagerEntity.class) | ||
- | public class VillagerEntityMixin { | ||
- | @Inject(at = @At(" | ||
- | private void initBrain(Brain< | ||
- | VillagerEntity $this = (VillagerEntity) (Object) this; | ||
- | |||
- | if (!$this.isBaby()) { | ||
- | brain.setTaskList( | ||
- | ReligiousVillagersMod.PRAY, | ||
- | createPrayTasks(0.75F) | ||
- | ); | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | And to their schedule: | ||
- | |||
- | |||
- | <code java [enable_line_numbers=" | ||
- | static { | ||
- | addScheduled(); | ||
- | } | ||
- | |||
- | private static void addScheduled() { | ||
- | ScheduleAccessor.setVillagerDefault( | ||
- | new ScheduleBuilder(Schedule.VILLAGER_DEFAULT).withActivity(10, | ||
- | } | ||
- | </ | ||
- | |||
- | If all went well and you have all the accessors, you should be able to put down an emerald block inside a village and villagers will pray before work: | ||
- | |||
- | {{ : |
tutorial/villager_activities.txt · Last modified: 2023/09/13 20:30 by nebelnidas