-
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 MMOLib (MythicLib) hook for stat rewards
- Loading branch information
Showing
6 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
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
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
16 changes: 16 additions & 0 deletions
16
src/main/java/gg/auroramc/quests/hooks/mmolib/MMOLibHook.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,16 @@ | ||
package gg.auroramc.quests.hooks.mmolib; | ||
|
||
import gg.auroramc.aurora.api.util.NamespacedId; | ||
import gg.auroramc.quests.AuroraQuests; | ||
import gg.auroramc.quests.hooks.Hook; | ||
|
||
public class MMOLibHook implements Hook { | ||
@Override | ||
public void hook(AuroraQuests plugin) { | ||
plugin.getQuestManager().getRewardFactory() | ||
.registerRewardType(NamespacedId.fromDefault("mmo_stat"), MMOStatReward.class); | ||
|
||
plugin.getQuestManager().getRewardAutoCorrector() | ||
.registerCorrector(NamespacedId.fromDefault("mmo_stat"), new MMOStatCorrector(plugin)); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/gg/auroramc/quests/hooks/mmolib/MMOStatCorrector.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,88 @@ | ||
package gg.auroramc.quests.hooks.mmolib; | ||
|
||
import com.google.common.collect.Maps; | ||
import gg.auroramc.aurora.api.reward.RewardCorrector; | ||
import gg.auroramc.aurora.api.util.NamespacedId; | ||
import gg.auroramc.quests.AuroraQuests; | ||
import io.lumine.mythic.lib.api.player.EquipmentSlot; | ||
import io.lumine.mythic.lib.api.player.MMOPlayerData; | ||
import io.lumine.mythic.lib.api.stat.StatMap; | ||
import io.lumine.mythic.lib.api.stat.modifier.StatModifier; | ||
import io.lumine.mythic.lib.player.modifier.ModifierSource; | ||
import io.lumine.mythic.lib.player.modifier.ModifierType; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class MMOStatCorrector implements RewardCorrector { | ||
private final AuroraQuests plugin; | ||
|
||
public MMOStatCorrector(AuroraQuests plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public void correctRewards(Player player) { | ||
var manager = plugin.getQuestManager(); | ||
|
||
MMOPlayerData playerData = MMOPlayerData.get(player); | ||
StatMap stats = playerData.getStatMap(); | ||
|
||
Map<String, MMOStat> statMap = Maps.newHashMap(); | ||
|
||
// Gather new stat modifiers | ||
for (var pool : manager.getQuestPools()) { | ||
if (pool.isGlobal()) { | ||
for (var quest : pool.getQuests()) { | ||
if (!quest.isCompleted(player)) continue; | ||
|
||
for (var reward : quest.getRewards().values()) { | ||
if (reward instanceof MMOStatReward statReward && statReward.isValid()) { | ||
var key = NamespacedId.of(MMOStatReward.getMMO_STAT(), statReward.getStat()).toString(); | ||
var current = statReward.getCurrentModifier(key, stats); | ||
UUID uuid = current != null ? current.getUniqueId() : UUID.randomUUID(); | ||
statMap.merge(statReward.getStat(), | ||
new MMOStat(statReward.getModifierType(), statReward.getValue(quest.getPlaceholders(player)), key, uuid), | ||
(a, b) -> new MMOStat(statReward.getModifierType(), a.value() + b.value(), a.key(), a.uuid())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
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 MMOStatReward statReward && statReward.isValid()) { | ||
var key = NamespacedId.of(MMOStatReward.getMMO_STAT(), statReward.getStat()).toString(); | ||
var current = statReward.getCurrentModifier(key, stats); | ||
UUID uuid = current != null ? current.getUniqueId() : UUID.randomUUID(); | ||
statMap.merge(statReward.getStat(), | ||
new MMOStat(statReward.getModifierType(), statReward.getValue(placeholders), key, uuid), | ||
(a, b) -> new MMOStat(statReward.getModifierType(), a.value() + b.value(), a.key(), a.uuid())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Bukkit.getGlobalRegionScheduler().runDelayed(plugin, (task) -> { | ||
for (var entry : statMap.entrySet()) { | ||
var statType = entry.getKey(); | ||
var s = entry.getValue(); | ||
|
||
new StatModifier(s.uuid, s.key, statType, s.value, s.modifierType, EquipmentSlot.OTHER, ModifierSource.OTHER) | ||
.register(playerData); | ||
} | ||
}, 3); | ||
} | ||
|
||
public record MMOStat(ModifierType modifierType, double value, String key, UUID uuid) { | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/gg/auroramc/quests/hooks/mmolib/MMOStatReward.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,75 @@ | ||
package gg.auroramc.quests.hooks.mmolib; | ||
|
||
import gg.auroramc.aurora.api.message.Placeholder; | ||
import gg.auroramc.aurora.api.reward.NumberReward; | ||
import gg.auroramc.aurora.api.util.NamespacedId; | ||
import gg.auroramc.quests.AuroraQuests; | ||
import io.lumine.mythic.lib.api.player.MMOPlayerData; | ||
import io.lumine.mythic.lib.api.stat.StatMap; | ||
import io.lumine.mythic.lib.api.stat.modifier.StatModifier; | ||
import io.lumine.mythic.lib.player.modifier.ModifierType; | ||
import lombok.Getter; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class MMOStatReward extends NumberReward { | ||
@Getter | ||
private static final String MMO_STAT = "aurora_quests"; | ||
private String stat; | ||
private ModifierType modifierType; | ||
private boolean valid = true; | ||
|
||
@Override | ||
public void execute(Player player, long level, List<Placeholder<?>> placeholders) { | ||
if (!valid) return; | ||
|
||
MMOPlayerData playerData = MMOPlayerData.get(player); | ||
StatMap statMap = playerData.getStatMap(); | ||
String key = NamespacedId.of(MMO_STAT, stat).toString(); | ||
StatModifier currentModifier = getCurrentModifier(key, statMap); | ||
|
||
double value = getValue(placeholders); | ||
|
||
if (currentModifier != null) { | ||
value += currentModifier.getValue(); | ||
currentModifier.unregister(playerData); | ||
} | ||
|
||
StatModifier modifier = new StatModifier(key, stat, value, modifierType); | ||
|
||
modifier.register(playerData); | ||
|
||
Bukkit.getGlobalRegionScheduler().runDelayed(AuroraQuests.getInstance(), (task) -> { | ||
statMap.getInstance(stat).update(); | ||
}, 3); | ||
} | ||
|
||
@Override | ||
public void init(ConfigurationSection args) { | ||
super.init(args); | ||
|
||
this.stat = args.getString("stat"); | ||
|
||
if (this.stat == null) { | ||
this.valid = false; | ||
AuroraQuests.logger().warning("Stat is not defined in MMOStatReward"); | ||
} | ||
|
||
this.modifierType = ModifierType.valueOf(args.getString("modifier", "FLAT")); | ||
} | ||
|
||
@Nullable | ||
public StatModifier getCurrentModifier(String key, StatMap statMap) { | ||
for (StatModifier stat : statMap.getInstance(stat).getModifiers()) { | ||
if (stat.getKey().equals(key)) { | ||
return stat; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ softdepend: | |
- "AuroraLevels" | ||
- "Oraxen" | ||
- "ShopGUIPlus" | ||
- "MythicLib" | ||
|
||
website: https://auroramc.gg | ||
|
||
|