Skip to content

Commit fb79d39

Browse files
authored
Allow remapping blockstates and block properties via addons (#759)
* Add abillity for resourcepack-extensions to define blockstate-properties and alter the blockstate id to resource mapping * Add caching for key-mapping
1 parent db3889a commit fb79d39

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

core/src/main/java/de/bluecolored/bluemap/core/map/hires/block/BlockStateModelRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void render(BlockNeighborhood block, BlockState blockState, TileModelView
8282
private void renderModel(BlockNeighborhood block, BlockState blockState, TileModelView tileModel, Color blockColor) {
8383
int modelStart = tileModel.getStart();
8484

85-
var stateResource = resourcePack.getBlockStates().get(blockState.getId());
85+
var stateResource = resourcePack.getBlockState(blockState);
8686
if (stateResource == null) return;
8787

8888
float blockColorOpacity = 0;

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public interface Extension<T extends ResourcePackExtension> extends Keyed {
8787

8888
@Getter private final BlockColorCalculatorFactory colorCalculatorFactory;
8989
private final BlockPropertiesConfig blockPropertiesConfig;
90+
91+
private final LoadingCache<de.bluecolored.bluemap.core.world.BlockState, BlockState> blockStateCache;
9092
private final LoadingCache<de.bluecolored.bluemap.core.world.BlockState, BlockProperties> blockPropertiesCache;
9193

9294
private final Map<Extension<?>, ResourcePackExtension> extensions;
@@ -104,6 +106,11 @@ public ResourcePack(PackVersion packVersion) {
104106
this.colorCalculatorFactory = new BlockColorCalculatorFactory();
105107
this.blockPropertiesConfig = new BlockPropertiesConfig();
106108

109+
this.blockStateCache = Caffeine.newBuilder()
110+
.executor(BlueMap.THREAD_POOL)
111+
.maximumSize(10000)
112+
.expireAfterAccess(1, TimeUnit.MINUTES)
113+
.build(this::loadBlockState);
107114
this.blockPropertiesCache = Caffeine.newBuilder()
108115
.executor(BlueMap.THREAD_POOL)
109116
.maximumSize(10000)
@@ -355,15 +362,36 @@ private Atlas getBlocksAtlas() {
355362
return new Atlas();
356363
}
357364

365+
public BlockState getBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
366+
return blockStateCache.get(blockState);
367+
}
368+
369+
private BlockState loadBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
370+
Key key = blockState.getId();
371+
for (ResourcePackExtension extension : extensions.values()) {
372+
key = extension.getBlockStateKey(key);
373+
}
374+
return blockStates.get(key);
375+
}
376+
358377
public BlockProperties getBlockProperties(de.bluecolored.bluemap.core.world.BlockState state) {
359378
return blockPropertiesCache.get(state);
360379
}
361380

362381
private BlockProperties loadBlockProperties(de.bluecolored.bluemap.core.world.BlockState state) {
363-
BlockProperties.Builder props = blockPropertiesConfig.getBlockProperties(state).toBuilder();
382+
BlockProperties.Builder props = BlockProperties.builder();
383+
384+
// collect properties from extensions
385+
for (ResourcePackExtension extension : extensions.values()) {
386+
extension.getBlockProperties(state, props);
387+
}
388+
389+
// explicitly configured properties always have priority -> overwrite
390+
props.from(blockPropertiesConfig.getBlockProperties(state));
364391

392+
// calculate culling and occlusion from model if UNDEFINED
365393
if (props.isOccluding() == Tristate.UNDEFINED || props.isCulling() == Tristate.UNDEFINED) {
366-
BlockState resource = blockStates.get(state.getId());
394+
BlockState resource = getBlockState(state);
367395
if (resource != null) {
368396
resource.forEach(state,0, 0, 0, variant -> {
369397
Model model = variant.getModel().getResource(models::get);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
import de.bluecolored.bluemap.core.resources.pack.PackExtension;
2828
import de.bluecolored.bluemap.core.util.Key;
29+
import de.bluecolored.bluemap.core.world.BlockProperties;
30+
import de.bluecolored.bluemap.core.world.BlockState;
31+
import org.jetbrains.annotations.Nullable;
2932

3033
import java.util.Set;
3134

@@ -35,4 +38,10 @@ default Set<Key> collectUsedTextureKeys() {
3538
return Set.of();
3639
}
3740

41+
default Key getBlockStateKey(Key key) {
42+
return key;
43+
}
44+
45+
default void getBlockProperties(BlockState blockState, BlockProperties.Builder propertiesBuilder) {}
46+
3847
}

0 commit comments

Comments
 (0)