-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AuraSkills stat rewards with auto correctors
- Loading branch information
Showing
4 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsCorrector.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,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<Stat, Double> 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())); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/main/java/gg/auroramc/quests/hooks/auraskills/AuraSkillsStatReward.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,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<Placeholder<?>> 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<Placeholder<?>> 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()))); | ||
} | ||
} |
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