Changed durability application method

This commit is contained in:
Indyuce 2022-03-05 13:13:13 +01:00
parent 848b402393
commit 1671df5ed9
2 changed files with 19 additions and 24 deletions

View File

@ -1,7 +1,6 @@
package net.Indyuce.mmocore.api.util;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.version.VersionMaterial;
import io.lumine.mythic.utils.holograms.Hologram;
import io.lumine.mythic.utils.serialize.Position;
@ -184,8 +183,11 @@ public class MMOCoreUtils {
/**
* Method used when mining a custom block or fishing, as the corresponding
* interaction event is cancelled durability is not handled. This method is
* needed and actually calls a damage event so that MMOItems can listen to
* it
* needed and actually calls a damage event so that MMOItems can listen to it.
* <p>
* This method only supports item types which DO have a durability bar like
* fishing rods or pickaxes. This shouldn't cause any issue because you can
* only use fishing rods to fish and pickaxes to mine stuff.
*
* @param player Player holding the item with durability
* @param slot The slot of the item with durability
@ -193,26 +195,21 @@ public class MMOCoreUtils {
*/
public static void decreaseDurability(Player player, EquipmentSlot slot, int damage) {
ItemStack item = player.getInventory().getItem(slot);
NBTItem nbt = NBTItem.get(item);
if (!nbt.hasTag("MMOITEMS_MAX_DURABILITY")) {
if (!item.hasItemMeta() || !(item.getItemMeta() instanceof Damageable) || item.getItemMeta().isUnbreakable())
return;
}
PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, item, damage);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
if (!nbt.getBoolean("Unbreakable") && item.getItemMeta() instanceof Damageable) {
ItemMeta meta = item.getItemMeta();
((Damageable) meta).setDamage(((Damageable) meta).getDamage() + damage);
ItemMeta meta = item.getItemMeta();
if (event.getDamage() + ((Damageable) meta).getDamage() >= item.getType().getMaxDurability()) {
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1F, 1F);
player.getInventory().setItem(slot, null);
} else {
((Damageable) meta).setDamage(((Damageable) meta).getDamage() + event.getDamage());
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

@ -96,7 +96,7 @@ public class FishingListener implements Listener {
lootFish();
}
public void criticalFish() {
public void setCriticalFish() {
currentPulls = fishStrength + 2;
}
@ -107,7 +107,7 @@ public class FishingListener implements Listener {
/**
* @return If the fish is weak enough to be looted by the player.
*/
public boolean pull() {
public boolean pullOnce() {
last = System.currentTimeMillis();
currentPulls++;
return currentPulls >= fishStrength;
@ -116,7 +116,7 @@ public class FishingListener implements Listener {
/**
* Critical fish's means you catch the fish on the very first try
*/
public boolean isCrit() {
public boolean isCriticalFish() {
return currentPulls > fishStrength + 1;
}
@ -148,13 +148,11 @@ public class FishingListener implements Listener {
return;
}
System.out.println("Pulls: " + currentPulls + " / " + fishStrength);
if (currentPulls == 0 && RANDOM.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_CHANCE) / 100)
criticalFish();
setCriticalFish();
// Check if enough pulls; if not, wait till the next fish event
if (pull())
if (pullOnce())
lootFish();
}
}
@ -167,7 +165,7 @@ public class FishingListener implements Listener {
(mainhand != null && mainhand.getType() == Material.FISHING_ROD) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND, 1);
// Critical fishing failure
if (!isCrit() && RANDOM.nextDouble() < PlayerData.get(player).getStats().getStat(StatType.CRITICAL_FISHING_FAILURE_CHANCE) / 100) {
if (!isCriticalFish() && 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);
return;
@ -189,7 +187,7 @@ public class FishingListener implements Listener {
// Calculate yeet velocity
Item item = hook.getWorld().dropItemNaturally(hook.getLocation(), collect);
MMOCoreUtils.displayIndicator(location.add(0, 1.25, 0),
MMOCore.plugin.configManager.getSimpleMessage("fish-out-water" + (isCrit() ? "-crit" : "")).message());
MMOCore.plugin.configManager.getSimpleMessage("fish-out-water" + (isCriticalFish() ? "-crit" : "")).message());
Vector vec = player.getLocation().subtract(hook.getLocation()).toVector();
vec.setY(vec.getY() * .031 + vec.length() * .05);
vec.setX(vec.getX() * .08);