diff options
Diffstat (limited to 'src/main/java/dev/mayaqq/ygasi/util/AdvUtils.java')
-rw-r--r-- | src/main/java/dev/mayaqq/ygasi/util/AdvUtils.java | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/main/java/dev/mayaqq/ygasi/util/AdvUtils.java b/src/main/java/dev/mayaqq/ygasi/util/AdvUtils.java new file mode 100644 index 0000000..b2227fe --- /dev/null +++ b/src/main/java/dev/mayaqq/ygasi/util/AdvUtils.java @@ -0,0 +1,51 @@ +package dev.mayaqq.ygasi.util; + +import net.minecraft.advancement.Advancement; +import net.minecraft.advancement.PlayerAdvancementTracker; +import net.minecraft.server.ServerAdvancementLoader; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +import static dev.mayaqq.ygasi.Ygasi.LOGGER; + +public class AdvUtils { + public static void grantAdvancementCriterion(ServerPlayerEntity player, Identifier advancementIdentifier, String criterion) { + if (player.getServer() == null) { + return; + } + ServerAdvancementLoader sal = player.getServer().getAdvancementLoader(); + PlayerAdvancementTracker tracker = player.getAdvancementTracker(); + + Advancement advancement = sal.get(advancementIdentifier); + if (advancement == null) { + LOGGER.error("Trying to grant a criterion \"" + criterion + "\" for an advancement that does not exist: " + advancementIdentifier); + } else { + if (!tracker.getProgress(advancement).isDone()) { + tracker.grantCriterion(advancement, criterion); + } + } + } + + public static void revokeAllAdvancements(ServerPlayerEntity player, String namespace ,String path) { + if (player.getServer() == null) { + return; + } + ServerAdvancementLoader sal = player.getServer().getAdvancementLoader(); + PlayerAdvancementTracker tracker = player.getAdvancementTracker(); + + for (Advancement advancement : sal.getAdvancements()) { + if (advancement.getId().getNamespace().equals(namespace) && advancement.getId().getPath().startsWith(path)) { + for (String criterion : advancement.getCriteria().keySet()) { + tracker.revokeCriterion(advancement, criterion); + } + } + } + } + + public static boolean getAdvancementProgress(ServerPlayerEntity player, String namespace, String path) { + if (player.getServer() == null) { + return false; + } + return player.getAdvancementTracker().getProgress(player.getServer().getAdvancementLoader().get(new Identifier(namespace, path))).isDone(); + } +}
\ No newline at end of file |