Skip to content

Commit 1b952ea

Browse files
Remove reflection-based DualWielding implementation, and update related 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]>
1 parent 7ccbeef commit 1b952ea

File tree

4 files changed

+137
-195
lines changed

4 files changed

+137
-195
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,36 @@ _Published one day_
1313
### Changed
1414

1515
#### `CraftingRecipes`
16+
1617
- :warning: All recipes are now required to provide names, therefore all helper methods to generate recipes take a
1718
mandatory name argument. The name is automatically namespaced with the plugin's name.
1819
- :warning: All helpers that were consuming the now-deprecated `MaterialData` now consume the more flexible `RecipeChoice` instead.
1920
- All helpers that generate shaped recipes (2x2, 2x2 diagonal, etc.) now return `ShapedRecipe`s explicitely, since there
2021
is no way those recipes can be anything other than shaped, and hiding this detail is not useful at all.
22+
23+
#### Glow effect
24+
25+
This tool was rewritten to register a namespaced enchantment, avoiding future incompatibilities with other plugins
26+
registering new enchants.
27+
28+
- :warning: The glow effect is now a QuartzLib component. You still use glow effect as usual (either using `GlowEffect`
29+
or through the `ItemStackBuilder`), but you have to load the effect using `loadComponents(GlowEffect.class)` in your
30+
plugin's `onEnable`.
31+
32+
#### `DualWielding`
33+
34+
This API was added when Bukkit had no support for dual wielding. As there is support now, we cleaned up all of this
35+
and removed some things. We kept some useful methods and moved things to other classes for coherence.
36+
37+
- Added `ItemUtils.consumeItemInOffHand`.
38+
- :warning: Moved `ItemUtils.consumeItem` to `ItemUtils.consumeItemInMainHand`.
39+
- :warning: Moved `ItemUtils.damageItemInHand` to `ItemUtils.damageItem`.
40+
- `ItemUtils.damageItem` now returns `true` if the damaged item was broken.
41+
- :warning: Moved `ItemUtils.breakItemInHand` methods to `InventoryUtils.breakItemInHand`.
42+
- :warning: Moved `DualWielding` to `InventoryUtils.DualWielding`.
43+
- :warning: Moved `DualWieldling` methods to `InventoryUtils`.
44+
- :warning: Removed `DualWieldling.setItemInHand` and `DualWieldling.getItemInHand` (use Bukkit API instead).
45+
2146

2247
#### `GlowEffect`
2348
- :warning: This class is not an enchantment anymore and has been re-implemented. It is now a `QuartzComponent` that

src/main/java/fr/zcraft/quartzlib/tools/items/DualWielding.java

Lines changed: 0 additions & 156 deletions
This file was deleted.

src/main/java/fr/zcraft/quartzlib/tools/items/InventoryUtils.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@
3333
import fr.zcraft.quartzlib.tools.reflection.Reflection;
3434
import fr.zcraft.quartzlib.tools.runners.RunTask;
3535
import java.lang.reflect.InvocationTargetException;
36+
import org.bukkit.Material;
37+
import org.bukkit.Sound;
3638
import org.bukkit.entity.HumanEntity;
3739
import org.bukkit.entity.Player;
3840
import org.bukkit.inventory.Inventory;
3941
import org.bukkit.inventory.ItemStack;
42+
import org.jetbrains.annotations.NotNull;
43+
import org.jetbrains.annotations.Nullable;
4044

4145
/**
4246
* This class provides various utilities for inventory management.
@@ -118,4 +122,74 @@ public static void updateInventoryLater(final Inventory inventory) {
118122
});
119123
}
120124

125+
/**
126+
* Returns which player's hand is holding the specific item.
127+
* If dual-wielding is not available, the item is tested against the
128+
* player's only hand.
129+
*
130+
* @param player The player
131+
* @param item The item
132+
* @return The hand holding the given item, or null if neither of them is holding it.
133+
*/
134+
public static @Nullable DualWielding getHoldingHand(@NotNull Player player, @NotNull ItemStack item) {
135+
if (player.getInventory().getItemInOffHand().equals(item)) {
136+
return DualWielding.OFF_HAND;
137+
}
138+
139+
if (player.getInventory().getItemInMainHand().equals(item)) {
140+
return DualWielding.MAIN_HAND;
141+
}
142+
143+
return null;
144+
}
145+
146+
/**
147+
* Breaks the item currently in the hand of the player.
148+
*
149+
* @param player The player.
150+
* @param hand The hand to retrieve the item from. This will always be the main hand if
151+
* the Bukkit build don't support dual-wielding.
152+
*/
153+
public static void breakItemInHand(Player player, @NotNull InventoryUtils.DualWielding hand) {
154+
final ItemStack item = new ItemStack(Material.AIR);
155+
156+
switch (hand) {
157+
case MAIN_HAND:
158+
player.getInventory().setItemInMainHand(item);
159+
break;
160+
case OFF_HAND:
161+
player.getInventory().setItemInOffHand(item);
162+
break;
163+
default: break;
164+
}
165+
166+
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 0.8f, 1);
167+
}
168+
169+
/**
170+
* Breaks the given item if it is found in one of the player's hands.
171+
*
172+
* @param player The player.
173+
* @param item The item to break.
174+
*/
175+
public static void breakItemInHand(Player player, ItemStack item) {
176+
DualWielding hand = getHoldingHand(player, item);
177+
if (hand != null) {
178+
breakItemInHand(player, hand);
179+
}
180+
}
181+
182+
/**
183+
* This class provides various utilities for handling dual-wielding.
184+
*/
185+
public enum DualWielding {
186+
/**
187+
* Represents the main hand of the player.
188+
*/
189+
MAIN_HAND,
190+
/**
191+
* Represents the off hand of the player.
192+
*/
193+
OFF_HAND;
194+
}
121195
}

src/main/java/fr/zcraft/quartzlib/tools/items/ItemUtils.java

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.bukkit.enchantments.Enchantment;
4848
import org.bukkit.entity.Player;
4949
import org.bukkit.inventory.ItemStack;
50+
import org.bukkit.inventory.meta.Damageable;
5051
import org.bukkit.inventory.meta.ItemMeta;
5152
import org.bukkit.inventory.meta.PotionMeta;
5253
import org.bukkit.potion.Potion;
@@ -67,16 +68,30 @@ private ItemUtils() {
6768
}
6869

6970
/**
70-
* Simulates the player consuming the itemstack in its hand, depending on
71-
* his game mode. This decreases the ItemStack's size by one, and replaces
71+
* Simulates the player consuming the ItemStack in their main hand, depending on
72+
* their game mode. This decreases the ItemStack's size by one, and replaces
7273
* it with air if nothing is left.
7374
*
7475
* @param player The player that will consume the stack.
75-
* @return The updated stack.
76+
* @return The updated ItemStack.
7677
*/
77-
public static ItemStack consumeItem(Player player) {
78-
ItemStack newStack = consumeItem(player, player.getItemInHand());
79-
player.setItemInHand(newStack);
78+
public static ItemStack consumeItemInMainHand(Player player) {
79+
ItemStack newStack = consumeItem(player, player.getInventory().getItemInMainHand());
80+
player.getInventory().setItemInMainHand(newStack);
81+
return newStack;
82+
}
83+
84+
/**
85+
* Simulates the player consuming the ItemStack in their main hand, depending on
86+
* their game mode. This decreases the ItemStack's size by one, and replaces
87+
* it with air if nothing is left.
88+
*
89+
* @param player The player that will consume the stack.
90+
* @return The updated ItemStack.
91+
*/
92+
public static ItemStack consumeItemInOffHand(Player player) {
93+
ItemStack newStack = consumeItem(player, player.getInventory().getItemInOffHand());
94+
player.getInventory().setItemInOffHand(newStack);
8095
return newStack;
8196
}
8297

@@ -182,48 +197,32 @@ public static boolean hasDisplayName(ItemStack stack, String displayName) {
182197
* @param player The player that is using the item.
183198
* @param item The item in the player's hand.
184199
* @param factor The amount of damage taken.
200+
* @return `true` if the damaged item was broken, `false` otherwise.
185201
*/
186-
public static void damageItemInHand(Player player, ItemStack item, int factor) {
187-
if (player == null) {
188-
throw new IllegalArgumentException("Player can't be null.");
189-
}
202+
public static boolean damageItem(@NotNull Player player, @NotNull ItemStack item, int factor) {
190203
if (player.getGameMode() == GameMode.CREATIVE) {
191-
return;
204+
return false;
192205
}
193206

194-
short newDurability = item.getDurability();
207+
ItemMeta meta = item.getItemMeta();
208+
209+
if (!(meta instanceof Damageable)) {
210+
return false;
211+
}
212+
213+
int newDurability = ((Damageable) meta).getDamage();
195214
newDurability += newDurability(item.getEnchantmentLevel(Enchantment.DURABILITY)) * factor;
196215

197216
if (newDurability >= item.getType().getMaxDurability()) {
198-
breakItemInHand(player, item);
217+
InventoryUtils.breakItemInHand(player, item);
218+
player.updateInventory();
219+
return true;
199220
} else {
200-
item.setDurability(newDurability);
201-
//player.getInventory().setItemInHand(item);
221+
((Damageable) meta).setDamage(newDurability);
222+
item.setItemMeta(meta);
223+
player.updateInventory();
224+
return false;
202225
}
203-
204-
player.updateInventory();
205-
}
206-
207-
/**
208-
* Breaks the item currently in the hand of the player.
209-
*
210-
* @param player The player.
211-
* @param hand The hand to retrieve the item from. This will always be the main hand if
212-
* the Bukkit build don't support dual-wielding.
213-
*/
214-
public static void breakItemInHand(Player player, DualWielding hand) {
215-
DualWielding.setItemInHand(player, hand, new ItemStack(Material.AIR));
216-
//player.playSound(player.getLocation(), Sound.ITEM_BREAK, 0.8f, 1);
217-
}
218-
219-
/**
220-
* Breaks the given item if it is found in one of the player's hands.
221-
*
222-
* @param player The player.
223-
* @param item The item to break.
224-
*/
225-
public static void breakItemInHand(Player player, ItemStack item) {
226-
breakItemInHand(player, DualWielding.getHoldingHand(player, item));
227226
}
228227

229228
/**

0 commit comments

Comments
 (0)