Add resourcepack-extensions to allow using bluemaps resourcepack system to load custom resources

This commit is contained in:
Lukas Rieger (Blue) 2024-11-02 16:55:16 +01:00
parent 462a7545d0
commit 4fc10b702e
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
4 changed files with 117 additions and 10 deletions

View File

@ -70,6 +70,8 @@ public class ResourcePack extends Pack {
private final BlockColorCalculatorFactory colorCalculatorFactory;
private final BlockPropertiesConfig blockPropertiesConfig;
private final Map<ResourcePackExtensionType<?>, ResourcePackExtension> resourcePackExtensions;
private final Map<String, ResourcePath<BlockState>> blockStatePaths;
private final Map<String, ResourcePath<Texture>> texturePaths;
private final LoadingCache<de.bluecolored.bluemap.core.world.BlockState, BlockProperties> blockPropertiesCache;
@ -87,6 +89,10 @@ public ResourcePack(int packVersion) {
this.colorCalculatorFactory = new BlockColorCalculatorFactory();
this.blockPropertiesConfig = new BlockPropertiesConfig();
this.resourcePackExtensions = new HashMap<>();
for (ResourcePackExtensionType<?> extensionType : ResourcePackExtensionType.REGISTRY.values())
resourcePackExtensions.put(extensionType, extensionType.create());
this.blockPropertiesCache = Caffeine.newBuilder()
.executor(BlueMap.THREAD_POOL)
.maximumSize(10000)
@ -203,6 +209,12 @@ private void loadResources(Path root) throws IOException {
if (cause != null) throw new IOException(cause);
throw new IOException(ex);
}
// invoke extensions
for (ResourcePackExtension extension : resourcePackExtensions.values()) {
extension.loadResources(root);
}
}
private void loadTextures(Path root) throws IOException {
@ -251,6 +263,13 @@ private void loadTextures(Path root) throws IOException {
if (cause != null) throw new IOException(cause);
throw new IOException(ex);
}
// invoke extensions
for (ResourcePackExtension extension : resourcePackExtensions.values()) {
extension.loadTextures(root)
.forEach(texture -> textures.put(texture.getResourcePath(), texture));
}
}
private void bake() throws IOException, InterruptedException {
@ -286,33 +305,33 @@ private void bake() throws IOException, InterruptedException {
if (grass == null) throw new IOException("Failed to bake resource-pack: No grass-colormap found!");
this.colorCalculatorFactory.setGrassMap(grass);
// invoke extensions
for (ResourcePackExtension extension : resourcePackExtensions.values()) {
extension.bake();
}
}
@Nullable
public BlockState getBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
public @Nullable BlockState getBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
ResourcePath<BlockState> path = blockStatePaths.get(blockState.getFormatted());
return path != null ? path.getResource(this::getBlockState) : MISSING_BLOCK_STATE.getResource(this::getBlockState);
}
@Nullable
public BlockState getBlockState(ResourcePath<BlockState> path) {
public @Nullable BlockState getBlockState(ResourcePath<BlockState> path) {
BlockState blockState = blockStates.get(path);
return blockState != null ? blockState : MISSING_BLOCK_STATE.getResource(blockStates::get);
}
@Nullable
public BlockModel getBlockModel(ResourcePath<BlockModel> path) {
public @Nullable BlockModel getBlockModel(ResourcePath<BlockModel> path) {
BlockModel blockModel = blockModels.get(path);
return blockModel != null ? blockModel : MISSING_BLOCK_MODEL.getResource(blockModels::get);
}
@Nullable
public ResourcePath<Texture> getTexturePath(String formatted) {
public @Nullable ResourcePath<Texture> getTexturePath(String formatted) {
return texturePaths.get(formatted);
}
@Nullable
public Texture getTexture(ResourcePath<Texture> path) {
public @Nullable Texture getTexture(ResourcePath<Texture> path) {
Texture texture = textures.get(path);
return texture != null ? texture : MISSING_TEXTURE.getResource(textures::get);
}
@ -348,4 +367,9 @@ private BlockProperties loadBlockProperties(de.bluecolored.bluemap.core.world.Bl
return props.build();
}
@SuppressWarnings({"unchecked", "unused"})
public <T extends ResourcePackExtension> @Nullable T getResourcePackExtension(ResourcePackExtensionType<T> extensionType) {
return (T) resourcePackExtensions.get(extensionType);
}
}

View File

@ -0,0 +1,43 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.resources.pack.resourcepack;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
public interface ResourcePackExtension {
default void loadResources(Path root) throws IOException {}
default Iterable<Texture> loadTextures(Path root) throws IOException {
return List.of();
}
default void bake() throws IOException {}
}

View File

@ -0,0 +1,36 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.resources.pack.resourcepack;
import de.bluecolored.bluemap.core.util.Keyed;
import de.bluecolored.bluemap.core.util.Registry;
public interface ResourcePackExtensionType<T extends ResourcePackExtension> extends Keyed {
Registry<ResourcePackExtensionType<?>> REGISTRY = new Registry<>();
T create();
}

View File

@ -98,6 +98,10 @@ public String getTexture() {
return texture;
}
public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage image) throws IOException {
return from(resourcePath, image, null);
}
public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage image, @Nullable AnimationMeta animation) throws IOException {
//check halfTransparency