Class ItemType

java.lang.Object
ch.njol.skript.aliases.ItemType
All Implemented Interfaces:
Unit, Container<org.bukkit.inventory.ItemStack>, YggdrasilSerializable, YggdrasilSerializable.YggdrasilExtendedSerializable, Cloneable, Iterable<ItemData>

@ContainerType(org.bukkit.inventory.ItemStack.class) public class ItemType extends Object implements Unit, Iterable<ItemData>, Container<org.bukkit.inventory.ItemStack>, YggdrasilSerializable.YggdrasilExtendedSerializable
  • Constructor Details

    • ItemType

      public ItemType()
    • ItemType

      public ItemType(org.bukkit.Material id)
    • ItemType

      public ItemType(org.bukkit.Material id, String tags)
    • ItemType

      public ItemType(ItemData d)
    • ItemType

      public ItemType(org.bukkit.inventory.ItemStack i)
    • ItemType

      public ItemType(org.bukkit.block.BlockState b)
    • ItemType

      public ItemType(org.bukkit.block.Block block)
  • Method Details

    • setTo

      public void setTo(ItemType i)
    • modified

      public void modified()
      Removes the item and block aliases from this alias as it now represents a different item.
    • getAmount

      public int getAmount()
      Returns amount of the item in stack that this type represents.
      Specified by:
      getAmount in interface Unit
      Returns:
      amount.
    • getInternalAmount

      public int getInternalAmount()
      Only use this method if you know what you're doing.
      Returns:
      The internal amount, i.e. same as getAmount() or additive inverse number of it.
    • setAmount

      public void setAmount(double amount)
      Specified by:
      setAmount in interface Unit
    • setAmount

      public void setAmount(int amount)
    • isAll

      public boolean isAll()
      Checks if this item type represents one of its items (OR) or all of them (AND). If this has only one item, it doesn't matter.
      Returns:
      Whether all of the items are represented.
    • setAll

      public void setAll(boolean all)
    • isOfType

      public boolean isOfType(@Nullable org.bukkit.inventory.ItemStack item)
    • isOfType

      public boolean isOfType(@Nullable org.bukkit.block.BlockState block)
    • isOfType

      public boolean isOfType(@Nullable org.bukkit.block.Block block)
    • isOfType

      public boolean isOfType(ItemData type)
    • isOfType

      public boolean isOfType(org.bukkit.Material id, @Nullable String tags)
    • isOfType

      public boolean isOfType(org.bukkit.Material id)
    • isSupertypeOf

      public boolean isSupertypeOf(ItemType other)
      Checks if this type represents all the items represented by given item type. This type may of course also represent other items.
      Parameters:
      other - Another item type.
      Returns:
      Whether this is supertype of the given item type.
    • getItem

      public ItemType getItem()
    • getBlock

      public ItemType getBlock()
    • hasItem

      public boolean hasItem()
      Returns:
      Whether this ItemType has at least one ItemData that represents an item
    • hasBlock

      public boolean hasBlock()
      Returns:
      Whether this ItemType has at least one ItemData that represents a block
    • setBlock

      public boolean setBlock(org.bukkit.block.Block block, boolean applyPhysics)
      Sets the given block to this ItemType
      Parameters:
      block - The block to set
      applyPhysics - Whether to run a physics check just after setting the block
      Returns:
      Whether the block was successfully set
    • sendBlockChange

      public void sendBlockChange(org.bukkit.entity.Player player, org.bukkit.Location location)
      Send a block change to a player

      This will send a fake block change to the player, and will not change the block on the server.

      Parameters:
      player - Player to send change to
      location - Location of block to change
    • intersection

      public @Nullable ItemType intersection(ItemType other)
      Intersects all ItemDatas with all ItemDatas of the given ItemType, returning an ItemType with at most n*m ItemDatas, where n = #ItemDatas of this ItemType, and m = #ItemDatas of the argument.
      Parameters:
      other -
      Returns:
      A new item type which is the intersection of the two item types or null if the intersection is empty.
      See Also:
    • add

      public void add(@Nullable ItemData type)
      Parameters:
      type - Some ItemData. Only a copy of it will be stored.
    • addAll

      public void addAll(Collection<ItemData> types)
    • remove

      public void remove(ItemData type)
    • containerIterator

      public Iterator<org.bukkit.inventory.ItemStack> containerIterator()
      Specified by:
      containerIterator in interface Container<org.bukkit.inventory.ItemStack>
      Returns:
      All element within this container in no particular order
    • getAll

      public Iterable<org.bukkit.inventory.ItemStack> getAll()
      Gets all ItemStacks this ItemType represents. Only use this if you know what you're doing, as it returns only one element if this is not an 'every' alias.
      Returns:
      An Iterable whose iterator will always return the same item(s)
    • removeAll

      public @Nullable org.bukkit.inventory.ItemStack removeAll(@Nullable org.bukkit.inventory.ItemStack item)
    • removeFrom

      public @Nullable org.bukkit.inventory.ItemStack removeFrom(@Nullable org.bukkit.inventory.ItemStack item)
      Removes this type from the item stack if appropriate
      Parameters:
      item -
      Returns:
      The passed ItemStack or null if the resulting amount is <= 0
    • addTo

      public @Nullable org.bukkit.inventory.ItemStack addTo(@Nullable org.bukkit.inventory.ItemStack item)
      Adds this ItemType to the given item stack
      Parameters:
      item -
      Returns:
      The passed ItemStack or a new one if the passed is null or air
    • clone

      public ItemType clone()
      Specified by:
      clone in interface Unit
      Overrides:
      clone in class Object
    • getRandom

      public org.bukkit.inventory.ItemStack getRandom()
      Returns:
      One random ItemStack that this ItemType represents. If you have a List or an Inventory, use addTo(Inventory) or addTo(List) respectively.
      See Also:
    • hasSpace

      public boolean hasSpace(org.bukkit.inventory.Inventory invi)
      Test whether this ItemType can be put into the given inventory completely.

      REMIND If this ItemType represents multiple items with OR, this function will immediately return false.
      CondCanHold currently blocks aliases without 'every'/'all' as temporary solution.

      Parameters:
      invi -
      Returns:
      Whether this item type can be added to the given inventory
    • getCopiedContents

      public static org.bukkit.inventory.ItemStack[] getCopiedContents(org.bukkit.inventory.Inventory invi)
    • getStorageContents

      public static org.bukkit.inventory.ItemStack[] getStorageContents(org.bukkit.inventory.Inventory invi)
      Gets copy of storage contents, i.e. ignores armor and off hand. This is due to Spigot 1.9 added armor slots, and off hand to default inventory index.
      Parameters:
      invi - Inventory
      Returns:
      Copied storage contents
    • getTypes

      public List<ItemData> getTypes()
      Returns:
      List of ItemDatas. The returned list is not modifiable, use add(ItemData) and remove(ItemData) if you need to change the list, or use the iterator().
    • numTypes

      public int numTypes()
    • numItems

      public int numItems()
      Returns:
      How many different items this item type represents
    • iterator

      public Iterator<ItemData> iterator()
      Specified by:
      iterator in interface Iterable<ItemData>
    • isContainedIn

      public boolean isContainedIn(Iterable<org.bukkit.inventory.ItemStack> items)
    • isContainedIn

      public boolean isContainedIn(org.bukkit.inventory.ItemStack[] items)
    • removeAll

      public boolean removeAll(org.bukkit.inventory.Inventory invi)
    • removeFrom

      public boolean removeFrom(org.bukkit.inventory.Inventory invi)
      Removes this type from the given inventory. Does not call updateInventory for players.
      Parameters:
      invi -
      Returns:
      Whether everything could be removed from the inventory
    • removeAll

      @SafeVarargs public final boolean removeAll(List<org.bukkit.inventory.ItemStack>... lists)
    • removeFrom

      @SafeVarargs public final boolean removeFrom(List<org.bukkit.inventory.ItemStack>... lists)
      Parameters:
      lists - The lists to remove this type from. Each list should implement RandomAccess.
      Returns:
      Whether this whole item type could be removed (i.e. returns false if the lists didn't contain this item type completely)
    • addTo

      public void addTo(List<org.bukkit.inventory.ItemStack> list)
      Adds this ItemType to the given list, without filling existing stacks.
      Parameters:
      list -
    • addTo

      public boolean addTo(org.bukkit.inventory.Inventory invi)
      Tries to add this ItemType to the given inventory. Does not call updateInventory for players.
      Parameters:
      invi -
      Returns:
      Whether everything could be added to the inventory
    • addTo

      public boolean addTo(org.bukkit.inventory.ItemStack[] buf)
    • isSubset

      public static boolean isSubset(ItemType[] set, ItemType[] sub)
      Tests whether a given set of ItemTypes is a subset of another set of ItemTypes.

      This method works differently that normal set operations, as is e.g. returns true if set == {everything}.

      Parameters:
      set -
      sub -
      Returns:
      Whether all item types in sub have at least one super type in set
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • isSimilar

      public boolean isSimilar(ItemType other)
      Compares two ItemTypes, ignoring stack size. Please note that ItemTypes do not need to be EXACTLY the same outside of stack size for this method to return true. Several factors influence the MatchQuality required for this method to return true. For example, if the other ItemType is an alias and this one is not, the ItemTypes must only share a material. In general though, this ItemType must have all of the qualities of the other ItemType. It may have additional qualities that the other ItemType does not have though.
      Parameters:
      other - The ItemType to compare with.
      Returns:
      Whether this ItemType is similar to the other ItemType.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Specified by:
      toString in interface Unit
      Overrides:
      toString in class Object
    • toString

      public String toString(int flags)
      Specified by:
      toString in interface Unit
    • toString

      public String toString(int flags, @Nullable Adjective a)
    • toString

      public static String toString(org.bukkit.inventory.ItemStack i)
    • toString

      public static String toString(org.bukkit.inventory.ItemStack i, int flags)
    • toString

      public static String toString(org.bukkit.block.Block b, int flags)
    • getDebugMessage

      public String getDebugMessage()
    • serialize

      public Fields serialize() throws NotSerializableException
      Description copied from interface: YggdrasilSerializable.YggdrasilExtendedSerializable
      Serialises this object. Only fields contained in the returned Fields object will be written to stream.

      You can use return new Fields(this); to emulate the default behaviour.

      Specified by:
      serialize in interface YggdrasilSerializable.YggdrasilExtendedSerializable
      Returns:
      A Fields object containing all fields that should be written to stream
      Throws:
      NotSerializableException - If this object or one of its fields is not serialisable
    • deserialize

      public void deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException
      Description copied from interface: YggdrasilSerializable.YggdrasilExtendedSerializable
      Deserialises this object. No fields have been set when this method is called, use fields.setFields(this, yggdrasil) to set all compatible non-transient and non-static fields (and call incompatible/missing field handlers if applicable – this implies that errors will be thrown if the fields object is invalid).

      You can use fields.setFields(this); to emulate the default behaviour.

      Specified by:
      deserialize in interface YggdrasilSerializable.YggdrasilExtendedSerializable
      Parameters:
      fields - A Fields object containing all fields read from stream
      Throws:
      StreamCorruptedException - If the Fields object is invalid, i.e. was not written by YggdrasilSerializable.YggdrasilExtendedSerializable.serialize() or Yggrdasil's default serialisation.
      NotSerializableException
    • getRawNames

      public List<String> getRawNames()
      Gets raw item names ("minecraft:some_item"). If they are not available, empty list will be returned.
      Returns:
      names List of names that could be retrieved.
    • getEnchantments

      @Deprecated public @Nullable Map<org.bukkit.enchantments.Enchantment,Integer> getEnchantments()
      Deprecated.
      Gets all enchantments of this item.
      Returns:
      Enchantments.
    • addEnchantments

      @Deprecated public void addEnchantments(Map<org.bukkit.enchantments.Enchantment,Integer> enchantments)
      Adds enchantments to this item type.
      Parameters:
      enchantments - Enchantments.
    • getEnchantmentTypes

      public @Nullable EnchantmentType[] getEnchantmentTypes()
      Gets all enchantments of this item.
      Returns:
      the enchantments of this item type.
    • getEnchantmentType

      public @Nullable EnchantmentType getEnchantmentType(org.bukkit.enchantments.Enchantment enchantment)
      Gets the EnchantmentType with the given Enchantment of this item type.
      Parameters:
      enchantment - the enchantment
      Returns:
      the enchantment type, or null if the item is not enchanted with the given enchantment
    • hasEnchantments

      public boolean hasEnchantments()
      Checks whether this item type has enchantments.
    • hasEnchantments

      public boolean hasEnchantments(org.bukkit.enchantments.Enchantment... enchantments)
      Checks whether this item type has the given enchantments.
      Parameters:
      enchantments - the enchantments to be checked.
    • hasAnyEnchantments

      public boolean hasAnyEnchantments(org.bukkit.enchantments.Enchantment... enchantments)
      Checks whether this item type contains at most one of the given enchantments.
      Parameters:
      enchantments - The enchantments to be checked.
    • hasEnchantments

      public boolean hasEnchantments(EnchantmentType... enchantments)
      Checks whether this item type contains the given enchantments. Also checks the enchantment level.
      Parameters:
      enchantments - The enchantments to be checked.
    • addEnchantments

      public void addEnchantments(EnchantmentType... enchantments)
      Adds the given enchantments to the item type.
      Parameters:
      enchantments - The enchantments to be added.
    • removeEnchantments

      public void removeEnchantments(EnchantmentType... enchantments)
      Removes the given enchantments from this item type.
      Parameters:
      enchantments - The enchantments to be removed.
    • clearEnchantments

      public void clearEnchantments()
      Clears all enchantments from this item type except the ones that are defined for individual item datas only.
    • getItemMeta

      public org.bukkit.inventory.meta.ItemMeta getItemMeta()
      Gets item meta that applies to all items represented by this type.
      Returns:
      Item meta.
    • setItemMeta

      public void setItemMeta(org.bukkit.inventory.meta.ItemMeta meta)
      Sets item meta that is applied for everything this type represents. Note that previous item meta is overridden if it exists.
      Parameters:
      meta - New item meta.
    • clearItemMeta

      public void clearItemMeta()
      Clears item meta from this type. Metas which individual item dates may have will not be touched.
    • getMaterial

      public org.bukkit.Material getMaterial()
    • getBaseType

      public ItemType getBaseType()
      Returns a base item type of this. Essentially, this calls ItemData.aliasCopy() on all datas and creates a new type containing the results.
      Returns:
      Base item type.