Skip to content

Only scan world if Villager profession has secondary POI - #14144

Merged
kennytv merged 2 commits into
PaperMC:mainfrom
Phoenix616:pr/improve-secondary-poi-sensor-performance
Aug 6, 2026
Merged

Only scan world if Villager profession has secondary POI#14144
kennytv merged 2 commits into
PaperMC:mainfrom
Phoenix616:pr/improve-secondary-poi-sensor-performance

Conversation

@Phoenix616

@Phoenix616 Phoenix616 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

In Vanilla all Villagers query (and potentially even load) all blocks in a 9x5x9 area around them via the SecondaryPoiSensor to search for valid secondary POIs even though most Villagers don't actually search for any. Only the profession "farmer" searches for a secondary POI (the farmland) in Vanilla making these.

As not the full logic can be seen in the patch, here is the unnecessary loop which is disabled unless the Villager's profession has secondary POI set:

        for (int x = -4; x <= 4; x++) {
            for (int y = -2; y <= 2; y++) {
                for (int z = -4; z <= 4; z++) {
                    BlockPos testPos = center.offset(x, y, z);
                    if (body.getVillagerData().profession().value().secondaryPoi().contains(level.getBlockState(testPos).getBlock())) {
                        jobSites.add(GlobalPos.of(dimensionType, testPos));
                    }
                }
            }
        }

        Brain<?> brain = body.getBrain();
        if (!jobSites.isEmpty()) {
            brain.setMemory(MemoryModuleType.SECONDARY_JOB_SITE, jobSites);
        } else {
            brain.eraseMemory(MemoryModuleType.SECONDARY_JOB_SITE);
        }

In order to keep the rest of the logic with the memory intact only this loop is disabled, not the whole sensor.

@Phoenix616
Phoenix616 requested a review from a team as a code owner August 5, 2026 13:55
@github-project-automation github-project-automation Bot moved this to Awaiting review in Paper PR Queue Aug 5, 2026
@electronicboy

Copy link
Copy Markdown
Member

This doesn't need to run the memory erasal logic?

@Phoenix616

Phoenix616 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

I'm unsure if I understand your question correctly. The memory erasure logic is still run (as the jobSites list will be empty) after the loop in order to guard against the case where the profession of a Villager or the secondaryPoi set of a profession changes without the SECONDARY_JOB_SITE memory being cleared. (I included the logic of the erasure now in the code block in the PR description)

@electronicboy

Copy link
Copy Markdown
Member

I blame my lack of sleep

@papermc-projects papermc-projects Bot moved this from Awaiting review to Full approval in Paper PR Queue Aug 5, 2026

@kennytv kennytv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for tracking purposes

Co-authored-by: Nassim Jahnke <nassim@njahnke.dev>
@MartijnMuijsers

Copy link
Copy Markdown
Contributor

@Phoenix616

Copy link
Copy Markdown
Contributor Author

Was already done in Lithium:

CaffeineMC/lithium@c42972b/common/src/main/java/net/caffeinemc/mods/lithium/mixin/ai/sensor/secondary_poi/SecondaryPoiSensorMixin.java#L13

Not sure if taken from it, but just mentioning the credit.

TIL. Good to know I'm not the only one coming to this conclusion but this was discovered independently while writing up this blog post.

@kennytv
kennytv merged commit 2035ad3 into PaperMC:main Aug 6, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from Full approval to Merged in Paper PR Queue Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

5 participants