From 0766c234e2bb021c507898a513c2ddbe1f56f029 Mon Sep 17 00:00:00 2001 From: ASangarin Date: Wed, 30 Dec 2020 20:10:22 +0100 Subject: [PATCH] Item Durability is now properly decreased when fishing/custom mining --- .../mmocore/api/util/MMOCoreUtils.java | 26 +++++++++++++++++++ .../mmocore/listener/BlockListener.java | 4 ++- .../listener/profession/FishingListener.java | 8 ++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java b/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java index f5fd82c4..b8131752 100644 --- a/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java +++ b/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java @@ -6,16 +6,22 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import net.mmogroup.mmolib.api.item.NBTItem; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Sound; import org.bukkit.attribute.Attribute; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; +import org.bukkit.event.player.PlayerItemDamageEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectOutputStream; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; @@ -194,4 +200,24 @@ public class MMOCoreUtils { if (!event.isCancelled()) target.setHealth(target.getHealth() + gain); } + + public static void decreaseDurability(Player player, EquipmentSlot slot) { + ItemStack item = player.getInventory().getItem(slot); + + PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, item, 1); + Bukkit.getPluginManager().callEvent(event); + if(event.isCancelled()) + return; + + NBTItem nbt = NBTItem.get(item); + if (!nbt.getBoolean("Unbreakable") && item.hasItemMeta() && item.getItemMeta() instanceof Damageable) { + ItemMeta meta = item.getItemMeta(); + ((Damageable) meta).setDamage(((Damageable) meta).getDamage() + 1); + item.setItemMeta(meta); + if(((Damageable) meta).getDamage() >= item.getType().getMaxDurability()) { + player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1F, 1F); + player.getInventory().setItem(slot, null); + } + } + } } diff --git a/src/main/java/net/Indyuce/mmocore/listener/BlockListener.java b/src/main/java/net/Indyuce/mmocore/listener/BlockListener.java index 13af6dd5..ca6c1700 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/BlockListener.java +++ b/src/main/java/net/Indyuce/mmocore/listener/BlockListener.java @@ -1,5 +1,6 @@ package net.Indyuce.mmocore.listener; +import net.Indyuce.mmocore.api.util.MMOCoreUtils; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; @@ -17,6 +18,7 @@ import org.bukkit.event.block.BlockPistonExtendEvent; import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerItemDamageEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; @@ -106,7 +108,7 @@ public class BlockListener implements Listener { if (!info.hasVanillaDrops()) { event.setCancelled(true); event.getBlock().setType(Material.AIR); - Bukkit.getPluginManager().callEvent(new PlayerItemDamageEvent(player, item, 1)); + MMOCoreUtils.decreaseDurability(player, EquipmentSlot.HAND); } /* diff --git a/src/main/java/net/Indyuce/mmocore/listener/profession/FishingListener.java b/src/main/java/net/Indyuce/mmocore/listener/profession/FishingListener.java index 594ea47d..9d975aa8 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/profession/FishingListener.java +++ b/src/main/java/net/Indyuce/mmocore/listener/profession/FishingListener.java @@ -5,8 +5,10 @@ import java.util.Random; import java.util.Set; import java.util.UUID; +import net.Indyuce.mmocore.api.util.MMOCoreUtils; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.entity.FishHook; import org.bukkit.entity.Item; @@ -17,6 +19,7 @@ import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerFishEvent.State; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; @@ -126,8 +129,6 @@ public class FishingListener implements Listener { /* * lose the catch if the current fish is gone! */ - // TODO: Cancelling the event also cancels Rod damage (so it's - // technically unbreakable) event.setCancelled(true); if (isTimedOut()) { close(); @@ -150,6 +151,9 @@ public class FishingListener implements Listener { */ close(); + ItemStack mainhand = player.getInventory().getItem(EquipmentSlot.HAND); + MMOCoreUtils.decreaseDurability(player, (mainhand != null && mainhand.getType() == Material.FISHING_ROD) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND); + if (!isCrit() && random.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_FAILURE_CHANCE) / 100) { player.setVelocity(hook.getLocation().subtract(player.getLocation()).toVector().setY(0).multiply(3).setY(.5)); hook.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 24, 0, 0, 0, .08);