diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/advancement/CriterionRegistry.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/advancement/CriterionRegistry.java new file mode 100644 index 000000000..6e1261bc6 --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/api/object/builder/v1/advancement/CriterionRegistry.java @@ -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.api.object.builder.v1.advancement; + +import net.minecraft.advancement.criterion.Criterion; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.mixin.object.builder.CriteriaAccessor; + +/** + * Allows registering advancement criteria for triggers. + * + *

A registered criterion (trigger) can be retrieved through + * {@link net.minecraft.advancement.criterion.Criteria#getById(Identifier)}.

+ * + * @see net.minecraft.advancement.criterion.Criteria + */ +public final class CriterionRegistry { + /** + * Registers a criterion for a trigger for advancements. + * + * @param the criterion's type + * @param criterion the criterion registered + * @return the criterion registered, for chaining + * @throws IllegalArgumentException if a criterion with the same {@link + * Criterion#getId() id} exists + */ + public static > T register(T criterion) { + CriteriaAccessor.callRegister(criterion); + return criterion; + } +} diff --git a/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/CriteriaAccessor.java b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/CriteriaAccessor.java new file mode 100644 index 000000000..1684caf23 --- /dev/null +++ b/fabric-object-builder-api-v1/src/main/java/net/fabricmc/fabric/mixin/object/builder/CriteriaAccessor.java @@ -0,0 +1,31 @@ +/* + * 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.object.builder; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +import net.minecraft.advancement.criterion.Criteria; +import net.minecraft.advancement.criterion.Criterion; + +@Mixin(Criteria.class) +public interface CriteriaAccessor { + @Invoker + static > T callRegister(T object) { + throw new AssertionError("Mixin dummy"); + } +} diff --git a/fabric-object-builder-api-v1/src/main/resources/assets/fabric-object-builder-v1/icon.png b/fabric-object-builder-api-v1/src/main/resources/assets/fabric-object-builder-api-v1/icon.png similarity index 100% rename from fabric-object-builder-api-v1/src/main/resources/assets/fabric-object-builder-v1/icon.png rename to fabric-object-builder-api-v1/src/main/resources/assets/fabric-object-builder-api-v1/icon.png diff --git a/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json b/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json index f7f41f15d..f7e60c94e 100644 --- a/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json +++ b/fabric-object-builder-api-v1/src/main/resources/fabric-object-builder-v1.mixins.json @@ -6,6 +6,7 @@ "AbstractBlockAccessor", "AbstractBlockSettingsAccessor", "AbstractBlockSettingsMixin", + "CriteriaAccessor", "DefaultAttributeRegistryAccessor", "DefaultAttributeRegistryMixin", "MaterialBuilderAccessor", diff --git a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/CriterionRegistryTest.java b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/CriterionRegistryTest.java new file mode 100644 index 000000000..ef1517e40 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/CriterionRegistryTest.java @@ -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.test.object.builder; + +import com.google.gson.JsonObject; + +import net.minecraft.advancement.criterion.ImpossibleCriterion; +import net.minecraft.predicate.entity.AdvancementEntityPredicateDeserializer; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.object.builder.v1.advancement.CriterionRegistry; + +public final class CriterionRegistryTest { + public static void init() { + CriterionRegistry.register(new CustomCriterion()); + } + + static class CustomCriterion extends ImpossibleCriterion { + static final Identifier ID = ObjectBuilderTestConstants.id("custom"); + + @Override + public Identifier getId() { + return ID; + } + + @Override + public Conditions conditionsFromJson(JsonObject jsonObject, AdvancementEntityPredicateDeserializer advancementEntityPredicateDeserializer) { + ObjectBuilderTestConstants.LOGGER.info("Loading custom criterion in advancement!"); + return super.conditionsFromJson(jsonObject, advancementEntityPredicateDeserializer); + } + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/ObjectBuilderTestConstants.java b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/ObjectBuilderTestConstants.java new file mode 100644 index 000000000..13679dfa1 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/java/net/fabricmc/fabric/test/object/builder/ObjectBuilderTestConstants.java @@ -0,0 +1,31 @@ +/* + * 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.object.builder; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.util.Identifier; + +public final class ObjectBuilderTestConstants { + public static final String MOD_ID = "fabric-object-builder-api-v1-testmod"; + public static final Logger LOGGER = LogManager.getLogger(MOD_ID); + + public static Identifier id(String name) { + return new Identifier(MOD_ID, name); + } +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/icon.png b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/icon.png new file mode 100644 index 000000000..2931efbf6 Binary files /dev/null and b/fabric-object-builder-api-v1/src/testmod/resources/assets/fabric-object-builder-api-v1-testmod/icon.png differ diff --git a/fabric-object-builder-api-v1/src/testmod/resources/data/fabric-object-builder-api-v1-testmod/advancements/criterion_registry_test.json b/fabric-object-builder-api-v1/src/testmod/resources/data/fabric-object-builder-api-v1-testmod/advancements/criterion_registry_test.json new file mode 100644 index 000000000..d96be7670 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/data/fabric-object-builder-api-v1-testmod/advancements/criterion_registry_test.json @@ -0,0 +1,28 @@ +{ + "display": { + "icon": { + "item": "minecraft:command_block_minecart" + }, + "title": { + "text": "Criterion registry test advancement" + }, + "description": { + "text": "Criterion registry test advancement description" + }, + "frame": "task", + "show_toast": false, + "announce_to_chat": false, + "hidden": false, + "background": "minecraft:textures/gui/advancements/backgrounds/stone.png" + }, + "criteria": { + "custom": { + "trigger": "fabric-object-builder-api-v1-testmod:custom" + } + }, + "requirements": [ + [ + "custom" + ] + ] +} diff --git a/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json b/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json new file mode 100644 index 000000000..9fb467867 --- /dev/null +++ b/fabric-object-builder-api-v1/src/testmod/resources/fabric.mod.json @@ -0,0 +1,27 @@ +{ + "schemaVersion": 1, + "id": "fabric-object-builder-api-v1-testmod", + "name": "Fabric Object Builder API (v1) Test Mod", + "version": "${version}", + "environment": "*", + "license": "Apache-2.0", + "icon": "assets/fabric-object-builder-api-v1-testmod/icon.png", + "contact": { + "homepage": "https://fabricmc.net", + "irc": "irc://irc.esper.net:6667/fabric", + "issues": "https://github.com/FabricMC/fabric/issues", + "sources": "https://github.com/FabricMC/fabric" + }, + "authors": [ + "FabricMC" + ], + "depends": { + "fabric-object-builder-api-v1": "*" + }, + "description": "Test mod for fabric object builder API v1.", + "entrypoints": { + "main": [ + "net.fabricmc.fabric.test.object.builder.CriterionRegistryTest::init" + ] + } +}