Skip to content

Commit

Permalink
Fix command permissions and reward correctors; Add mythic_stat reward
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Aug 5, 2024
1 parent 47642ff commit 7fe89b9
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/gg/auroramc/quests/command/QuestsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onReload(CommandSender sender) {
@Subcommand("open")
@Description("Opens the quest menu for another player in a specific pool")
@CommandCompletion("@players @pools true|false")
@CommandPermission("aurora.collections.admin.open")
@CommandPermission("aurora.quests.admin.open")
public void onOpenMenu(CommandSender sender, @Flags("other") Player target, @Default("none") String poolId, @Default("false") Boolean silent) {
if (poolId.equals("none") || poolId.equals("all")) {
new MainMenu(target).open();
Expand All @@ -62,7 +62,7 @@ public void onOpenMenu(CommandSender sender, @Flags("other") Player target, @Def
@Subcommand("reroll")
@Description("Rerolls quests for another player in a specific pool")
@CommandCompletion("@players @pools true|false")
@CommandPermission("aurora.collections.admin.reroll")
@CommandPermission("aurora.quests.admin.reroll")
public void onReroll(CommandSender sender, @Flags("other") Player target, @Default("all") String poolId, @Default("false") Boolean silent) {
if (poolId.equals("none") || poolId.equals("all")) {
plugin.getQuestManager().getQuestPools().forEach((pool) -> pool.reRollQuests(target, !silent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand All @@ -14,7 +13,6 @@

public class AuraSkillsCorrector implements RewardCorrector {


@Override
public void correctRewards(Player player) {
var manager = AuroraQuests.getInstance().getQuestManager();
Expand All @@ -41,7 +39,7 @@ public void correctRewards(Player player) {
}

// Correct quest pool leveling
if (!pool.hasLeveling()) return;
if (!pool.hasLeveling()) continue;
var level = pool.getPlayerLevel(player);

for (int i = 1; i < level + 1; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void correctRewards(Player player) {
}

// Correct quest pool leveling
if (!pool.hasLeveling()) return;
if (!pool.hasLeveling()) continue;
var level = pool.getPlayerLevel(player);

for (int i = 1; i < level + 1; i++) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gg/auroramc/quests/hooks/mythicmobs/MythicHook.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package gg.auroramc.quests.hooks.mythicmobs;

import gg.auroramc.aurora.api.util.NamespacedId;
import gg.auroramc.quests.AuroraQuests;
import gg.auroramc.quests.hooks.Hook;
import gg.auroramc.quests.hooks.mythicmobs.reward.MythicStatCorrector;
import gg.auroramc.quests.hooks.mythicmobs.reward.MythicStatReward;
import org.bukkit.Bukkit;

public class MythicHook implements Hook {
@Override
public void hook(AuroraQuests plugin) {
Bukkit.getPluginManager().registerEvents(new MythicMobListener(), plugin);

plugin.getQuestManager().getRewardFactory()
.registerRewardType(NamespacedId.fromDefault("mythic_stat"), MythicStatReward.class);

plugin.getQuestManager().getRewardAutoCorrector()
.registerCorrector(NamespacedId.fromDefault("mythic_stat"), new MythicStatCorrector());

AuroraQuests.logger().info("Hooked into MythicMobs for KILL_MYTHIC_MOB task type");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gg.auroramc.quests.hooks.mythicmobs.reward;

import io.lumine.mythic.core.skills.stats.StatSource;

public class AuroraQuestsStatSource implements StatSource {
@Override
public boolean removeOnReload() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package gg.auroramc.quests.hooks.mythicmobs.reward;

import com.google.common.collect.Maps;
import gg.auroramc.aurora.api.reward.RewardCorrector;
import gg.auroramc.quests.AuroraQuests;
import io.lumine.mythic.bukkit.MythicBukkit;
import io.lumine.mythic.core.skills.stats.StatModifierType;
import io.lumine.mythic.core.skills.stats.StatType;
import org.bukkit.entity.Player;

import java.util.Map;

public class MythicStatCorrector implements RewardCorrector {

@Override
public void correctRewards(Player player) {
var plugin = AuroraQuests.getInstance();
var manager = plugin.getQuestManager();
var mythic = MythicBukkit.inst();
var registry = mythic.getPlayerManager().getProfile(player).getStatRegistry();

Map<StatType, Map<StatModifierType, Double>> statMap = Maps.newHashMap();

mythic.getStatManager().getStats().values()
.forEach(statType -> {
if (!statType.isEnabled()) return;
registry.removeValue(statType, MythicStatReward.getSource());
});

// 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 MythicStatReward statReward && statReward.isValid()) {
statMap.computeIfAbsent(statReward.getStatType(), (key) -> Maps.newHashMap())
.merge(statReward.getModifierType(), statReward.getValue(quest.getPlaceholders(player)), Double::sum);
}
}
}
}

// Correct quest pool leveling
if (!pool.hasLeveling()) continue;
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 MythicStatReward statReward && statReward.isValid()) {
statMap.computeIfAbsent(statReward.getStatType(), (key) -> Maps.newHashMap())
.merge(statReward.getModifierType(), statReward.getValue(placeholders), Double::sum);
}
}
}
}

// Apply the new stat modifiers
for (var entry : statMap.entrySet()) {
var statType = entry.getKey();
for (var modifierEntry : entry.getValue().entrySet()) {
var modifierType = modifierEntry.getKey();
var value = modifierEntry.getValue();
AuroraQuests.logger().debug("Adding stat " + statType.getKey() + " with value " + value + " to player " + player.getName());
registry.putValue(statType, MythicStatReward.getSource(), modifierType, value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package gg.auroramc.quests.hooks.mythicmobs.reward;

import gg.auroramc.aurora.api.message.Placeholder;
import gg.auroramc.aurora.api.reward.NumberReward;
import gg.auroramc.quests.AuroraQuests;
import io.lumine.mythic.bukkit.MythicBukkit;
import io.lumine.mythic.core.skills.stats.StatModifierType;
import io.lumine.mythic.core.skills.stats.StatRegistry;
import io.lumine.mythic.core.skills.stats.StatSource;
import io.lumine.mythic.core.skills.stats.StatType;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import java.util.List;

@Getter
public class MythicStatReward extends NumberReward {
@Getter
private static final StatSource source = new AuroraQuestsStatSource();

private StatType statType;
private StatModifierType modifierType;
private boolean valid = true;

@Override
public void execute(Player player, long level, List<Placeholder<?>> placeholders) {
if (!valid) return;

StatRegistry registry = MythicBukkit.inst().getPlayerManager().getProfile(player).getStatRegistry();

if (registry.getStatData(statType).isPresent()) {

StatRegistry.StatMap data = registry.getStatData(statType).get();
double current = 0;

switch (modifierType) {
case ADDITIVE -> current = data.getAdditives().getOrDefault(source, 0.0);
case ADDITIVE_MULTIPLIER -> current = data.getAdditiveMultipliers().getOrDefault(source, 0.0);
case COMPOUND_MULTIPLIER -> current = data.getCompoundMultipliers().getOrDefault(source, 0.0);
case SETTER -> current = data.getSetters().getOrDefault(source, 0.0);
}

registry.putValue(statType, source, modifierType, current + getValue(placeholders));
} else {
registry.putValue(statType, source, modifierType, getValue(placeholders));
}
}

@Override
public void init(ConfigurationSection args) {
super.init(args);
var stat = args.getString("stat");

if (stat == null) {
this.valid = false;
AuroraQuests.logger().warning("Stat is not defined in MythicStatReward");
return;
}

this.statType = MythicBukkit.inst().getStatManager().getStats().get(stat);

if (statType == null) {
this.valid = false;
AuroraQuests.logger().warning("Invalid stat: " + stat + " in MythicStatReward");
return;
}

if (!statType.isEnabled()) {
this.valid = false;
AuroraQuests.logger().warning("Stat: " + stat + " is not enabled in MythicMobs. Cannot create MythicStatReward.");
return;
}

var modifier = args.getString("modifier", "additive");

try {
this.modifierType = StatModifierType.valueOf(modifier.toUpperCase());
} catch (IllegalArgumentException e) {
this.valid = false;
AuroraQuests.logger().warning("Invalid modifier type: " + modifier + " in MythicStatReward");
}
}

@Override
public String getDisplay(Player player, List<Placeholder<?>> placeholders) {
var display = super.getDisplay(player, placeholders);

if (!valid) {
return display;
}

var value = getValue(placeholders);

return Placeholder.execute(display,
Placeholder.of("{stat}", statType.getDisplay()),
Placeholder.of("{stat_display}", statType.getFormattedValue(modifierType, value))
);
}
}

0 comments on commit 7fe89b9

Please sign in to comment.