Package com.minecolonies.api.util
Class ItemStackUtils
java.lang.Object
com.minecolonies.api.util.ItemStackUtils
Utility methods for the inventories.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic HashMap<net.minecraft.world.item.Item, Set<net.minecraft.core.component.DataComponentType<?>>> List of the checked nbt sets for itemstack comparisons.static final net.minecraft.world.item.ItemStackVariable representing the empty itemstack in 1.10.static final @NotNull Predicate<net.minecraft.world.item.ItemStack> Predicate to check if an itemStack is empty.static final Predicate<net.minecraft.world.item.ItemStack> True if this stack is a standard food item (has at least some healing and some saturation, not purely for effects).static final Predicate<net.minecraft.world.item.ItemStack> Predicate to check for compost items.static Predicate<net.minecraft.world.item.ItemStack> Predicate describing things which work in the furnace.static Predicate<net.minecraft.world.item.ItemStack> Predicate describing cookables.static final Predicate<net.minecraft.world.item.ItemStack> True if this stack is a standard food item that isn't excluded for colonists.static final @NotNull Predicate<net.minecraft.world.item.ItemStack> Negation of the itemStack empty predicate (not empty). -
Method Summary
Modifier and TypeMethodDescriptionstatic List<net.minecraft.world.item.ItemStack> allItemsPlusInventory(@NotNull net.minecraft.world.entity.player.Player player) Obtains a list of all basic items in the game, plus any extra items present in the player's inventory (allowing for items with custom NBT, e.g.static @NotNull BooleanareItemStacksMergable(net.minecraft.world.item.ItemStack existingStack, net.minecraft.world.item.ItemStack mergingStack) Method to check if two ItemStacks can be merged together.static voidchangeSize(@NotNull net.minecraft.world.item.ItemStack stack, int amount) Increase or decrease the stack size.static booleancompareItemStackListIgnoreStackSize(List<net.minecraft.world.item.ItemStack> stacks, net.minecraft.world.item.ItemStack stack) Method to check if a stack is in a list of stacks.static booleancompareItemStackListIgnoreStackSize(List<net.minecraft.world.item.ItemStack> stacks, net.minecraft.world.item.ItemStack stack, boolean matchDamage, boolean matchNBT) Method to check if a stack is in a list of stacks.static @NotNull BooleancompareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2) Method to compare to stacks, ignoring their stacksize.static booleancompareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2, boolean matchDamage, boolean matchNBT) Method to compare to stacks, ignoring their stacksize.static booleancompareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2, boolean matchDamage, boolean matchNBT, boolean min) Method to compare to stacks, ignoring their stacksize.static booleancompareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2, boolean matchDamage, boolean matchNBT, boolean min, boolean matchNBTExactly) Method to compare to stacks, ignoring their stacksize.static booleancompareItemStorageListIgnoreStackSize(List<ItemStorage> stacks, net.minecraft.world.item.ItemStack stack) Method to check if a stack is in a list of ItemStorage items.static voidconsumeFood(net.minecraft.world.item.ItemStack foodStack, AbstractEntityCitizen citizen, @Nullable net.minecraft.world.entity.player.Player player) Consume food helper.static @NotNull net.minecraft.world.item.ItemStackdeserializeFromNBT(@NotNull net.minecraft.nbt.CompoundTag compound, net.minecraft.core.HolderLookup.Provider provider) Update method to allow for easy reading the ItemStack data from NBT.static booleandoesItemServeAsWeapon(@NotNull net.minecraft.world.item.ItemStack stack) Checks if an item serves as a weapon.static intgetArmorLevel(net.minecraft.world.item.ItemStack itemStack) This routine converts thegetArmorValue(ItemStack, EquipmentSlot)} of an item stack into a given request system level, based on the standard leather - netherite armor levels.static intgetDurability(@NotNull net.minecraft.world.item.ItemStack stack) get the Durability of the stack.static intgetFortuneOf(@Nullable net.minecraft.world.item.ItemStack tool, net.minecraft.world.level.Level level) Calculates the fortune level this tool has.static doublegetItemStackAttributeValue(net.minecraft.world.item.ItemStack itemStack, net.minecraft.world.entity.EquipmentSlot equipmentSlot, net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute) Get an attribute value for a given item stack.static List<ItemStorage> getListOfStackForEntity(net.minecraft.world.entity.Entity entity, net.minecraft.world.entity.Entity placer) Adds entities to the builder building if he needs it.static intgetMaxEnchantmentLevel(net.minecraft.world.item.ItemStack itemStack) Calculates the max level enchantment this equipment has.static intgetSize(@NotNull net.minecraft.world.item.ItemStack stack) get the size of the stackstatic Optional<net.minecraft.tags.TagKey<net.minecraft.world.item.Item>> getTagEquivalent(@NotNull net.minecraft.world.item.ItemStack[] stacks) Attempts to find a tag that exactly matches the given list of item stacks.static net.minecraft.network.chat.ComponentgetTranslatedName(@NotNull net.neoforged.neoforge.common.crafting.SizedIngredient ingredient) Given anIngredient, tries to produce a reasonable friendly UI name for its contents.static booleanhasBrewableAndNoFuel(net.minecraft.world.level.block.entity.BrewingStandBlockEntity entity) Check if the brewingStand has smeltable in it and fuel empty.static booleanhasEquipmentLevel(@Nullable net.minecraft.world.item.ItemStack stack, EquipmentTypeEntry equipmentType, int minimalLevel, int maximumLevel) Verifies if there is equipment with an acceptable level in a worker's inventory.static booleanhasFuelAndNoBrewable(net.minecraft.world.level.block.entity.BrewingStandBlockEntity entity) Check if the brewingStand has fuel in it and smeltable empty.static booleanhasFuelInFurnaceAndNoSmeltable(net.minecraft.world.level.block.entity.FurnaceBlockEntity entity) Check if the furnace has fuel in it and smeltable empty.static booleanhasNeitherFuelNorBrewable(net.minecraft.world.level.block.entity.BrewingStandBlockEntity entity) Check if the brewingStand has smeltable in it and fuel empty.static booleanhasNeitherFuelNorSmeltAble(net.minecraft.world.level.block.entity.FurnaceBlockEntity entity) Check if the furnace has smeltable in it and fuel empty.static booleanhasSmeltableInFurnaceAndNoFuel(net.minecraft.world.level.block.entity.FurnaceBlockEntity entity) Check if the furnace has smeltable in it and fuel empty.static booleanhasTag(@NotNull net.minecraft.world.item.ItemStack stack) Reports if this stack has a custom Tag value that is not purely a damage value.static net.minecraft.world.item.ItemStackidToItemStack(String itemData, net.minecraft.core.HolderLookup.Provider provider) Convert an Item string with NBT to an ItemStackstatic booleanisBetterEquipment(net.minecraft.world.item.ItemStack stack1, net.minecraft.world.item.ItemStack stack2) Check if the first stack is better equipment than the second stack.static booleanisEmpty(@Nullable net.minecraft.world.item.ItemStack stack) Wrapper method to check if a stack is empty.static booleanisNotEmpty(@Nullable net.minecraft.world.item.ItemStack stack) static booleanisStackSapling(@Nullable net.minecraft.world.item.ItemStack stack) Checks if a stack is a type of sapling, using OredictparseIdTemplate(@Nullable String itemId, @NotNull net.minecraft.resources.ResourceLocation baseItemId) Parses an item id that may contain replaceable template components:static voidsetSize(@NotNull net.minecraft.world.item.ItemStack stack, int size) set the size of the stack.static net.minecraft.network.chat.MutableComponentswapArmorGrade(int toolGrade) Assigns a string containing the grade of the toolGrade.static net.minecraft.network.chat.MutableComponentswapToolGrade(int toolGrade) Assigns a string containing the grade of the armor grade.static booleanverifyEquipmentLevel(@NotNull net.minecraft.world.item.ItemStack itemStack, int equipmentLevel, int minimalLevel, int maximumLevel) Verifies if an item has an appropriated grade.
-
Field Details
-
EMPTY
public static final net.minecraft.world.item.ItemStack EMPTYVariable representing the empty itemstack in 1.10. Used for easy updating to 1.11 -
EMPTY_PREDICATE
Predicate to check if an itemStack is empty. -
NOT_EMPTY_PREDICATE
@NotNull public static final @NotNull Predicate<net.minecraft.world.item.ItemStack> NOT_EMPTY_PREDICATENegation of the itemStack empty predicate (not empty). -
CHECKED_NBT_KEYS
public static HashMap<net.minecraft.world.item.Item,Set<net.minecraft.core.component.DataComponentType<?>>> CHECKED_NBT_KEYSList of the checked nbt sets for itemstack comparisons. -
IS_ANY_FOOD
True if this stack is a standard food item (has at least some healing and some saturation, not purely for effects). -
ISFOOD
True if this stack is a standard food item that isn't excluded for colonists. -
IS_SMELTABLE
Predicate describing things which work in the furnace. -
ISCOOKABLE
Predicate describing cookables. -
IS_COMPOST
Predicate to check for compost items.
-
-
Method Details
-
getListOfStackForEntity
public static List<ItemStorage> getListOfStackForEntity(net.minecraft.world.entity.Entity entity, net.minecraft.world.entity.Entity placer) Adds entities to the builder building if he needs it.- Parameters:
entity- the entity object.placer- the entity placer.- Returns:
- a list of stacks.
-
hasEquipmentLevel
public static boolean hasEquipmentLevel(@Nullable @Nullable net.minecraft.world.item.ItemStack stack, EquipmentTypeEntry equipmentType, int minimalLevel, int maximumLevel) Verifies if there is equipment with an acceptable level in a worker's inventory.- Parameters:
stack- the stack to test.equipmentType- the type of equipment neededminimalLevel- the minimum level for the equipment to find.maximumLevel- the maximum level for the equipment to find.- Returns:
- true if equipment is acceptable
-
isEmpty
public static boolean isEmpty(@Nullable @Nullable net.minecraft.world.item.ItemStack stack) Wrapper method to check if a stack is empty. Used for easy updating to 1.11.- Parameters:
stack- The stack to check.- Returns:
- True when the stack is empty, false when not.
-
isNotEmpty
public static boolean isNotEmpty(@Nullable @Nullable net.minecraft.world.item.ItemStack stack) -
verifyEquipmentLevel
public static boolean verifyEquipmentLevel(@NotNull @NotNull net.minecraft.world.item.ItemStack itemStack, int equipmentLevel, int minimalLevel, int maximumLevel) Verifies if an item has an appropriated grade.- Parameters:
itemStack- the equipmentequipmentLevel- the equipment levelminimalLevel- the minimum level neededmaximumLevel- the maximum level needed (usually the worker's hut level)- Returns:
- true if equipment is acceptable
-
getMaxEnchantmentLevel
public static int getMaxEnchantmentLevel(net.minecraft.world.item.ItemStack itemStack) Calculates the max level enchantment this equipment has.- Parameters:
itemStack- the equipment to check.- Returns:
- max enchantment level.
-
getArmorLevel
public static int getArmorLevel(net.minecraft.world.item.ItemStack itemStack) This routine converts thegetArmorValue(ItemStack, EquipmentSlot)} of an item stack into a given request system level, based on the standard leather - netherite armor levels.- Parameters:
itemStack- the input item stack.- Returns:
- armor level
-
isBetterEquipment
public static boolean isBetterEquipment(net.minecraft.world.item.ItemStack stack1, net.minecraft.world.item.ItemStack stack2) Check if the first stack is better equipment than the second stack.- Parameters:
stack1- the first stack to check.stack2- the second to compare with.- Returns:
- true if better, false if worse or either of them are not equipment.
-
getFortuneOf
public static int getFortuneOf(@Nullable @Nullable net.minecraft.world.item.ItemStack tool, net.minecraft.world.level.Level level) Calculates the fortune level this tool has.- Parameters:
tool- the tool to check.- Returns:
- fortune level.
-
doesItemServeAsWeapon
public static boolean doesItemServeAsWeapon(@NotNull @NotNull net.minecraft.world.item.ItemStack stack) Checks if an item serves as a weapon.- Parameters:
stack- the stack to analyze.- Returns:
- true if it is a tool or sword.
-
swapArmorGrade
public static net.minecraft.network.chat.MutableComponent swapArmorGrade(int toolGrade) Assigns a string containing the grade of the toolGrade.- Parameters:
toolGrade- the number of the grade of a tool- Returns:
- a string corresponding to the tool
-
swapToolGrade
public static net.minecraft.network.chat.MutableComponent swapToolGrade(int toolGrade) Assigns a string containing the grade of the armor grade.- Parameters:
toolGrade- the number of the grade of an armor- Returns:
- a string corresponding to the armor
-
areItemStacksMergable
@NotNull public static @NotNull Boolean areItemStacksMergable(net.minecraft.world.item.ItemStack existingStack, net.minecraft.world.item.ItemStack mergingStack) Method to check if two ItemStacks can be merged together.- Parameters:
existingStack- The existing stack.mergingStack- The merging stack- Returns:
- True when they can be merged, false when not.
-
compareItemStacksIgnoreStackSize
@NotNull public static @NotNull Boolean compareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2) Method to compare to stacks, ignoring their stacksize.- Parameters:
itemStack1- The left stack to compare.itemStack2- The right stack to compare.- Returns:
- True when they are equal except the stacksize, false when not.
-
getSize
public static int getSize(@NotNull @NotNull net.minecraft.world.item.ItemStack stack) get the size of the stack- Parameters:
stack- to get the size from- Returns:
- the size of the stack
-
getDurability
public static int getDurability(@NotNull @NotNull net.minecraft.world.item.ItemStack stack) get the Durability of the stack.- Parameters:
stack- to get the size from- Returns:
- the size of the stack
-
compareItemStacksIgnoreStackSize
public static boolean compareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2, boolean matchDamage, boolean matchNBT) Method to compare to stacks, ignoring their stacksize.- Parameters:
itemStack1- The left stack to compare.itemStack2- The right stack to compare.matchDamage- Set to true to match damage data.matchNBT- Set to true to match nbt- Returns:
- True when they are equal except the stacksize, false when not.
-
compareItemStacksIgnoreStackSize
public static boolean compareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2, boolean matchDamage, boolean matchNBT, boolean min) Method to compare to stacks, ignoring their stacksize.- Parameters:
itemStack1- The left stack to compare.itemStack2- The right stack to compare.matchDamage- Set to true to match damage data.matchNBT- Set to true to match nbtmin- if the count of stack2 has to be at least the same as stack1.- Returns:
- True when they are equal except the stacksize, false when not.
-
compareItemStacksIgnoreStackSize
public static boolean compareItemStacksIgnoreStackSize(net.minecraft.world.item.ItemStack itemStack1, net.minecraft.world.item.ItemStack itemStack2, boolean matchDamage, boolean matchNBT, boolean min, boolean matchNBTExactly) Method to compare to stacks, ignoring their stacksize.- Parameters:
itemStack1- The left stack to compare.itemStack2- The right stack to compare.matchDamage- Set to true to match damage data.matchNBT- Set to true to match nbtmin- if the count of stack2 has to be at least the same as stack1.- Returns:
- True when they are equal except the stacksize, false when not.
-
compareItemStackListIgnoreStackSize
public static boolean compareItemStackListIgnoreStackSize(List<net.minecraft.world.item.ItemStack> stacks, net.minecraft.world.item.ItemStack stack) Method to check if a stack is in a list of stacks.- Parameters:
stacks- the list of stacks.stack- the stack.- Returns:
- true if so.
-
compareItemStorageListIgnoreStackSize
public static boolean compareItemStorageListIgnoreStackSize(List<ItemStorage> stacks, net.minecraft.world.item.ItemStack stack) Method to check if a stack is in a list of ItemStorage items.- Parameters:
stacks- the list of stacks.stack- the stack.- Returns:
- true if so.
-
compareItemStackListIgnoreStackSize
public static boolean compareItemStackListIgnoreStackSize(List<net.minecraft.world.item.ItemStack> stacks, net.minecraft.world.item.ItemStack stack, boolean matchDamage, boolean matchNBT) Method to check if a stack is in a list of stacks.- Parameters:
stacks- the list of stacks.stack- the stack.matchDamage- if damage has to match.matchNBT- if nbt has to match.- Returns:
- true if so.
-
setSize
public static void setSize(@NotNull @NotNull net.minecraft.world.item.ItemStack stack, int size) set the size of the stack. This is for compatibility between 1.10 and 1.11- Parameters:
stack- to set the size tosize- of the stack
-
changeSize
public static void changeSize(@NotNull @NotNull net.minecraft.world.item.ItemStack stack, int amount) Increase or decrease the stack size.- Parameters:
stack- to set the size toamount- to increase the stack's size of (negative value to decrease)
-
deserializeFromNBT
@NotNull public static @NotNull net.minecraft.world.item.ItemStack deserializeFromNBT(@NotNull @NotNull net.minecraft.nbt.CompoundTag compound, @NotNull net.minecraft.core.HolderLookup.Provider provider) Update method to allow for easy reading the ItemStack data from NBT.- Parameters:
compound- The compound to read from.- Returns:
- The ItemStack stored in the NBT Data.
-
isStackSapling
public static boolean isStackSapling(@Nullable @Nullable net.minecraft.world.item.ItemStack stack) Checks if a stack is a type of sapling, using Oredict- Parameters:
stack- the stack to check.- Returns:
- true if sapling.
-
hasSmeltableInFurnaceAndNoFuel
public static boolean hasSmeltableInFurnaceAndNoFuel(net.minecraft.world.level.block.entity.FurnaceBlockEntity entity) Check if the furnace has smeltable in it and fuel empty.- Parameters:
entity- the furnace.- Returns:
- true if so.
-
hasNeitherFuelNorSmeltAble
public static boolean hasNeitherFuelNorSmeltAble(net.minecraft.world.level.block.entity.FurnaceBlockEntity entity) Check if the furnace has smeltable in it and fuel empty.- Parameters:
entity- the furnace.- Returns:
- true if so.
-
hasFuelInFurnaceAndNoSmeltable
public static boolean hasFuelInFurnaceAndNoSmeltable(net.minecraft.world.level.block.entity.FurnaceBlockEntity entity) Check if the furnace has fuel in it and smeltable empty.- Parameters:
entity- the furnace.- Returns:
- true if so.
-
hasBrewableAndNoFuel
public static boolean hasBrewableAndNoFuel(net.minecraft.world.level.block.entity.BrewingStandBlockEntity entity) Check if the brewingStand has smeltable in it and fuel empty.- Parameters:
entity- the brewingStand.- Returns:
- true if so.
-
hasNeitherFuelNorBrewable
public static boolean hasNeitherFuelNorBrewable(net.minecraft.world.level.block.entity.BrewingStandBlockEntity entity) Check if the brewingStand has smeltable in it and fuel empty.- Parameters:
entity- the brewingStand.- Returns:
- true if so.
-
hasFuelAndNoBrewable
public static boolean hasFuelAndNoBrewable(net.minecraft.world.level.block.entity.BrewingStandBlockEntity entity) Check if the brewingStand has fuel in it and smeltable empty.- Parameters:
entity- the brewingStand.- Returns:
- true if so.
-
idToItemStack
public static net.minecraft.world.item.ItemStack idToItemStack(String itemData, net.minecraft.core.HolderLookup.Provider provider) Convert an Item string with NBT to an ItemStack- Parameters:
itemData- ie: minecraft:potion{"minecraft:potion_contents":{"potion":"minecraft:water"}}- Returns:
- stack with any defined NBT
-
parseIdTemplate
@NotNull public static @NotNull Tuple<Boolean,String> parseIdTemplate(@Nullable @Nullable String itemId, @NotNull @NotNull net.minecraft.resources.ResourceLocation baseItemId) Parses an item id that may contain replaceable template components:[NS] =>baseItemId.getNamespace()[PATH] =>baseItemId.getPath()[PATH:foo=bar] =>baseItemId.getPath()but with "foo" replaced with "bar"- Parameters:
itemId- the id to parsebaseItemId- the base item id to use to fill in the components- Returns:
- a tuple of (boolean, result), where the boolean is false if result didn't resolve to a valid item
-
hasTag
public static boolean hasTag(@NotNull @NotNull net.minecraft.world.item.ItemStack stack) Reports if this stack has a custom Tag value that is not purely a damage value.- Parameters:
stack- the stack to inspect- Returns:
- true if the stack has a non-damage tag value
-
allItemsPlusInventory
public static List<net.minecraft.world.item.ItemStack> allItemsPlusInventory(@NotNull @NotNull net.minecraft.world.entity.player.Player player) Obtains a list of all basic items in the game, plus any extra items present in the player's inventory (allowing for items with custom NBT, e.g. DO blocks or dyed armour).- Parameters:
player- The player whose inventory to check.- Returns:
- The set of items.
-
consumeFood
public static void consumeFood(net.minecraft.world.item.ItemStack foodStack, AbstractEntityCitizen citizen, @Nullable @Nullable net.minecraft.world.entity.player.Player player) Consume food helper.- Parameters:
foodStack- the itemstack of food.citizen- the citizen entity.player- optional player providing the food.
-
getTranslatedName
public static net.minecraft.network.chat.Component getTranslatedName(@NotNull @NotNull net.neoforged.neoforge.common.crafting.SizedIngredient ingredient) Given anIngredient, tries to produce a reasonable friendly UI name for its contents.- Parameters:
ingredient- the ingredient to check.- Returns:
- the friendly name.
-
getTagEquivalent
public static Optional<net.minecraft.tags.TagKey<net.minecraft.world.item.Item>> getTagEquivalent(@NotNull @NotNull net.minecraft.world.item.ItemStack[] stacks) Attempts to find a tag that exactly matches the given list of item stacks.- Parameters:
stacks- a list of item stacks.- Returns:
- a tag that seems to match it, if found.
-
getItemStackAttributeValue
public static double getItemStackAttributeValue(net.minecraft.world.item.ItemStack itemStack, net.minecraft.world.entity.EquipmentSlot equipmentSlot, net.minecraft.core.Holder<net.minecraft.world.entity.ai.attributes.Attribute> attribute) Get an attribute value for a given item stack.- Parameters:
itemStack- the input item stack.equipmentSlot- the equipment slot the item goes in.attribute- the attribute to get the value for.- Returns:
- the computed value of the attribute with all modifiers.
-