Java Code Examples for net.minecraft.entity.player.EntityPlayer#getCapability()

The following examples show how to use net.minecraft.entity.player.EntityPlayer#getCapability() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: TinkersToolLeveling   File: DamageXpHandler.java    License: MIT License 6 votes vote down vote up
private void distributeXpToPlayerForTool(EntityPlayer player, ItemStack tool, float damage) {
  if(!tool.isEmpty() && player.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
    IItemHandler itemHandler = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    // check for identity. should work in most cases because the entity was killed without loading/unloading
    for(int i = 0; i < itemHandler.getSlots(); i++) {
      if(itemHandler.getStackInSlot(i) == tool) {
        TinkerToolLeveling.modToolLeveling.addXp(tool, Math.round(damage), player);
        return;
      }
    }

    // check for equal stack in case instance equality didn't find it
    for(int i = 0; i < itemHandler.getSlots(); i++) {
      if(ToolCore.isEqualTinkersItem(itemHandler.getStackInSlot(i), tool)) {
        TinkerToolLeveling.modToolLeveling.addXp(itemHandler.getStackInSlot(i), Math.round(damage), player);
        return;
      }
    }
  }
}
 
Example 2
/**
 * Returns the ItemStacks of enabled Pickup Managers in the player's inventory
 */
public static List<ItemStack> getEnabledItems(EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.PICKUP_MANAGER);
    List<ItemStack> enabledItems = new ArrayList<ItemStack>();

    for (int slot : slots)
    {
        ItemStack stack = playerInv.getStackInSlot(slot);

        if (isEnabled(stack))
        {
            enabledItems.add(stack);
        }
    }

    return enabledItems;
}
 
Example 3
/**
 * Returns an ItemStack containing an enabled Handy Bag in the player's inventory, or null if none is found.
 */
public static ItemStack getOpenableBag(EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.HANDY_BAG);

    for (int slot : slots)
    {
        ItemStack stack = playerInv.getStackInSlot(slot);

        if (bagIsOpenable(stack))
        {
            return stack;
        }
    }

    return ItemStack.EMPTY;
}
 
Example 4
/**
 * Returns an ItemStack containing an enabled Inventory Swapper in the player's inventory, or null if none is found.
 */
public static ItemStack getEnabledItem(EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.QUICK_STACKER);

    for (int slot : slots)
    {
        ItemStack stack = playerInv.getStackInSlot(slot);

        if (isEnabled(stack))
        {
            return stack;
        }
    }

    return ItemStack.EMPTY;
}
 
Example 5
/**
 * Returns the slot number of the first enabled/usable Inventory Swapper in the player's inventory, or -1 if none is found.
 */
private int getSlotContainingEnabledItem(EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.INVENTORY_SWAPPER);

    for (int slot : slots)
    {
        if (this.isEnabled(playerInv.getStackInSlot(slot)))
        {
            return slot;
        }
    }

    return -1;
}
 
Example 6
/**
 * Returns either the player's inventory, or a bound, linked inventory that has items matching <b>templateStack</b>,
 * or null if no matching items are found.
 */
public static IItemHandler getPlayerOrBoundInventoryWithItems(ItemStack toolStack, ItemStack templateStack, EntityPlayer player)
{
    IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    int slot = InventoryUtils.getSlotOfFirstMatchingItemStack(inv, templateStack);

    if (slot != -1)
    {
        return inv;
    }

    inv = getBoundInventory(toolStack, player, 30);

    if (inv != null)
    {
        slot = InventoryUtils.getSlotOfFirstMatchingItemStack(inv, templateStack);

        if (slot != -1)
        {
            return inv;
        }
    }

    return null;
}
 
Example 7
/**
 * Returns the first matching item from the player's inventory, or an empty stack.
 */
public static ItemStack getFirstItemOfType(EntityPlayer player, Class<?> clazz)
{
    IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    final int numSlots = inv.getSlots();

    for (int slot = 0; slot < numSlots; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && clazz.isAssignableFrom(stack.getItem().getClass()))
        {
            return stack;
        }
    }

    return ItemStack.EMPTY;
}
 
Example 8
/**
 * Returns the first matching item from the player's inventory, or an empty stack.
 */
public static ItemStack getFirstItemOfType(EntityPlayer player, Item item)
{
    IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    final int numSlots = inv.getSlots();

    for (int slot = 0; slot < numSlots; slot++)
    {
        ItemStack stack = inv.getStackInSlot(slot);

        if (stack.isEmpty() == false && stack.getItem() == item)
        {
            return stack;
        }
    }

    return ItemStack.EMPTY;
}
 
Example 9
private IItemHandler getLinkedInventoryWithChecks(ItemStack toolStack, EntityPlayer player)
{
    DropsMode mode = DropsMode.fromStack(toolStack);
    // Modes: 0: normal; 1: Add drops to player's inventory; 2: Transport drops to Link Crystal's bound destination

    // 0: normal mode; do nothing
    if (mode == DropsMode.NORMAL)
    {
        return null;
    }

    // 1: Add drops to player's inventory; To allow this, we require at least the lowest tier Ender Core (active) installed
    if (mode == DropsMode.PLAYER && this.getMaxModuleTier(toolStack, ModuleType.TYPE_ENDERCORE) >= ItemEnderPart.ENDER_CORE_TYPE_ACTIVE_BASIC)
    {
        return player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP); // main inventory
    }
    // 2: Teleport drops to the Link Crystal's bound target; To allow this, we require an active second tier Ender Core
    // For cross-dimensional item teleport we require the third tier of active Ender Core
    else if (mode == DropsMode.REMOTE &&
            this.getMaxModuleTier(toolStack, ModuleType.TYPE_ENDERCORE) >= ItemEnderPart.ENDER_CORE_TYPE_ACTIVE_ENHANCED &&
            UtilItemModular.useEnderCharge(toolStack, ENDER_CHARGE_COST, true))
    {
        TargetData target = TargetData.getTargetFromSelectedModule(toolStack, ModuleType.TYPE_LINKCRYSTAL);

        // For cross-dimensional item teleport we require the third tier of active Ender Core
        if (target == null ||
            (target.dimension != player.getEntityWorld().provider.getDimension() &&
             this.getMaxModuleTier(toolStack, ModuleType.TYPE_ENDERCORE) != ItemEnderPart.ENDER_CORE_TYPE_ACTIVE_ADVANCED))
        {
            return null;
        }

        return UtilItemModular.getBoundInventory(toolStack, player, 15);
    }

    return null;
}
 
Example 10
/**
 * Adds one more of each item in the recipe into the crafting grid, if possible
 * @param invId
 * @param recipeId
 */
protected boolean addOneSetOfRecipeItemsIntoGrid(IItemHandler invCrafting, int invId, int recipeId, EntityPlayer player)
{
    invId = MathHelper.clamp(invId, 0, 1);
    int maskOreDict = invId == 1 ? MODE_BIT_RIGHT_CRAFTING_OREDICT : MODE_BIT_LEFT_CRAFTING_OREDICT;
    boolean useOreDict = (this.modeMask & maskOreDict) != 0;

    IItemHandlerModifiable playerInv = (IItemHandlerModifiable) player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
    IItemHandler invWrapper = new CombinedInvWrapper(this.itemInventory, playerInv);

    NonNullList<ItemStack> template = this.getRecipeItems(invId);
    InventoryUtils.clearInventoryToMatchTemplate(invCrafting, invWrapper, template);

    return InventoryUtils.restockInventoryBasedOnTemplate(invCrafting, invWrapper, template, 1, true, useOreDict);
}
 
Example 11
private static ItemStack handleItems(ItemStack itemsIn, ItemStack bagStack, EntityPlayer player)
{
    PickupMode pickupMode = PickupMode.fromStack(bagStack);
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    // First try to fill all existing stacks in the player's inventory
    if (pickupMode != PickupMode.NONE)
    {
        itemsIn = InventoryUtils.tryInsertItemStackToExistingStacksInInventory(playerInv, itemsIn);
    }

    if (itemsIn.isEmpty())
    {
        return ItemStack.EMPTY;
    }

    InventoryItemModular bagInv = getInventoryForBag(bagStack, player);

    if (bagInv != null)
    {
        IItemHandler wrappedBagInv = getWrappedEnabledInv(bagStack, bagInv);

        // If there is no space left in existing stacks in the player's inventory
        // then add the items to the bag, if one of the pickup modes is enabled.
        if (pickupMode == PickupMode.ALL ||
            (pickupMode == PickupMode.MATCHING && InventoryUtils.getSlotOfFirstMatchingItemStack(wrappedBagInv, itemsIn) != -1))
        {
            itemsIn = InventoryUtils.tryInsertItemStackToInventory(wrappedBagInv, itemsIn);
        }
    }

    return itemsIn;
}
 
Example 12
public static void quickStackToInventories(World world, EntityPlayer player, long enabledSlotsMask,
        List<BlockPosDistance> positions, FilterSettings filter)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    boolean movedSome = false;

    for (BlockPosDistance posDist : positions)
    {
        TileEntity te = world.getTileEntity(posDist.pos);

        if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP))
        {
            IItemHandler inv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);

            if (inv != null)
            {
                Result result = quickStackItems(playerInv, inv, enabledSlotsMask, player.isSneaking() == false, filter);

                if (result != Result.MOVED_NONE)
                {
                    Effects.spawnParticlesFromServer(world.provider.getDimension(), posDist.pos, EnumParticleTypes.VILLAGER_HAPPY);
                    movedSome = true;
                }

                if (result == Result.MOVED_ALL)
                {
                    break;
                }
            }
        }
    }

    if (movedSome)
    {
        world.playSound(null, player.getPosition(), SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.MASTER, 0.5f, 1.8f);
    }
}
 
Example 13
public static Result quickStackItems(EntityPlayer player, ItemStack stackerStack, IItemHandler inventory)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    Result ret = TileEntityQuickStackerAdvanced.quickStackItems(playerInv, inventory,
            getEnabledSlotsMask(stackerStack), player.isSneaking() == false, null);

    if (ret != Result.MOVED_NONE)
    {
        player.getEntityWorld().playSound(null, player.getPosition(), SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.MASTER, 0.2f, 1.8f);
    }

    return ret;
}
 
Example 14
private static ItemStack handleItems(ItemStack itemsIn, ItemStack nullifierStack, EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

    // First try to fill all existing stacks in the player's inventory
    itemsIn = InventoryUtils.tryInsertItemStackToExistingStacksInInventory(playerInv, itemsIn);

    if (itemsIn.isEmpty() == false)
    {
        itemsIn = tryInsertItemsToNullifier(itemsIn, nullifierStack, player);
    }

    return itemsIn;
}
 
Example 15
public static void getDropAndSetToAir(World world, EntityPlayer player, BlockPos pos, EnumFacing side, boolean addToInventory)
{
    if (player.capabilities.isCreativeMode == false)
    {
        ItemStack stack = BlockUtils.getPickBlockItemStack(world, pos, player, side);

        if (stack.isEmpty() == false)
        {
            if (addToInventory)
            {
                IItemHandler inv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

                if (inv != null)
                {
                    stack = InventoryUtils.tryInsertItemStackToInventory(inv, stack);
                }
            }

            if (stack.isEmpty() == false)
            {
                EntityUtils.dropItemStacksInWorld(world, pos, stack, -1, true);
            }
        }
    }

    setBlockToAirWithBreakSound(world, pos);
}
 
Example 16
public static PlayerMana getPlayerMana(EntityPlayer player) {
    return player.getCapability(PLAYER_MANA, null);
}
 
Example 17
public static PlayerCivilizationCapability get(EntityPlayer player) {
	if (player == null) {
		throw new NullPointerException("NULL player");
	}
	return player.getCapability(PlayerCivilizationCapabilityImpl.INSTANCE, null);
}
 
Example 18
/**
 * Tries to first fill the matching stacks in the player's inventory,
 * and then depending on the bag's mode, tries to add the remaining items
 * to the bag's inventory.
 * @param event
 * @return true if all items were handled and further processing of the event should not occur
 */
public static boolean onItemPickupEvent(PlayerItemPickupEvent event)
{
    if (event.getEntityPlayer().getEntityWorld().isRemote)
    {
        return false;
    }

    boolean pickedUp = false;
    EntityPlayer player = event.getEntityPlayer();
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    List<Integer> bagSlots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.HANDY_BAG);
    Iterator<ItemStack> iter = event.drops.iterator();

    while (iter.hasNext())
    {
        ItemStack stack = iter.next();

        if (stack.isEmpty())
        {
            iter.remove();
            continue;
        }

        // Not all the items could fit into existing stacks in the player's inventory, move them directly to the bag
        for (int slot : bagSlots)
        {
            ItemStack bagStack = playerInv.getStackInSlot(slot);

            // Bag is not locked
            if (bagStack.isEmpty() == false && bagStack.getItem() == EnderUtilitiesItems.HANDY_BAG && ItemHandyBag.bagIsOpenable(bagStack))
            {
                ItemStack stackOrig = stack;
                stack = handleItems(stack, bagStack, player);

                if (stack.isEmpty())
                {
                    iter.remove();
                    pickedUp = true;
                }
                else if (stackOrig.getCount() != stack.getCount())
                {
                    stackOrig.setCount(stack.getCount());
                    pickedUp = true;
                }
            }
        }
    }

    // At least some items were picked up
    if (pickedUp)
    {
        player.getEntityWorld().playSound(null, player.getPosition(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.MASTER, 0.2F,
                ((itemRand.nextFloat() - itemRand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
    }

    if (event.drops.isEmpty())
    {
        event.setCanceled(true);
        return true;
    }

    return false;
}
 
Example 19
/**
 * Tries to first fill the matching stacks in the player's inventory,
 * and then depending on the bag's mode, tries to add the remaining items
 * to the bag's inventory.
 * @param event
 * @return true if all items were handled and further processing of the event should not occur
 */
public static boolean onEntityItemPickupEvent(EntityItemPickupEvent event)
{
    EntityItem entityItem = event.getItem();
    ItemStack stack = entityItem.getItem();
    EntityPlayer player = event.getEntityPlayer();

    if (player.getEntityWorld().isRemote || entityItem.isDead || stack.isEmpty())
    {
        return true;
    }

    ItemStack origStack = ItemStack.EMPTY;
    final int origStackSize = stack.getCount();
    int stackSizeLast = origStackSize;
    boolean ret = false;

    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    // Not all the items could fit into existing stacks in the player's inventory, move them directly to the nullifier
    List<Integer> slots = InventoryUtils.getSlotNumbersOfMatchingItems(playerInv, EnderUtilitiesItems.NULLIFIER);

    for (int slot : slots)
    {
        ItemStack nullifierStack = playerInv.getStackInSlot(slot);

        // Nullifier is not disabled
        if (nullifierStack.isEmpty() == false && isNullifierEnabled(nullifierStack))
        {
            // Delayed the stack copying until we know if there is a valid bag,
            // so check if the stack was copied already or not.
            if (origStack == ItemStack.EMPTY)
            {
                origStack = stack.copy();
            }

            stack = handleItems(stack, nullifierStack, player);

            if (stack.isEmpty() || stack.getCount() != stackSizeLast)
            {
                if (stack.isEmpty())
                {
                    entityItem.setDead();
                    event.setCanceled(true);
                    ret = true;
                    break;
                }

                ItemStack pickedUpStack = origStack.copy();
                pickedUpStack.setCount(stackSizeLast - stack.getCount());

                FMLCommonHandler.instance().firePlayerItemPickupEvent(player, entityItem, pickedUpStack);
                player.onItemPickup(entityItem, origStackSize);
            }

            stackSizeLast = stack.getCount();
        }
    }

    // Not everything was handled, update the stack
    if (entityItem.isDead == false && stack.getCount() != origStackSize)
    {
        entityItem.setItem(stack);
    }

    // At least some items were picked up
    if (entityItem.isSilent() == false && (entityItem.isDead || stack.getCount() != origStackSize))
    {
        player.getEntityWorld().playSound(null, player.getPosition(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.MASTER,
                0.2F, ((itemRand.nextFloat() - itemRand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
    }

    return ret;
}
 
Example 20
protected void quickStackToTargetInventories(EntityPlayer player)
{
    IItemHandler playerInv = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    boolean movedSome = false;

    for (int slot = 0, bit = 0x1; slot < NUM_TARGET_INVENTORIES; slot++)
    {
        if ((this.enabledTargetsMask & bit) != 0)
        {
            ItemStack lcStack = this.getBaseItemHandler().getStackInSlot(slot);

            if (lcStack.isEmpty() == false)
            {
                TargetData target = TargetData.getTargetFromItem(lcStack);

                if (target != null && this.getWorld().isBlockLoaded(target.pos, true) &&
                    target.dimension == this.getWorld().provider.getDimension() &&
                    PositionUtils.isWithinRange(target.pos, this.getPos(), 32, 32, 32) && target.isTargetBlockUnchanged())
                {
                    TileEntity te = this.getWorld().getTileEntity(target.pos);

                    if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, target.facing))
                    {
                        IItemHandler externalInv = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, target.facing);

                        if (externalInv != null)
                        {
                            FilterSettings filter = this.getFilterSettings(slot);
                            Result result = quickStackItems(playerInv, externalInv, this.slotMask, player.isSneaking() == false, filter);

                            if (result != Result.MOVED_NONE)
                            {
                                Effects.spawnParticlesFromServer(player.getEntityWorld().provider.getDimension(), target.pos, EnumParticleTypes.VILLAGER_HAPPY);
                                movedSome = true;
                            }
                        }
                    }
                }
            }
        }

        bit <<= 1;
    }

    if (movedSome)
    {
        player.getEntityWorld().playSound(null, player.getPosition(), SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.MASTER, 0.5f, 1.8f);
    }
}