Skip to content

Commit

Permalink
Remove reflection-based DualWielding implementation, and update relat…
Browse files Browse the repository at this point in the history
…ed utilities (#70)

* Remove reflection-based DualWielding implementation, and update related utilities

* Fix checkstyle

* Bring back break sound

* Updated CHANGELOG.

Co-authored-by: Amaury Carrade <[email protected]>
  • Loading branch information
prokopyl and AmauryCarrade authored Apr 11, 2021
1 parent 7ccbeef commit 1b952ea
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 195 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,36 @@ _Published one day_
### Changed

#### `CraftingRecipes`

- :warning: All recipes are now required to provide names, therefore all helper methods to generate recipes take a
mandatory name argument. The name is automatically namespaced with the plugin's name.
- :warning: All helpers that were consuming the now-deprecated `MaterialData` now consume the more flexible `RecipeChoice` instead.
- All helpers that generate shaped recipes (2x2, 2x2 diagonal, etc.) now return `ShapedRecipe`s explicitely, since there
is no way those recipes can be anything other than shaped, and hiding this detail is not useful at all.

#### Glow effect

This tool was rewritten to register a namespaced enchantment, avoiding future incompatibilities with other plugins
registering new enchants.

- :warning: The glow effect is now a QuartzLib component. You still use glow effect as usual (either using `GlowEffect`
or through the `ItemStackBuilder`), but you have to load the effect using `loadComponents(GlowEffect.class)` in your
plugin's `onEnable`.

#### `DualWielding`

This API was added when Bukkit had no support for dual wielding. As there is support now, we cleaned up all of this
and removed some things. We kept some useful methods and moved things to other classes for coherence.

- Added `ItemUtils.consumeItemInOffHand`.
- :warning: Moved `ItemUtils.consumeItem` to `ItemUtils.consumeItemInMainHand`.
- :warning: Moved `ItemUtils.damageItemInHand` to `ItemUtils.damageItem`.
- `ItemUtils.damageItem` now returns `true` if the damaged item was broken.
- :warning: Moved `ItemUtils.breakItemInHand` methods to `InventoryUtils.breakItemInHand`.
- :warning: Moved `DualWielding` to `InventoryUtils.DualWielding`.
- :warning: Moved `DualWieldling` methods to `InventoryUtils`.
- :warning: Removed `DualWieldling.setItemInHand` and `DualWieldling.getItemInHand` (use Bukkit API instead).


#### `GlowEffect`
- :warning: This class is not an enchantment anymore and has been re-implemented. It is now a `QuartzComponent` that
Expand Down
156 changes: 0 additions & 156 deletions src/main/java/fr/zcraft/quartzlib/tools/items/DualWielding.java

This file was deleted.

74 changes: 74 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,14 @@
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.Sound;
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 +122,74 @@ 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) {
final 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.ENTITY_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

0 comments on commit 1b952ea

Please sign in to comment.