-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fire an advancement trigger when reading a book
(implements #714)
- Loading branch information
1 parent
4522fbb
commit 1ba70b0
Showing
5 changed files
with
71 additions
and
0 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
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
50 changes: 50 additions & 0 deletions
50
Xplat/src/main/java/vazkii/patchouli/common/advancement/BookOpenTrigger.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,50 @@ | ||
package vazkii.patchouli.common.advancement; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.advancements.critereon.*; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.level.ServerPlayer; | ||
|
||
import net.minecraft.util.ExtraCodecs; | ||
import vazkii.patchouli.api.PatchouliAPI; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* An advancement trigger for opening Patchouli books. | ||
*/ | ||
public class BookOpenTrigger extends SimpleCriterionTrigger<BookOpenTrigger.TriggerInstance> { | ||
public static final ResourceLocation ID = new ResourceLocation(PatchouliAPI.MOD_ID, "open_book"); | ||
public static final BookOpenTrigger INSTANCE = new BookOpenTrigger(); | ||
|
||
@NotNull | ||
@Override | ||
public Codec<TriggerInstance> codec() { | ||
return BookOpenTrigger.TriggerInstance.CODEC; | ||
} | ||
|
||
public void trigger(@NotNull ServerPlayer player, @NotNull ResourceLocation book) { | ||
trigger(player, instance -> instance.matches(book, null, 0)); | ||
} | ||
|
||
public void trigger(@NotNull ServerPlayer player, @NotNull ResourceLocation book, @Nullable ResourceLocation entry, int page) { | ||
trigger(player, instance -> instance.matches(book, entry, page)); | ||
} | ||
|
||
public record TriggerInstance(Optional<ContextAwarePredicate> player, ResourceLocation book, Optional<ResourceLocation> entry, MinMaxBounds.Ints page) implements SimpleInstance { | ||
public static Codec<BookOpenTrigger.TriggerInstance> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "player").forGetter(TriggerInstance::player), | ||
ResourceLocation.CODEC.fieldOf("book").forGetter(TriggerInstance::book), | ||
ExtraCodecs.strictOptionalField(ResourceLocation.CODEC, "entry").forGetter(TriggerInstance::entry), | ||
ExtraCodecs.strictOptionalField(MinMaxBounds.Ints.CODEC, "page", MinMaxBounds.Ints.ANY).forGetter(TriggerInstance::page) | ||
).apply(instance, TriggerInstance::new)); | ||
|
||
public boolean matches(@NotNull ResourceLocation book, @Nullable ResourceLocation entry, int page) { | ||
return this.book.equals(book) && (this.entry.isEmpty() || this.entry.get().equals(entry)) && this.page.matches(page); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Xplat/src/main/java/vazkii/patchouli/common/advancement/PatchouliCriteriaTriggers.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,14 @@ | ||
package vazkii.patchouli.common.advancement; | ||
|
||
import net.minecraft.advancements.CriterionTrigger; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
public class PatchouliCriteriaTriggers { | ||
public static final BookOpenTrigger BOOK_OPEN = new BookOpenTrigger(); | ||
|
||
public static void submitTriggerRegistrations(BiConsumer<ResourceLocation, CriterionTrigger<?>> consumer) { | ||
consumer.accept(BookOpenTrigger.ID, BOOK_OPEN); | ||
} | ||
} |
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