mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-21 02:38:26 -05:00
Resource Conditions in Pack Overlays (#3872)
* Overlay conditions 1.21 * Move overlay test to gametest * Fix style error * Fix another style error * Implement requested changes * Import style fix --------- Co-authored-by: modmuss50 <modmuss50@gmail.com>
This commit is contained in:
parent
31b5766379
commit
8dc279b1d0
10 changed files with 177 additions and 3 deletions
|
@ -4,4 +4,8 @@ loom {
|
|||
accessWidenerPath = file("src/main/resources/fabric-resource-conditions-api-v1.accesswidener")
|
||||
}
|
||||
|
||||
testDependencies(project, [':fabric-gametest-api-v1'])
|
||||
testDependencies(project, [
|
||||
':fabric-gametest-api-v1',
|
||||
':fabric-lifecycle-events-v1',
|
||||
':fabric-resource-loader-v0'
|
||||
])
|
||||
|
|
|
@ -48,6 +48,11 @@ public final class ResourceConditions {
|
|||
*/
|
||||
public static final String CONDITIONS_KEY = "fabric:load_conditions";
|
||||
|
||||
/**
|
||||
* The JSON key for conditional overlays in pack.mcmeta files.
|
||||
*/
|
||||
public static final String OVERLAYS_KEY = "fabric:overlays";
|
||||
|
||||
private ResourceConditions() {
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
"RecipeManagerMixin",
|
||||
"RegistryLoaderMixin",
|
||||
"ReloadableRegistriesMixin",
|
||||
"ResourcePackProfileMixin",
|
||||
"ServerAdvancementLoaderMixin",
|
||||
"SinglePreparationResourceReloaderMixin",
|
||||
"TagManagerLoaderMixin"
|
||||
|
|
|
@ -121,4 +121,19 @@ public class ConditionalResourcesTest {
|
|||
|
||||
context.complete();
|
||||
}
|
||||
|
||||
@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
|
||||
public void conditionalOverlays(TestContext context) {
|
||||
ReloadableRegistries.Lookup registries = context.getWorld().getServer().getReloadableRegistries();
|
||||
|
||||
if (!registries.getRegistryManager().get(RegistryKeys.PREDICATE).containsId(id("do_overlay"))) {
|
||||
throw new AssertionError("do_overlay predicate should have been overlayed.");
|
||||
}
|
||||
|
||||
if (registries.getRegistryManager().get(RegistryKeys.PREDICATE).containsId(id("dont_overlay"))) {
|
||||
throw new AssertionError("dont_overlay predicate should not have been overlayed.");
|
||||
}
|
||||
|
||||
context.complete();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"condition": "minecraft:survives_explosion"
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"condition": "minecraft:survives_explosion"
|
||||
}
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
|
@ -37,6 +37,7 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import net.minecraft.recipe.RecipeManager;
|
||||
import net.minecraft.registry.RegistryWrapper;
|
||||
import net.minecraft.resource.OverlayResourcePack;
|
||||
import net.minecraft.resource.ResourcePack;
|
||||
import net.minecraft.resource.ResourcePackInfo;
|
||||
import net.minecraft.resource.ResourcePackPosition;
|
||||
|
@ -145,8 +146,19 @@ public class ResourceManagerHelperImpl implements ResourceManagerHelper {
|
|||
|
||||
@Override
|
||||
public ResourcePack openWithOverlays(ResourcePackInfo var1, ResourcePackProfile.Metadata metadata) {
|
||||
// Don't support overlays in builtin res packs.
|
||||
return entry.getRight();
|
||||
ModNioResourcePack pack = entry.getRight();
|
||||
|
||||
if (metadata.overlays().isEmpty()) {
|
||||
return pack;
|
||||
}
|
||||
|
||||
List<ResourcePack> overlays = new ArrayList<>(metadata.overlays().size());
|
||||
|
||||
for (String overlay : metadata.overlays()) {
|
||||
overlays.add(pack.createOverlay(overlay));
|
||||
}
|
||||
|
||||
return new OverlayResourcePack(pack, overlays);
|
||||
}
|
||||
}, resourceType, info2);
|
||||
consumer.accept(profile);
|
||||
|
|
Loading…
Reference in a new issue