Skip to content

Commit

Permalink
Only use a one villager instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrbysco committed Sep 6, 2024
1 parent 7ccc5b2 commit 6f2b60c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mrbysco.justenoughprofessions;

import com.mrbysco.justenoughprofessions.platform.Services;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.function.Function;

public class VillagerCache {
private static Villager cachedVillager;

@Nullable
public static Villager getVillagerEntity(VillagerProfession profession) {
if (cachedVillager == null) {
CompoundTag nbt = new CompoundTag();
nbt.putString("id", Objects.requireNonNull(Services.PLATFORM.getEntityKey(EntityType.VILLAGER)).toString());
Minecraft mc = Minecraft.getInstance();
Level level = mc.hasSingleplayerServer() && mc.getSingleplayerServer() != null ? mc.getSingleplayerServer().getAllLevels().iterator().next() : mc.level;
if (level != null) {
Villager villager = (Villager) EntityType.loadEntityRecursive(nbt, level, Function.identity());
if (villager != null) {
cachedVillager = villager;
}
}
} else {
cachedVillager.setVillagerData(cachedVillager.getVillagerData().setProfession(profession));
return cachedVillager;
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.mrbysco.justenoughprofessions.jei;

import com.mrbysco.justenoughprofessions.platform.Services;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.EntityType;
import com.mrbysco.justenoughprofessions.VillagerCache;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
* A record to hold the profession and the block stacks for the profession
Expand All @@ -27,17 +21,6 @@ public record ProfessionEntry(VillagerProfession profession, List<ItemStack> blo
*/
@Nullable
public Villager getVillagerEntity() {
CompoundTag nbt = new CompoundTag();
nbt.putString("id", Objects.requireNonNull(Services.PLATFORM.getEntityKey(EntityType.VILLAGER)).toString());
Minecraft mc = Minecraft.getInstance();
Level level = mc.hasSingleplayerServer() && mc.getSingleplayerServer() != null ? mc.getSingleplayerServer().getAllLevels().iterator().next() : mc.level;
if (level != null) {
Villager villager = (Villager) EntityType.loadEntityRecursive(nbt, level, Function.identity());
if (villager != null) {
villager.setVillagerData(villager.getVillagerData().setProfession(this.profession));
return villager;
}
}
return null;
return VillagerCache.getVillagerEntity(this.profession);
}
}

0 comments on commit 6f2b60c

Please sign in to comment.