Skip to content

Commit

Permalink
Overlay conditions 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollounknowndev committed Jun 21, 2024
1 parent 716be68 commit 53830e5
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 3 deletions.
6 changes: 5 additions & 1 deletion fabric-resource-conditions-api-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.api.resource.conditions.v1;

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;

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> getAppliedOverlays() {
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 net.fabricmc.fabric.api.resource.conditions.v1.OverlayConditionsMetadata;

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;


@Mixin(ResourcePackProfile.class)
public class ResourcePackProfileMixin {
@ModifyVariable(method = "loadMetadata", at = @At("STORE"))
private static List<String> fabric_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.getAppliedOverlays());
}

return appliedOverlays;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"RecipeManagerMixin",
"RegistryLoaderMixin",
"ReloadableRegistriesMixin",
"ResourcePackProfileMixin",
"ServerAdvancementLoaderMixin",
"SinglePreparationResourceReloaderMixin",
"TagManagerLoaderMixin"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.test.resource.conditions;

import java.util.List;
import java.util.Optional;

import net.minecraft.util.Identifier;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;

public class OverlayConditionsTest implements ModInitializer {
private static final String MOD_ID = "fabric-resource-conditions-api-v1-testmod";

private static Identifier id(String path) {
return Identifier.of(MOD_ID, path);
}

@Override
public void onInitialize() {
Optional<ModContainer> container = FabricLoader.getInstance().getModContainer(MOD_ID);

if (container.isEmpty() || !ResourceManagerHelper.registerBuiltinResourcePack(Identifier.of(MOD_ID, "overlay_test"), container.get(), ResourcePackActivationType.DEFAULT_ENABLED)) {
throw new AssertionError("Could not register overlay_test datapack.");
}

ServerLifecycleEvents.SERVER_STARTING.register(server -> {
List<Identifier> recipes = server.getRecipeManager().keys().toList();

if (recipes.contains(id("dont_overlay"))) {
throw new AssertionError("dont_overlay recipe should not have been overlayed.");
}

if (!recipes.contains(id("do_overlay"))) {
throw new AssertionError("do_overlay recipe should have been overlayed.");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"fabric-resource-conditions-api-v1": "*"
},
"entrypoints": {
"main": [
"net.fabricmc.fabric.test.resource.conditions.OverlayConditionsTest"
],
"fabric-gametest": [
"net.fabricmc.fabric.test.resource.conditions.ConditionalResourcesTest",
"net.fabricmc.fabric.test.resource.conditions.DefaultResourceConditionsTest"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "minecraft:raw_iron"
}
],
"result": {
"id": "minecraft:raw_copper"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "minecraft:raw_gold"
}
],
"result": {
"id": "minecraft:raw_copper"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "minecraft:raw_copper"
}
],
"result": {
"id": "minecraft:raw_gold"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"pack": {
"pack_format": 48,
"description": "Fabric Overlay Conditions Test Datapack."
},
"fabric:overlays": {
"entries": [
{
"directory": "do_overlay",
"condition": {
"condition": "fabric:true"
}
},
{
"directory": "dont_overlay",
"condition": {
"condition": "fabric:not",
"value": {
"condition": "fabric:true"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.util.function.Consumer;

import com.google.common.collect.Lists;

import net.minecraft.resource.OverlayResourcePack;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -134,8 +137,16 @@ public ResourcePack open(ResourcePackInfo var1) {

@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);
Expand Down

0 comments on commit 53830e5

Please sign in to comment.