Fabric Resource Conditions interop

This commit is contained in:
Johannes Frohnmeyer 2022-01-23 15:27:00 +01:00
parent 3126d060a1
commit 38e585a77a
Signed by: Johannes
GPG Key ID: E76429612C2929F4
8 changed files with 109 additions and 9 deletions

View File

@ -0,0 +1,54 @@
# Fabric Resource Conditions
## Fabric Conditions from Respackopts
A json object containing a singular entry whose key is `fabric:load_conditions` will be treated as a Fabric Resource Condition.
Example:
```json
{
"condition": {
"fabric:load_conditions": {
"condition": "fabric:not",
"value": {
"condition": "fabric:all_mods_loaded",
"values": [
"a"
]
}
}
}
}
```
## Respackopts conditions from Fabric
You may access respackopts conditions from Fabric API Resource Condition blocks by specifying a condition with the ID
`config`. Its value will be treated as a respackopts condition.
Please be aware that fabric resource conditions are not tied to resource packs and entry IDs must therefore be specified fully.
(Using `someTexture` instead of `examplePack:someTexture` is impossible)
Example:
```json
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "minecraft:stick"
}
],
"result": {
"item": "minecraft:diamond"
},
"fabric:conditions": [
{
"condition": "fabric:not",
"value": {
"condition": "respackopts:config",
"value": {
"and": [
"examplePack:someTexture"
]
}
}
}
]
}
```

View File

@ -1,11 +1,11 @@
org.gradle.jvmargs=-Xmx1G
# https://fabricmc.net/versions.html
# https://fabricmc.net/develop/
minecraft_version=1.18.1
yarn_mappings=build.18
yarn_mappings=build.22
loader_version=0.12.12
maven_group=io.gitlab.jfronny
archives_base_name=respackopts
fabric_version=0.45.2+1.18
fabric_version=0.46.2+1.18
jfapi_version=2.2.0
modrinth_id=TiF5QWZY

View File

@ -18,8 +18,9 @@ nav:
- 'ToggleFiles.md'
- 'ToggleFilesWithFallback.md'
- 'MultipleFileSelection.md'
- 'Other usages':
- 'Integrations':
- 'Shaders.md'
- 'FabricResourceConditions.md'
- 'Old wiki':
- About: 'old/README.md'
- home: 'old/home.md'

View File

@ -1,8 +1,19 @@
{
"conditions": [
"lumi:subcategoryTest.enableLang",
{
"fabric:load_conditions": {
"condition": "respackopts:config",
"value": "lumi:subcategoryTest.enableLang"
}
},
{
"not": "subcategoryTest.enableLangForceDisable"
},
{
"fabric:load_conditions": {
"condition": "fabric:all_mods_loaded",
"values": ["respackopts"]
}
}
],
"fallback": "assets/minecraft/lang/en_us_joke.json",

View File

@ -2,6 +2,8 @@ package io.gitlab.jfronny.respackopts;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import io.gitlab.jfronny.respackopts.filters.DirFilterEventImpl;
import io.gitlab.jfronny.respackopts.filters.FileFilterEventImpl;
import io.gitlab.jfronny.respackopts.gson.*;
@ -21,19 +23,22 @@ import io.gitlab.jfronny.respackopts.model.tree.ConfigNumericEntry;
import io.gitlab.jfronny.respackopts.util.GuiFactory;
import io.gitlab.jfronny.respackopts.util.MetaCache;
import io.gitlab.jfronny.respackopts.util.RpoCommand;
import io.gitlab.jfronny.respackopts.util.RpoFormatException;
import meteordevelopment.starscript.Script;
import meteordevelopment.starscript.StandardLib;
import meteordevelopment.starscript.Starscript;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.impl.gui.FabricGuiEntry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.util.JsonHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
@ -50,10 +55,11 @@ public class Respackopts implements ClientModInitializer {
public static Starscript STAR_SCRIPT;
public static final String ID = "respackopts";
public static final Logger LOGGER = LogManager.getFormatterLogger(ID);
public static final Logger LOGGER = LoggerFactory.getLogger(ID);
public static final Identifier CONF_ID = new Identifier(ID, "conf.json");
public static final Set<Runnable> SAVE_ACTIONS = new HashSet<>();
public static final GuiFactory GUI_FACTORY = new GuiFactory();
public static final Identifier FAPI_CONDITION = new Identifier(ID, "config");
public static boolean forcePackReload = false;
public static Path FALLBACK_CONF_DIR;
@ -116,6 +122,15 @@ public class Respackopts implements ClientModInitializer {
if (FabricLoader.getInstance().isModLoaded("frex")) {
FrexCompat.onInitializeFrex();
}
ResourceConditions.register(FAPI_CONDITION, jsonObject -> {
try {
if (!jsonObject.has("value")) throw new RpoFormatException(FAPI_CONDITION + " does not have a value");
return GSON.fromJson(jsonObject.get("value"), Condition.class).evaluate("fabric");
} catch (RpoFormatException | JsonSyntaxException e) {
LOGGER.error("Could not parse condition", e);
return false;
}
});
}
public static String sanitizeString(String s) {

View File

@ -3,6 +3,7 @@ package io.gitlab.jfronny.respackopts.gson;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import io.gitlab.jfronny.respackopts.model.condition.*;
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions;
import java.lang.reflect.Type;
import java.util.List;
@ -29,6 +30,12 @@ public class ConditionDeserializer implements JsonDeserializer<Condition> {
throw new JsonParseException("More than one key in a condition object");
for (Map.Entry<String, JsonElement> entry : jo.entrySet()) {
String name = entry.getKey();
if (name.equals(ResourceConditions.CONDITIONS_KEY)) {
if (entry.getValue() instanceof JsonObject fabricCondition) {
return new FabricCondition(fabricCondition);
}
else throw new JsonParseException("Expected " + ResourceConditions.CONDITIONS_KEY + " to be an object condition");
}
for (CollectingConditionFactory factory : factories) {
if (factory.getNames().contains(name))
return factory.build(context.deserialize(entry.getValue(), conditionListType));

View File

@ -26,7 +26,7 @@ public class ScriptDeserializer implements JsonDeserializer<Script> {
Parser.Result result = Parser.parse(s);
if (result.hasErrors()) {
for (Error error : result.errors) {
Respackopts.LOGGER.error(error);
Respackopts.LOGGER.error(error.toString());
}
throw new JsonParseException("Could not parse script: See errors above");
}

View File

@ -0,0 +1,12 @@
package io.gitlab.jfronny.respackopts.model.condition;
import com.google.gson.JsonObject;
import io.gitlab.jfronny.respackopts.util.RpoFormatException;
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions;
public record FabricCondition(JsonObject condition) implements Condition {
@Override
public boolean evaluate(String packId) throws RpoFormatException {
return ResourceConditions.conditionMatches(condition);
}
}