6
6
package dev .lukebemish .dynamicassetgenerator .impl ;
7
7
8
8
import dev .lukebemish .dynamicassetgenerator .api .ResourceCache ;
9
+ import dev .lukebemish .dynamicassetgenerator .impl .util .ReentryDetector ;
9
10
import net .minecraft .resources .ResourceLocation ;
10
11
import net .minecraft .server .packs .PackResources ;
11
12
import net .minecraft .server .packs .PackType ;
22
23
public class GeneratedPackResources implements PackResources {
23
24
24
25
private final ResourceCache cache ;
25
- private Map <ResourceLocation , IoSupplier <InputStream >> streams ;
26
+ private @ Nullable Map <ResourceLocation , IoSupplier <InputStream >> streams ;
26
27
27
28
public GeneratedPackResources (ResourceCache cache ) {
28
29
this .cache = cache ;
@@ -50,34 +51,49 @@ public IoSupplier<InputStream> getRootResource(String @NonNull ... strings) {
50
51
@ Nullable
51
52
@ Override
52
53
public IoSupplier <InputStream > getResource (@ NonNull PackType packType , @ NonNull ResourceLocation location ) {
53
- if ( packType == cache . getPackType ( )) {
54
- if (getStreams (). containsKey ( location )) {
55
- return getStreams (). get ( location ) ;
54
+ try ( var lock = getResourceDetector . reentrant ( new GetResource ( packType , location ) )) {
55
+ if (lock . reentrant ( )) {
56
+ return null ;
56
57
}
58
+ if (packType == cache .getPackType ()) {
59
+ if (getStreams ().containsKey (location )) {
60
+ return getStreams ().get (location );
61
+ }
62
+ }
63
+ return null ;
57
64
}
58
- return null ;
59
65
}
60
66
61
67
@ Override
62
68
public void listResources (@ NonNull PackType packType , @ NonNull String namespace , @ NonNull String directory , @ NonNull ResourceOutput resourceOutput ) {
63
- if (packType == cache .getPackType ()) {
64
- for (ResourceLocation key : getStreams ().keySet ()) {
65
- if (key .getPath ().startsWith (directory ) && key .getNamespace ().equals (namespace ) && getStreams ().get (key ) != null ) {
66
- resourceOutput .accept (key , getStreams ().get (key ));
69
+ try (var lock = listResourcesDetector .reentrant (new ListResources (packType , namespace , directory ))) {
70
+ if (lock .reentrant ()) {
71
+ return ;
72
+ }
73
+ if (packType == cache .getPackType ()) {
74
+ for (ResourceLocation key : getStreams ().keySet ()) {
75
+ if (key .getPath ().startsWith (directory ) && key .getNamespace ().equals (namespace ) && getStreams ().get (key ) != null ) {
76
+ resourceOutput .accept (key , getStreams ().get (key ));
77
+ }
67
78
}
68
79
}
69
80
}
70
81
}
71
82
72
83
@ Override
73
84
public @ NonNull Set <String > getNamespaces (@ NonNull PackType type ) {
74
- Set <String > namespaces = new HashSet <>();
75
- if (type == cache .getPackType ()) {
76
- for (ResourceLocation key : getStreams ().keySet ()) {
77
- namespaces .add (key .getNamespace ());
85
+ try (var lock = getNamespacesDetector .reentrant (type )) {
86
+ if (lock .reentrant ()) {
87
+ return Set .of ();
78
88
}
89
+ Set <String > namespaces = new HashSet <>();
90
+ if (type == cache .getPackType ()) {
91
+ for (ResourceLocation key : getStreams ().keySet ()) {
92
+ namespaces .add (key .getNamespace ());
93
+ }
94
+ }
95
+ return namespaces ;
79
96
}
80
- return namespaces ;
81
97
}
82
98
83
99
@ SuppressWarnings ("unchecked" )
@@ -92,11 +108,19 @@ public <T> T getMetadataSection(MetadataSectionSerializer<T> deserializer) {
92
108
93
109
@ Override
94
110
public @ NonNull String packId () {
95
- return DynamicAssetGenerator .MOD_ID + '/' + cache .getName (). toString ();
111
+ return DynamicAssetGenerator .MOD_ID + '/' + cache .getName ();
96
112
}
97
113
98
114
@ Override
99
115
public void close () {
100
116
101
117
}
118
+
119
+ private record GetResource (@ NonNull PackType packType , @ NonNull ResourceLocation location ) {}
120
+ private final ReentryDetector <GetResource > getResourceDetector = new ReentryDetector <>();
121
+
122
+ private record ListResources (@ NonNull PackType packType , @ NonNull String namespace , @ NonNull String directory ) {}
123
+ private final ReentryDetector <ListResources > listResourcesDetector = new ReentryDetector <>();
124
+
125
+ private final ReentryDetector <PackType > getNamespacesDetector = new ReentryDetector <>();
102
126
}
0 commit comments