Skip to content

Commit 4fc10b7

Browse files
committed
Add resourcepack-extensions to allow using bluemaps resourcepack system to load custom resources
1 parent 462a754 commit 4fc10b7

File tree

4 files changed

+117
-10
lines changed

4 files changed

+117
-10
lines changed

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class ResourcePack extends Pack {
7070
private final BlockColorCalculatorFactory colorCalculatorFactory;
7171
private final BlockPropertiesConfig blockPropertiesConfig;
7272

73+
private final Map<ResourcePackExtensionType<?>, ResourcePackExtension> resourcePackExtensions;
74+
7375
private final Map<String, ResourcePath<BlockState>> blockStatePaths;
7476
private final Map<String, ResourcePath<Texture>> texturePaths;
7577
private final LoadingCache<de.bluecolored.bluemap.core.world.BlockState, BlockProperties> blockPropertiesCache;
@@ -87,6 +89,10 @@ public ResourcePack(int packVersion) {
8789
this.colorCalculatorFactory = new BlockColorCalculatorFactory();
8890
this.blockPropertiesConfig = new BlockPropertiesConfig();
8991

92+
this.resourcePackExtensions = new HashMap<>();
93+
for (ResourcePackExtensionType<?> extensionType : ResourcePackExtensionType.REGISTRY.values())
94+
resourcePackExtensions.put(extensionType, extensionType.create());
95+
9096
this.blockPropertiesCache = Caffeine.newBuilder()
9197
.executor(BlueMap.THREAD_POOL)
9298
.maximumSize(10000)
@@ -203,6 +209,12 @@ private void loadResources(Path root) throws IOException {
203209
if (cause != null) throw new IOException(cause);
204210
throw new IOException(ex);
205211
}
212+
213+
// invoke extensions
214+
for (ResourcePackExtension extension : resourcePackExtensions.values()) {
215+
extension.loadResources(root);
216+
}
217+
206218
}
207219

208220
private void loadTextures(Path root) throws IOException {
@@ -251,6 +263,13 @@ private void loadTextures(Path root) throws IOException {
251263
if (cause != null) throw new IOException(cause);
252264
throw new IOException(ex);
253265
}
266+
267+
// invoke extensions
268+
for (ResourcePackExtension extension : resourcePackExtensions.values()) {
269+
extension.loadTextures(root)
270+
.forEach(texture -> textures.put(texture.getResourcePath(), texture));
271+
}
272+
254273
}
255274

256275
private void bake() throws IOException, InterruptedException {
@@ -286,33 +305,33 @@ private void bake() throws IOException, InterruptedException {
286305
if (grass == null) throw new IOException("Failed to bake resource-pack: No grass-colormap found!");
287306
this.colorCalculatorFactory.setGrassMap(grass);
288307

308+
// invoke extensions
309+
for (ResourcePackExtension extension : resourcePackExtensions.values()) {
310+
extension.bake();
311+
}
312+
289313
}
290314

291-
@Nullable
292-
public BlockState getBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
315+
public @Nullable BlockState getBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
293316
ResourcePath<BlockState> path = blockStatePaths.get(blockState.getFormatted());
294317
return path != null ? path.getResource(this::getBlockState) : MISSING_BLOCK_STATE.getResource(this::getBlockState);
295318
}
296319

297-
@Nullable
298-
public BlockState getBlockState(ResourcePath<BlockState> path) {
320+
public @Nullable BlockState getBlockState(ResourcePath<BlockState> path) {
299321
BlockState blockState = blockStates.get(path);
300322
return blockState != null ? blockState : MISSING_BLOCK_STATE.getResource(blockStates::get);
301323
}
302324

303-
@Nullable
304-
public BlockModel getBlockModel(ResourcePath<BlockModel> path) {
325+
public @Nullable BlockModel getBlockModel(ResourcePath<BlockModel> path) {
305326
BlockModel blockModel = blockModels.get(path);
306327
return blockModel != null ? blockModel : MISSING_BLOCK_MODEL.getResource(blockModels::get);
307328
}
308329

309-
@Nullable
310-
public ResourcePath<Texture> getTexturePath(String formatted) {
330+
public @Nullable ResourcePath<Texture> getTexturePath(String formatted) {
311331
return texturePaths.get(formatted);
312332
}
313333

314-
@Nullable
315-
public Texture getTexture(ResourcePath<Texture> path) {
334+
public @Nullable Texture getTexture(ResourcePath<Texture> path) {
316335
Texture texture = textures.get(path);
317336
return texture != null ? texture : MISSING_TEXTURE.getResource(textures::get);
318337
}
@@ -348,4 +367,9 @@ private BlockProperties loadBlockProperties(de.bluecolored.bluemap.core.world.Bl
348367
return props.build();
349368
}
350369

370+
@SuppressWarnings({"unchecked", "unused"})
371+
public <T extends ResourcePackExtension> @Nullable T getResourcePackExtension(ResourcePackExtensionType<T> extensionType) {
372+
return (T) resourcePackExtensions.get(extensionType);
373+
}
374+
351375
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.core.resources.pack.resourcepack;
26+
27+
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
28+
29+
import java.io.IOException;
30+
import java.nio.file.Path;
31+
import java.util.List;
32+
33+
public interface ResourcePackExtension {
34+
35+
default void loadResources(Path root) throws IOException {}
36+
37+
default Iterable<Texture> loadTextures(Path root) throws IOException {
38+
return List.of();
39+
}
40+
41+
default void bake() throws IOException {}
42+
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.core.resources.pack.resourcepack;
26+
27+
import de.bluecolored.bluemap.core.util.Keyed;
28+
import de.bluecolored.bluemap.core.util.Registry;
29+
30+
public interface ResourcePackExtensionType<T extends ResourcePackExtension> extends Keyed {
31+
32+
Registry<ResourcePackExtensionType<?>> REGISTRY = new Registry<>();
33+
34+
T create();
35+
36+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public String getTexture() {
9898
return texture;
9999
}
100100

101+
public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage image) throws IOException {
102+
return from(resourcePath, image, null);
103+
}
104+
101105
public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage image, @Nullable AnimationMeta animation) throws IOException {
102106

103107
//check halfTransparency

0 commit comments

Comments
 (0)