mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-05 19:47:00 -04:00
parent
2b04e6acb7
commit
13eda06580
9 changed files with 86 additions and 20 deletions
fabric-resource-conditions-api-v1/src
main
java/net/fabricmc/fabric
api/resource/conditions/v1
impl/resource/conditions
mixin/resource/conditions
resources
testmod
java/net/fabricmc/fabric/test/resource/conditions
resources
data/fabric-resource-conditions-api-v1-testmod
fabric.mod.json
|
@ -22,12 +22,10 @@ import com.google.gson.JsonObject;
|
|||
import net.minecraft.block.Block;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.tag.BlockTags;
|
||||
import net.minecraft.tag.FluidTags;
|
||||
import net.minecraft.tag.ItemTags;
|
||||
import net.minecraft.tag.Tag;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
import net.fabricmc.fabric.impl.resource.conditions.ResourceConditionsImpl;
|
||||
|
||||
|
@ -129,9 +127,9 @@ public final class DefaultResourceConditions {
|
|||
});
|
||||
ResourceConditions.register(ALL_MODS_LOADED, object -> ResourceConditionsImpl.modsLoadedMatch(object, true));
|
||||
ResourceConditions.register(ANY_MOD_LOADED, object -> ResourceConditionsImpl.modsLoadedMatch(object, false));
|
||||
ResourceConditions.register(BLOCK_TAGS_POPULATED, object -> ResourceConditionsImpl.tagsPopulatedMatch(object, BlockTags.getTagGroup()));
|
||||
ResourceConditions.register(FLUID_TAGS_POPULATED, object -> ResourceConditionsImpl.tagsPopulatedMatch(object, FluidTags.getTagGroup()));
|
||||
ResourceConditions.register(ITEM_TAGS_POPULATED, object -> ResourceConditionsImpl.tagsPopulatedMatch(object, ItemTags.getTagGroup()));
|
||||
ResourceConditions.register(BLOCK_TAGS_POPULATED, object -> ResourceConditionsImpl.tagsPopulatedMatch(object, Registry.BLOCK_KEY));
|
||||
ResourceConditions.register(FLUID_TAGS_POPULATED, object -> ResourceConditionsImpl.tagsPopulatedMatch(object, Registry.FLUID_KEY));
|
||||
ResourceConditions.register(ITEM_TAGS_POPULATED, object -> ResourceConditionsImpl.tagsPopulatedMatch(object, Registry.ITEM_KEY));
|
||||
}
|
||||
|
||||
private DefaultResourceConditions() {
|
||||
|
|
|
@ -25,10 +25,12 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import net.minecraft.tag.ServerTagManagerHolder;
|
||||
import net.minecraft.tag.Tag;
|
||||
import net.minecraft.tag.TagGroup;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.JsonHelper;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
|
||||
import net.fabricmc.fabric.api.resource.conditions.v1.ConditionJsonProvider;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
@ -123,15 +125,15 @@ public class ResourceConditionsImpl {
|
|||
return and;
|
||||
}
|
||||
|
||||
public static <T> boolean tagsPopulatedMatch(JsonObject object, TagGroup<T> tagGroup) {
|
||||
public static <T> boolean tagsPopulatedMatch(JsonObject object, RegistryKey<? extends Registry<T>> registryKey) {
|
||||
JsonArray array = JsonHelper.getArray(object, "values");
|
||||
|
||||
for (JsonElement element : array) {
|
||||
if (element.isJsonPrimitive()) {
|
||||
Identifier id = new Identifier(element.getAsString());
|
||||
Tag<T> tag = tagGroup.getTag(id);
|
||||
Tag<T> tag = ServerTagManagerHolder.getTagManager().getOrCreateTagGroup(registryKey).getTagOrEmpty(id);
|
||||
|
||||
if (tag == null || tag.values().size() == 0) {
|
||||
if (tag.values().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -24,9 +24,6 @@ import com.google.gson.JsonObject;
|
|||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import net.minecraft.resource.JsonDataLoader;
|
||||
import net.minecraft.resource.ResourceManager;
|
||||
|
@ -37,16 +34,17 @@ import net.fabricmc.fabric.api.resource.conditions.v1.ResourceConditions;
|
|||
import net.fabricmc.fabric.impl.resource.conditions.ResourceConditionsImpl;
|
||||
|
||||
@Mixin(JsonDataLoader.class)
|
||||
public class JsonDataLoaderMixin {
|
||||
public class JsonDataLoaderMixin extends SinglePreparationResourceReloaderMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private String dataType;
|
||||
|
||||
@Inject(at = @At("RETURN"), method = "prepare")
|
||||
public void applyResourceConditions(ResourceManager resourceManager, Profiler profiler, CallbackInfoReturnable<Map<Identifier, JsonElement>> cir) {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void fabric_applyResourceConditions(ResourceManager resourceManager, Profiler profiler, Object object) {
|
||||
profiler.push("Fabric resource conditions: %s".formatted(dataType));
|
||||
|
||||
Iterator<Map.Entry<Identifier, JsonElement>> it = cir.getReturnValue().entrySet().iterator();
|
||||
Iterator<Map.Entry<Identifier, JsonElement>> it = ((Map<Identifier, JsonElement>) object).entrySet().iterator();
|
||||
boolean debugLogEnabled = ResourceConditionsImpl.LOGGER.isDebugEnabled();
|
||||
|
||||
while (it.hasNext()) {
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* 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 org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.minecraft.resource.ResourceManager;
|
||||
import net.minecraft.resource.SinglePreparationResourceReloader;
|
||||
import net.minecraft.util.profiler.Profiler;
|
||||
|
||||
@Mixin(SinglePreparationResourceReloader.class)
|
||||
public class SinglePreparationResourceReloaderMixin {
|
||||
// thenAcceptAsync in reload
|
||||
@Inject(at = @At("HEAD"), method = "method_18790")
|
||||
private void applyResourceConditions(ResourceManager resourceManager, Profiler profiler, Object object, CallbackInfo ci) {
|
||||
fabric_applyResourceConditions(resourceManager, profiler, object);
|
||||
}
|
||||
|
||||
protected void fabric_applyResourceConditions(ResourceManager resourceManager, Profiler profiler, Object object) {
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
"package": "net.fabricmc.fabric.mixin.resource.conditions",
|
||||
"compatibilityLevel": "JAVA_16",
|
||||
"mixins": [
|
||||
"JsonDataLoaderMixin"
|
||||
"JsonDataLoaderMixin",
|
||||
"SinglePreparationResourceReloaderMixin"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -42,8 +42,12 @@ public class ConditionalResourcesTest {
|
|||
throw new AssertionError("loaded recipe should have been loaded.");
|
||||
}
|
||||
|
||||
if (manager.get(id("item_tags_populated")).isEmpty()) {
|
||||
throw new AssertionError("item_tags_populated recipe should have been loaded.");
|
||||
}
|
||||
|
||||
long loadedRecipes = manager.values().stream().filter(r -> r.getId().getNamespace().equals(MOD_ID)).count();
|
||||
if (loadedRecipes != 1) throw new AssertionError("Unexpected loaded recipe count: " + loadedRecipes);
|
||||
if (loadedRecipes != 2) throw new AssertionError("Unexpected loaded recipe count: " + loadedRecipes);
|
||||
|
||||
context.complete();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"type": "minecraft:crafting_shapeless",
|
||||
"ingredients": [
|
||||
{
|
||||
"tag": "fabric-resource-conditions-api-v1-testmod:test_condition"
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"item": "minecraft:diamond"
|
||||
},
|
||||
"fabric:load_conditions": [
|
||||
{
|
||||
"condition": "fabric:item_tags_populated",
|
||||
"values": [
|
||||
"fabric-resource-conditions-api-v1-testmod:test_condition"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"replace": false,
|
||||
"values": [
|
||||
"minecraft:dirt"
|
||||
]
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
"environment": "*",
|
||||
"license": "Apache-2.0",
|
||||
"depends": {
|
||||
"fabric-api-lookup-api-v1": "*"
|
||||
"fabric-resource-conditions-api-v1": "*"
|
||||
},
|
||||
"entrypoints": {
|
||||
"fabric-gametest": [
|
||||
|
|
Loading…
Add table
Reference in a new issue