Skip to content

Commit

Permalink
Fix pale_oak_leaves color and add loading of nested feature-datapacks
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Oct 17, 2024
1 parent b30cc4d commit d9a0850
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.adapter;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import de.bluecolored.bluemap.core.util.Key;

import java.io.IOException;

public class KeyAdapter extends TypeAdapter<Key> {

@Override
public void write(JsonWriter out, Key value) throws IOException {
out.value(value.getFormatted());
}

@Override
public Key read(JsonReader in) throws IOException {
return Key.parse(in.nextString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ResourcesGson {

public static GsonBuilder addAdapter(GsonBuilder builder) {
return builder
.registerTypeAdapter(Key.class, new KeyAdapter())
.registerTypeAdapter(Axis.class, new AxisAdapter())
.registerTypeAdapter(Color.class, new ColorAdapter())
.registerTypeAdapter(Direction.class, new DirectionAdapter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.adapter.ResourcesGson;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.util.Key;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletionException;
import java.util.stream.Stream;

Expand All @@ -48,6 +52,11 @@
public abstract class Pack {

private final int packVersion;
private final @Nullable Set<Key> enabledFeatures;

public Pack(int packVersion) {
this(packVersion, null);
}

public abstract void loadResources(Iterable<Path> roots) throws IOException, InterruptedException;

Expand All @@ -73,30 +82,72 @@ protected void loadResourcePath(Path root, ResourcePack.PathLoader resourceLoade
if (rootElement.has("jars")) {
for (JsonElement element : rootElement.getAsJsonArray("jars")) {
Path file = root.resolve(element.getAsJsonObject().get("file").getAsString());
if (Files.exists(file)) loadResourcePath(file, resourceLoader);
if (Files.exists(file)) {
try {
loadResourcePath(file, resourceLoader);
} catch (Exception ex) {
Logger.global.logDebug("Failed to read '" + root + "': " + ex);
}
}
}
}
} catch (Exception ex) {
Logger.global.logDebug("Failed to read fabric.mod.json: " + ex);
}
}

// load overlays
// load pack-meta
PackMeta packMeta;
Path packMetaFile = root.resolve("pack.mcmeta");
if (Files.isRegularFile(packMetaFile)) {
try (BufferedReader reader = Files.newBufferedReader(packMetaFile)) {
PackMeta packMeta = ResourcesGson.INSTANCE.fromJson(reader, PackMeta.class);
PackMeta.Overlay[] overlays = packMeta.getOverlays().getEntries();
for (int i = overlays.length - 1; i >= 0; i--) {
PackMeta.Overlay overlay = overlays[i];
String dir = overlay.getDirectory();
if (dir != null && overlay.getFormats().includes(this.packVersion)) {
Path overlayRoot = root.resolve(dir);
if (Files.exists(overlayRoot)) loadResourcePath(overlayRoot, resourceLoader);
}
}
packMeta = ResourcesGson.INSTANCE.fromJson(reader, PackMeta.class);
} catch (Exception ex) {
Logger.global.logDebug("Failed to read pack.mcmeta: " + ex);
packMeta = new PackMeta();
}
} else {
packMeta = new PackMeta();
}

// stop loading pack if feature is not enabled
if (enabledFeatures != null && !enabledFeatures.containsAll(packMeta.getFeatures().getEnabled())) {
Logger.global.logDebug("Skipping resources from '%s' because not all required features (%s) are enabled (%s)"
.formatted(
root,
Arrays.toString(packMeta.getFeatures().getEnabled().toArray()),
Arrays.toString(enabledFeatures.toArray())
));
return;
}

// load nested datapacks
list(root.resolve("data"))
.map(namespaceRoot -> namespaceRoot.resolve("datapacks"))
.filter(Files::isDirectory)
.flatMap(Pack::list)
.forEach(nestedPack -> {
try {
loadResourcePath(nestedPack, resourceLoader);
} catch (Exception ex) {
Logger.global.logDebug("Failed to load nested datapack '" + nestedPack + "': " + ex);
}
});

// load overlays
PackMeta.Overlay[] overlays = packMeta.getOverlays().getEntries();
for (int i = overlays.length - 1; i >= 0; i--) {
PackMeta.Overlay overlay = overlays[i];
String dir = overlay.getDirectory();
if (dir != null && overlay.getFormats().includes(this.packVersion)) {
Path overlayRoot = root.resolve(dir);
if (Files.exists(overlayRoot)) {
try {
loadResourcePath(overlayRoot, resourceLoader);
} catch (Exception ex) {
Logger.global.logDebug("Failed to load overlay '" + overlayRoot + "': " + ex);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import de.bluecolored.bluemap.core.resources.AbstractTypeAdapterFactory;
import de.bluecolored.bluemap.core.util.Key;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;

@Getter
@SuppressWarnings({"FieldMayBeFinal", "unused"})
public class PackMeta {

private Pack pack = new Pack();
private Overlays overlays = new Overlays();
private Features features = new Features();

@Getter
public static class Pack {
Expand All @@ -61,6 +65,11 @@ public static class Overlay {
private @Nullable String directory;
}

@Getter
public static class Features {
private Collection<Key> enabled = Set.of();
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"minecraft:blue_shulker_box": "#3c44aa",
"minecraft:purple_shulker_box": "#8932b8",
"minecraft:magenta_shulker_box": "#c74ebd",
"minecraft:pink_shulker_box": "#f38baa"
"minecraft:pink_shulker_box": "#f38baa",
"minecraft:pale_oak_leaves": "#ffffff"
}

0 comments on commit d9a0850

Please sign in to comment.