2525package de .bluecolored .bluemap .core .resources ;
2626
2727import com .flowpowered .math .GenericMath ;
28+ import com .github .benmanes .caffeine .cache .Caffeine ;
29+ import com .github .benmanes .caffeine .cache .LoadingCache ;
2830import com .google .gson .stream .JsonReader ;
31+ import de .bluecolored .bluemap .core .BlueMap ;
32+ import de .bluecolored .bluemap .core .util .Key ;
2933import de .bluecolored .bluemap .core .util .math .Color ;
34+ import de .bluecolored .bluemap .core .world .BlockProperties ;
3035import de .bluecolored .bluemap .core .world .BlockState ;
3136import de .bluecolored .bluemap .core .world .biome .Biome ;
3237import de .bluecolored .bluemap .core .world .block .BlockAccess ;
3742import java .io .IOException ;
3843import java .nio .file .Files ;
3944import java .nio .file .Path ;
40- import java .util .HashMap ;
41- import java .util .Map ;
45+ import java .util .*;
46+ import java .util .concurrent .ConcurrentHashMap ;
47+ import java .util .concurrent .TimeUnit ;
4248
4349public class BlockColorCalculatorFactory {
4450
@@ -52,14 +58,22 @@ public class BlockColorCalculatorFactory {
5258 BLEND_MIN_Z = - BLEND_RADIUS_H ,
5359 BLEND_MAX_Z = BLEND_RADIUS_H ;
5460
61+ private static final ColorFunction DEFAULT_COLOR_FUNCTION =
62+ (c , b , target ) -> target .set (0xffffffff );
63+
5564 private int [] foliageMap = new int [0 ];
5665 private int [] dryFoliageMap = new int [0 ];
5766 private int [] grassMap = new int [0 ];
5867
59- private final Map <String , ColorFunction > blockColorMap ;
68+ private final Map <Key , List <BlockStateMapping <ColorFunction >>> mappings ;
69+ private final LoadingCache <BlockState , ColorFunction > colorFunctionCache = Caffeine .newBuilder ()
70+ .executor (BlueMap .THREAD_POOL )
71+ .maximumSize (10000 )
72+ .expireAfterAccess (1 , TimeUnit .MINUTES )
73+ .build (this ::findColorFunction );
6074
6175 public BlockColorCalculatorFactory () {
62- this .blockColorMap = new HashMap <>();
76+ this .mappings = new ConcurrentHashMap <>();
6377 }
6478
6579 public void load (Path configFile ) throws IOException {
@@ -70,7 +84,7 @@ public void load(Path configFile) throws IOException {
7084 json .beginObject ();
7185 while (json .hasNext ()) {
7286
73- String key = json .nextName ();
87+ BlockState key = BlockState . fromString ( json .nextName () );
7488 String value = json .nextString ();
7589
7690 ColorFunction colorFunction ;
@@ -97,8 +111,10 @@ public void load(Path configFile) throws IOException {
97111 break ;
98112 }
99113
114+ BlockStateMapping <ColorFunction > mapping = new BlockStateMapping <>(key , colorFunction );
115+
100116 // don't overwrite already present values, higher priority resources are loaded first
101- blockColorMap . putIfAbsent (key , colorFunction );
117+ mappings . computeIfAbsent (key . getId (), k -> new LinkedList <>()). add ( mapping );
102118 }
103119
104120 json .endObject ();
@@ -120,6 +136,19 @@ public void setGrassMap(BufferedImage map) {
120136 map .getRGB (0 , 0 , 256 , 256 , this .grassMap , 0 , 256 );
121137 }
122138
139+ private ColorFunction getColorFunction (BlockState blockState ) {
140+ return colorFunctionCache .get (blockState );
141+ }
142+
143+ private ColorFunction findColorFunction (BlockState blockState ) {
144+ for (BlockStateMapping <ColorFunction > bm : mappings .getOrDefault (blockState .getId (), Collections .emptyList ())){
145+ if (bm .fitsTo (blockState )){
146+ return bm .getMapping ();
147+ }
148+ }
149+ return DEFAULT_COLOR_FUNCTION ;
150+ }
151+
123152 public BlockColorCalculator createCalculator () {
124153 return new BlockColorCalculator ();
125154 }
@@ -140,13 +169,7 @@ public Color getBlockColor(BlockNeighborhood block, Color target) {
140169
141170 @ SuppressWarnings ("UnusedReturnValue" )
142171 public Color getBlockColor (BlockNeighborhood block , BlockState blockState , Color target ) {
143- String blockId = blockState .getId ().getFormatted ();
144-
145- ColorFunction colorFunction = blockColorMap .get (blockId );
146- if (colorFunction == null ) colorFunction = blockColorMap .get ("default" );
147- if (colorFunction == null ) colorFunction = BlockColorCalculator ::getBlendedFoliageColor ;
148-
149- return colorFunction .invoke (this , block , target );
172+ return getColorFunction (blockState ).invoke (this , block , target );
150173 }
151174
152175 public Color getRedstoneColor (BlockAccess block , Color target ) {
0 commit comments