Skip to content

Commit

Permalink
Port color manager from tinkers
Browse files Browse the repository at this point in the history
Uses a deprecated tinkering path for now
  • Loading branch information
KnightMiner committed Jun 4, 2022
1 parent 526c5ff commit a18d174
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/slimeknights/mantle/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.repository.FileRepository;
import slimeknights.mantle.client.model.FallbackModelLoader;
import slimeknights.mantle.client.model.NBTKeyModel;
Expand Down Expand Up @@ -60,6 +59,7 @@ static void registerEntityRenderers(EntityRenderersEvent.RegisterRenderers event
static void registerListeners(RegisterClientReloadListenersEvent event) {
event.registerReloadListener(ModelHelper.LISTENER);
event.registerReloadListener(new BookLoader());
ResourceColorManager.init(event);
}

@SubscribeEvent
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/slimeknights/mantle/client/ResourceColorManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package slimeknights.mantle.client;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.minecraft.network.chat.TextColor;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import slimeknights.mantle.data.ISafeManagerReloadListener;
import slimeknights.mantle.util.JsonHelper;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* Class allowing the resource pack to set colors for various things. Safe to call in serverside code, but will have no effect
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Log4j2
public class ResourceColorManager implements ISafeManagerReloadListener {
/** Modifier file to load, has merging behavior but forge prevents multiple mods from loading the same file */
private static final String COLORS_PATH = "mantle/colors.json";
/** Modifier file to load, has merging behavior but forge prevents multiple mods from loading the same file */
private static final String FALLBACK_PATH = "tinkering/colors.json";
/** Default color so the getter can be nonnull */
public static final TextColor WHITE = TextColor.fromRgb(-1);
/** Instance of this manager */
public static final ResourceColorManager INSTANCE = new ResourceColorManager();

/** Model overrides, if not in this map the default is used */
private static Map<String,TextColor> COLORS = Collections.emptyMap();

/**
* Initializes this manager, registering it with the resource manager
* @param manager Manager
*/
public static void init(RegisterClientReloadListenersEvent manager) {
manager.registerReloadListener(INSTANCE);
}

/** Recursively parses the given objects */
private static void parseRecursive(String prefix, JsonObject json, Map<String,TextColor> colors) {
// right now just do simply key value pairs
for (Entry<String,JsonElement> entry : json.entrySet()) {
String key = entry.getKey();
JsonElement element = entry.getValue();
// json means we combine the keys
if (element.isJsonObject()) {
parseRecursive(prefix + key + ".", element.getAsJsonObject(), colors);
} else if (element.isJsonPrimitive()) {
String fullPath = prefix + key;
if (!colors.containsKey(fullPath)) {
String text = element.getAsString();
TextColor color = TextColor.parseColor(text);
if (color == null) {
log.error("Color at key '{}' could not be parsed, got '{}'", fullPath, text);
} else {
colors.put(fullPath, color);
}
}
// treat nulls as comments
} else if (!element.isJsonNull()) {
log.error("Skipping color key '{}' as the value is not a string", key);
}
}
}

@Override
public void onReloadSafe(ResourceManager manager) {
// start building the model map
Map<String,TextColor> colors = new HashMap<>();

// get a list of files from all namespaces
List<JsonObject> jsonFiles = JsonHelper.getFileInAllDomainsAndPacks(manager, COLORS_PATH, null);
// first object is bottom most pack, so upper resource packs will replace it
for (int i = jsonFiles.size() - 1; i >= 0; i--) {
parseRecursive("", jsonFiles.get(i), colors);
}
// load in fallback files second, so mantle files take precedence
jsonFiles = JsonHelper.getFileInAllDomainsAndPacks(manager, FALLBACK_PATH, COLORS_PATH);
// first object is bottom most pack, so upper resource packs will replace it
for (int i = jsonFiles.size() - 1; i >= 0; i--) {
parseRecursive("", jsonFiles.get(i), colors);
}
// replace the map
COLORS = colors;
}

/** Gets the text color at the given path, or null if undefined */
@Nullable
public static TextColor getOrNull(String path) {
return COLORS.get(path);
}

/** Gets the text color at the given path */
public static TextColor getTextColor(String path) {
return COLORS.getOrDefault(path, WHITE);
}

/** Gets an integer color for the given path */
public static int getColor(String path) {
return getTextColor(path).getValue();
}
}
8 changes: 6 additions & 2 deletions src/main/java/slimeknights/mantle/util/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static JsonObject getJson(Resource resource) {
}

/** Gets a list of JSON objects for a single path in all domains and packs, for a language file like loader */
public static List<JsonObject> getFileInAllDomainsAndPacks(ResourceManager manager, String path) {
public static List<JsonObject> getFileInAllDomainsAndPacks(ResourceManager manager, String path, @Nullable String preferredPath) {
return manager
.getNamespaces().stream()
.filter(ResourceLocation::isValidNamespace)
Expand All @@ -257,7 +257,11 @@ public static List<JsonObject> getFileInAllDomainsAndPacks(ResourceManager manag
}
return Stream.empty();
})
.map(JsonHelper::getJson)
.map(preferredPath != null ? resource -> {
ResourceLocation loaded = resource.getLocation();
Mantle.logger.warn("Using deprecated path {} in pack {} - use {}:{} instead", resource.getSourceName(), loaded, loaded.getNamespace(), preferredPath);
return getJson(resource);
} : JsonHelper::getJson)
.filter(Objects::nonNull).toList();
}

Expand Down

0 comments on commit a18d174

Please sign in to comment.