Skip to content

Commit

Permalink
Added MMOLib (MythicLib) hook for stat rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Aug 7, 2024
1 parent 2c73ef4 commit 46db275
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 5 deletions.
7 changes: 4 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ repositories {
maven("https://jitpack.io/")
maven("https://repo.projectshard.dev/repository/releases/")
maven("https://repo.oraxen.com/releases")
maven("https://nexus.phoenixdevt.fr/repository/maven-public/")
}

dependencies {
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
compileOnly("gg.auroramc:Aurora:1.5.5")
compileOnly("gg.auroramc:AuroraLevels:1.3.0")
compileOnly("gg.auroramc:AuroraCollections:1.0.0-SNAPSHOT")
compileOnly("gg.auroramc:Aurora:1.5.9")
compileOnly("gg.auroramc:AuroraLevels:1.5.1")
compileOnly("net.luckperms:api:5.4")
compileOnly("dev.aurelium:auraskills-api-bukkit:2.2.0")
compileOnly("io.lumine:Mythic-Dist:5.6.1")
Expand All @@ -52,6 +52,7 @@ dependencies {
compileOnly("com.github.Gypopo:EconomyShopGUI-API:1.7.1")
compileOnly("io.th0rgal:oraxen:1.179.0")
compileOnly("com.github.brcdev-minecraft:shopgui-api:3.0.0")
compileOnly("io.lumine:MythicLib-dist:1.6.2-SNAPSHOT")

implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT")

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/gg/auroramc/quests/hooks/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gg.auroramc.quests.hooks.citizens.CitizensHook;
import gg.auroramc.quests.hooks.customfishing.CustomFishingHook;
import gg.auroramc.quests.hooks.luckperms.LuckPermsHook;
import gg.auroramc.quests.hooks.mmolib.MMOLibHook;
import gg.auroramc.quests.hooks.mythicmobs.MythicHook;
import gg.auroramc.quests.hooks.oraxen.OraxenHook;
import gg.auroramc.quests.hooks.shopguiplus.ShopGUIPlusHook;
Expand All @@ -25,6 +26,7 @@ public enum Hooks {
CITIZENS(CitizensHook.class, "Citizens"),
SHOPKEEPERS(ShopkeepersHook.class, "Shopkeepers"),
ORAXEN(OraxenHook.class, "Oraxen"),
MMOLIB(MMOLibHook.class, "MythicLib"),
SHOP_GUI_PLUS(ShopGUIPlusHook.class, "ShopGUIPlus"),
ECONOMY_SHOP_GUI(CitizensHook.class, Set.of("EconomyShopGUI", "EconomyShopGUI-Premium")),
LUCK_PERMS(LuckPermsHook.class, "LuckPerms");
Expand All @@ -43,8 +45,8 @@ public enum Hooks {
}

public boolean canHook() {
for(String plugin : plugins) {
if(Bukkit.getPluginManager().getPlugin(plugin) != null) {
for (String plugin : plugins) {
if (Bukkit.getPluginManager().getPlugin(plugin) != null) {
return true;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/gg/auroramc/quests/hooks/mmolib/MMOLibHook.java
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));
}
}
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 src/main/java/gg/auroramc/quests/hooks/mmolib/MMOStatReward.java
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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ softdepend:
- "AuroraLevels"
- "Oraxen"
- "ShopGUIPlus"
- "MythicLib"

website: https://auroramc.gg

Expand Down

0 comments on commit 46db275

Please sign in to comment.