-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fabric Resource Conditions API #1656
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conditions should be registered using identifiers instead of strings.
That would make the real name of the builtin conditions something like BTW: isn't |
Actually is does have a use under a different name
|
I don't know how useful it would be, but you can also add some syntatic sugar by supporting things like:
"ex falso quodlibet" :-) |
Shouldn't this error handling be inside the framework? Something like:
I am also thinking of possible future enhancement if additional cross cutting logic needs to applied when a condition is tested.
That's just an example, not a serious suggestion. :-) |
These are good suggestions (besides the last one :-P), but I think this API is pending a datagen API, as we will have to integrate it. Perhaps a more complex approach where serializers have to be registered could be useful for datagen, but for that we need a datagen API (and thus global access wideners... hopefully we will have those in a few weeks :)). |
What's the status on this PR? |
Waiting for datagen API, which itself is waiting for #1802. |
@Technici4n Do you plan on working on this soon now that #1824 and #1802 are merged? |
Welp, both of these PRs have already been merged, so... |
Yes, see #api on discord for the moment as there are some early design decisions that need to be made. |
bfeb102
to
cf080ce
Compare
cf080ce
to
3cceea6
Compare
Updated the PR with a new approach, going to continue this over the coming days. |
* Check if the passed JSON object either has no {@code fabric:conditions} tag, or all of its conditions match. | ||
* This should be called for objects that may contain a conditions entry. | ||
*/ | ||
public static boolean objectMatchesConditions(JsonObject object) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this? I am not so sure about this...
public static boolean objectMatchesConditions(JsonObject object) { | |
public static boolean resourceMatchesConditions(JsonObject object) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that these conditions can also be used for nested objects, not necessarily the resource itself. For example if you only want some outputs to be produced if a mod is loaded.
...1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/FabricResourceConditions.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still believe that a file/directory-level (instead of file content-based) approach would be simpler and easier to use. For one, it allows to exclude entire directories by default without duplicating the same fabric:load_conditions
block to each JSON file. This works for mods that organise their mod compat files by the supported mod (data/adorn/loot_tables/blocks/terrestria
etc) It also works automatically for each and every data type.
In any case, here's a review of what you have now.
...i-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ConditionJsonProvider.java
Outdated
Show resolved
Hide resolved
...i-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ConditionJsonProvider.java
Outdated
Show resolved
Hide resolved
...-api-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ResourceConditions.java
Outdated
Show resolved
Hide resolved
.../src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/DefaultResourceConditions.java
Show resolved
Hide resolved
...-api-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ResourceConditions.java
Outdated
Show resolved
Hide resolved
...pi-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLootTableProvider.java
Outdated
Show resolved
Hide resolved
/** | ||
* Used to keep track of conditions associated to generated objects. | ||
*/ | ||
private static final Map<Object, ConditionJsonProvider[]> CONDITIONS_MAP = new IdentityHashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit cursed, but I can't think of any better solutions either
...i-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ConditionJsonProvider.java
Show resolved
Hide resolved
...-api-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ResourceConditions.java
Outdated
Show resolved
Hide resolved
...i-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ConditionJsonProvider.java
Outdated
Show resolved
Hide resolved
...i-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ConditionJsonProvider.java
Outdated
Show resolved
Hide resolved
...-api-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ResourceConditions.java
Outdated
Show resolved
Hide resolved
...-api-v1/src/main/java/net/fabricmc/fabric/api/resource/conditions/v1/ResourceConditions.java
Outdated
Show resolved
Hide resolved
I recall shedaniel's draft had metadata files defined for any resource file (like |
@liach Note that tags don't need it since the values can be marked as optional, in which case they're ignored if not present. |
Message I wrote on discord:
TL;DR 1 is good enough, guaranteed not to be slower and much easier to implement. |
} | ||
|
||
Preconditions.checkArgument(conditions.length > 0, "Must write at least one condition."); // probably a programmer error | ||
if (conditionalObject.has(ResourceConditions.CONDITIONS_KEY)) throw new IllegalArgumentException("Object already has a condition entry: " + conditionalObject); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we try to merge the conditions if there are already existing ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Datagen already merges multiple ConditionJsonProvider[] together.
* @throws IllegalArgumentException if the JSON object already contains that array | ||
*/ | ||
static void write(JsonObject conditionalObject, ConditionJsonProvider @Nullable... conditions) { | ||
if (conditions == null) { // no condition -> skip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be better if we handle the null and the 0-length array case the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 0-length case is usually a programmer error, while the null
case is conveniently used to skip unspecified entries in the datagen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 0-length case is usually a programmer error, while the
null
case is conveniently used to skip unspecified entries in the datagen.
Then why not just ask the modders to not call this method if they have nothing to write to the conditions after all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I guess modders could make use of it in their own datagen if they need their own conditions
* First completed draft * Update PR, should be close to ready now * Add fluid/item_tags_populated conditions * Log processed resource with trace log level * Use debug instead of trace log level * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Code review suggestions * writeAdditional -> writeParameters * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * CONDITION_ID -> CONDITION_ID_KEY
5eea774
to
abf02f7
Compare
Going to merge and release now. Sorry for the delay ive been busy IRL 👍 |
* First completed draft * Update PR, should be close to ready now * Add fluid/item_tags_populated conditions * Log processed resource with trace log level * Use debug instead of trace log level * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Code review suggestions * writeAdditional -> writeParameters * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * CONDITION_ID -> CONDITION_ID_KEY
* First completed draft * Update PR, should be close to ready now * Add fluid/item_tags_populated conditions * Log processed resource with trace log level * Use debug instead of trace log level * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Code review suggestions * writeAdditional -> writeParameters * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * CONDITION_ID -> CONDITION_ID_KEY (cherry picked from commit 1254045)
* First completed draft * Update PR, should be close to ready now * Add fluid/item_tags_populated conditions * Log processed resource with trace log level * Use debug instead of trace log level * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Code review suggestions * writeAdditional -> writeParameters * Apply suggestions from code review Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * CONDITION_ID -> CONDITION_ID_KEY
How can we use it to set a potion as a recipe ingredient? |
Closes #1644.
Example recipe
Example condition implementation, with the helper to generate json entries too:
Please let me know what you think of the approach, and which additional conditions should be provided.
TODO: