Skip to content

Commit 29fb3ac

Browse files
committed
Lazy load of structure template pools
1 parent 167f5c4 commit 29fb3ac

4 files changed

Lines changed: 61 additions & 69 deletions

File tree

common/src/main/java/com/faboslav/structurify/common/config/StructurifyConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Map<String, StructureTemplatePoolData> getStructureTemplatePoolsData() {
7777
}
7878

7979
public Map<String, StructureTemplatePoolData> getStructureTemplatePoolsDataForStructure(String structureId) {
80-
Set<String> structureTemplatePools = StructurifyTemplatePoolProvider.getStructureTemplatePoolIds().get(structureId);
80+
Set<String> structureTemplatePools = StructurifyTemplatePoolProvider.getStructureTemplatePoolIdsForStructure(structureId);
8181
return this.structureTemplatePoolsData.entrySet().stream()
8282
.filter(entry -> structureTemplatePools != null && structureTemplatePools.contains(entry.getKey()))
8383
.collect(java.util.stream.Collectors.toMap(

common/src/main/java/com/faboslav/structurify/common/registry/StructurifyResourcePackProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,19 @@ public static ArrayList<RepositorySource> getModsResourcePackProviders() {
5555
}
5656

5757
public static PackRepository getResourcePackRepository() {
58-
Structurify.getLogger().info("Loading resource pack repository...");
58+
// TODO maybe keep the logs only for debug purposes
59+
//Structurify.getLogger().info("Loading resource pack repository...");
5960
var resourcePackProviders = StructurifyResourcePackProvider.getResourcePackProviders();
6061

6162
for (var resourcePackProvider : resourcePackProviders) {
62-
Structurify.getLogger().info("Loaded resource pack provider: " + resourcePackProvider.getClass().getSimpleName());
63+
//Structurify.getLogger().info("Loaded resource pack provider: " + resourcePackProvider.getClass().getSimpleName());
6364
}
6465

65-
var resourcePackManager = new PackRepository(StructurifyResourcePackProvider.getResourcePackProviders().toArray(new RepositorySource[0]));
66+
var resourcePackManager = new PackRepository(resourcePackProviders.toArray(new RepositorySource[0]));
6667
PlatformHooks.PLATFORM_RESOURCE_PACK_PROVIDER.loadPlatformResourcePacks(resourcePackManager);
6768
resourcePackManager.reload();
6869
resourcePackManager.setSelected(resourcePackManager.getAvailableIds());
69-
Structurify.getLogger().info("Finished loading resource pack repository");
70+
//Structurify.getLogger().info("Finished loading resource pack repository");
7071

7172
return resourcePackManager;
7273
}

common/src/main/java/com/faboslav/structurify/common/registry/StructurifyTemplatePoolProvider.java

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import com.faboslav.structurify.common.mixin.structure.jigsaw.JigsawStructureAccessor;
66
import com.google.gson.JsonElement;
77
import com.google.gson.JsonObject;
8-
import com.mojang.serialization.JsonOps;
8+
import net.minecraft.core.Holder;
9+
import net.minecraft.core.registries.Registries;
910
import net.minecraft.nbt.CompoundTag;
1011
import net.minecraft.nbt.ListTag;
1112
import net.minecraft.nbt.NbtAccounter;
1213
import net.minecraft.nbt.NbtIo;
1314
import net.minecraft.resources.Identifier;
15+
import net.minecraft.resources.ResourceKey;
1416
import net.minecraft.server.packs.PackType;
1517
import net.minecraft.server.packs.resources.MultiPackResourceManager;
1618
import net.minecraft.server.packs.resources.Resource;
@@ -25,35 +27,8 @@
2527

2628
public final class StructurifyTemplatePoolProvider
2729
{
28-
private static boolean isLoading = false;
29-
private static Map<String, Map<String, Integer>> templatePoolElementWeights = Map.of();
30-
private static Map<String, Set<String>> structureTemplatePoolIds = Map.of();
31-
32-
public static void loadStructureTemplatePools() {
33-
if (isLoading) {
34-
return;
35-
}
36-
37-
isLoading = true;
38-
39-
try {
40-
Structurify.getLogger().info("Loading structure template pools...");
41-
var resourcePackRepository = StructurifyResourcePackProvider.getResourcePackRepository();
42-
43-
try (var resourceManager = new MultiPackResourceManager(
44-
PackType.SERVER_DATA,
45-
resourcePackRepository.openAllSelected()
46-
)) {
47-
loadStructureTemplatePools(resourceManager);
48-
}
49-
50-
Structurify.getLogger().info("Finished loading structure template pools");
51-
} catch (Exception exception) {
52-
Structurify.getLogger().error("Failed to load structure template pools.", exception);
53-
} finally {
54-
isLoading = false;
55-
}
56-
}
30+
private static final Map<String, Map<String, Integer>> templatePoolElementWeights = new TreeMap<>();
31+
private static final Map<String, Set<String>> structureTemplatePoolIds = new TreeMap<>();
5732

5833
@Nullable
5934
public static String getStructurePoolElementLocation(StructurePoolElement structurePoolElement) {
@@ -106,50 +81,70 @@ public static Map<String, Map<String, Integer>> getStructureTemplatePoolElements
10681
structureTemplatePools.put(structureTemplatePoolId, structureTemplatePoolElementsWithWeight);
10782
}
10883

109-
templatePoolElementWeights = structureTemplatePools;
84+
85+
templatePoolElementWeights.putAll(structureTemplatePools);
11086
}
11187

11288
return templatePoolElementWeights;
11389
}
11490

115-
public static Map<String, Set<String>> getStructureTemplatePoolIds() {
116-
if (structureTemplatePoolIds.isEmpty()) {
117-
loadStructureTemplatePools();
91+
public static Set<String> getStructureTemplatePoolIdsForStructure(String structureId) {
92+
if (!structureTemplatePoolIds.containsKey(structureId)) {
93+
var resourcePackRepository = StructurifyResourcePackProvider.getResourcePackRepository();
94+
95+
try (var resourceManager = new MultiPackResourceManager(
96+
PackType.SERVER_DATA,
97+
resourcePackRepository.openAllSelected()
98+
)) {
99+
var structureTemplatePools = loadStructureTemplatePoolsForStructure(resourceManager, structureId);
100+
structureTemplatePoolIds.put(structureId, structureTemplatePools);
101+
}
118102
}
119103

120-
return structureTemplatePoolIds;
104+
return structureTemplatePoolIds.get(structureId);
121105
}
122106

123-
private static void loadStructureTemplatePools(ResourceManager resourceManager) throws IOException {
124-
107+
private static Set<String> loadStructureTemplatePoolsForStructure(ResourceManager resourceManager, String structureId) {
125108
var structureRegistry = StructurifyRegistryManagerProvider.getStructureRegistry();
109+
126110
if (structureRegistry == null) {
127-
structureTemplatePoolIds = Map.of();
128-
return;
111+
return Set.of();
129112
}
130-
Map<String, Set<String>> structureTemplatePoolIds = new HashMap<>();
131-
for (var structureReference : structureRegistry.listElements().toList()) {
132-
Identifier structureId = structureReference.key()/*? if >= 1.21.11 {*/.identifier()/*?} else {*//*.location()*//*?}*/;
133-
Structure structure = structureReference.value();
134-
if (!(structure instanceof JigsawStructure jigsawStructure)) {
135-
continue;
136-
}
137-
var startPool = ((JigsawStructureAccessor) (Object) jigsawStructure).structurify$getOriginalStartPool().unwrapKey().orElse(null);
138-
if (startPool == null) {
139-
continue;
140-
}
141-
Set<Identifier> usedTemplatePools = collectStructureTemplatePools(resourceManager, startPool/*? if >= 1.21.11 {*/.identifier()/*?} else {*//*.location()*//*?}*/);
142-
if (!usedTemplatePools.isEmpty()) {
143-
structureTemplatePoolIds.put(
144-
structureId.toString(),
145-
usedTemplatePools.stream()
146-
.map(Identifier::toString)
147-
.collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new))
148-
);
149-
}
113+
114+
Optional<Holder.Reference<Structure>> structure = structureRegistry.get(ResourceKey.create(Registries.STRUCTURE, Structurify.makeNamespacedId(structureId)));
115+
116+
if(structure.isEmpty()) {
117+
return Set.of();
150118
}
151-
StructurifyTemplatePoolProvider.structureTemplatePoolIds = Map.copyOf(structureTemplatePoolIds);
152119

120+
return loadStructureTemplatePoolsForStructure(resourceManager, structure.get().value(), structureId);
121+
}
122+
123+
private static Set<String> loadStructureTemplatePoolsForStructure(ResourceManager resourceManager, Structure structure, String structureId) {
124+
if (!(structure instanceof JigsawStructure jigsawStructure)) {
125+
return Set.of();
126+
}
127+
128+
var startPool = ((JigsawStructureAccessor) (Object) jigsawStructure).structurify$getOriginalStartPool().unwrapKey().orElse(null);
129+
130+
if (startPool == null) {
131+
return Set.of();
132+
}
133+
134+
try {
135+
Set<Identifier> structureTemplatePools = collectStructureTemplatePools(resourceManager, startPool/*? if >= 1.21.11 {*/.identifier()/*?} else {*//*.location()*//*?}*/);
136+
137+
if(structureTemplatePools.isEmpty()) {
138+
return Set.of();
139+
}
140+
141+
return structureTemplatePools.stream()
142+
.map(Identifier::toString)
143+
.collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new));
144+
} catch(Exception e) {
145+
// TODO log error
146+
return Set.of();
147+
}
153148
}
154149

155150
private static Set<Identifier> collectStructureTemplatePools(

forge/src/main/java/com/faboslav/structurify/forge/platform/ForgePlatformHelper.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
import com.faboslav.structurify.common.Structurify;
44
import com.faboslav.structurify.common.platform.ModIconInfo;
55
import com.faboslav.structurify.common.platform.PlatformHelper;
6-
import com.mojang.blaze3d.platform.NativeImage;
7-
import net.minecraft.client.Minecraft;
8-
import net.minecraft.client.renderer.texture.DynamicTexture;
96
import net.minecraftforge.fml.ModList;
107
import net.minecraftforge.fml.loading.FMLPaths;
118
import com.faboslav.structurify.common.util.FileUtil;
129

1310
import javax.annotation.Nullable;
14-
import java.nio.file.Files;
1511
import java.nio.file.Path;
1612
import java.util.Optional;
1713

0 commit comments

Comments
 (0)