Skip to content

Commit

Permalink
Add resourcepack-extensions to allow using bluemaps resourcepack syst…
Browse files Browse the repository at this point in the history
…em to load custom resources
  • Loading branch information
TBlueF committed Nov 2, 2024
1 parent 462a754 commit 4fc10b7
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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 {}

}
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4fc10b7

Please sign in to comment.