From 47642fffe4fe184ebb62878e1254263e519558c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Szab=C3=B3?= Date: Mon, 5 Aug 2024 15:08:21 +0200 Subject: [PATCH] Added AuraSkills stat rewards with auto correctors --- .../hooks/auraskills/AuraSkillsCorrector.java | 78 +++++++++++++++++++ .../hooks/auraskills/AuraSkillsHook.java | 10 ++- .../auraskills/AuraSkillsStatReward.java | 61 +++++++++++++++ .../hooks/luckperms/PermissionCorrector.java | 2 +- 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsCorrector.java create mode 100644 src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsStatReward.java diff --git a/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsCorrector.java b/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsCorrector.java new file mode 100644 index 0000000..72bb7a2 --- /dev/null +++ b/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsCorrector.java @@ -0,0 +1,78 @@ +package gg.auroramc.quests.hooks.auraskills; + +import com.google.common.collect.Maps; +import dev.aurelium.auraskills.api.AuraSkillsApi; +import dev.aurelium.auraskills.api.stat.Stat; +import dev.aurelium.auraskills.api.stat.StatModifier; +import gg.auroramc.aurora.api.reward.PermissionReward; +import gg.auroramc.aurora.api.reward.RewardCorrector; +import gg.auroramc.quests.AuroraQuests; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class AuraSkillsCorrector implements RewardCorrector { + + + @Override + public void correctRewards(Player player) { + var manager = AuroraQuests.getInstance().getQuestManager(); + Map statMap = Maps.newHashMap(); + + // Reset all stat modifiers first + for (var stat : AuraSkillsApi.get().getGlobalRegistry().getStats()) { + statMap.put(stat, 0.0); + } + + // Gather new stat modifiers + for (var pool : manager.getQuestPools()) { + // Correct global quests + if (pool.isGlobal()) { + for (var quest : pool.getQuests()) { + if (!quest.isCompleted(player)) continue; + + for (var reward : quest.getRewards().values()) { + if (reward instanceof AuraSkillsStatReward statReward) { + statMap.merge(statReward.getStat(), statReward.getValue(quest.getPlaceholders(player)), Double::sum); + } + } + } + } + + // Correct quest pool leveling + if (!pool.hasLeveling()) return; + var level = pool.getPlayerLevel(player); + + for (int i = 1; i < level + 1; i++) { + var matcher = pool.getMatcherManager().getBestMatcher(i); + if (matcher == null) continue; + var placeholders = pool.getLevelPlaceholders(player, i); + for (var reward : matcher.computeRewards(i)) { + if (reward instanceof AuraSkillsStatReward statReward) { + statMap.merge(statReward.getStat(), statReward.getValue(placeholders), Double::sum); + } + } + } + } + + Bukkit.getGlobalRegionScheduler().run(AuroraQuests.getInstance(), (task) -> { + for (var entry : statMap.entrySet()) { + var statKey = AuraSkillsStatReward.getAURA_SKILLS_STAT() + entry.getKey().getId().toString(); + var user = AuraSkillsApi.get().getUser(player.getUniqueId()); + + var oldModifier = user.getStatModifier(statKey); + + if (oldModifier == null) { + if (entry.getValue() > 0) { + user.addStatModifier(new StatModifier(statKey, entry.getKey(), entry.getValue())); + } + } else if (entry.getValue() <= 0) { + user.removeStatModifier(statKey); + } else if (entry.getValue() != oldModifier.value()) { + user.addStatModifier(new StatModifier(statKey, entry.getKey(), entry.getValue())); + } + } + }); + } +} diff --git a/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsHook.java b/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsHook.java index e6ab0e7..b4ffc84 100644 --- a/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsHook.java +++ b/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsHook.java @@ -1,5 +1,6 @@ package gg.auroramc.quests.hooks.auraskills; +import gg.auroramc.aurora.api.util.NamespacedId; import gg.auroramc.quests.AuroraQuests; import gg.auroramc.quests.hooks.Hook; import org.bukkit.Bukkit; @@ -8,6 +9,13 @@ public class AuraSkillsHook implements Hook { @Override public void hook(AuroraQuests plugin) { Bukkit.getPluginManager().registerEvents(new AuraSkillsListener(), plugin); - AuroraQuests.logger().info("Hooked into AuraSkills for GAIN_AURASKILLS_XP task types"); + + plugin.getQuestManager().getRewardFactory() + .registerRewardType(NamespacedId.fromDefault("auraskills_stat"), AuraSkillsStatReward.class); + + plugin.getQuestManager().getRewardAutoCorrector() + .registerCorrector(NamespacedId.fromDefault("auraskills_stat"), new AuraSkillsCorrector()); + + AuroraQuests.logger().info("Hooked into AuraSkills for GAIN_AURASKILLS_XP task types and for auraskills_stat rewards"); } } diff --git a/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsStatReward.java b/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsStatReward.java new file mode 100644 index 0000000..e241c3b --- /dev/null +++ b/src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsStatReward.java @@ -0,0 +1,61 @@ +package gg.auroramc.quests.hooks.auraskills; + +import dev.aurelium.auraskills.api.AuraSkillsApi; +import dev.aurelium.auraskills.api.registry.NamespacedId; +import dev.aurelium.auraskills.api.stat.Stat; +import dev.aurelium.auraskills.api.stat.StatModifier; +import gg.auroramc.aurora.api.message.Placeholder; +import gg.auroramc.aurora.api.reward.NumberReward; +import gg.auroramc.quests.AuroraQuests; +import lombok.Getter; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; + +import java.util.List; + +public class AuraSkillsStatReward extends NumberReward { + @Getter + private static final String AURA_SKILLS_STAT = "aurora_quests_"; + @Getter + private Stat stat; + + + @Override + public void execute(Player player, long level, List> placeholders) { + if (stat == null) return; + + var modId = AURA_SKILLS_STAT + stat.getId().toString(); + double value = getValue(placeholders); + + var user = AuraSkillsApi.get().getUser(player.getUniqueId()); + + var currentMod = user.getStatModifier(modId); + if (currentMod != null) { + value = currentMod.value() + value; + } + + user.addStatModifier(new StatModifier(modId, stat, value)); + } + + @Override + public void init(ConfigurationSection args) { + super.init(args); + + stat = AuraSkillsApi.get().getGlobalRegistry() + .getStat(NamespacedId.fromDefault(args.getString("stat", "invalid_stat"))); + + if (stat == null) { + AuroraQuests.logger().warning("Invalid stat in AuraSkillsStatReward: " + args.getString("stat")); + } + } + + @Override + public String getDisplay(Player player, List> placeholders) { + var display = super.getDisplay(player, placeholders); + var user = AuraSkillsApi.get().getUser(player.getUniqueId()); + + return Placeholder.execute(display, + Placeholder.of("{symbol}", stat.getSymbol(user.getLocale())), + Placeholder.of("{stat}", stat.getDisplayName(user.getLocale()))); + } +} diff --git a/src/main/java/gg/auroramc/quests/hooks/luckperms/PermissionCorrector.java b/src/main/java/gg/auroramc/quests/hooks/luckperms/PermissionCorrector.java index bc5dddb..8d17328 100644 --- a/src/main/java/gg/auroramc/quests/hooks/luckperms/PermissionCorrector.java +++ b/src/main/java/gg/auroramc/quests/hooks/luckperms/PermissionCorrector.java @@ -39,7 +39,7 @@ public void correctRewards(Player player) { var level = pool.getPlayerLevel(player); for (int i = 1; i < level + 1; i++) { - var matcher = pool.getMatcherManager().getBestMatcher(level); + var matcher = pool.getMatcherManager().getBestMatcher(i); if (matcher == null) continue; var placeholders = pool.getLevelPlaceholders(player, i); for (var reward : matcher.computeRewards(i)) {