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

The following examples show how to use net.minecraft.entity.player.EntityPlayer#hasCapability() . 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;
      }
    }
  }
}