1
1
package dev .engine_room .flywheel .backend ;
2
2
3
3
import java .util .List ;
4
- import java .util .function .Function ;
5
4
6
- import org .jetbrains .annotations .Nullable ;
7
5
import org .jetbrains .annotations .Unmodifiable ;
8
6
9
7
import dev .engine_room .flywheel .api .material .CutoutShader ;
10
8
import dev .engine_room .flywheel .api .material .FogShader ;
11
- import dev .engine_room .flywheel .api .registry .Registry ;
12
9
import it .unimi .dsi .fastutil .objects .Object2IntMap ;
13
10
import it .unimi .dsi .fastutil .objects .Object2IntOpenHashMap ;
14
11
import it .unimi .dsi .fastutil .objects .ObjectArrayList ;
15
12
import it .unimi .dsi .fastutil .objects .ObjectList ;
16
13
import net .minecraft .resources .ResourceLocation ;
17
14
18
15
public final class MaterialShaderIndices {
19
- @ Nullable
20
- private static Index fogSources ;
21
- @ Nullable
22
- private static Index cutoutSources ;
16
+ private static final Index fogSources = new Index ();
17
+ private static final Index cutoutSources = new Index ();
23
18
24
19
private MaterialShaderIndices () {
25
20
}
26
21
27
22
public static Index fogSources () {
28
- if (fogSources == null ) {
29
- fogSources = indexFromRegistry (FogShader .REGISTRY , FogShader ::source );
30
- }
31
23
return fogSources ;
32
24
}
33
25
34
26
public static Index cutoutSources () {
35
- // if (cutoutSources == null) {
36
- // cutoutSources = indexFromRegistry(CutoutShader.REGISTRY, CutoutShader::source);
37
- // }
38
27
return cutoutSources ;
39
28
}
40
29
@@ -43,30 +32,23 @@ public static int fogIndex(FogShader fogShader) {
43
32
}
44
33
45
34
public static int cutoutIndex (CutoutShader cutoutShader ) {
46
- return 0 ;//cutoutSources().index(cutoutShader.source());
47
- }
48
-
49
- private static <T > Index indexFromRegistry (Registry <T > registry , Function <T , ResourceLocation > sourceFunc ) {
50
- if (!registry .isFrozen ()) {
51
- throw new IllegalStateException ("Cannot create index from registry that is not frozen!" );
52
- }
53
-
54
- var builder = new IndexBuilder ();
55
-
56
- for (T object : registry ) {
57
- builder .add (sourceFunc .apply (object ));
58
- }
59
-
60
- return builder .build ();
35
+ return cutoutSources ().index (cutoutShader .source ());
61
36
}
62
37
63
38
public static class Index {
64
39
private final Object2IntMap <ResourceLocation > sources2Index ;
65
40
private final ObjectList <ResourceLocation > sources ;
66
41
67
- private Index (IndexBuilder builder ) {
68
- this .sources2Index = new Object2IntOpenHashMap <>(builder .sources2Index );
69
- this .sources = new ObjectArrayList <>(builder .sources );
42
+ private Index () {
43
+ this .sources2Index = new Object2IntOpenHashMap <>();
44
+ sources2Index .defaultReturnValue (-1 );
45
+ this .sources = new ObjectArrayList <>();
46
+ }
47
+
48
+ public void add (ResourceLocation source ) {
49
+ if (sources2Index .putIfAbsent (source , sources .size ()) == -1 ) {
50
+ sources .add (source );
51
+ }
70
52
}
71
53
72
54
public int index (ResourceLocation source ) {
@@ -82,27 +64,4 @@ public List<ResourceLocation> all() {
82
64
return sources ;
83
65
}
84
66
}
85
-
86
- private static class IndexBuilder {
87
- private final Object2IntMap <ResourceLocation > sources2Index ;
88
- private final ObjectList <ResourceLocation > sources ;
89
- private int index = 0 ;
90
-
91
- public IndexBuilder () {
92
- sources2Index = new Object2IntOpenHashMap <>();
93
- sources2Index .defaultReturnValue (-1 );
94
- sources = new ObjectArrayList <>();
95
- }
96
-
97
- public void add (ResourceLocation source ) {
98
- if (sources2Index .putIfAbsent (source , index ) == -1 ) {
99
- sources .add (source );
100
- index ++;
101
- }
102
- }
103
-
104
- public Index build () {
105
- return new Index (this );
106
- }
107
- }
108
67
}
0 commit comments