Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 0 additions & 156 deletions src/main/java/fr/zcraft/quartzlib/tools/items/DualWielding.java

This file was deleted.

71 changes: 71 additions & 0 deletions src/main/java/fr/zcraft/quartzlib/tools/items/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
import fr.zcraft.quartzlib.tools.reflection.Reflection;
import fr.zcraft.quartzlib.tools.runners.RunTask;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* This class provides various utilities for inventory management.
Expand Down Expand Up @@ -118,4 +121,72 @@ public static void updateInventoryLater(final Inventory inventory) {
});
}

/**
* Returns which player's hand is holding the specific item.
* If dual-wielding is not available, the item is tested against the
* player's only hand.
*
* @param player The player
* @param item The item
* @return The hand holding the given item, or null if neither of them is holding it.
*/
public static @Nullable DualWielding getHoldingHand(@NotNull Player player, @NotNull ItemStack item) {
if (player.getInventory().getItemInOffHand().equals(item)) {
return DualWielding.OFF_HAND;
}

if (player.getInventory().getItemInMainHand().equals(item)) {
return DualWielding.MAIN_HAND;
}

return null;
}

/**
* Breaks the item currently in the hand of the player.
*
* @param player The player.
* @param hand The hand to retrieve the item from. This will always be the main hand if
* the Bukkit build don't support dual-wielding.
*/
public static void breakItemInHand(Player player, @NotNull InventoryUtils.DualWielding hand) {
ItemStack item = new ItemStack(Material.AIR);
switch (hand) {
case MAIN_HAND:
player.getInventory().setItemInMainHand(item);
break;
case OFF_HAND:
player.getInventory().setItemInOffHand(item);
break;
default: break;
}
//player.playSound(player.getLocation(), Sound.ITEM_BREAK, 0.8f, 1);
}

/**
* Breaks the given item if it is found in one of the player's hands.
*
* @param player The player.
* @param item The item to break.
*/
public static void breakItemInHand(Player player, ItemStack item) {
DualWielding hand = getHoldingHand(player, item);
if (hand != null) {
breakItemInHand(player, hand);
}
}

/**
* This class provides various utilities for handling dual-wielding.
*/
public enum DualWielding {
/**
* Represents the main hand of the player.
*/
MAIN_HAND,
/**
* Represents the off hand of the player.
*/
OFF_HAND;
}
}
77 changes: 38 additions & 39 deletions src/main/java/fr/zcraft/quartzlib/tools/items/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.Potion;
Expand All @@ -67,16 +68,30 @@ private ItemUtils() {
}

/**
* Simulates the player consuming the itemstack in its hand, depending on
* his game mode. This decreases the ItemStack's size by one, and replaces
* Simulates the player consuming the ItemStack in their main hand, depending on
* their game mode. This decreases the ItemStack's size by one, and replaces
* it with air if nothing is left.
*
* @param player The player that will consume the stack.
* @return The updated stack.
* @return The updated ItemStack.
*/
public static ItemStack consumeItem(Player player) {
ItemStack newStack = consumeItem(player, player.getItemInHand());
player.setItemInHand(newStack);
public static ItemStack consumeItemInMainHand(Player player) {
ItemStack newStack = consumeItem(player, player.getInventory().getItemInMainHand());
player.getInventory().setItemInMainHand(newStack);
return newStack;
}

/**
* Simulates the player consuming the ItemStack in their main hand, depending on
* their game mode. This decreases the ItemStack's size by one, and replaces
* it with air if nothing is left.
*
* @param player The player that will consume the stack.
* @return The updated ItemStack.
*/
public static ItemStack consumeItemInOffHand(Player player) {
ItemStack newStack = consumeItem(player, player.getInventory().getItemInOffHand());
player.getInventory().setItemInOffHand(newStack);
return newStack;
}

Expand Down Expand Up @@ -182,48 +197,32 @@ public static boolean hasDisplayName(ItemStack stack, String displayName) {
* @param player The player that is using the item.
* @param item The item in the player's hand.
* @param factor The amount of damage taken.
* @return `true` if the damaged item was broken, `false` otherwise.
*/
public static void damageItemInHand(Player player, ItemStack item, int factor) {
if (player == null) {
throw new IllegalArgumentException("Player can't be null.");
}
public static boolean damageItem(@NotNull Player player, @NotNull ItemStack item, int factor) {
if (player.getGameMode() == GameMode.CREATIVE) {
return;
return false;
}

short newDurability = item.getDurability();
ItemMeta meta = item.getItemMeta();

if (!(meta instanceof Damageable)) {
return false;
}

int newDurability = ((Damageable) meta).getDamage();
newDurability += newDurability(item.getEnchantmentLevel(Enchantment.DURABILITY)) * factor;

if (newDurability >= item.getType().getMaxDurability()) {
breakItemInHand(player, item);
InventoryUtils.breakItemInHand(player, item);
player.updateInventory();
return true;
} else {
item.setDurability(newDurability);
//player.getInventory().setItemInHand(item);
((Damageable) meta).setDamage(newDurability);
item.setItemMeta(meta);
player.updateInventory();
return false;
}

player.updateInventory();
}

/**
* Breaks the item currently in the hand of the player.
*
* @param player The player.
* @param hand The hand to retrieve the item from. This will always be the main hand if
* the Bukkit build don't support dual-wielding.
*/
public static void breakItemInHand(Player player, DualWielding hand) {
DualWielding.setItemInHand(player, hand, new ItemStack(Material.AIR));
//player.playSound(player.getLocation(), Sound.ITEM_BREAK, 0.8f, 1);
}

/**
* Breaks the given item if it is found in one of the player's hands.
*
* @param player The player.
* @param item The item to break.
*/
public static void breakItemInHand(Player player, ItemStack item) {
breakItemInHand(player, DualWielding.getHoldingHand(player, item));
}

/**
Expand Down