Skip to content

Commit

Permalink
Fix *old* stat updating
Browse files Browse the repository at this point in the history
  • Loading branch information
Sentropic committed Mar 25, 2024
1 parent 042424a commit bc466ab
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package su.nightexpress.quantumrpg.stats.items.api;

import mc.promcteam.engine.utils.DataUT;
import mc.promcteam.engine.utils.ItemUT;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -51,11 +52,32 @@ public boolean add(@NotNull ItemStack item, @NotNull Z value, int line) {

public boolean add(@NotNull ItemStack item, @NotNull Z value, int index, int line) {
if (index < 0 && value instanceof StatBonus) {
for (int i = 0, size = this.getAmount(item); i < size; i++) {
StatBonus existing = (StatBonus) this.getRaw(item, i);
if (existing != null && Objects.equals(existing.getCondition(), ((StatBonus) value).getCondition())) {
index = i;
break;
StatBonus statBonus = ((StatBonus) value);

// Look for legacy format
if (statBonus.getCondition() == null && !statBonus.isPercent()) {
ItemMeta meta = item.getItemMeta();
if (meta != null) {
PersistentDataContainer container = meta.getPersistentDataContainer();
for (NamespacedKey key : this.keys) {
if (container.has(key, PersistentDataType.DOUBLE) || container.has(key, DataUT.DOUBLE_ARRAY)) {
line = ItemUT.getLoreIndex(item, key.getKey());
index = 0;
container.remove(key);
item.setItemMeta(meta);
break;
}
}
}
}
if (index < 0) {
// Look for existing stats with same condition
for (int i = 0, size = this.getAmount(item); i < size; i++) {
StatBonus existing = (StatBonus) this.getRaw(item, i);
if (existing != null && Objects.equals(existing.getCondition(), (statBonus).getCondition())) {
index = i;
break;
}
}
}
}
Expand Down Expand Up @@ -224,6 +246,10 @@ public final int getLoreIndex(@NotNull ItemStack item, int index) {
for (NamespacedKey key : this.keys) {
found = ItemUT.getLoreIndex(item, key.getKey()+i);
if (found >= 0) break;

// Try legacy format
found = ItemUT.getLoreIndex(item, key.getKey());
if (found >= 0) break;
}

if (found >= 0 && index == count++) {
Expand Down

0 comments on commit bc466ab

Please sign in to comment.