mirror of
https://gitlab.com/phoenix-dvpmt/mmoitems.git
synced 2025-04-12 19:36:03 +02:00
!Cleaner code with 1.12 support being dropped
This commit is contained in:
parent
3b8ea72f95
commit
ac35eb5553
BIN
lib/MMOLib.jar
BIN
lib/MMOLib.jar
Binary file not shown.
@ -9,6 +9,7 @@ import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import net.Indyuce.mmoitems.MMOItems;
|
||||
@ -275,15 +276,17 @@ public class Consumable extends UseItem {
|
||||
}
|
||||
|
||||
// vanilla durability
|
||||
if (!target.getBoolean("Unbreakable")
|
||||
&& MMOLib.plugin.getVersion().getWrapper().isDamaged(target.getItem(), target.getItem().getItemMeta())) {
|
||||
if (!target.getBoolean("Unbreakable") && target.getItem().hasItemMeta() && target.getItem().getItemMeta() instanceof Damageable
|
||||
&& ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) {
|
||||
|
||||
RepairItemEvent called = new RepairItemEvent(playerData, mmoitem, target, repairPower);
|
||||
Bukkit.getPluginManager().callEvent(called);
|
||||
if (called.isCancelled())
|
||||
return false;
|
||||
|
||||
MMOLib.plugin.getVersion().getWrapper().repair(target.getItem(), called.getRepaired());
|
||||
ItemMeta meta = target.getItem().getItemMeta();
|
||||
((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired()));
|
||||
target.getItem().setItemMeta(meta);
|
||||
Message.REPAIRED_ITEM
|
||||
.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", "" + called.getRepaired())
|
||||
.send(player);
|
||||
|
@ -2,12 +2,12 @@ package net.Indyuce.mmoitems.api.interaction.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import net.Indyuce.mmoitems.api.player.PlayerData;
|
||||
import net.mmogroup.mmolib.MMOLib;
|
||||
@ -19,17 +19,32 @@ public class DurabilityItem {
|
||||
private final Player player;
|
||||
private final int maxDurability, unbreakingLevel;
|
||||
|
||||
/*
|
||||
* broken if below than 0
|
||||
*/
|
||||
private int durability;
|
||||
|
||||
private static final Random random = new Random();
|
||||
|
||||
/**
|
||||
* Use to handle durability changes for MMOItems without using heavy MMOItem
|
||||
* class methods
|
||||
*
|
||||
* @param player
|
||||
* Player holding the item
|
||||
* @param item
|
||||
* Item with durability
|
||||
*/
|
||||
public DurabilityItem(Player player, ItemStack item) {
|
||||
this(player, MMOLib.plugin.getVersion().getWrapper().getNBTItem(item));
|
||||
}
|
||||
|
||||
/**
|
||||
* Use to handle durability changes for MMOItems without using heavy MMOItem
|
||||
* class methods
|
||||
*
|
||||
* @param player
|
||||
* Player holding the item
|
||||
* @param item
|
||||
* Item with durability
|
||||
*/
|
||||
public DurabilityItem(Player player, NBTItem item) {
|
||||
this.player = player;
|
||||
this.nbtItem = item;
|
||||
@ -72,6 +87,7 @@ public class DurabilityItem {
|
||||
}
|
||||
|
||||
public DurabilityItem addDurability(int gain) {
|
||||
Validate.isTrue(gain > 0, "Durability gain must be greater than 0");
|
||||
durability = Math.max(0, Math.min(durability + gain, getMaxDurability()));
|
||||
return this;
|
||||
}
|
||||
@ -79,7 +95,7 @@ public class DurabilityItem {
|
||||
public DurabilityItem decreaseDurability(int loss) {
|
||||
|
||||
/*
|
||||
* calculate the chance of the item not losing any durability because of
|
||||
* Calculate the chance of the item not losing any durability because of
|
||||
* the vanilla unbreaking enchantment ; an item with unbreaking X has 1
|
||||
* 1 chance out of (X + 1) to lose a durability point, that's 50% chance
|
||||
* -> 33% chance -> 25% chance -> 20% chance...
|
||||
@ -89,7 +105,7 @@ public class DurabilityItem {
|
||||
|
||||
addDurability(-loss);
|
||||
|
||||
// when the item breaks
|
||||
// When the item breaks
|
||||
if (durability <= 0) {
|
||||
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
|
||||
PlayerData.get(player).scheduleDelayedInventoryUpdate();
|
||||
@ -101,30 +117,20 @@ public class DurabilityItem {
|
||||
public ItemStack toItem() {
|
||||
|
||||
/*
|
||||
* calculate the new durability state and update it in the item lore. if
|
||||
* the durability state is null, it either means the durability state is
|
||||
* out of the lore format or the state display was changed, thus in both
|
||||
* cases it shall no be updated
|
||||
*/
|
||||
nbtItem.addTag(new ItemTag("MMOITEMS_DURABILITY", durability));
|
||||
ItemStack item = nbtItem.toItem();
|
||||
|
||||
/*
|
||||
* update vanilla durability
|
||||
* Cross multiplication to display the current item durability on the
|
||||
* item durability bar. (1 - ratio) because minecraft works with item
|
||||
* damage, and item damage is the complementary of the remaining
|
||||
* durability.
|
||||
*
|
||||
* Make sure the vanilla bar displays at least 1 damage for display
|
||||
* issues. Also makes sure the item can be mended using the vanilla
|
||||
* enchant.
|
||||
*/
|
||||
double ratio = (double) durability / maxDurability;
|
||||
int damage = (int) ((1. - ratio) * item.getType().getMaxDurability());
|
||||
int damage = (int) ((1. - ratio) * nbtItem.getItem().getType().getMaxDurability());
|
||||
damage = Math.max(durability == maxDurability ? 1 : 0, damage);
|
||||
nbtItem.addTag(new ItemTag("MMOITEMS_DURABILITY", durability), new ItemTag("Damage", damage));
|
||||
|
||||
/*
|
||||
* make sure the vanilla bar displays at least 1 damage so the item can
|
||||
* always be mended
|
||||
*/
|
||||
damage = Math.max(ratio < 1 ? 1 : 0, damage);
|
||||
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
MMOLib.plugin.getVersion().getWrapper().applyDurability(item, meta, damage);
|
||||
item.setItemMeta(meta);
|
||||
|
||||
return item;
|
||||
return nbtItem.toItem();
|
||||
}
|
||||
}
|
||||
|
@ -9,27 +9,37 @@ public class InteractItem {
|
||||
private final EquipmentSlot slot;
|
||||
private final ItemStack item;
|
||||
|
||||
/*
|
||||
* determines in which hand the player has a specific item, prioritizing the
|
||||
* main hand. it is used to easily replace one of the player's hand items
|
||||
* when he uses tools like flint & steel, shears, bows, etc.
|
||||
/**
|
||||
* Used to determine in which hand a player has a specific item prioritizing
|
||||
* the main hand just like vanilla MC. For example, this is used when
|
||||
* throwing tridents to register weapon effects on the trident
|
||||
*
|
||||
* @param player
|
||||
* Player doing the action
|
||||
* @param material
|
||||
* Item to look for
|
||||
*/
|
||||
public InteractItem(Player player, Material material) {
|
||||
this.slot = hasItem(player.getInventory().getItemInMainHand(), material) ? EquipmentSlot.HAND : hasItem(player.getInventory().getItemInOffHand(), material) ? EquipmentSlot.OFF_HAND : null;
|
||||
this.slot = hasItem(player.getInventory().getItemInMainHand(), material) ? EquipmentSlot.HAND
|
||||
: hasItem(player.getInventory().getItemInOffHand(), material) ? EquipmentSlot.OFF_HAND : null;
|
||||
this.item = slot == EquipmentSlot.HAND ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
|
||||
}
|
||||
|
||||
/*
|
||||
* works the same but with a material suffit like _HOE which allows to use
|
||||
* that class for any tool made with any ingot/material
|
||||
/**
|
||||
* Used to determine in which hand a player has a specific item prioritizing
|
||||
* the main hand just like vanilla MC. For example, this is used when
|
||||
* throwing tridents to register weapon effects on the trident
|
||||
*
|
||||
* @param player
|
||||
* Player doing the action
|
||||
* @param suffix
|
||||
* Material suffix to look for eg "_HOE" looks for a hoe in the
|
||||
* player hands
|
||||
*/
|
||||
@Deprecated
|
||||
public InteractItem(Player player, String suffix) {
|
||||
this.slot = hasItem(player.getInventory().getItemInMainHand(), suffix) ? EquipmentSlot.HAND : hasItem(player.getInventory().getItemInOffHand(), suffix) ? EquipmentSlot.OFF_HAND : null;
|
||||
this.item = slot == EquipmentSlot.HAND ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
|
||||
}
|
||||
|
||||
public InteractItem(Player player, EquipmentSlot slot) {
|
||||
this.slot = slot;
|
||||
this.slot = hasItem(player.getInventory().getItemInMainHand(), suffix) ? EquipmentSlot.HAND
|
||||
: hasItem(player.getInventory().getItemInOffHand(), suffix) ? EquipmentSlot.OFF_HAND : null;
|
||||
this.item = slot == EquipmentSlot.HAND ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
|
||||
}
|
||||
|
||||
@ -48,9 +58,4 @@ public class InteractItem {
|
||||
private boolean hasItem(ItemStack item, String suffix) {
|
||||
return item != null && item.getType().name().endsWith(suffix);
|
||||
}
|
||||
|
||||
public void setItem(ItemStack item) {
|
||||
if (item != null && hasItem())
|
||||
this.item.setItemMeta(item.getItemMeta());
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,9 @@ public class UntargetedDurabilityItem extends DurabilityItem {
|
||||
private final EquipmentSlot slot;
|
||||
|
||||
/*
|
||||
* allows to delete items when they are right clicked while using the same
|
||||
* durability system as vanilla items
|
||||
* Allows to handle custom durability for target weapons when they are
|
||||
* left/right click while using the same durability system for both weapon
|
||||
* types
|
||||
*/
|
||||
public UntargetedDurabilityItem(Player player, NBTItem item, EquipmentSlot slot) {
|
||||
super(player, item);
|
||||
@ -22,7 +23,7 @@ public class UntargetedDurabilityItem extends DurabilityItem {
|
||||
public UntargetedDurabilityItem decreaseDurability(int loss) {
|
||||
return (UntargetedDurabilityItem) super.decreaseDurability(loss);
|
||||
}
|
||||
|
||||
|
||||
public void update() {
|
||||
|
||||
if (isBroken() && isLostWhenBroken()) {
|
||||
|
@ -22,6 +22,7 @@ import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import net.Indyuce.mmoitems.MMOItems;
|
||||
@ -712,8 +713,8 @@ public class MMOItemsCommand implements CommandExecutor {
|
||||
}
|
||||
|
||||
if (item.hasItemMeta()) {
|
||||
if (MMOLib.plugin.getVersion().getWrapper().isDamaged(item, item.getItemMeta()))
|
||||
config.getConfig().set(name + ".durability", MMOLib.plugin.getVersion().getWrapper().getDurability(item, item.getItemMeta()));
|
||||
if (item.getItemMeta() instanceof Damageable)
|
||||
config.getConfig().set(name + ".durability", ((Damageable) item.getItemMeta()).getDamage());
|
||||
if (item.getItemMeta().hasDisplayName())
|
||||
config.getConfig().set(name + ".name", item.getItemMeta().getDisplayName().replace("§", "&"));
|
||||
if (item.getItemMeta().hasLore()) {
|
||||
@ -731,7 +732,7 @@ public class MMOItemsCommand implements CommandExecutor {
|
||||
if (MMOLib.plugin.getVersion().getWrapper().getNBTItem(item).getBoolean("Unbreakable"))
|
||||
config.getConfig().set(name + ".unbreakable", true);
|
||||
for (Enchantment enchant : item.getEnchantments().keySet())
|
||||
config.getConfig().set(name + ".enchants." + MMOLib.plugin.getVersion().getWrapper().getName(enchant),
|
||||
config.getConfig().set(name + ".enchants." + enchant.getKey().getKey(),
|
||||
item.getEnchantmentLevel(enchant));
|
||||
}
|
||||
config.getConfig().set(name + ".material", args[0].equalsIgnoreCase("load") ? item.getType().name() : type.getItem().getType().name());
|
||||
|
@ -7,7 +7,6 @@ import org.bukkit.event.player.PlayerItemDamageEvent;
|
||||
import org.bukkit.event.player.PlayerItemMendEvent;
|
||||
|
||||
import net.Indyuce.mmoitems.api.interaction.util.DurabilityItem;
|
||||
import net.mmogroup.mmolib.MMOLib;
|
||||
|
||||
public class DurabilityListener implements Listener {
|
||||
|
||||
@ -16,23 +15,23 @@ public class DurabilityListener implements Listener {
|
||||
DurabilityItem item = new DurabilityItem(event.getPlayer(), event.getItem());
|
||||
|
||||
if (item.isValid()) {
|
||||
|
||||
/*
|
||||
* calculate item durability loss
|
||||
* Calculate item durability loss
|
||||
*/
|
||||
item.decreaseDurability(event.getDamage());
|
||||
|
||||
/*
|
||||
* if the item is broken and if it is meant to be lost when broken,
|
||||
* If the item is broken and if it is meant to be lost when broken,
|
||||
* do NOT cancel the event and make sure the item is destroyed
|
||||
*/
|
||||
if (item.isBroken() && item.isLostWhenBroken()) {
|
||||
MMOLib.plugin.getVersion().getWrapper().applyDurability(event.getItem(), event.getItem().getItemMeta(), event.getItem().getType().getMaxDurability());
|
||||
event.setDamage(999);
|
||||
return;
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
MMOLib.plugin.getVersion().getWrapper().applyDurabilityData(event.getItem(), item.toItem());
|
||||
event.getItem().setItemMeta(item.toItem().getItemMeta());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user