Item Durability is now properly decreased when fishing/custom mining

This commit is contained in:
ASangarin 2020-12-30 20:10:22 +01:00
parent 05a6d46a0d
commit 0766c234e2
3 changed files with 35 additions and 3 deletions

View File

@ -6,16 +6,22 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import net.mmogroup.mmolib.api.item.NBTItem;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; 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.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream; import org.bukkit.util.io.BukkitObjectOutputStream;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
@ -194,4 +200,24 @@ public class MMOCoreUtils {
if (!event.isCancelled()) if (!event.isCancelled())
target.setHealth(target.getHealth() + gain); 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);
}
}
}
} }

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.listener; package net.Indyuce.mmocore.listener;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; 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.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerItemDamageEvent; import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
@ -106,7 +108,7 @@ public class BlockListener implements Listener {
if (!info.hasVanillaDrops()) { if (!info.hasVanillaDrops()) {
event.setCancelled(true); event.setCancelled(true);
event.getBlock().setType(Material.AIR); event.getBlock().setType(Material.AIR);
Bukkit.getPluginManager().callEvent(new PlayerItemDamageEvent(player, item, 1)); MMOCoreUtils.decreaseDurability(player, EquipmentSlot.HAND);
} }
/* /*

View File

@ -5,8 +5,10 @@ import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.entity.FishHook; import org.bukkit.entity.FishHook;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
@ -17,6 +19,7 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerFishEvent.State; import org.bukkit.event.player.PlayerFishEvent.State;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -126,8 +129,6 @@ public class FishingListener implements Listener {
/* /*
* lose the catch if the current fish is gone! * 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); event.setCancelled(true);
if (isTimedOut()) { if (isTimedOut()) {
close(); close();
@ -150,6 +151,9 @@ public class FishingListener implements Listener {
*/ */
close(); 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) { 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)); 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); hook.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 24, 0, 0, 0, .08);