Fixing unarmed not picking up items properly

This commit is contained in:
nossr50 2019-01-29 16:13:04 -08:00
parent 8ad9419240
commit cef9fad49f
2 changed files with 95 additions and 103 deletions

View File

@ -39,6 +39,7 @@ import org.bukkit.entity.*;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*; import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
@ -350,56 +351,58 @@ public class PlayerListener implements Listener {
* @param event The event to modify * @param event The event to modify
*/ */
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerPickupItem(PlayerPickupItemEvent event) { public void onPlayerPickupItem(EntityPickupItemEvent event) {
/* WORLD BLACKLIST CHECK */ /* WORLD BLACKLIST CHECK */
if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld())) if(WorldBlacklist.isWorldBlacklisted(event.getEntity().getWorld()))
return; return;
Player player = event.getPlayer(); if(event.getEntity() instanceof Player)
/* WORLD GUARD MAIN FLAG CHECK */
if(WorldGuardUtils.isWorldGuardLoaded())
{ {
if(!WorldGuardManager.getInstance().hasMainFlag(player)) Player player = (Player) event.getEntity();
return;
}
if (!UserManager.hasPlayerDataKey(player)) { /* WORLD GUARD MAIN FLAG CHECK */
return; if(WorldGuardUtils.isWorldGuardLoaded())
} {
if(!WorldGuardManager.getInstance().hasMainFlag(player))
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); return;
Item drop = event.getItem();
ItemStack dropStack = drop.getItemStack();
if (drop.hasMetadata(mcMMO.disarmedItemKey)) {
if (!player.getName().equals(drop.getMetadata(mcMMO.disarmedItemKey).get(0).asString())) {
event.setCancelled(true);
} }
return; if (!UserManager.hasPlayerDataKey(player)) {
}
if (!drop.hasMetadata(mcMMO.droppedItemKey) && mcMMOPlayer.inParty() && ItemUtils.isSharable(dropStack)) {
event.setCancelled(ShareHandler.handleItemShare(drop, mcMMOPlayer));
if (event.isCancelled()) {
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
return; return;
} }
}
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) { McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
boolean pickupSuccess = Unarmed.handleItemPickup(player.getInventory(), drop);
boolean cancel = Config.getInstance().getUnarmedItemPickupDisabled() || pickupSuccess; Item drop = event.getItem();
event.setCancelled(cancel); ItemStack dropStack = drop.getItemStack();
if (drop.hasMetadata(mcMMO.disarmedItemKey)) {
if (!player.getName().equals(drop.getMetadata(mcMMO.disarmedItemKey).get(0).asString())) {
event.setCancelled(true);
}
if (pickupSuccess) {
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
player.updateInventory();
return; return;
} }
if (!drop.hasMetadata(mcMMO.droppedItemKey) && mcMMOPlayer.inParty() && ItemUtils.isSharable(dropStack)) {
event.setCancelled(ShareHandler.handleItemShare(drop, mcMMOPlayer));
if (event.isCancelled()) {
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
return;
}
}
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
Unarmed.handleItemPickup(player, event);
/*boolean cancel = Config.getInstance().getUnarmedItemPickupDisabled() || pickupSuccess;
event.setCancelled(cancel);
if (pickupSuccess) {
return;
}*/
}
} }
} }

View File

@ -1,90 +1,79 @@
package com.gmail.nossr50.skills.unarmed; package com.gmail.nossr50.skills.unarmed;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import org.bukkit.entity.Item; import com.gmail.nossr50.util.sounds.SoundManager;
import com.gmail.nossr50.util.sounds.SoundType;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
public class Unarmed { 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) { public static void handleItemPickup(Player player, EntityPickupItemEvent event) {
ItemStack dropStack = drop.getItemStack(); ItemStack[] storageContents = player.getInventory().getStorageContents();
int firstEmpty = inventory.firstEmpty(); ItemStack itemDrop = event.getItem().getItemStack();
int dropAmount = dropStack.getAmount(); int heldItemSlotID = player.getInventory().getHeldItemSlot();
if (inventory.containsAtLeast(dropStack, 1)) { int amount = itemDrop.getAmount();
int nextSlot = 0; boolean grabbedItem = false;
ItemStack[] items = inventory.getStorageContents(); for(int i = 0; i <= storageContents.length-1; i++)
for (ItemStack itemstack : items) { {
if (dropStack.isSimilar(itemstack)) { if(amount <= 0)
int itemAmount = itemstack.getAmount(); break;
int itemMax = itemstack.getMaxStackSize();
ItemStack addStack = itemstack.clone(); if(i == heldItemSlotID)
continue;
if (dropAmount + itemAmount <= itemMax) { //EMPTY SLOT!
drop.remove(); if(storageContents[i] == null)
addStack.setAmount(dropAmount + itemAmount); {
items[nextSlot] = addStack; player.getInventory().setItem(i, itemDrop);
inventory.setStorageContents(items); amount = 0;
return true; grabbedItem = true;
} break;
}
else if(itemDrop.isSimilar(storageContents[i]) && storageContents[i].getAmount() < storageContents[i].getMaxStackSize())
{
//If we can fit this whole itemstack into this item
if(amount + storageContents[i].getAmount() <= storageContents[i].getMaxStackSize())
{
ItemStack modifiedAmount = storageContents[i];
modifiedAmount.setAmount(amount + storageContents[i].getAmount());
addStack.setAmount(itemMax); player.getInventory().setItem(i, modifiedAmount);
dropAmount = dropAmount + itemAmount - itemMax; grabbedItem = true;
items[nextSlot] = addStack; amount = 0;
inventory.setStorageContents(items); } else {
//Add what we can from this stack
ItemStack modifiedAmount = storageContents[i];
int amountThatCanFit = storageContents[i].getMaxStackSize() - storageContents[i].getAmount();
modifiedAmount.setAmount(amountThatCanFit);
player.getInventory().setItem(i, modifiedAmount);
//Remove the amount we've added
grabbedItem = true;
amount -= amountThatCanFit;
} }
if (dropAmount == 0) {
drop.remove();
return true;
}
nextSlot++;
} }
} }
if (firstEmpty == inventory.getHeldItemSlot()) { if(amount <= 0)
int nextSlot = firstEmpty + 1; event.getItem().remove(); //Cleanup Item
else
event.getItem().getItemStack().setAmount(amount);
ItemStack[] items = inventory.getStorageContents(); event.setCancelled(true);
for (; nextSlot < items.length; nextSlot++) {
ItemStack itemstack = items[nextSlot];
if (itemstack == null) { if(grabbedItem)
drop.remove(); {
dropStack.setAmount(dropAmount); SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
items[nextSlot] = dropStack; player.updateInventory();
inventory.setStorageContents(items);
return true;
}
nextSlot++;
}
// Inventory is full - cancel the item pickup
if (dropStack.getAmount() == dropAmount) {
return false;
} else {
drop.remove();
dropStack.setAmount(dropAmount);
((Item) drop.getWorld().dropItem(drop.getLocation(), dropStack)).setPickupDelay(0);
return true;
}
} }
else if (firstEmpty != -1) {
drop.remove();
dropStack.setAmount(dropAmount);
inventory.setItem(firstEmpty, dropStack);
return true;
}
drop.remove();
return true;
} }
} }