Java Code Examples for net.minecraft.item.ItemStack#hasCapability()

The following examples show how to use net.minecraft.item.ItemStack#hasCapability() . 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 check out the related API usage on the sidebar.
Example 1
Source File: GTUtility.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Applies specific amount of damage to item, either to durable items (which implement IDamagableItem)
 * or to electric items, which have capability IElectricItem
 * Damage amount is equal to EU amount used for electric items
 *
 * @return if damage was applied successfully
 */
//TODO get rid of that
public static boolean doDamageItem(ItemStack itemStack, int vanillaDamage, boolean simulate) {
    Item item = itemStack.getItem();
    if (item instanceof IToolItem) {
        //if item implements IDamagableItem, it manages it's own durability itself
        IToolItem damagableItem = (IToolItem) item;
        return damagableItem.damageItem(itemStack, vanillaDamage, simulate);

    } else if (itemStack.hasCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null)) {
        //if we're using electric item, use default energy multiplier for textures
        IElectricItem capability = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
        int energyNeeded = vanillaDamage * ConfigHolder.energyUsageMultiplier;
        //noinspection ConstantConditions
        return capability.discharge(energyNeeded, Integer.MAX_VALUE, true, false, simulate) == energyNeeded;

    } else if (itemStack.isItemStackDamageable()) {
        if (!simulate && itemStack.attemptDamageItem(vanillaDamage, new Random(), null)) {
            //if we can't accept more damage, just shrink stack and mark it as broken
            //actually we would play broken animation here, but we don't have an entity who holds item
            itemStack.shrink(1);
        }
        return true;
    }
    return false;
}
 
Example 2
Source File: TileCrucible.java    From ExNihiloAdscensio with MIT License 6 votes vote down vote up
public boolean onBlockActivated(ItemStack stack, EntityPlayer player) {
	if (stack == null)
		return false;
	
	//Bucketing out the fluid.
	if (stack != null && stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null)) {
		boolean result = FluidUtil.interactWithFluidHandler(stack, tank, player);
		if (result) {
			PacketHandler.sendNBTUpdate(this);
		}
		return true;
	}
	
	//Adding a meltable.
	ItemStack addStack = stack.copy(); addStack.stackSize = 1;
	ItemStack insertStack = itemHandler.insertItem(0, addStack, true);
	if (!ItemStack.areItemStacksEqual(addStack, insertStack)) {
		itemHandler.insertItem(0, addStack, false);
		stack.stackSize--;
		PacketHandler.sendNBTUpdate(this);
		return true;
	}
	return true;
}
 
Example 3
Source File: WrenchOverlayRenderer.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean shouldDrawOverlayForItem(ItemStack itemStack, TileEntity tileEntity) {
    if(tileEntity instanceof MetaTileEntityHolder &&
        itemStack.hasCapability(GregtechCapabilities.CAPABILITY_WRENCH, null)) {
        return true;
    }
    if(tileEntity.hasCapability(GregtechTileCapabilities.CAPABILITY_COVERABLE, null)) {
        if(itemStack.hasCapability(GregtechCapabilities.CAPABILITY_SCREWDRIVER, null)) {
            return true;
        }
        if (itemStack.getItem() instanceof MetaItem) {
            MetaItem<?> metaItem = (MetaItem<?>) itemStack.getItem();
            MetaItem<?>.MetaValueItem valueItem = metaItem.getItem(itemStack);
            if (valueItem != null) {
                List<IItemBehaviour> behaviourList = valueItem.getBehaviours();
                return behaviourList.stream().anyMatch(it ->
                    it instanceof CoverPlaceBehavior || it instanceof CrowbarBehaviour);
            }
        }
    }
    return false;
}
 
Example 4
Source File: TileEntityEnderFurnace.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks if the given ItemStack contains a valid fluid fuel source for the furnace.
 * Valid fuels are currently just lava.
 * @param stack
 * @return
 */
private static boolean itemContainsFluidFuel(ItemStack stack)
{
    if (stack.isEmpty() || stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null) == false)
    {
        return false;
    }

    IFluidHandler handler = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);

    if (handler == null)
    {
        return false;
    }

    FluidStack fluidStack = handler.drain(Fluid.BUCKET_VOLUME, false);
    //System.out.printf("itemContainsFluidFuelFluidCapability: %s - %d\n", (fluidStack != null ? fluidStack.getFluid().getName() : "null"), fluidStack != null ? fluidStack.amount : 0);

    if (fluidStack == null || fluidStack.amount <= 0)
    {
        return false;
    }

    return fluidStack.getFluid() == FluidRegistry.LAVA;
}
 
Example 5
Source File: MetaTileEntityQuantumTank.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void update() {
    super.update();
    if (!getWorld().isRemote && getTimer() % 5 == 0) {
        ItemStack itemStack = containerInventory.getStackInSlot(0);
        Capability<IFluidHandlerItem> capability = CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY;
        if (!itemStack.isEmpty() && itemStack.hasCapability(capability, null)) {
            //if we can't drain anything, try filling container. Otherwise, drain it into the quantum chest tank
            if (itemStack.getCapability(capability, null).drain(Integer.MAX_VALUE, false) == null) {
                fillContainerFromInternalTank(containerInventory, containerInventory, 0, 1);
                pushFluidsIntoNearbyHandlers(getFrontFacing());
            } else {
                fillInternalTankFromFluidContainer(containerInventory, containerInventory, 0, 1);
                pullFluidsFromNearbyHandlers(getFrontFacing());
            }
        }
    }
}
 
Example 6
Source File: MetaTileEntityTransformer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onRightClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
    ItemStack itemStack = playerIn.getHeldItem(hand);
    if(!itemStack.isEmpty() && itemStack.hasCapability(GregtechCapabilities.CAPABILITY_MALLET, null)) {
        ISoftHammerItem softHammerItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_MALLET, null);

        if (getWorld().isRemote) {
            return true;
        }
        if(!softHammerItem.damageItem(DamageValues.DAMAGE_FOR_SOFT_HAMMER, false)) {
            return false;
        }

        if (isTransformUp) {
            setTransformUp(false);
            playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer.message_transform_down",
                energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));
            return true;
        } else {
            setTransformUp(true);
            playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer.message_transform_up",
                energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: ItemSpaceChest.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
@Override
public boolean isItemValidForSlot(ItemStack stack, int slot) {
	if(slot >= 2)
		return true;

	FluidStack fstack;
	return !stack.isEmpty() && stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP) && 
			((fstack = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP).getTankProperties()[0].getContents()) == null || FluidUtils.areFluidsSameType(fstack.getFluid(), AdvancedRocketryFluids.fluidOxygen));
}
 
Example 8
Source File: TileEntityFuelingStation.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
	if(stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
		FluidStack fstack = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP).getTankProperties()[0].getContents();
		return fstack != null && FuelRegistry.instance.isFuel(FuelType.LIQUID, fstack.getFluid());
	}
	return FuelRegistry.instance.isFuel(FuelType.LIQUID,stack);
}
 
Example 9
Source File: ContentGuiContainer.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private Object getClientGuiElementForItem(EntityPlayer player, World world, EnumHand hand)
{
    ItemStack stack = player.getHeldItem(hand);
    if (!stack.isEmpty() && stack.hasCapability(CapabilityItemHandlerSupplier.ITEM_HANDLER_SUPPLIER_CAPABILITY, null))
    {
        return ReflectionHelper.newInstance(guiConstructor, this, createContainer(stack, player, hand), ProgressBarSource.EMPTY, FluidSource.EMPTY);
    }

    return null;
}
 
Example 10
Source File: ContentGuiContainer.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private Object getServerGuiElementForItem(EntityPlayer player, World world, EnumHand hand)
{
    ItemStack stack = player.getHeldItem(hand);
    if (!stack.isEmpty() && stack.hasCapability(CapabilityItemHandlerSupplier.ITEM_HANDLER_SUPPLIER_CAPABILITY, null))
    {
        return createContainer(stack, player, hand);
    }

    return null;
}
 
Example 11
Source File: TileEntitySaltFurnace.java    From TofuCraftReload with MIT License 5 votes vote down vote up
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
    switch (i) {
        case 0:
            return TileEntityFurnace.isItemFuel(itemstack);
        case 2:
            return itemstack.isItemEqual(new ItemStack(Items.GLASS_BOTTLE, 1)) ||
                    itemstack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
    }
    return false;
}
 
Example 12
Source File: DefaultSubItemHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void getSubItems(ItemStack itemStack, CreativeTabs creativeTab, NonNullList<ItemStack> subItems) {
    subItems.add(itemStack.copy());
    IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
    if (electricItem != null) {
        electricItem.charge(Long.MAX_VALUE, Integer.MAX_VALUE, true, false);
        subItems.add(itemStack);
    }
    if (creativeTab == CreativeTabs.SEARCH) {
        if (itemStack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
            addFluidContainerVariants(itemStack, subItems);
        }
    }
}
 
Example 13
Source File: ToolMetaItem.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public double getDurabilityForDisplay(ItemStack stack) {
    T item = getItem(stack);
    if (item != null && item.getDurabilityManager() != null) {
        return item.getDurabilityManager().getDurabilityForDisplay(stack);
    }
    //if itemstack has electric charge ability, show electric charge percentage
    if (stack.hasCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null)) {
        IElectricItem electricItem = stack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
        //noinspection ConstantConditions
        return 1.0 - (electricItem.getCharge() / (electricItem.getMaxCharge() * 1.0));
    }
    //otherwise, show actual durability percentage
    return getItemDamage(stack) / (getMaxItemDamage(stack) * 1.0);
}
 
Example 14
Source File: ToolMetaItem.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean showDurabilityBar(ItemStack stack) {
    T item = getItem(stack);
    if (item != null && item.getDurabilityManager() != null) {
        return item.getDurabilityManager().showsDurabilityBar(stack);
    }
    //don't show durability if item is not electric and it's damage is zero
    return stack.hasCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null) || getItemDamage(stack) != 0;
}
 
Example 15
Source File: TankWidget.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@SideOnly(Side.CLIENT)
public boolean mouseClicked(int mouseX, int mouseY, int button) {
    if (isMouseOverElement(mouseX, mouseY)) {
        ItemStack currentStack = gui.entityPlayer.inventory.getItemStack();
        if (button == 0 && (allowClickEmptying || allowClickFilling) &&
            currentStack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
            boolean isShiftKeyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
            writeClientAction(1, writer -> writer.writeBoolean(isShiftKeyDown));
            playButtonClickSound();
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: ItemSpaceChest.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
/**
 * Decrements air in the suit by amt
 * @param stack the item stack to operate on
 * @param amt amount of air by which to decrement
 * @return The amount of air extracted from the suit
 */
@Override
public int decrementAir(ItemStack stack, int amt) {

	if(stack.hasTagCompound()) {
		EmbeddedInventory inv = new EmbeddedInventory(getNumSlots(stack));
		inv.readFromNBT(stack.getTagCompound());
		List<ItemStack> list = new LinkedList<ItemStack>();

		for(int i = 0; i < inv.getSizeInventory(); i++) {
			if(!inv.getStackInSlot(i).isEmpty())
				list.add(inv.getStackInSlot(i));
		}
		int amtDrained = amt;
		for(ItemStack component : list) {
			if(component.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
				IFluidHandlerItem fluidItem = component.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
				FluidStack fluidStack = FluidUtils.getFluidForItem(component);

				FluidStack fluidDrained = null;

				if(fluidStack != null && FluidUtils.areFluidsSameType(fluidStack.getFluid(), AdvancedRocketryFluids.fluidOxygen))
					fluidDrained = fluidItem.drain(amtDrained, true);

				if(fluidDrained != null)
					amtDrained -= fluidDrained.amount;

				if(amtDrained == 0)
					break;
			}
		}
		saveEmbeddedInventory(stack, inv);
		return amt - amtDrained;

	}
	return 0;

	/*NBTTagCompound nbt;
	if(stack.hasTagCompound()) {
		nbt = stack.getTagCompound();
	}
	else {
		nbt = new NBTTagCompound();
	}

	int prevAmt = nbt.getInteger("air");
	int newAmt = Math.max(prevAmt - amt,0);
	nbt.setInteger("air", newAmt);
	stack.setTagCompound(nbt);

	return prevAmt - newAmt;*/
}
 
Example 17
Source File: AtmosphereVacuum.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
public boolean protectsFrom(ItemStack stack) {
	return (ItemAirUtils.INSTANCE.isStackValidAirContainer(stack) && new ItemAirUtils.ItemAirWrapper(stack).protectsFromSubstance(this, stack, true) ) || (!stack.isEmpty() && stack.hasCapability(CapabilitySpaceArmor.PROTECTIVEARMOR, null) &&
			stack.getCapability(CapabilitySpaceArmor.PROTECTIVEARMOR, null).protectsFromSubstance(this, stack, true));
}
 
Example 18
Source File: TileEntityFuelingStation.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
private boolean useBucket( int slot, ItemStack stack) {
	if(slot == 0 && stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
		IFluidHandlerItem fluidItem = stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
		FluidStack fluidStack = fluidItem.getTankProperties()[0].getContents();

		if(fluidStack != null && FuelRegistry.instance.isFuel(FuelType.LIQUID, fluidStack.getFluid()) && tank.getFluidAmount() + fluidItem.getTankProperties()[0].getCapacity() <= tank.getCapacity()) {

			ItemStack emptyContainer = stack.copy();
			emptyContainer.setCount(1);
			emptyContainer.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP).drain(8000, true);

			//disposable tank
			if(emptyContainer.isEmpty()) {
				tank.fill(fluidStack, true);
				decrStackSize(0, 1);
			}
			else
			{
				emptyContainer = emptyContainer.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP).getContainer();

				if(!emptyContainer.isEmpty() && inventory.getStackInSlot(1).isEmpty() || (emptyContainer.isItemEqual(inventory.getStackInSlot(1)) && inventory.getStackInSlot(1).getCount() < inventory.getStackInSlot(1).getMaxStackSize())) {
					tank.fill(fluidStack, true);

					if(inventory.getStackInSlot(1).isEmpty())
						super.setInventorySlotContents(1, emptyContainer);
					else {
						inventory.getStackInSlot(1).setCount(inventory.getStackInSlot(1).getCount() + 1);
					}
					decrStackSize(0, 1);
				}
				else 
					return false;
			}
		}
		else
			return false;
	}
	else
		return false;

	return true;
}
 
Example 19
Source File: AtmosphereLowOxygen.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
public boolean protectsFrom(ItemStack stack) {
	return (ItemAirUtils.INSTANCE.isStackValidAirContainer(stack) && new ItemAirUtils.ItemAirWrapper(stack).protectsFromSubstance(this, stack, true) ) || (stack != null && stack.hasCapability(CapabilitySpaceArmor.PROTECTIVEARMOR, null) &&
			stack.getCapability(CapabilitySpaceArmor.PROTECTIVEARMOR, null).protectsFromSubstance(this, stack, true));
}
 
Example 20
Source File: TileEntitySaltFurnace.java    From TofuCraftReload with MIT License 4 votes vote down vote up
private boolean isFluidContainer(ItemStack stack) {
    return stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
}