|
29 | 29 | import org.bukkit.event.Listener; |
30 | 30 | import org.bukkit.event.player.PlayerInteractEntityEvent; |
31 | 31 | import org.bukkit.inventory.ItemStack; |
| 32 | +import org.bukkit.inventory.PlayerInventory; |
32 | 33 | import org.bukkit.inventory.meta.ItemMeta; |
33 | 34 | import org.bukkit.persistence.PersistentDataContainer; |
34 | 35 | import org.bukkit.persistence.PersistentDataType; |
@@ -57,19 +58,52 @@ public void onShear(PlayerInteractEntityEvent event) { |
57 | 58 | if (!(entity instanceof Sheep)) { |
58 | 59 | return; |
59 | 60 | } |
60 | | - // check if the player is right-clicking with shears only |
61 | | - int heldItemSlot = player.getInventory().getHeldItemSlot(); |
62 | | - ItemStack heldItem = player.getInventory().getItem(heldItemSlot); |
63 | | - if (heldItem == null || heldItem.getType() == Material.AIR || heldItem.getType() != Material.SHEARS) { |
| 61 | + |
| 62 | + // make sure the player is holding shears |
| 63 | + // player can shear with their offhand |
| 64 | + PlayerInventory inventory = player.getInventory(); |
| 65 | + boolean isMainHand = inventory.getItemInMainHand().getType() == Material.SHEARS; |
| 66 | + boolean isOffHand = inventory.getItemInOffHand().getType() == Material.SHEARS; |
| 67 | + ItemStack shears = null; |
| 68 | + if (isMainHand) { |
| 69 | + shears = inventory.getItemInMainHand(); |
| 70 | + toolStats.logger.info("main"); |
| 71 | + } |
| 72 | + if (isOffHand) { |
| 73 | + shears = inventory.getItemInOffHand(); |
| 74 | + toolStats.logger.info("offhand"); |
| 75 | + } |
| 76 | + |
| 77 | + // if the player is hold fishing rods in both hands |
| 78 | + // default to main hand since that takes priority |
| 79 | + if (isMainHand && isOffHand) { |
| 80 | + shears = inventory.getItemInMainHand(); |
| 81 | + toolStats.logger.info("both"); |
| 82 | + } |
| 83 | + |
| 84 | + // player swapped items? |
| 85 | + if (shears == null) { |
64 | 86 | return; |
65 | 87 | } |
66 | 88 |
|
67 | 89 | Sheep sheep = (Sheep) entity; |
68 | 90 | // make sure the sheep is not sheared |
69 | | - if (!sheep.isSheared()) { |
70 | | - ItemStack newShears = addLore(heldItem); |
71 | | - if (newShears != null) { |
72 | | - Bukkit.getScheduler().runTaskLater(toolStats, () -> player.getInventory().setItem(heldItemSlot, newShears), 1); |
| 91 | + if (sheep.isSheared()) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // update the stats |
| 96 | + ItemStack newShears = addLore(shears); |
| 97 | + if (newShears != null) { |
| 98 | + if (isMainHand && isOffHand) { |
| 99 | + Bukkit.getScheduler().runTaskLater(toolStats, () -> inventory.setItemInMainHand(newShears), 1); |
| 100 | + return; |
| 101 | + } |
| 102 | + if (isMainHand) { |
| 103 | + Bukkit.getScheduler().runTaskLater(toolStats, () -> inventory.setItemInMainHand(newShears), 1); |
| 104 | + } |
| 105 | + if (isOffHand) { |
| 106 | + Bukkit.getScheduler().runTaskLater(toolStats, () -> inventory.setItemInOffHand(newShears), 1); |
73 | 107 | } |
74 | 108 | } |
75 | 109 | } |
|
0 commit comments