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.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot;
@ -350,12 +351,14 @@ public class PlayerListener implements Listener {
* @param event The event to modify
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
public void onPlayerPickupItem(EntityPickupItemEvent event) {
/* WORLD BLACKLIST CHECK */
if(WorldBlacklist.isWorldBlacklisted(event.getPlayer().getWorld()))
if(WorldBlacklist.isWorldBlacklisted(event.getEntity().getWorld()))
return;
Player player = event.getPlayer();
if(event.getEntity() instanceof Player)
{
Player player = (Player) event.getEntity();
/* WORLD GUARD MAIN FLAG CHECK */
if(WorldGuardUtils.isWorldGuardLoaded())
@ -391,14 +394,14 @@ public class PlayerListener implements Listener {
}
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
boolean pickupSuccess = Unarmed.handleItemPickup(player.getInventory(), drop);
boolean cancel = Config.getInstance().getUnarmedItemPickupDisabled() || pickupSuccess;
Unarmed.handleItemPickup(player, event);
/*boolean cancel = Config.getInstance().getUnarmedItemPickupDisabled() || pickupSuccess;
event.setCancelled(cancel);
if (pickupSuccess) {
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
player.updateInventory();
return;
}*/
}
}
}

View File

@ -1,90 +1,79 @@
package com.gmail.nossr50.skills.unarmed;
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.PlayerInventory;
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();
int dropAmount = dropStack.getAmount();
public static void handleItemPickup(Player player, EntityPickupItemEvent event) {
ItemStack[] storageContents = player.getInventory().getStorageContents();
ItemStack itemDrop = event.getItem().getItemStack();
int heldItemSlotID = player.getInventory().getHeldItemSlot();
if (inventory.containsAtLeast(dropStack, 1)) {
int nextSlot = 0;
int amount = itemDrop.getAmount();
boolean grabbedItem = false;
ItemStack[] items = inventory.getStorageContents();
for (ItemStack itemstack : items) {
if (dropStack.isSimilar(itemstack)) {
int itemAmount = itemstack.getAmount();
int itemMax = itemstack.getMaxStackSize();
for(int i = 0; i <= storageContents.length-1; i++)
{
if(amount <= 0)
break;
ItemStack addStack = itemstack.clone();
if(i == heldItemSlotID)
continue;
if (dropAmount + itemAmount <= itemMax) {
drop.remove();
addStack.setAmount(dropAmount + itemAmount);
items[nextSlot] = addStack;
inventory.setStorageContents(items);
return true;
//EMPTY SLOT!
if(storageContents[i] == null)
{
player.getInventory().setItem(i, itemDrop);
amount = 0;
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);
dropAmount = dropAmount + itemAmount - itemMax;
items[nextSlot] = addStack;
inventory.setStorageContents(items);
}
if (dropAmount == 0) {
drop.remove();
return true;
}
nextSlot++;
}
}
if (firstEmpty == inventory.getHeldItemSlot()) {
int nextSlot = firstEmpty + 1;
ItemStack[] items = inventory.getStorageContents();
for (; nextSlot < items.length; nextSlot++) {
ItemStack itemstack = items[nextSlot];
if (itemstack == null) {
drop.remove();
dropStack.setAmount(dropAmount);
items[nextSlot] = dropStack;
inventory.setStorageContents(items);
return true;
}
nextSlot++;
}
// Inventory is full - cancel the item pickup
if (dropStack.getAmount() == dropAmount) {
return false;
player.getInventory().setItem(i, modifiedAmount);
grabbedItem = true;
amount = 0;
} else {
drop.remove();
dropStack.setAmount(dropAmount);
((Item) drop.getWorld().dropItem(drop.getLocation(), dropStack)).setPickupDelay(0);
return true;
//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;
}
}
else if (firstEmpty != -1) {
drop.remove();
dropStack.setAmount(dropAmount);
inventory.setItem(firstEmpty, dropStack);
return true;
}
drop.remove();
return true;
if(amount <= 0)
event.getItem().remove(); //Cleanup Item
else
event.getItem().getItemStack().setAmount(amount);
event.setCancelled(true);
if(grabbedItem)
{
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
player.updateInventory();
}
}
}