Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now firing events before changing durability #5071

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.gmail.nossr50.util.skills.CombatUtils;
import com.gmail.nossr50.util.skills.ParticleEffectUtils;
import com.gmail.nossr50.util.skills.RankUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
Expand Down Expand Up @@ -127,7 +126,7 @@ public void impactCheck(@NotNull LivingEntity target) {
for (ItemStack armor : equipment.getArmorContents()) {
if (armor != null && ItemUtils.isArmor(armor)) {
if (isSkillRNGSuccessful(SubSkillType.AXES_ARMOR_IMPACT, mmoPlayer, mmoPlayer.getAttackStrength())) {
handleArmorDurabilityChange(armor, durabilityDamage, 1);
handleArmorDurabilityChange(target, armor, durabilityDamage, 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ public void gigaDrillBreaker(BlockState blockState) {
excavationBlockCheck(blockState);
excavationBlockCheck(blockState);

SkillUtils.handleDurabilityChange(getPlayer().getInventory().getItemInMainHand(), mcMMO.p.getGeneralConfig().getAbilityToolDamage());
SkillUtils.handleDurabilityChange(getPlayer(), getPlayer().getInventory().getItemInMainHand(), mcMMO.p.getGeneralConfig().getAbilityToolDamage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void miningBlockCheck(BlockState blockState) {
}

if (mmoPlayer.getAbilityMode(mcMMO.p.getSkillTools().getSuperAbility(skill))) {
SkillUtils.handleDurabilityChange(getPlayer().getInventory().getItemInMainHand(), mcMMO.p.getGeneralConfig().getAbilityToolDamage());
SkillUtils.handleDurabilityChange(getPlayer(), getPlayer().getInventory().getItemInMainHand(), mcMMO.p.getGeneralConfig().getAbilityToolDamage());
}

if (!mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(PrimarySkillType.MINING, blockState.getType()) || !canDoubleDrop())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void handleRepair(ItemStack item) {
}

// Repair the item!
item.setDurability(newDurability);
SkillUtils.applyDurabilityChange(player, item, newDurability);
}

private float getPercentageRepaired(short startDurability, short newDurability, short totalDurability) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
import com.gmail.nossr50.util.skills.CombatUtils;
import com.gmail.nossr50.util.skills.RankUtils;
import com.gmail.nossr50.util.skills.SkillUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -250,16 +248,7 @@ private static boolean handleDurabilityLoss(@NotNull Set<BlockState> treeFellerB
}
}

// Call PlayerItemDamageEvent first to make sure it's not cancelled
//TODO: Put this event stuff in handleDurabilityChange
final PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, inHand, durabilityLoss);
Bukkit.getPluginManager().callEvent(event);

if (event.isCancelled()) {
return true;
}

SkillUtils.handleDurabilityChange(inHand, durabilityLoss);
SkillUtils.handleDurabilityChange(player, inHand, durabilityLoss);
int durability = meta instanceof Damageable ? ((Damageable) meta).getDamage(): 0;
return (durability < (mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability()));
}
Expand Down
61 changes: 46 additions & 15 deletions src/main/java/com/gmail/nossr50/util/skills/SkillUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
Expand Down Expand Up @@ -225,8 +230,8 @@ public static void removeAbilityBuff(@Nullable ItemStack itemStack) {
}
}

public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) {
handleDurabilityChange(itemStack, durabilityModifier, 1.0);
public static void handleDurabilityChange(LivingEntity holder, ItemStack itemStack, int durabilityModifier) {
handleDurabilityChange(holder, itemStack, durabilityModifier, 1.0, false);
}

/**
Expand All @@ -236,16 +241,50 @@ public static void handleDurabilityChange(ItemStack itemStack, int durabilityMod
* @param durabilityModifier the amount to modify the durability by
* @param maxDamageModifier the amount to adjust the max damage by
*/
public static void handleDurabilityChange(ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
public static void handleDurabilityChange(LivingEntity holder, ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
handleDurabilityChange(holder, itemStack, durabilityModifier, maxDamageModifier, false);
}

public static void applyDurabilityChange(Player player, ItemStack itemStack, int finalDurability) {
int damage = finalDurability - itemStack.getDurability();

// Call PlayerItemDamageEvent first to make sure it's not cancelled
if (damage > 0) { // Damaged item
PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, itemStack, finalDurability);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled() && event.getDamage() != 0) {
itemStack.setDurability((short) (itemStack.getDurability() + event.getDamage()));
}
} else if (damage < 0) { // Repaired item
damage = -damage;
ExperienceOrb auxOrb = (ExperienceOrb) player.getWorld().spawnEntity(player.getLocation(), EntityType.EXPERIENCE_ORB);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this experience orb stuff about? I don't get it.
Doesn't seem related to your PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the damage is negative, that means the item has been repaired. The PlayerItemMendEvent has an ExperienceOrb as an argument, so I needed to spawn one with the quantity of xp needed to repair the item for the specified amount if you had the mending enchantment. After the event has been called, the xp orb has to be despawned so no one can pick it up.
After your review I noticed I forgot to call the event, I just instantiate it and check if it has been canceled.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better to call the mend event instead of the damage event when repairing an item, yet I think everything would also work with a damage event. I just did that for the sake of semantics.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thank you for the explanation, I will think on that some more.

auxOrb.setExperience(damage / 2);
PlayerItemMendEvent event = new PlayerItemMendEvent(player, itemStack, itemStack.getType().getEquipmentSlot(), auxOrb, damage);
if (!event.isCancelled() && event.getRepairAmount() != 0) {
itemStack.setDurability((short) (itemStack.getDurability() - event.getRepairAmount()));
}
// Cleanup
auxOrb.remove();
}
}

private static void handleDurabilityChange(LivingEntity holder, ItemStack itemStack, double durabilityModifier, double maxDamageModifier, boolean isArmor) {
if (itemStack.hasItemMeta() && itemStack.getItemMeta().isUnbreakable()) {
return;
}

Material type = itemStack.getType();
short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability();
durabilityModifier = (int) Math.min(durabilityModifier / (itemStack.getEnchantmentLevel(mcMMO.p.getEnchantmentMapper().getUnbreaking()) + 1), maxDurability * maxDamageModifier);
durabilityModifier = durabilityModifier * ((isArmor ? 0.6 : 0) + ((isArmor ? 0.4 : 1) / (itemStack.getEnchantmentLevel(mcMMO.p.getEnchantmentMapper().getUnbreaking()) + 1)));
durabilityModifier = (int) Math.min(durabilityModifier, maxDurability * maxDamageModifier);

itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability));
short finalDurability = (short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability);
if (holder instanceof Player player) {
applyDurabilityChange(player, itemStack, finalDurability);
}
else {
itemStack.setDurability(finalDurability);
}
}

private static boolean isLocalizedSkill(String skillName) {
Expand All @@ -266,16 +305,8 @@ private static boolean isLocalizedSkill(String skillName) {
* @param durabilityModifier the amount to modify the durability by
* @param maxDamageModifier the amount to adjust the max damage by
*/
public static void handleArmorDurabilityChange(ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
if (itemStack.hasItemMeta() && itemStack.getItemMeta().isUnbreakable()) {
return;
}

Material type = itemStack.getType();
short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability();
durabilityModifier = (int) Math.min(durabilityModifier * (0.6 + 0.4/ (itemStack.getEnchantmentLevel(mcMMO.p.getEnchantmentMapper().getUnbreaking()) + 1)), maxDurability * maxDamageModifier);

itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability));
public static void handleArmorDurabilityChange(LivingEntity holder, ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
handleDurabilityChange(holder, itemStack, durabilityModifier, maxDamageModifier, true);
}

@Nullable
Expand Down