Skip to content

Commit

Permalink
Added AuraSkills stat rewards with auto correctors
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Aug 5, 2024
1 parent 5799d7b commit 47642ff
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 2 deletions.
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()));
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}
}
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())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 47642ff

Please sign in to comment.