Better handling of picking up items with unarmed.

This commit is contained in:
GJ 2013-04-10 13:20:25 -04:00
parent 8a14d9ac26
commit f4681a282c
2 changed files with 73 additions and 24 deletions

View File

@ -1,7 +1,5 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
import java.util.Iterator;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; 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.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.chat.ChatManager; 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.mining.MiningManager;
import com.gmail.nossr50.skills.repair.Repair; import com.gmail.nossr50.skills.repair.Repair;
import com.gmail.nossr50.skills.taming.TamingManager; 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.BlockUtils;
import com.gmail.nossr50.util.ChimaeraWing; import com.gmail.nossr50.util.ChimaeraWing;
import com.gmail.nossr50.util.HardcoreManager; import com.gmail.nossr50.util.HardcoreManager;
@ -220,30 +218,18 @@ public class PlayerListener implements Listener {
if (mcMMOPlayer.inParty() && ItemUtils.isShareable(dropStack)) { if (mcMMOPlayer.inParty() && ItemUtils.isShareable(dropStack)) {
event.setCancelled(ShareHandler.handleItemShare(drop, mcMMOPlayer)); event.setCancelled(ShareHandler.handleItemShare(drop, mcMMOPlayer));
if (event.isCancelled()) {
return;
}
} }
if (event.isCancelled()) { if (mcMMOPlayer.isUsingUnarmed() && ItemUtils.isShareable(dropStack)) {
return; event.setCancelled(Unarmed.handleItemPickup(player.getInventory(), drop));
}
PlayerInventory inventory = player.getInventory(); if (event.isCancelled()) {
int firstEmpty = inventory.firstEmpty(); player.updateInventory();
return;
if (mcMMOPlayer.isUsingUnarmed() && ItemUtils.isShareable(dropStack) && firstEmpty == inventory.getHeldItemSlot()) {
int nextSlot = firstEmpty + 1;
for (Iterator<ItemStack> 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++;
} }
} }
} }

View File

@ -1,5 +1,11 @@
package com.gmail.nossr50.skills.unarmed; 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.AdvancedConfig;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
@ -19,4 +25,61 @@ public class Unarmed {
public static boolean blockCrackerSmoothBrick = Config.getInstance().getUnarmedBlockCrackerSmoothbrickToCracked(); public static boolean blockCrackerSmoothBrick = Config.getInstance().getUnarmedBlockCrackerSmoothbrickToCracked();
public static double berserkDamageModifier = 1.5; 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<ItemStack> 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<ItemStack> iterator = inventory.iterator(nextSlot); iterator.hasNext();) {
ItemStack itemstack = iterator.next();
if (itemstack == null) {
drop.remove();
inventory.setItem(nextSlot, dropStack);
return true;
}
nextSlot++;
}
}
return false;
}
} }