The CriterionTriggers will allow you to control the advancements in your mod with the class RestAdvancement. You can create a CriterionTrigger for any advancement, so you can check if the player has the advancement or not. Additionally, you can grant the advancement to the player.
In order to register criterion triggers you will have to create a registry. To do this, you must use the following code within the init() function:
//This is the init() inside your main class.
public static void init() {
//Here we create the registry so we can use it later.
CriterionTriggerRegistry.CreateRegistry(MOD_ID);
}
In order to register criterion triggers you will have to create a registry, then the following:
//This is another separate class from the initial one. I recommend you do this for organization.
public class AdvancementTriggerManager {
//Here we create the criterion trigger.
public static final RestAdvancement advancementTrigger = CriterionTriggerRegistry.CreateAdvancementTrigger(YourModId, YourAdvancementId);
/*
Parameters:
- YourModId = Your mod id as string.
- YourAdvancementId = Your advancement's id as string.
*/
//You can create a method/function like this to call it from the main class.
public static void register() {
//This registers all of your criterion triggers into minecraft.
CriterionTriggerRegistry.Register(YourModId);
}
}
To do this, you can do this:
//Here we have your CriterionTrigger.
public static final RestAdvancement advancementTrigger = CriterionTriggerRegistry.CreateAdvancementTrigger(YourModId, YourAdvancementId);
public static void yourMethod() {
//serverPlayer = A player in the server-side, using the ServerPlayer class.
advancementTrigger.award(serverPlayer);
}
To do this, you can do this:
//Here we have your CriterionTrigger.
public static final RestAdvancement advancementTrigger = CriterionTriggerRegistry.CreateAdvancementTrigger(YourModId, YourAdvancementId);
public static void yourMethod() {
//serverPlayer = A player in the server-side, using the ServerPlayer class.
if (advancementTrigger.hasAdvancement(serverPlayer)) {
doSomethingCool();
}
}