-
Notifications
You must be signed in to change notification settings - Fork 417
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
[1.21] Resource Conditions in Pack Overlays #3872
Merged
modmuss50
merged 7 commits into
FabricMC:1.21
from
Apollounknowndev:overlay-conditions-1.21
Aug 4, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
53830e5
Overlay conditions 1.21
Apollounknowndev f1e7152
Move overlay test to gametest
Apollounknowndev ae909f0
Fix style error
Apollounknowndev d7d9233
Fix another style error
Apollounknowndev 411cc33
Implement requested changes
Apollounknowndev bec1551
Import style fix
Apollounknowndev b089b05
Merge remote-tracking branch 'origin/1.21' into fork/Apollounknowndev…
modmuss50 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...src/main/java/net/fabricmc/fabric/impl/resource/conditions/OverlayConditionsMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.impl.resource.conditions; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
import net.minecraft.resource.metadata.ResourceMetadataSerializer; | ||
|
||
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceCondition; | ||
import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions; | ||
|
||
public record OverlayConditionsMetadata(List<Entry> overlays) { | ||
public static final Codec<OverlayConditionsMetadata> CODEC = Entry.CODEC.listOf().fieldOf("entries").xmap(OverlayConditionsMetadata::new, OverlayConditionsMetadata::overlays).codec(); | ||
public static final ResourceMetadataSerializer<OverlayConditionsMetadata> SERIALIZER = ResourceMetadataSerializer.fromCodec(ResourceConditions.OVERLAYS_KEY, CODEC); | ||
|
||
public List<String> appliedOverlays() { | ||
List<String> appliedOverlays = new ArrayList<>(); | ||
|
||
for (Entry entry : this.overlays()) { | ||
if (entry.condition().test(null)) { | ||
appliedOverlays.add(entry.directory()); | ||
} | ||
} | ||
|
||
return appliedOverlays; | ||
} | ||
|
||
public record Entry(String directory, ResourceCondition condition) { | ||
public static final Codec<Entry> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Codec.STRING.validate(Entry::validateDirectory).fieldOf("directory").forGetter(Entry::directory), | ||
ResourceCondition.CODEC.fieldOf("condition").forGetter(Entry::condition) | ||
).apply(instance, Entry::new)); | ||
private static final Pattern DIRECTORY_NAME_PATTERN = Pattern.compile("[-_a-zA-Z0-9.]+"); | ||
|
||
private static DataResult<String> validateDirectory(String directory) { | ||
boolean valid = DIRECTORY_NAME_PATTERN.matcher(directory).matches(); | ||
return valid ? DataResult.success(directory) : DataResult.error(() -> "Directory name is invalid"); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...src/main/java/net/fabricmc/fabric/mixin/resource/conditions/ResourcePackProfileMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.mixin.resource.conditions; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.llamalad7.mixinextras.sugar.Local; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.ModifyVariable; | ||
|
||
import net.minecraft.resource.ResourcePack; | ||
import net.minecraft.resource.ResourcePackProfile; | ||
|
||
import net.fabricmc.fabric.impl.resource.conditions.OverlayConditionsMetadata; | ||
|
||
@Mixin(ResourcePackProfile.class) | ||
public class ResourcePackProfileMixin { | ||
@ModifyVariable(method = "loadMetadata", at = @At("STORE")) | ||
private static List<String> applyOverlayConditions(List<String> overlays, @Local ResourcePack resourcePack) throws IOException { | ||
List<String> appliedOverlays = new ArrayList<>(overlays); | ||
OverlayConditionsMetadata overlayMetadata = resourcePack.parseMetadata(OverlayConditionsMetadata.SERIALIZER); | ||
|
||
if (overlayMetadata != null) { | ||
appliedOverlays.addAll(overlayMetadata.appliedOverlays()); | ||
} | ||
|
||
return List.copyOf(appliedOverlays); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...urces/do_overlay/data/fabric-resource-conditions-api-v1-testmod/predicate/do_overlay.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"condition": "minecraft:survives_explosion" | ||
} |
3 changes: 3 additions & 0 deletions
3
...s/dont_overlay/data/fabric-resource-conditions-api-v1-testmod/predicate/dont_overlay.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"condition": "minecraft:survives_explosion" | ||
} |
25 changes: 25 additions & 0 deletions
25
fabric-resource-conditions-api-v1/src/testmod/resources/pack.mcmeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"pack": { | ||
"pack_format": 48, | ||
"description": "" | ||
}, | ||
"fabric:overlays": { | ||
"entries": [ | ||
{ | ||
"directory": "do_overlay", | ||
"condition": { | ||
"condition": "fabric:true" | ||
} | ||
}, | ||
{ | ||
"directory": "dont_overlay", | ||
"condition": { | ||
"condition": "fabric:not", | ||
"value": { | ||
"condition": "fabric:true" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why isn't this a public API class? Without this class pack.mcmeta datagen is impossible
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.
Oops, I see this was asked to be changed a month ago — in my defence, the PR kept getting closed and it was a bit hard to keep track of.
In any case I still think this class should be an API anyway
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.
We should definally be able to datagenerate this, however I worry just moving this to API isnt the ideal solution.
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.
However, this class is very similar to (even copies) the vanilla
PackOverlaysMetadata
, which makes it easy to understand how to use itThe only thing is that
ResourceFilter
requires AW forBlockEntry::<init>
, but that's the only such case. I datagenpack.mcmeta
to filter recipes & advancements(yes, another TAW candidate)
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, actually, the static factory function sounds ok, the main thing is to give a name similar to the vanilla class (yarn)
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.
Going to merge this PR as-is, we can have a follow up PR making this better for datagen.