|
5 | 5 | import com.faboslav.structurify.common.mixin.structure.jigsaw.JigsawStructureAccessor; |
6 | 6 | import com.google.gson.JsonElement; |
7 | 7 | import com.google.gson.JsonObject; |
8 | | -import com.mojang.serialization.JsonOps; |
| 8 | +import net.minecraft.core.Holder; |
| 9 | +import net.minecraft.core.registries.Registries; |
9 | 10 | import net.minecraft.nbt.CompoundTag; |
10 | 11 | import net.minecraft.nbt.ListTag; |
11 | 12 | import net.minecraft.nbt.NbtAccounter; |
12 | 13 | import net.minecraft.nbt.NbtIo; |
13 | 14 | import net.minecraft.resources.Identifier; |
| 15 | +import net.minecraft.resources.ResourceKey; |
14 | 16 | import net.minecraft.server.packs.PackType; |
15 | 17 | import net.minecraft.server.packs.resources.MultiPackResourceManager; |
16 | 18 | import net.minecraft.server.packs.resources.Resource; |
|
25 | 27 |
|
26 | 28 | public final class StructurifyTemplatePoolProvider |
27 | 29 | { |
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<>(); |
57 | 32 |
|
58 | 33 | @Nullable |
59 | 34 | public static String getStructurePoolElementLocation(StructurePoolElement structurePoolElement) { |
@@ -106,50 +81,70 @@ public static Map<String, Map<String, Integer>> getStructureTemplatePoolElements |
106 | 81 | structureTemplatePools.put(structureTemplatePoolId, structureTemplatePoolElementsWithWeight); |
107 | 82 | } |
108 | 83 |
|
109 | | - templatePoolElementWeights = structureTemplatePools; |
| 84 | + |
| 85 | + templatePoolElementWeights.putAll(structureTemplatePools); |
110 | 86 | } |
111 | 87 |
|
112 | 88 | return templatePoolElementWeights; |
113 | 89 | } |
114 | 90 |
|
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 | + } |
118 | 102 | } |
119 | 103 |
|
120 | | - return structureTemplatePoolIds; |
| 104 | + return structureTemplatePoolIds.get(structureId); |
121 | 105 | } |
122 | 106 |
|
123 | | - private static void loadStructureTemplatePools(ResourceManager resourceManager) throws IOException { |
124 | | - |
| 107 | + private static Set<String> loadStructureTemplatePoolsForStructure(ResourceManager resourceManager, String structureId) { |
125 | 108 | var structureRegistry = StructurifyRegistryManagerProvider.getStructureRegistry(); |
| 109 | + |
126 | 110 | if (structureRegistry == null) { |
127 | | - structureTemplatePoolIds = Map.of(); |
128 | | - return; |
| 111 | + return Set.of(); |
129 | 112 | } |
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(); |
150 | 118 | } |
151 | | - StructurifyTemplatePoolProvider.structureTemplatePoolIds = Map.copyOf(structureTemplatePoolIds); |
152 | 119 |
|
| 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 | + } |
153 | 148 | } |
154 | 149 |
|
155 | 150 | private static Set<Identifier> collectStructureTemplatePools( |
|
0 commit comments