-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
19 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
common/src/main/java/com/mrbysco/justenoughprofessions/VillagerCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters