-
-
Notifications
You must be signed in to change notification settings - Fork 130
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
- Loading branch information
1 parent
b87e91a
commit 3e9f6ea
Showing
7 changed files
with
137 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
105 changes: 105 additions & 0 deletions
105
Xplat/src/main/java/vazkii/patchouli/common/advancement/PatchouliBookOpenTrigger.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,105 @@ | ||
package vazkii.patchouli.common.advancement; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
|
||
import net.minecraft.advancements.critereon.*; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.level.ServerPlayer; | ||
|
||
import vazkii.patchouli.api.PatchouliAPI; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class PatchouliBookOpenTrigger extends SimpleCriterionTrigger<PatchouliBookOpenTrigger.Instance> { | ||
public static final ResourceLocation ID = new ResourceLocation(PatchouliAPI.MOD_ID, "open_book"); | ||
public static final PatchouliBookOpenTrigger INSTANCE = new PatchouliBookOpenTrigger(); | ||
|
||
private PatchouliBookOpenTrigger() {} | ||
|
||
@NotNull | ||
@Override | ||
public ResourceLocation getId() { | ||
return ID; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
protected PatchouliBookOpenTrigger.Instance createInstance(@NotNull JsonObject json, @NotNull ContextAwarePredicate playerPred, @NotNull DeserializationContext conditions) { | ||
JsonElement bookElement = json.get("book"); | ||
ResourceLocation book = bookElement != null && bookElement.isJsonPrimitive() ? new ResourceLocation(bookElement.getAsString()) : null; | ||
JsonElement entryElement = json.get("entry"); | ||
ResourceLocation entry = entryElement != null && entryElement.isJsonPrimitive() ? new ResourceLocation(entryElement.getAsString()) : null; | ||
JsonElement pageElement = json.get("page"); | ||
int page = pageElement != null && pageElement.isJsonPrimitive() ? pageElement.getAsInt() : 0; | ||
return new PatchouliBookOpenTrigger.Instance(playerPred, book, entry, page); | ||
} | ||
|
||
public void trigger(@NotNull ServerPlayer player, @NotNull ResourceLocation book) { | ||
trigger(player, instance -> instance.test(book, null, 0)); | ||
} | ||
|
||
public void trigger(@NotNull ServerPlayer player, @NotNull ResourceLocation book, @Nullable ResourceLocation entry, int page) { | ||
trigger(player, instance -> instance.test(book, entry, page)); | ||
} | ||
|
||
public static class Instance extends AbstractCriterionTriggerInstance { | ||
private final ResourceLocation book; | ||
private final ResourceLocation entry; | ||
private final int page; | ||
|
||
public Instance(@NotNull ContextAwarePredicate playerPred, @Nullable ResourceLocation book, @Nullable ResourceLocation entry, int page) { | ||
super(ID, playerPred); | ||
this.book = book; | ||
this.entry = entry; | ||
this.page = page; | ||
} | ||
|
||
public Instance(@NotNull ContextAwarePredicate playerPred, @NotNull ResourceLocation book) { | ||
this(playerPred, book, null, 0); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public ResourceLocation getCriterion() { | ||
return ID; | ||
} | ||
|
||
@Nullable | ||
public ResourceLocation getBook() { | ||
return this.book; | ||
} | ||
|
||
@Nullable | ||
public ResourceLocation getEntry() { | ||
return this.entry; | ||
} | ||
|
||
public int getPage() { | ||
return this.page; | ||
} | ||
|
||
boolean test(ResourceLocation bookId, ResourceLocation entry, int page) { | ||
return this.book != null && this.book.equals(bookId) | ||
&& (this.entry == null || this.entry.equals(entry)) | ||
&& (this.page > 0 && this.page == page); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public JsonObject serializeToJson(@NotNull SerializationContext serializationContext) { | ||
JsonObject json = super.serializeToJson(serializationContext); | ||
if (book != null) { | ||
json.addProperty("book", book.toString()); | ||
} | ||
if (entry != null) { | ||
json.addProperty("entry", entry.toString()); | ||
} | ||
if (page != -1) { | ||
json.addProperty("page", page); | ||
} | ||
return json; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
package vazkii.patchouli.common.advancement; | ||
|
||
import vazkii.patchouli.mixin.AccessorCriteriaTriggers; | ||
|
||
public class PatchouliCriteriaTriggers { | ||
public static void init() { | ||
AccessorCriteriaTriggers.patchouli$register(PatchouliBookOpenTrigger.INSTANCE); | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
Xplat/src/main/java/vazkii/patchouli/mixin/AccessorCriteriaTriggers.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,15 @@ | ||
package vazkii.patchouli.mixin; | ||
|
||
import net.minecraft.advancements.CriteriaTriggers; | ||
import net.minecraft.advancements.CriterionTrigger; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Invoker; | ||
|
||
@Mixin(CriteriaTriggers.class) | ||
public interface AccessorCriteriaTriggers { | ||
@Invoker("register") | ||
static <T extends CriterionTrigger<?>> T patchouli$register(T thing) { | ||
throw new IllegalStateException(); | ||
} | ||
} |
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