Skip to content

Commit 6dedf12

Browse files
committed
Add support for properties for the block-color config and fix melon and pumpkin stems, fixes: #734
1 parent 7da80d0 commit 6dedf12

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

core/src/main/java/de/bluecolored/bluemap/core/resources/BlockColorCalculatorFactory.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
package de.bluecolored.bluemap.core.resources;
2626

2727
import com.flowpowered.math.GenericMath;
28+
import com.github.benmanes.caffeine.cache.Caffeine;
29+
import com.github.benmanes.caffeine.cache.LoadingCache;
2830
import com.google.gson.stream.JsonReader;
31+
import de.bluecolored.bluemap.core.BlueMap;
32+
import de.bluecolored.bluemap.core.util.Key;
2933
import de.bluecolored.bluemap.core.util.math.Color;
34+
import de.bluecolored.bluemap.core.world.BlockProperties;
3035
import de.bluecolored.bluemap.core.world.BlockState;
3136
import de.bluecolored.bluemap.core.world.biome.Biome;
3237
import de.bluecolored.bluemap.core.world.block.BlockAccess;
@@ -37,8 +42,9 @@
3742
import java.io.IOException;
3843
import java.nio.file.Files;
3944
import 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

4349
public 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) {

core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePack.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.Map;
6161
import java.util.Set;
6262
import java.util.concurrent.CompletableFuture;
63+
import java.util.concurrent.TimeUnit;
6364
import java.util.function.Predicate;
6465

6566
public class ResourcePack extends Pack {
@@ -106,6 +107,7 @@ public ResourcePack(PackVersion packVersion) {
106107
this.blockPropertiesCache = Caffeine.newBuilder()
107108
.executor(BlueMap.THREAD_POOL)
108109
.maximumSize(10000)
110+
.expireAfterAccess(1, TimeUnit.MINUTES)
109111
.build(this::loadBlockProperties);
110112

111113
this.extensions = new HashMap<>();

core/src/main/resourceExtensions/assets/minecraft/blockColors.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"default": "#ffffff",
32
"minecraft:water": "@water",
43
"minecraft:water_cauldron": "@water",
54
"minecraft:redstone_wire": "@redstone",
@@ -72,5 +71,23 @@
7271
"minecraft:purple_shulker_box": "#8932b8",
7372
"minecraft:magenta_shulker_box": "#c74ebd",
7473
"minecraft:pink_shulker_box": "#f38baa",
75-
"minecraft:lily_pad": "#208030"
74+
"minecraft:lily_pad": "#208030",
75+
"minecraft:pumpkin_stem[age=0]": "#00ff00",
76+
"minecraft:pumpkin_stem[age=1]": "#20f704",
77+
"minecraft:pumpkin_stem[age=2]": "#40ef08",
78+
"minecraft:pumpkin_stem[age=3]": "#60e70c",
79+
"minecraft:pumpkin_stem[age=4]": "#80df10",
80+
"minecraft:pumpkin_stem[age=5]": "#a0d714",
81+
"minecraft:pumpkin_stem[age=6]": "#c0cf18",
82+
"minecraft:pumpkin_stem[age=7]": "#e0c71c",
83+
"minecraft:attached_pumpkin_stem": "#e0c71c",
84+
"minecraft:melon_stem[age=0]": "#00ff00",
85+
"minecraft:melon_stem[age=1]": "#20f704",
86+
"minecraft:melon_stem[age=2]": "#40ef08",
87+
"minecraft:melon_stem[age=3]": "#60e70c",
88+
"minecraft:melon_stem[age=4]": "#80df10",
89+
"minecraft:melon_stem[age=5]": "#a0d714",
90+
"minecraft:melon_stem[age=6]": "#c0cf18",
91+
"minecraft:melon_stem[age=7]": "#e0c71c",
92+
"minecraft:attached_melon_stem": "#e0c71c"
7693
}

0 commit comments

Comments
 (0)