-
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.
Optimize stat correctors to use less threads and player schedulers; A…
…dd EcoSkills as supported stat provider?
- Loading branch information
Showing
12 changed files
with
290 additions
and
137 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
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
70 changes: 70 additions & 0 deletions
70
src/main/java/gg/auroramc/levels/hooks/ecoskills/EcoSkillsCorrector.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,70 @@ | ||
package gg.auroramc.levels.hooks.ecoskills; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.willfp.ecoskills.api.EcoSkillsAPI; | ||
import com.willfp.ecoskills.api.modifiers.ModifierOperation; | ||
import com.willfp.ecoskills.api.modifiers.StatModifier; | ||
import com.willfp.ecoskills.stats.Stats; | ||
import gg.auroramc.aurora.api.reward.RewardCorrector; | ||
import gg.auroramc.levels.AuroraLevels; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.Map; | ||
|
||
public class EcoSkillsCorrector implements RewardCorrector { | ||
private final AuroraLevels plugin; | ||
|
||
public EcoSkillsCorrector(AuroraLevels plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public void correctRewards(Player player) { | ||
var leveler = plugin.getLeveler(); | ||
var data = leveler.getUserData(player); | ||
var level = data.getLevel(); | ||
|
||
Map<String, Map<ModifierOperation, Double>> statMap = Maps.newHashMap(); | ||
|
||
Stats.INSTANCE.values().forEach(stat -> { | ||
var map = statMap.computeIfAbsent(stat.getId(), (key) -> Maps.newHashMap()); | ||
for (var operation : ModifierOperation.values()) { | ||
map.put(operation, 0.0); | ||
} | ||
}); | ||
|
||
// Gather new stat modifiers | ||
for (int i = 1; i < level + 1; i++) { | ||
var matcher = leveler.getLevelMatcher().getBestMatcher(i); | ||
if (matcher == null) continue; | ||
var formulaPlaceholders = leveler.getRewardFormulaPlaceholders(player, i); | ||
for (var reward : matcher.computeRewards(i)) { | ||
if (reward instanceof EcoSkillsStatReward statReward && statReward.isValid()) { | ||
statMap.computeIfAbsent(statReward.getStat().getId(), (key) -> Maps.newHashMap()) | ||
.merge(statReward.getOperation(), statReward.getValue(formulaPlaceholders), Double::sum); | ||
} | ||
} | ||
} | ||
|
||
// Apply the new stat modifiers | ||
player.getScheduler().run(plugin, (task) -> { | ||
for (var entry : statMap.entrySet()) { | ||
var statType = entry.getKey(); | ||
var stat = Stats.INSTANCE.getByID(statType); | ||
if (stat == null) continue; | ||
|
||
for (var modifierEntry : entry.getValue().entrySet()) { | ||
var operation = modifierEntry.getKey(); | ||
var value = modifierEntry.getValue(); | ||
var uuid = EcoSkillsStatReward.createStatModifierUUID(stat, operation); | ||
if (value <= 0) { | ||
EcoSkillsAPI.removeStatModifier(player, uuid); | ||
} else { | ||
AuroraLevels.logger().debug("Adding stat " + statType + " with value " + value + "/" + operation.name() + " to player " + player.getName()); | ||
EcoSkillsAPI.addStatModifier(player, new StatModifier(uuid, stat, value, operation)); | ||
} | ||
} | ||
} | ||
}, null); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/gg/auroramc/levels/hooks/ecoskills/EcoSkillsHook.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,17 @@ | ||
package gg.auroramc.levels.hooks.ecoskills; | ||
|
||
import gg.auroramc.aurora.api.util.NamespacedId; | ||
import gg.auroramc.levels.AuroraLevels; | ||
import gg.auroramc.levels.hooks.Hook; | ||
|
||
public class EcoSkillsHook implements Hook { | ||
@Override | ||
public void hook(AuroraLevels plugin) { | ||
plugin.getLeveler().getRewardFactory() | ||
.registerRewardType(NamespacedId.fromDefault("ecoskills_stat"), EcoSkillsStatReward.class); | ||
plugin.getLeveler().getRewardAutoCorrector() | ||
.registerCorrector(NamespacedId.fromDefault("ecoskills_stat"), new EcoSkillsCorrector(plugin)); | ||
|
||
AuroraLevels.logger().info("Hooked into AuraSkills for stat rewards with reward type: 'ecoskills_stat'. Auto reward corrector for stats is registered."); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/gg/auroramc/levels/hooks/ecoskills/EcoSkillsStatReward.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,73 @@ | ||
package gg.auroramc.levels.hooks.ecoskills; | ||
|
||
import com.willfp.ecoskills.api.EcoSkillsAPI; | ||
import com.willfp.ecoskills.api.modifiers.ModifierOperation; | ||
import com.willfp.ecoskills.api.modifiers.StatModifier; | ||
import com.willfp.ecoskills.stats.Stat; | ||
import com.willfp.ecoskills.stats.Stats; | ||
import gg.auroramc.aurora.api.message.Placeholder; | ||
import gg.auroramc.aurora.api.reward.NumberReward; | ||
import gg.auroramc.levels.AuroraLevels; | ||
import lombok.Getter; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
public class EcoSkillsStatReward extends NumberReward { | ||
public static final String prefix = "aurora_levels/stat/"; | ||
|
||
private boolean valid = true; | ||
private Stat stat; | ||
private ModifierOperation operation = ModifierOperation.ADD; | ||
|
||
private UUID createStatModifierUUID() { | ||
return createStatModifierUUID(stat, operation); | ||
} | ||
|
||
public static UUID createStatModifierUUID(Stat stat, ModifierOperation operation) { | ||
return UUID.nameUUIDFromBytes((prefix + stat.getId() + "/" + operation.name()).getBytes()); | ||
} | ||
|
||
@Override | ||
public void execute(Player player, long level, List<Placeholder<?>> placeholders) { | ||
if (!valid) return; | ||
var uuid = createStatModifierUUID(); | ||
var current = EcoSkillsAPI.getStatModifier(player, uuid); | ||
|
||
double value = getValue(placeholders) + (current != null ? current.getModifier() : 0); | ||
|
||
// Since the UUID is always the same this should overwrite the previous value | ||
EcoSkillsAPI.addStatModifier(player, new StatModifier(uuid, stat, value, operation)); | ||
} | ||
|
||
@Override | ||
public void init(ConfigurationSection args) { | ||
super.init(args); | ||
var statName = args.getString("stat", ""); | ||
stat = Stats.INSTANCE.getByID(statName); | ||
if (stat == null) { | ||
valid = false; | ||
AuroraLevels.logger().warning("Couldn't find EcoSkills stat: " + statName); | ||
} | ||
|
||
var operationName = args.getString("operation", "add").toUpperCase(Locale.ROOT); | ||
|
||
try { | ||
operation = ModifierOperation.valueOf(operationName); | ||
} catch (IllegalArgumentException e) { | ||
valid = false; | ||
AuroraLevels.logger().warning("Couldn't find EcoSkills operation: " + operationName); | ||
} | ||
} | ||
|
||
@Override | ||
public String getDisplay(Player player, List<Placeholder<?>> placeholders) { | ||
var display = super.getDisplay(player, placeholders); | ||
if (!valid) return display; | ||
return Placeholder.execute(display, Placeholder.of("{stat}", stat.getName())); | ||
} | ||
} |
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
Oops, something went wrong.