Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.jetbrains.annotations.NotNull;
import studio.magemonkey.codex.manager.IListener;
import studio.magemonkey.codex.util.ItemUT;
Expand Down Expand Up @@ -41,6 +40,22 @@ public void onDuraItemDamage(PlayerItemDamageEvent e) {
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onMending(PlayerItemMendEvent e) {
ItemStack item = e.getItem();
if (!ItemStats.hasStat(item, null, TypedStat.Type.DURABILITY)) return;

e.setCancelled(true); // block vanilla mending

double[] dur = duraStat.getRaw(item);
if (dur == null || dur[1] <= 0 || dur[0] >= dur[1]) return; // full, unbreakable, or no data

int repairAmount = e.getRepairAmount();
double newCurrent = Math.min(dur[0] + repairAmount, dur[1]);
duraStat.add(item, new double[]{newCurrent, dur[1]}, -1);
duraStat.syncVanillaBar(item);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onDuraBreak(BlockBreakEvent e) {
Player player = e.getPlayer();
Expand Down Expand Up @@ -115,48 +130,4 @@ public void onDuraHoe(PlayerInteractEvent e) {
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onMend(PlayerItemMendEvent e) {
ItemStack item = e.getItem();

if (!ItemStats.hasStat(item, null, TypedStat.Type.DURABILITY)) return;

double[] durability = duraStat.getRaw(item);
if (durability == null || duraStat.isUnbreakable(item)) return;

double current = durability[0];
double max = durability[1];
int vanillaMax = item.getType().getMaxDurability();

if (vanillaMax <= 0) return;

int repair = e.getRepairAmount();
// Scale the repair amount to the durability max, so we can
// properly update the custom durability amount.
double customRepair = ((double) repair / vanillaMax) * max;
double newValue = current + customRepair;

// Cap the durability at the max
if (newValue > max) {
newValue = max;
}

newValue = Math.round(newValue * 100.0) / 100.0;

duraStat.add(item, new double[]{newValue, max}, -1);
duraStat.syncVanillaBar(item, newValue, max);

e.setCancelled(true);

Damageable damageable = (Damageable) item.getItemMeta();
if (damageable != null) {
int vanillaDamage = damageable.getDamage();
if (vanillaDamage == 0) {
duraStat.add(item, new double[]{max, max}, -1);
duraStat.syncVanillaBar(item, max, max);
e.setCancelled(true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void generate(@NotNull ItemStack item, int itemLevel) {
} else if (stat instanceof DurabilityStat) {
DurabilityStat rStat = (DurabilityStat) stat;
rStat.add(item, new double[]{vFin, vFin}, -1);
rStat.syncVanillaBar(item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ ItemStack getResult(@NotNull ItemStack target, @NotNull Player player) {
double max = arr[1];
ItemStack result = new ItemStack(target);
this.duraStat.add(result, new double[]{max, max}, -1);
this.duraStat.syncVanillaBar(result);

return result;
}
Expand Down Expand Up @@ -382,6 +383,7 @@ protected boolean onDragDrop(
durNow = (int) Math.min(durMax, durNow + durMax * 1D * (rPerc * 1D / 100D));

this.duraStat.add(target, new double[]{durNow, durMax}, -1);
this.duraStat.syncVanillaBar(target);
e.setCurrentItem(target);

if (lost != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
public class DurabilityStat extends ItemLoreStat<double[]> implements TypedStat {
private double cap;

public DurabilityStat(@NotNull String name, @NotNull String format, double cap) {
super(TypedStat.Type.DURABILITY.name(),
public DurabilityStat(
@NotNull String name,
@NotNull String format,
double cap) {
super(
TypedStat.Type.DURABILITY.name(),
name,
format,
"%ITEM_STAT_" + TypedStat.Type.DURABILITY.name() + "%",
Expand Down Expand Up @@ -111,7 +115,9 @@ public boolean isBroken(@NotNull ItemStack item) {
return durability != null && durability[0] == 0 && !EngineCfg.ATTRIBUTES_DURABILITY_BREAK_ITEMS;
}

public boolean reduceDurability(@NotNull LivingEntity li, @NotNull ItemStack item, int amount) {
public boolean reduceDurability(
@NotNull LivingEntity li, @NotNull ItemStack item, int amount) {

if (!(li instanceof Player) && !EngineCfg.ATTRIBUTES_DURABILITY_REDUCE_FOR_MOBS) return false;
if (this.isUnbreakable(item)) return false;

Expand Down Expand Up @@ -151,44 +157,50 @@ public boolean reduceDurability(@NotNull LivingEntity li, @NotNull ItemStack ite
}

boolean result = this.add(item, new double[]{lose, max}, -1);

if (result) {
syncVanillaBar(item, lose, max);
}

if (result) syncVanillaBar(item);
return result;

}

@Override
@NotNull
public String formatValue(@NotNull ItemStack item, double[] values) {
return EngineCfg.getDurabilityFormat((int) values[0], (int) values[1]);
}

/**
* Syncs the durability stat with the vanilla durability bar. Should be called after any change to the durability stat.
* Note: This method assumes that the durability stat is already updated with the new values before calling it.
*
* @param item the item that needs updating
* @param current the current durability value on the item
* @param maxCustom the max value possible to be set for the item
* @deprecated the current/max arguments are no longer used — the stat now reads its own
* values directly, since deriving the vanilla-bar percentage from caller-supplied
* values (rather than the stat's own state) was the source of the sync bug this
* replaced. Kept for source/binary compatibility with existing callers; use
* {@link #syncVanillaBar(ItemStack)} instead.
*/
@Deprecated
public void syncVanillaBar(@NotNull ItemStack item, double current, double maxCustom) {
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof Damageable)) return;
syncVanillaBar(item);
}

Damageable damageable = (Damageable) meta;
/**
* Synchronizes the vanilla durability bar to reflect Divinity custom durability as a percentage.
* Safeguard: if vanilla bar would show 100% but Divinity dura is not max, vanilla bar shows at least 1 damage.
*/
public void syncVanillaBar(@NotNull ItemStack item) {
double[] dur = this.getRaw(item);
if (dur == null || dur[1] <= 0) return;
if (!(item.getItemMeta() instanceof Damageable)) return;

double percent = dur[0] / dur[1];
int maxVanilla = item.getType().getMaxDurability();
if (maxVanilla <= 0) return;

double percent = current / maxCustom;
// Scale the vanilla value to the custom percentage
int vanillaDamage = (int) ((1.0 - percent) * maxVanilla);
int vanillaDamage = (int) Math.round(maxVanilla * (1.0 - percent));

// Safeguard: don't show full vanilla bar when divinity dura is not max
if (vanillaDamage == 0 && dur[0] < dur[1]) {
vanillaDamage = 1;
}

damageable.setDamage(vanillaDamage);
item.setItemMeta(damageable);
Damageable meta = (Damageable) item.getItemMeta();
meta.setDamage(vanillaDamage);
item.setItemMeta((ItemMeta) meta);
}

}
@Override
@NotNull
public String formatValue(@NotNull ItemStack item, double[] values) {
return EngineCfg.getDurabilityFormat((int) values[0], (int) values[1]);
}
}
Loading