diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 7cb3cdef8..703438de6 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -1,7 +1,5 @@ package com.gmail.nossr50.listeners; -import java.util.Iterator; - import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.block.Block; @@ -27,7 +25,6 @@ import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.chat.ChatManager; @@ -44,6 +41,7 @@ import com.gmail.nossr50.skills.herbalism.HerbalismManager; import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.repair.Repair; import com.gmail.nossr50.skills.taming.TamingManager; +import com.gmail.nossr50.skills.unarmed.Unarmed; import com.gmail.nossr50.util.BlockUtils; import com.gmail.nossr50.util.ChimaeraWing; import com.gmail.nossr50.util.HardcoreManager; @@ -220,30 +218,18 @@ public class PlayerListener implements Listener { if (mcMMOPlayer.inParty() && ItemUtils.isShareable(dropStack)) { event.setCancelled(ShareHandler.handleItemShare(drop, mcMMOPlayer)); + + if (event.isCancelled()) { + return; + } } - if (event.isCancelled()) { - return; - } + if (mcMMOPlayer.isUsingUnarmed() && ItemUtils.isShareable(dropStack)) { + event.setCancelled(Unarmed.handleItemPickup(player.getInventory(), drop)); - PlayerInventory inventory = player.getInventory(); - int firstEmpty = inventory.firstEmpty(); - - if (mcMMOPlayer.isUsingUnarmed() && ItemUtils.isShareable(dropStack) && firstEmpty == inventory.getHeldItemSlot()) { - int nextSlot = firstEmpty + 1; - - for (Iterator iterator = inventory.iterator(nextSlot); iterator.hasNext();) { - ItemStack itemstack = iterator.next(); - - if (itemstack == null) { - drop.remove(); - inventory.setItem(nextSlot, dropStack); - player.updateInventory(); - event.setCancelled(true); - return; - } - - nextSlot++; + if (event.isCancelled()) { + player.updateInventory(); + return; } } } diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/Unarmed.java b/src/main/java/com/gmail/nossr50/skills/unarmed/Unarmed.java index d33f3089d..b00fa3347 100644 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/Unarmed.java +++ b/src/main/java/com/gmail/nossr50/skills/unarmed/Unarmed.java @@ -1,5 +1,11 @@ package com.gmail.nossr50.skills.unarmed; +import java.util.Iterator; + +import org.bukkit.entity.Item; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.Config; @@ -19,4 +25,61 @@ public class Unarmed { public static boolean blockCrackerSmoothBrick = Config.getInstance().getUnarmedBlockCrackerSmoothbrickToCracked(); public static double berserkDamageModifier = 1.5; + + public static boolean handleItemPickup(PlayerInventory inventory, Item drop) { + ItemStack dropStack = drop.getItemStack(); + int firstEmpty = inventory.firstEmpty(); + + if (inventory.containsAtLeast(dropStack, 1)) { + int dropAmount = dropStack.getAmount(); + int nextSlot = 0; + + for (Iterator iterator = inventory.iterator(); iterator.hasNext();) { + ItemStack itemstack = iterator.next(); + + if (itemstack.isSimilar(dropStack)) { + int itemAmount = itemstack.getAmount(); + int itemMax = itemstack.getMaxStackSize(); + + ItemStack addStack = itemstack.clone(); + + if (dropAmount + itemAmount <= itemMax) { + drop.remove(); + addStack.setAmount(dropAmount + itemAmount); + inventory.setItem(nextSlot, addStack); + return true; + } + + addStack.setAmount(itemMax); + dropAmount = dropAmount + itemAmount - itemMax; + inventory.setItem(nextSlot, addStack); + } + + if (dropAmount == 0) { + drop.remove(); + return true; + } + + nextSlot++; + } + } + + if (firstEmpty == inventory.getHeldItemSlot()) { + int nextSlot = firstEmpty + 1; + + for (Iterator iterator = inventory.iterator(nextSlot); iterator.hasNext();) { + ItemStack itemstack = iterator.next(); + + if (itemstack == null) { + drop.remove(); + inventory.setItem(nextSlot, dropStack); + return true; + } + + nextSlot++; + } + } + + return false; + } }