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

The following examples show how to use net.minecraft.item.ItemStack#hasTagCompound() . 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: CyberwareAPI.java    From Cyberware with MIT License 6 votes vote down vote up
/**
 * Clears all NBT data from Cyberware related to its function, things like power storage or oxygen storage
 * This ensures that removed Cyberware will stack. This should only be called on Cyberware that is being removed
 * from the body or otherwise reset - otherwise it may interrupt functionality.
 * 
 * @param stack	The ItemStack to sanitize
 * @return		A sanitized version of the stack
 */
public static ItemStack sanitize(ItemStack stack)
{
	if (stack != null)
	{
		if (stack.hasTagCompound() && stack.getTagCompound().hasKey(DATA_TAG))
		{
			stack.getTagCompound().removeTag(DATA_TAG);
		}
		if (stack.hasTagCompound() && stack.getTagCompound().hasNoTags())
		{
			stack.setTagCompound(null);
		}
	}
	
	return stack;
}
 
Example 2
Source File: StackHelper.java    From AgriCraft with MIT License 6 votes vote down vote up
/**
 * Determines if two ItemStacks are compatible as to be merged.
 * <br>
 * Two ItemStacks, a & b, are considered compatible if and only if:
 * <ul>
 * <li>both are non-null</li>
 * <li>both are non-empty</li>
 * <li>both have the same item</li>
 * <li>both are stackable</li>
 * <li>both have no subtypes or have the same metadata</li>
 * <li>both have the same item as determined by {@link #areItemsEqual()}</li>
 * <li>both have the same damage</li>
 * <li>both have equivalent NBTTags as determined by {@link #areItemStackTagsEqual()}</li>
 * </ul>
 *
 * @param a
 * @param b
 * @return
 */
public static final boolean areCompatible(@Nonnull ItemStack a, @Nonnull ItemStack b) {
    // Validate
    Preconditions.checkNotNull(a);
    Preconditions.checkNotNull(b);

    // The following is 'borrowed' from ItemHandlerHelper.canItemsStack
    if (a.isEmpty() || b.isEmpty() || a.getItem() != b.getItem()) {
        return false;
    }

    if (!a.isStackable()) {
        return false;
    }

    if (a.getHasSubtypes() && a.getMetadata() != b.getMetadata()) {
        return false;
    }

    if (a.hasTagCompound() != b.hasTagCompound()) {
        return false;
    }

    return (!a.hasTagCompound() || a.getTagCompound().equals(b.getTagCompound())) && a.areCapsCompatible(b);
}
 
Example 3
Source File: ItemMorphSword.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 6 votes vote down vote up
@Override
@SideOnly(Side.CLIENT)
public int getColorFromItemStack(ItemStack itemstack, int renderpass) {
    if (renderpass != 1)
        return 16777215;
    else {
        if (!itemstack.hasTagCompound())
            return 0x980000;
        byte phase = itemstack.getTagCompound().getByte("phase");
        if (phase == 1)
            return 0x0010CC;
        else if (phase == 2)
            return 0xE5DA00;
        else
            return 0x980000;
    }
}
 
Example 4
Source File: ItemHeat.java    From TFC2 with GNU General Public License v3.0 6 votes vote down vote up
public static void Decrease(ItemStack is, float heatToSub)
{
	if(!is.hasTagCompound())
	{
		is.setTagCompound(new NBTTagCompound());
	}

	float heat = is.getTagCompound().getFloat(HEAT_TAG) - heatToSub;

	if(heat <= 0)
		is.getTagCompound().removeTag(HEAT_TAG);
	else
		is.getTagCompound().setFloat(HEAT_TAG, heat);

	if(is.getTagCompound().getSize() == 0)
		is.setTagCompound(null);
}
 
Example 5
Source File: DestinationProviderItems.java    From Signals with GNU General Public License v3.0 6 votes vote down vote up
public static boolean areStacksEqual(ItemStack stack1, ItemStack stack2, boolean checkMeta, boolean checkNBT, boolean checkOreDict, boolean checkModSimilarity){
    if(stack1.isEmpty() && stack2.isEmpty()) return true;
    if(stack1.isEmpty() && !stack2.isEmpty() || !stack1.isEmpty() && stack2.isEmpty()) return false;

    if(checkModSimilarity) {
        ResourceLocation id1 = Item.REGISTRY.getNameForObject(stack1.getItem());
        ResourceLocation id2 = Item.REGISTRY.getNameForObject(stack2.getItem());
        return id1 != null && id2 != null && id1.getResourceDomain().equals(id2.getResourceDomain());
    }
    if(checkOreDict) {
        return isSameOreDictStack(stack1, stack2);
    }

    if(stack1.getItem() != stack2.getItem()) return false;

    boolean metaSame = stack1.getItemDamage() == stack2.getItemDamage();
    boolean nbtSame = stack1.hasTagCompound() ? stack1.getTagCompound().equals(stack2.getTagCompound()) : !stack2.hasTagCompound();

    return (!checkMeta || metaSame) && (!checkNBT || nbtSame);
}
 
Example 6
Source File: TemplateRecipeHandler.java    From NotEnoughItems with MIT License 6 votes vote down vote up
/**
 * Set all variable ingredients to this permutation.
 *
 * @param ingredient
 */
public void setIngredientPermutation(Collection<PositionedStack> ingredients, ItemStack ingredient) {
    for (PositionedStack stack : ingredients) {
        for (int i = 0; i < stack.items.length; i++) {
            if (NEIServerUtils.areStacksSameTypeCrafting(ingredient, stack.items[i])) {
                stack.item = stack.items[i];
                stack.item.setItemDamage(ingredient.getItemDamage());
                if (ingredient.hasTagCompound()) {
                    stack.item.setTagCompound((NBTTagCompound) ingredient.getTagCompound().copy());
                }
                stack.items = new ItemStack[]{stack.item};
                stack.setPermutationToRender(0);
                break;
            }
        }
    }
}
 
Example 7
Source File: EntityUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean hasToolItemInHand(EntityLivingBase entity, EnumHand hand)
{
    // If the configured tool item has NBT data, then the NBT is compared, otherwise it's ignored

    ItemStack toolItem = DataManager.getToolItem();

    if (toolItem.isEmpty())
    {
        return entity.getHeldItemMainhand().isEmpty();
    }

    ItemStack stackHand = entity.getHeldItem(hand);

    if (ItemStack.areItemsEqualIgnoreDurability(toolItem, stackHand))
    {
        return toolItem.hasTagCompound() == false || ItemStack.areItemStackTagsEqual(toolItem, stackHand);
    }

    return false;
}
 
Example 8
Source File: ItemBlueprint.java    From Cyberware with MIT License 6 votes vote down vote up
@Override
public ItemStack[] getRequirementsForDisplay(ItemStack stack)
{
	if (stack.hasTagCompound())
	{
		NBTTagCompound comp = stack.getTagCompound();
		if (comp.hasKey("blueprintItem"))
		{
			ItemStack blueprintItem = ItemStack.loadItemStackFromNBT(comp.getCompoundTag("blueprintItem"));
			if (blueprintItem != null && CyberwareAPI.canDeconstruct(blueprintItem))
			{
				return CyberwareAPI.getComponents(blueprintItem).clone();
			}
		}
	}
	return null;
}
 
Example 9
Source File: GTBlockQuantumTank.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
	if (stack.hasTagCompound()) {
		NBTTagCompound nbt;
		nbt = StackUtil.getNbtData(stack);
		if (nbt.hasKey("Fluid")) {
			FluidStack fluid = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag("Fluid"));
			tooltip.add(TextFormatting.AQUA + I18n.format(fluid.amount + "mB of " + fluid.getLocalizedName()));
		}
	}
	super.addInformation(stack, worldIn, tooltip, flagIn);
}
 
Example 10
Source File: ShapedArcaneRecipe.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 5 votes vote down vote up
private boolean checkItemEquals(ItemStack target, ItemStack input)
{
    if (input == null && target != null || input != null && target == null)
    {
        return false;
    }
    return (target.getItem() == input.getItem() && 
    		(!target.hasTagCompound() || ThaumcraftApiHelper.areItemStackTagsEqualForCrafting(input,target)) &&
    		(target.getItemDamage() == OreDictionary.WILDCARD_VALUE|| target.getItemDamage() == input.getItemDamage()));
}
 
Example 11
Source File: ItemExtendedNodeJar.java    From Gadomancy with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void setAspects(ItemStack itemstack, AspectList aspects) {
    if (!itemstack.hasTagCompound()) {
        itemstack.setTagCompound(new NBTTagCompound());
    }
    aspects.writeToNBT(itemstack.getTagCompound());
}
 
Example 12
Source File: BlockLantern.java    From GardenCollection with MIT License 5 votes vote down vote up
public boolean isGlass (ItemStack item) {
    if (item.hasTagCompound()) {
        NBTTagCompound tag = item.getTagCompound();
        if (tag.hasKey("glass"))
            return tag.getBoolean("glass");
    }

    return false;
}
 
Example 13
Source File: ItemTerraTool.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
public ItemStack onRepair(ItemStack is)
{
	if(!is.hasTagCompound())
	{
		is.setTagCompound(new NBTTagCompound());
	}
	is.getTagCompound().setInteger("reducedDur", Math.max((is.hasTagCompound() ? is.getTagCompound().getInteger("reducedDur") : 0)*2, 1));
	return is;
}
 
Example 14
Source File: ItemIdWithName.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
public String getName(ItemStack stack) {
	if(stack.hasTagCompound()) {
		NBTTagCompound nbt = stack.getTagCompound();
		return nbt.getString("name");
	}

	return "";
}
 
Example 15
Source File: BlockMachine.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
    MetaTileEntityHolder holder = (MetaTileEntityHolder) worldIn.getTileEntity(pos);
    MetaTileEntity sampleMetaTileEntity = GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(stack.getItemDamage());
    if (holder != null && sampleMetaTileEntity != null) {
        MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
        if (stack.hasTagCompound()) {
            metaTileEntity.initFromItemStackData(stack.getTagCompound());
        }
        EnumFacing placeFacing = placer.getHorizontalFacing().getOpposite();
        if (metaTileEntity.isValidFrontFacing(placeFacing)) {
            metaTileEntity.setFrontFacing(placeFacing);
        }
    }
}
 
Example 16
Source File: ItemRailConfigurator.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
public void setLinkedRail(ItemStack stack, MCPos railPos){
    if(railPos != null) {
        NBTTagCompound tag = stack.getOrCreateSubCompound("linkingRail");
        railPos.writeToNBT(tag);
    } else {
        if(stack.hasTagCompound()) stack.getTagCompound().removeTag("linkingRail");
    }
}
 
Example 17
Source File: TileEntitySolderingStation.java    From ExtraCells1 with MIT License 5 votes vote down vote up
public void changeStorage(EntityPlayer player, int slotID, int storageDelta)
{
	ItemStack storage = player.inventory.getCurrentItem().copy();
	if (storage != null && storage.getItem() == ItemEnum.STORAGEPHYSICAL.getItemInstance() && storage.getItemDamage() == 5)
	{
		if (!storage.hasTagCompound())
		{
			storage.setTagCompound(new NBTTagCompound());
		}
		NBTTagCompound nbt = storage.getTagCompound();
		int oldSize = nbt.getInteger("custom_size");
		if (oldSize + storageDelta >= 4096 && storageDelta != 0)
		{
			if (storageDelta > 0)
			{
				if (decreaseItemStack(Materials.matStorageCell.copy(), player.inventory))
					nbt.setInteger("custom_size", oldSize + storageDelta);
				player.inventory.mainInventory[player.inventory.currentItem] = storage;
			} else if (storageDelta < 0)
			{
				if (player.inventory.addItemStackToInventory(Materials.matStorageCell.copy()))
					nbt.setInteger("custom_size", oldSize + storageDelta);
				player.inventory.mainInventory[player.inventory.currentItem] = storage;
			}
		}
	}
}
 
Example 18
Source File: ItemPlanetIdentificationChip.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
/**
 * @param stack itemStack of this item-type
 * @return the DimensionProperties of the dimId stored on the item or null if invalid
 */
public DimensionProperties getDimension(ItemStack stack) {
	if(stack.hasTagCompound()) {
		return DimensionManager.getInstance().getDimensionProperties(stack.getTagCompound().getInteger(dimensionIdIdentifier));
	}
	return null;
}
 
Example 19
Source File: ShapedArcaneRecipe.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
private boolean checkItemEquals(ItemStack target, ItemStack input)
{
    if (input == null && target != null || input != null && target == null)
    {
        return false;
    }
    return (target.getItem() == input.getItem() && 
    		(!target.hasTagCompound() || ThaumcraftApiHelper.areItemStackTagsEqualForCrafting(input,target)) &&
    		(target.getItemDamage() == OreDictionary.WILDCARD_VALUE|| target.getItemDamage() == input.getItemDamage()));
}
 
Example 20
Source File: ItemAsteroidChip.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
public String getType(ItemStack stack) {
	if(stack.hasTagCompound())
		return stack.getTagCompound().getString(astType);
	return null;
}