Skip to content

Commit

Permalink
Fix refined stats lore display #243
Browse files Browse the repository at this point in the history
  • Loading branch information
Sentropic committed Jul 4, 2024
1 parent d662289 commit dbb643e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package studio.magemonkey.divinity.modules.list.refine;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
Expand All @@ -16,6 +17,7 @@
import studio.magemonkey.codex.util.actions.ActionManipulator;
import studio.magemonkey.codex.util.random.Rnd;
import studio.magemonkey.divinity.Divinity;
import studio.magemonkey.divinity.config.EngineCfg;
import studio.magemonkey.divinity.modules.EModule;
import studio.magemonkey.divinity.modules.RatedItem;
import studio.magemonkey.divinity.modules.api.QModuleDrop;
Expand Down Expand Up @@ -118,7 +120,7 @@ public void setup() {
}

path = "format.item-lore.";
this.formatLoreStat = StringUT.color(cfg.getString(path + "format", "&8(&7+%amount%&8)"));
this.formatLoreStat = StringUT.color(cfg.getString(path + "format", " &8(&7%+%%amount%&8)"));
this.formatLoreText = StringUT.color(cfg.getStringList(path + "text"));

this.cfg.saveChanges();
Expand Down Expand Up @@ -366,21 +368,6 @@ private void addFines(@NotNull ItemStack item, @NotNull RefineItem stone, int re
bMap.getDefenseBonuses().forEach((bStat, bFunc) -> refineValues.put(bStat,
BonusCalculator.SIMPLE_BONUS.apply(bStat.getTotal(item, null), Collections.singletonList(bFunc))));

Map<ItemLoreStat<?>, String> statTags = new HashMap<>();

refineValues.forEach((stat, value) -> {
if (value == 0) return;
int pos = stat.getLoreIndex(item);
if (pos < 0) return;

String line = lore.get(pos);

String format = this.formatLoreStat.replace("%amount%", NumberUT.format(value));
String lineStat = new StringBuilder(line).append(format).toString();
lore.set(pos, lineStat);
statTags.put(stat, lineStat);
});

// Update item description with refine values
meta.setDisplayName(name);
meta.setLore(lore);
Expand All @@ -392,9 +379,6 @@ private void addFines(@NotNull ItemStack item, @NotNull RefineItem stone, int re
ItemUT.addNameTag(item, TAG_REFINE_NAME, preName);
ItemUT.addLoreTag(item, TAG_REFINE_LORE, loreTag.toString());

// Replace old stat lore tags with refined ones.
statTags.forEach((stat, value) -> ItemUT.addLoreTag(item, stat.getMetaId(item), value));

if (fortifyManager != null) {
fortifyManager.formatItemName(item);
}
Expand Down Expand Up @@ -472,6 +456,19 @@ private void resetFines(@NotNull ItemStack item) {
ItemUT.delNameTag(item, TAG_REFINE_NAME);
}

@NotNull
public String getFormatLoreStat(ItemStack item, ItemLoreStat<?> stat, double value) {
List<BiFunction<Boolean, Double, Double>> bonuses = new ArrayList<>();
bonuses.add((isPercent, input) -> isPercent ? input : input + value);
bonuses.add(this.getRefinedBonus(item, stat));
double diff = BonusCalculator.SIMPLE_FULL.apply(0D, bonuses)-value;
if (diff == 0) return "";
return this.formatLoreStat.replace("%amount%", (diff < 0 ? EngineCfg.LORE_CHAR_NEGATIVE : "")
+ ChatColor.stripColor(EngineCfg.LORE_STYLE_DAMAGE_FORMAT_SINGLE
.replace("%value%", String.valueOf(Math.round(diff*100)/100.0))))
.replace("%+%", diff > 0 ? EngineCfg.LORE_CHAR_POSITIVE : "");
}

@NotNull
private ClickText getResultMessage(
@NotNull ItemStack target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,31 @@ public double getDamageModifierByMythicFaction(@Nullable String faction) {
@NotNull
public String formatValue(@NotNull ItemStack item, @NotNull StatBonus value) {
double[] array = value.getValue();
String sVal;
if (array.length == 1) {
return EngineCfg.LORE_STYLE_DAMAGE_FORMAT_SINGLE
.replace("%value%", NumberUT.format(array[0]) + (value.isPercent() ? "%" : ""));
sVal = NumberUT.format(array[0]);
if (value.isPercent()) {
sVal += EngineCfg.LORE_CHAR_PERCENT;
} else if (value.getCondition() == null) { // This is the base stat, apply refines
RefineManager refine = Divinity.getInstance().getModuleCache().getRefineManager();
if (refine != null) sVal += refine.getFormatLoreStat(item, this, array[0]);
}
sVal = EngineCfg.LORE_STYLE_DAMAGE_FORMAT_SINGLE.replace("%value%", sVal);
} else {
return EngineCfg.LORE_STYLE_DAMAGE_FORMAT_RANGE
.replace("%min%", NumberUT.format(array[0]))
.replace("%max%", NumberUT.format(array[1]));

String sMin = NumberUT.format(array[0]);
String sMax = NumberUT.format(array[1]);
if (value.getCondition() == null) { // This is the base stat, apply refines
RefineManager refine = Divinity.getInstance().getModuleCache().getRefineManager();
if (refine != null) {
sMin += refine.getFormatLoreStat(item, this, array[0]);
sMax += refine.getFormatLoreStat(item, this, array[1]);
}
}
sVal = EngineCfg.LORE_STYLE_DAMAGE_FORMAT_RANGE
.replace("%min%", sMin)
.replace("%max%", sMax);
}
return sVal;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ public boolean isDefault() {
@Override
@NotNull
public String formatValue(@NotNull ItemStack item, @NotNull StatBonus statBonus) {
return NumberUT.format(statBonus.getValue()[0]) + (statBonus.isPercent() ? "%" : "");
String sVal = NumberUT.format(statBonus.getValue()[0]);
if (statBonus.isPercent()) {
sVal += EngineCfg.LORE_CHAR_PERCENT;
} else if (statBonus.getCondition() == null) { // This is the base stat, apply refines
RefineManager refine = Divinity.getInstance().getModuleCache().getRefineManager();
if (refine != null) sVal += refine.getFormatLoreStat(item, this, statBonus.getValue()[0]);
}
return sVal;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ public Type getDependStat() {
@NotNull
public String formatValue(@NotNull ItemStack item, StatBonus statBonus) {
double val = this.fineValue(statBonus.getValue()[0]);
if (val == 0) {
return "";
}
if (val == 0) return "";

boolean bonus = !this.isMainItem(item);
String sVal = NumberUT.format(val);

if (this.canBeNegative() || bonus) {
sVal = (val > 0 ? EngineCfg.LORE_CHAR_POSITIVE : EngineCfg.LORE_CHAR_NEGATIVE) + sVal;
}
if (this.isPercent() || bonus) {
if (this.isPercent() ) {
sVal += EngineCfg.LORE_CHAR_PERCENT;
} else {
if (this.statType == Type.CRITICAL_DAMAGE) sVal += EngineCfg.LORE_CHAR_MULTIPLIER;
if (statBonus.getCondition() == null) { // This is the base stat, apply refines
RefineManager refine = Divinity.getInstance().getModuleCache().getRefineManager();
if (refine != null) sVal += refine.getFormatLoreStat(item, this, statBonus.getValue()[0]);
}
}
if (this.statType == Type.CRITICAL_DAMAGE && !bonus) {
sVal += EngineCfg.LORE_CHAR_MULTIPLIER;
}

return sVal;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/modules/refine/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ format:
'1': '+%level% '
'3': '&c&l+%level% '
item-lore:
format: ' &8(&7+%amount%&8)'
format: ' &8(&7%+%%amount%&8)'
text:
- '%ITEM_LORE%'
- '&7'
Expand Down

0 comments on commit dbb643e

Please sign in to comment.