forked from FabricMC/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
716be68
commit 53830e5
Showing
12 changed files
with
248 additions
and
3 deletions.
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
57 changes: 57 additions & 0 deletions
57
...c/main/java/net/fabricmc/fabric/api/resource/conditions/v1/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,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"); | ||
} | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
...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,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; | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
.../src/testmod/java/net/fabricmc/fabric/test/resource/conditions/OverlayConditionsTest.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,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."); | ||
} | ||
}); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...ks/overlay_test/data/fabric-resource-conditions-api-v1-testmod/recipe/always_applied.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,11 @@ | ||
{ | ||
"type": "minecraft:crafting_shapeless", | ||
"ingredients": [ | ||
{ | ||
"item": "minecraft:raw_iron" | ||
} | ||
], | ||
"result": { | ||
"id": "minecraft:raw_copper" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...lay_test/do_overlay/data/fabric-resource-conditions-api-v1-testmod/recipe/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,11 @@ | ||
{ | ||
"type": "minecraft:crafting_shapeless", | ||
"ingredients": [ | ||
{ | ||
"item": "minecraft:raw_gold" | ||
} | ||
], | ||
"result": { | ||
"id": "minecraft:raw_copper" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...test/dont_overlay/data/fabric-resource-conditions-api-v1-testmod/recipe/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,11 @@ | ||
{ | ||
"type": "minecraft:crafting_shapeless", | ||
"ingredients": [ | ||
{ | ||
"item": "minecraft:raw_copper" | ||
} | ||
], | ||
"result": { | ||
"id": "minecraft:raw_gold" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...c-resource-conditions-api-v1/src/testmod/resources/resourcepacks/overlay_test/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 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" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} |
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