mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-22 21:26:46 +01:00
Fixing unarmed not picking up items properly
This commit is contained in:
parent
8ad9419240
commit
cef9fad49f
@ -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,56 +351,58 @@ 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();
|
||||
|
||||
/* WORLD GUARD MAIN FLAG CHECK */
|
||||
if(WorldGuardUtils.isWorldGuardLoaded())
|
||||
if(event.getEntity() instanceof Player)
|
||||
{
|
||||
if(!WorldGuardManager.getInstance().hasMainFlag(player))
|
||||
return;
|
||||
}
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
if (!UserManager.hasPlayerDataKey(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
|
||||
|
||||
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);
|
||||
/* WORLD GUARD MAIN FLAG CHECK */
|
||||
if(WorldGuardUtils.isWorldGuardLoaded())
|
||||
{
|
||||
if(!WorldGuardManager.getInstance().hasMainFlag(player))
|
||||
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);
|
||||
if (!UserManager.hasPlayerDataKey(player)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
|
||||
boolean pickupSuccess = Unarmed.handleItemPickup(player.getInventory(), drop);
|
||||
boolean cancel = Config.getInstance().getUnarmedItemPickupDisabled() || pickupSuccess;
|
||||
event.setCancelled(cancel);
|
||||
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (pickupSuccess) {
|
||||
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
|
||||
player.updateInventory();
|
||||
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;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
player.getInventory().setItem(i, modifiedAmount);
|
||||
grabbedItem = true;
|
||||
amount = 0;
|
||||
} 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()) {
|
||||
int nextSlot = firstEmpty + 1;
|
||||
if(amount <= 0)
|
||||
event.getItem().remove(); //Cleanup Item
|
||||
else
|
||||
event.getItem().getItemStack().setAmount(amount);
|
||||
|
||||
ItemStack[] items = inventory.getStorageContents();
|
||||
for (; nextSlot < items.length; nextSlot++) {
|
||||
ItemStack itemstack = items[nextSlot];
|
||||
event.setCancelled(true);
|
||||
|
||||
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;
|
||||
} else {
|
||||
drop.remove();
|
||||
dropStack.setAmount(dropAmount);
|
||||
((Item) drop.getWorld().dropItem(drop.getLocation(), dropStack)).setPickupDelay(0);
|
||||
return true;
|
||||
}
|
||||
if(grabbedItem)
|
||||
{
|
||||
SoundManager.sendSound(player, player.getLocation(), SoundType.POP);
|
||||
player.updateInventory();
|
||||
}
|
||||
else if (firstEmpty != -1) {
|
||||
drop.remove();
|
||||
dropStack.setAmount(dropAmount);
|
||||
inventory.setItem(firstEmpty, dropStack);
|
||||
return true;
|
||||
}
|
||||
|
||||
drop.remove();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user