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

The following examples show how to use net.minecraft.item.ItemStack#getEnchantmentTagList() . 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: ContainerEnchantmentModifier.java    From NotEnoughItems with MIT License 6 votes vote down vote up
@Deprecated
//TODO String variant.
public void removeEnchantment(int e) {
    ItemStack stack = inventorySlots.get(0).getStack();
    NBTTagList nbttaglist = stack.getEnchantmentTagList();
    if (nbttaglist != null) {
        for (int i = 0; i < nbttaglist.tagCount(); i++) {
            int ID = nbttaglist.getCompoundTagAt(i).getShort("id");
            if (ID == e) {
                nbttaglist.removeTag(i);
                if (nbttaglist.tagCount() == 0) {
                    stack.getTagCompound().removeTag("ench");
                }
                if (stack.getTagCompound().hasNoTags()) {
                    stack.setTagCompound(null);
                }
                return;
            }
        }
    }
}
 
Example 2
Source File: EventHandlerRedirect.java    From Gadomancy with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static int getRealEnchantmentLevel(int enchantmentId, ItemStack stack) {
    if (stack == null) {
        return 0;
    } else {
        NBTTagList nbttaglist = stack.getEnchantmentTagList();
        if (nbttaglist == null) {
            return 0;
        } else {
            for (int j = 0; j < nbttaglist.tagCount(); ++j) {
                short id = nbttaglist.getCompoundTagAt(j).getShort("id");
                if (id == enchantmentId) {
                    return nbttaglist.getCompoundTagAt(j).getShort("lvl");
                }
            }
            return 0;
        }
    }
}
 
Example 3
Source File: ItemAirUtils.java    From AdvancedRocketry with MIT License 6 votes vote down vote up
public boolean isStackValidAirContainer(ItemStack stack) {
	if(stack == null)
		return false;

	//Check for enchantment
	boolean isEnchanted = false;
	NBTTagList enchList = stack.getEnchantmentTagList();
	if(enchList != null) {
		for(int i = 0 ; i < enchList.tagCount(); i++) {
			NBTTagCompound compound = enchList.getCompoundTagAt(i);
			isEnchanted = compound.getShort("id") == Enchantment.getEnchantmentID(AdvancedRocketryAPI.enchantmentSpaceProtection);
			if(isEnchanted)
				break;
		}
	}
	return isEnchanted;
}
 
Example 4
Source File: ContainerEnchantmentModifier.java    From NotEnoughItems with MIT License 6 votes vote down vote up
public void removeEnchantment(int e) {
    ItemStack stack = ((Slot) inventorySlots.get(0)).getStack();
    NBTTagList nbttaglist = stack.getEnchantmentTagList();
    if (nbttaglist != null) {
        for (int i = 0; i < nbttaglist.tagCount(); i++) {
            int ID = nbttaglist.getCompoundTagAt(i).getShort("id");
            if (ID == e) {
                nbttaglist.removeTag(i);
                if (nbttaglist.tagCount() == 0)
                    stack.getTagCompound().removeTag("ench");
                if (stack.getTagCompound().hasNoTags())
                    stack.setTagCompound(null);
                return;
            }
        }
    }
}
 
Example 5
Source File: ItemUtils.java    From LiquidBounce with GNU General Public License v3.0 5 votes vote down vote up
public static int getEnchantment(ItemStack itemStack, Enchantment enchantment) {
    if (itemStack == null || itemStack.getEnchantmentTagList() == null || itemStack.getEnchantmentTagList().hasNoTags())
        return 0;

    for (int i = 0; i < itemStack.getEnchantmentTagList().tagCount(); i++) {
        final NBTTagCompound tagCompound = itemStack.getEnchantmentTagList().getCompoundTagAt(i);

        if ((tagCompound.hasKey("ench") && tagCompound.getShort("ench") == enchantment.effectId) || (tagCompound.hasKey("id") && tagCompound.getShort("id") == enchantment.effectId))
            return tagCompound.getShort("lvl");
    }

    return 0;
}
 
Example 6
Source File: ItemUtils.java    From LiquidBounce with GNU General Public License v3.0 5 votes vote down vote up
public static int getEnchantmentCount(ItemStack itemStack) {
    if (itemStack == null || itemStack.getEnchantmentTagList() == null || itemStack.getEnchantmentTagList().hasNoTags())
        return 0;

    int c = 0;

    for (int i = 0; i < itemStack.getEnchantmentTagList().tagCount(); i++) {
        NBTTagCompound tagCompound = itemStack.getEnchantmentTagList().getCompoundTagAt(i);

        if ((tagCompound.hasKey("ench") || tagCompound.hasKey("id")))
            c++;
    }

    return c;
}
 
Example 7
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static List<int[]> getEnchantments(ItemStack itemstack) {
    ArrayList<int[]> arraylist = new ArrayList<>();
    if (!itemstack.isEmpty()) {
        NBTTagList nbttaglist = itemstack.getEnchantmentTagList();
        if (nbttaglist != null) {
            for (int i = 0; i < nbttaglist.tagCount(); i++) {
                NBTTagCompound tag = nbttaglist.getCompoundTagAt(i);
                arraylist.add(new int[] { tag.getShort("id"), tag.getShort("lvl") });
            }
        }
    }
    return arraylist;
}
 
Example 8
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static List<int[]> getEnchantments(ItemStack itemstack) {
    ArrayList<int[]> arraylist = new ArrayList<int[]>();
    if (itemstack != null) {
        NBTTagList nbttaglist = itemstack.getEnchantmentTagList();
        if (nbttaglist != null)
            for (int i = 0; i < nbttaglist.tagCount(); i++) {
                NBTTagCompound tag = nbttaglist.getCompoundTagAt(i);
                arraylist.add(new int[]{tag.getShort("id"), tag.getShort("lvl")});
            }
    }
    return arraylist;
}
 
Example 9
Source File: ProxyCommon.java    From WearableBackpacks with MIT License 5 votes vote down vote up
/** Called when a mob spawns with a backpack with a 1 tick delay. */
private void onSpawnedWith(EntityLivingBase entity, BackpackCapability backpack, BackpackEntry entry) {
	ItemStack stack = new ItemStack(entry.getBackpackItem());

	if (entry.colorRange != null) // Set a random color!
		NbtUtils.set(stack, entry.colorRange.getRandom(), "display", "color");

	// Set damage to a random amount (25% - 75%).
	int maxDamage = stack.getMaxDamage();
	int damage = maxDamage / 4 + ((maxDamage / 2 > 0)
		? entity.world.rand.nextInt(maxDamage / 2) : 0);
	stack.setItemDamage(damage);

	if (BackpackHelper.equipAsChestArmor) {
		// If the entity spawned with enchanted armor,
		// then move over all compatible enchantments.
		ItemStack armor = entity.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
		if ((armor != null) && armor.isItemEnchanted()) {
			NBTTagList enchList = armor.getEnchantmentTagList();
			for (int i = 0; i < enchList.tagCount(); ++i) {
				NBTTagCompound enchTag = enchList.getCompoundTagAt(i);
				Enchantment enchantment = Enchantment.getEnchantmentByID(enchTag.getShort("id"));
				// If the enchantment doesn't work with the backpack, remove it.
				if (!enchantment.canApply(stack)) enchList.removeTag(i--);
			}
			if (enchList.tagCount() > 0)
				NbtUtils.set(stack, enchList, "ench");
		}
	}

	IBackpackType type = entry.getBackpackItem();
	IBackpackData data = type.createBackpackData(stack);
	BackpackHelper.setEquippedBackpack(entity, stack, data);
	type.onSpawnedWith(entity, backpack, entry.lootTable);
	backpack.spawnWith  = null;
	backpack.mayDespawn = true;
}
 
Example 10
Source File: EnchantmentMetaProvider.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@Override
public Object getMeta(Item target, ItemStack stack) {
	NBTTagList ench = stack.getEnchantmentTagList();
	return ench != null? ModuleVanilla.listEnchantments(ench) : null;
}
 
Example 11
Source File: UpgradingRecipe.java    From SimplyJetpacks with MIT License 4 votes vote down vote up
public UpgradingRecipe(ItemStack result, Object... recipe) {
    super(result, recipe);
    this.resultItem = (IEnergyContainerItem) result.getItem();
    this.resultMeta = result.getItemDamage();
    result.getEnchantmentTagList();
}