Added extra comments

This commit is contained in:
Indyuce 2021-01-01 23:43:26 +01:00
parent 0e17de3e6e
commit e2bf9ff034
3 changed files with 19 additions and 10 deletions

View File

@ -6,7 +6,6 @@ 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;
@ -28,6 +27,7 @@ import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.NBTItem;
import net.mmogroup.mmolib.version.VersionMaterial;
public class MMOCoreUtils {
@ -201,20 +201,30 @@ public class MMOCoreUtils {
target.setHealth(target.getHealth() + gain);
}
public static void decreaseDurability(Player player, EquipmentSlot slot) {
/**
* 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
*
* @param player Player holding the item with durability
* @param slot The slot of the item with durability
* @param damage Damage that needs to be applied
*/
public static void decreaseDurability(Player player, EquipmentSlot slot, int damage) {
ItemStack item = player.getInventory().getItem(slot);
PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, item, 1);
PlayerItemDamageEvent event = new PlayerItemDamageEvent(player, item, damage);
Bukkit.getPluginManager().callEvent(event);
if(event.isCancelled())
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);
((Damageable) meta).setDamage(((Damageable) meta).getDamage() + damage);
item.setItemMeta(meta);
if(((Damageable) meta).getDamage() >= item.getType().getMaxDurability()) {
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,6 +1,5 @@
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,7 +16,6 @@ import org.bukkit.event.block.BlockFormEvent;
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;
@ -27,6 +25,7 @@ import net.Indyuce.mmocore.api.block.BlockInfo;
import net.Indyuce.mmocore.api.block.VanillaBlockType;
import net.Indyuce.mmocore.api.event.CustomBlockMineEvent;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.mmogroup.mmolib.UtilityMethods;
public class BlockListener implements Listener {
@ -108,7 +107,7 @@ public class BlockListener implements Listener {
if (!info.hasVanillaDrops()) {
event.setCancelled(true);
event.getBlock().setType(Material.AIR);
MMOCoreUtils.decreaseDurability(player, EquipmentSlot.HAND);
MMOCoreUtils.decreaseDurability(player, EquipmentSlot.HAND, 1);
}
/*

View File

@ -152,7 +152,7 @@ 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);
MMOCoreUtils.decreaseDurability(player, (mainhand != null && mainhand.getType() == Material.FISHING_ROD) ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND, 1);
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));