From 4fc10b702e667d404c477e81276d197c0148bee1 Mon Sep 17 00:00:00 2001 From: "Lukas Rieger (Blue)" Date: Sat, 2 Nov 2024 16:55:16 +0100 Subject: [PATCH] Add resourcepack-extensions to allow using bluemaps resourcepack system to load custom resources --- .../pack/resourcepack/ResourcePack.java | 44 ++++++++++++++----- .../resourcepack/ResourcePackExtension.java | 43 ++++++++++++++++++ .../ResourcePackExtensionType.java | 36 +++++++++++++++ .../pack/resourcepack/texture/Texture.java | 4 ++ 4 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtension.java create mode 100644 core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtensionType.java diff --git a/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePack.java b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePack.java index 28495ded..700f0c9c 100644 --- a/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePack.java +++ b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePack.java @@ -70,6 +70,8 @@ public class ResourcePack extends Pack { private final BlockColorCalculatorFactory colorCalculatorFactory; private final BlockPropertiesConfig blockPropertiesConfig; + private final Map, ResourcePackExtension> resourcePackExtensions; + private final Map> blockStatePaths; private final Map> texturePaths; private final LoadingCache 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 path = blockStatePaths.get(blockState.getFormatted()); return path != null ? path.getResource(this::getBlockState) : MISSING_BLOCK_STATE.getResource(this::getBlockState); } - @Nullable - public BlockState getBlockState(ResourcePath path) { + public @Nullable BlockState getBlockState(ResourcePath path) { BlockState blockState = blockStates.get(path); return blockState != null ? blockState : MISSING_BLOCK_STATE.getResource(blockStates::get); } - @Nullable - public BlockModel getBlockModel(ResourcePath path) { + public @Nullable BlockModel getBlockModel(ResourcePath path) { BlockModel blockModel = blockModels.get(path); return blockModel != null ? blockModel : MISSING_BLOCK_MODEL.getResource(blockModels::get); } - @Nullable - public ResourcePath getTexturePath(String formatted) { + public @Nullable ResourcePath getTexturePath(String formatted) { return texturePaths.get(formatted); } - @Nullable - public Texture getTexture(ResourcePath path) { + public @Nullable Texture getTexture(ResourcePath 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 @Nullable T getResourcePackExtension(ResourcePackExtensionType extensionType) { + return (T) resourcePackExtensions.get(extensionType); + } + } diff --git a/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtension.java b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtension.java new file mode 100644 index 00000000..ad3b9392 --- /dev/null +++ b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtension.java @@ -0,0 +1,43 @@ +/* + * This file is part of BlueMap, licensed under the MIT License (MIT). + * + * Copyright (c) Blue (Lukas Rieger) + * 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 loadTextures(Path root) throws IOException { + return List.of(); + } + + default void bake() throws IOException {} + +} diff --git a/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtensionType.java b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtensionType.java new file mode 100644 index 00000000..ccc8b9b8 --- /dev/null +++ b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/ResourcePackExtensionType.java @@ -0,0 +1,36 @@ +/* + * This file is part of BlueMap, licensed under the MIT License (MIT). + * + * Copyright (c) Blue (Lukas Rieger) + * 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 extends Keyed { + + Registry> REGISTRY = new Registry<>(); + + T create(); + +} diff --git a/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/texture/Texture.java b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/texture/Texture.java index b20cdbf6..80716cd0 100644 --- a/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/texture/Texture.java +++ b/core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/texture/Texture.java @@ -98,6 +98,10 @@ public String getTexture() { return texture; } + public static Texture from(ResourcePath resourcePath, BufferedImage image) throws IOException { + return from(resourcePath, image, null); + } + public static Texture from(ResourcePath resourcePath, BufferedImage image, @Nullable AnimationMeta animation) throws IOException { //check halfTransparency