Package ch.njol.skript.aliases
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface ch.njol.skript.util.Container
Container.ContainerType
Nested classes/interfaces inherited from interface ch.njol.yggdrasil.YggdrasilSerializable
YggdrasilSerializable.YggdrasilExtendedSerializable, YggdrasilSerializable.YggdrasilRobustEnum, YggdrasilSerializable.YggdrasilRobustSerializable
-
Constructor Summary
ConstructorDescriptionItemType()
ItemType
(org.bukkit.block.Block block) ItemType
(org.bukkit.block.BlockState blockState) Deprecated.ItemType
(org.bukkit.block.data.BlockData blockData) ItemType
(org.bukkit.inventory.ItemStack i) ItemType
(org.bukkit.Material id) ItemType
(org.bukkit.Material... ids) ItemType
(org.bukkit.Tag<org.bukkit.Material> tag) -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
addAll
(Collection<ItemData> types) void
addEnchantments
(EnchantmentType... enchantments) Adds the given enchantments to the item type.void
addEnchantments
(Map<org.bukkit.enchantments.Enchantment, Integer> enchantments) Deprecated.void
Adds this ItemType to the given list, without filling existing stacks.@Nullable org.bukkit.inventory.ItemStack
addTo
(@Nullable org.bukkit.inventory.ItemStack item) Adds this ItemType to the given item stackboolean
addTo
(org.bukkit.inventory.Inventory invi) Tries to add this ItemType to the given inventory.boolean
addTo
(org.bukkit.inventory.ItemStack[] buf) void
Clears all enchantments from this item type except the ones that are defined for individual item datas only.void
Clears item meta from this type.clone()
Iterator
<org.bukkit.inventory.ItemStack> void
deserialize
(Fields fields) Deserializes this object.boolean
Iterable
<org.bukkit.inventory.ItemStack> getAll()
Gets all ItemStacks this ItemType represents.int
Returns amount of the item in stack that this type represents.Returns a base item type of this.getBlock()
static org.bukkit.inventory.ItemStack[]
getCopiedContents
(org.bukkit.inventory.Inventory invi) Deprecated.@Nullable EnchantmentType
getEnchantmentType
(org.bukkit.enchantments.Enchantment enchantment) Gets theEnchantmentType
with the givenEnchantment
of this item type.@Nullable EnchantmentType[]
Gets all enchantments of this item.int
Only use this method if you know what you're doing.getItem()
org.bukkit.inventory.meta.ItemMeta
Gets item meta that applies to all items represented by this type.org.bukkit.Material
@Nullable org.bukkit.inventory.ItemStack
Gets raw item names ("minecraft:some_item").static org.bukkit.inventory.ItemStack[]
getStorageContents
(org.bukkit.inventory.Inventory invi) Gets copy of storage contents, i.e.getTypes()
boolean
hasAnyEnchantments
(org.bukkit.enchantments.Enchantment... enchantments) Checks whether this item type contains at most one of the given enchantments.boolean
hasBlock()
boolean
Checks whether this item type has enchantments.boolean
hasEnchantments
(EnchantmentType... enchantments) Checks whether this item type contains the given enchantments.boolean
hasEnchantments
(org.bukkit.enchantments.Enchantment... enchantments) Checks whether this item type has the given enchantments.int
hashCode()
boolean
hasItem()
boolean
hasSpace
(org.bukkit.inventory.Inventory invi) Test whether this ItemType can be put into the given inventory completely.boolean
hasType()
Useful for checking if materials represent an item or a block.@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.boolean
isAll()
Checks if this item type represents one of its items (OR) or all of them (AND).boolean
isContainedIn
(Iterable<org.bukkit.inventory.ItemStack> items) boolean
isContainedIn
(org.bukkit.inventory.ItemStack[] items) boolean
boolean
isOfType
(@Nullable org.bukkit.block.Block block) boolean
isOfType
(@Nullable org.bukkit.block.BlockState blockState) Deprecated.UseisOfType(BlockData)
insteadboolean
isOfType
(@Nullable org.bukkit.block.data.BlockData blockData) boolean
isOfType
(@Nullable org.bukkit.inventory.ItemStack item) boolean
isOfType
(org.bukkit.Material id) boolean
boolean
Compares two ItemTypes, ignoring stack size.static boolean
Tests whether a given set of ItemTypes is a subset of another set of ItemTypes.boolean
isSupertypeOf
(ItemType other) Checks if this type represents all the items represented by given item type.iterator()
void
modified()
Removes the item and block aliases from this alias as it now represents a different item.int
numItems()
int
numTypes()
void
final boolean
final boolean
@Nullable org.bukkit.inventory.ItemStack
removeAll
(@Nullable org.bukkit.inventory.ItemStack item) boolean
removeAll
(org.bukkit.inventory.Inventory invi) void
removeEnchantments
(EnchantmentType... enchantments) Removes the given enchantments from this item type.final boolean
removeFrom
(boolean replaceWithNull, List<org.bukkit.inventory.ItemStack>... lists) Removes this ItemType from given lists of ItemStacks.final boolean
removeFrom
(List<org.bukkit.inventory.ItemStack>... lists) Removes this ItemType from given lists of ItemStacks.@Nullable org.bukkit.inventory.ItemStack
removeFrom
(@Nullable org.bukkit.inventory.ItemStack item) Removes this type from the item stack if appropriateboolean
removeFrom
(org.bukkit.inventory.Inventory invi) Removes this type from the given inventory.void
sendBlockChange
(org.bukkit.entity.Player player, org.bukkit.Location location) Send a block change to a playerSerialises this object.void
setAll
(boolean all) void
setAmount
(double amount) void
setAmount
(int amount) boolean
setBlock
(org.bukkit.block.Block block, boolean applyPhysics) Sets the given block to this ItemTypevoid
setItemMeta
(org.bukkit.inventory.meta.ItemMeta meta) Sets item meta that is applied for everything this type represents.void
toString()
toString
(int flags) static String
toString
(org.bukkit.block.Block b, int flags) static String
toString
(org.bukkit.inventory.ItemStack i) static String
toString
(org.bukkit.inventory.ItemStack i, int flags) Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ItemType
public ItemType() -
ItemType
public ItemType(org.bukkit.Material id) -
ItemType
public ItemType(org.bukkit.Material... ids) -
ItemType
public ItemType(org.bukkit.Tag<org.bukkit.Material> tag) -
ItemType
-
ItemType
-
ItemType
public ItemType(org.bukkit.inventory.ItemStack i) -
ItemType
Deprecated.UseItemType(BlockData)
instead -
ItemType
public ItemType(org.bukkit.block.data.BlockData blockData) -
ItemType
public ItemType(org.bukkit.block.Block block)
-
-
Method Details
-
setTo
-
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. -
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) -
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 @Nullable org.bukkit.inventory.ItemStack item) -
isOfType
Deprecated.UseisOfType(BlockData)
instead -
isOfType
public boolean isOfType(@Nullable @Nullable org.bukkit.block.data.BlockData blockData) -
isOfType
public boolean isOfType(@Nullable @Nullable org.bukkit.block.Block block) -
isOfType
-
isOfType
-
isOfType
public boolean isOfType(org.bukkit.Material id) -
isSupertypeOf
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
-
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
-
hasType
public boolean hasType()Useful for checking if materials represent an item or a block. Materials that are not items don't have ItemData- Returns:
- Whether this ItemType has at least one ItemData that represents it whether it's a block or an item
-
setBlock
public boolean setBlock(org.bukkit.block.Block block, boolean applyPhysics) Sets the given block to this ItemType- Parameters:
block
- The block to setapplyPhysics
- 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 playerThis will send a fake block change to the player, and will not change the block on the server.
- Parameters:
player
- Player to send change tolocation
- Location of block to change
-
intersection
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
- Parameters:
type
- Some ItemData. Only a copy of it will be stored.
-
addAll
-
remove
-
containerIterator
- Specified by:
containerIterator
in interfaceContainer<org.bukkit.inventory.ItemStack>
- Returns:
- All element within this container in no particular order
-
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
@Nullable public @Nullable org.bukkit.inventory.ItemStack removeAll(@Nullable @Nullable org.bukkit.inventory.ItemStack item) -
removeFrom
@Nullable public @Nullable org.bukkit.inventory.ItemStack removeFrom(@Nullable @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 invalid input: '<'= 0
-
addTo
@Nullable public @Nullable org.bukkit.inventory.ItemStack addTo(@Nullable @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
-
getRandom
@Nullable public @Nullable org.bukkit.inventory.ItemStack getRandom()- Returns:
- One random ItemStack that this ItemType represents. If you have a List or an Inventory, use
addTo(Inventory)
oraddTo(List)
respectively. - See Also:
-
getRandomStackOrMaterial
- Returns:
- One random ItemStack or Material that this ItemType represents.
A Material may only be returned for ItemStacks containing a Material where
Material.isItem()
is false.
-
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
- Returns:
- List of ItemDatas. The returned list is not modifiable, use
add(ItemData)
andremove(ItemData)
if you need to change the list, or use theiterator()
.
-
numTypes
public int numTypes() -
numItems
public int numItems()- Returns:
- How many different items this item type represents
-
iterator
-
isContainedIn
-
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
-
removeAll
@SafeVarargs public final boolean removeAll(boolean replaceWithNull, List<org.bukkit.inventory.ItemStack>... lists) -
removeFrom
Removes this ItemType from given lists of ItemStacks. If an ItemStack is completely removed, that index in the list is set to null, instead of being removed.- Parameters:
lists
- The lists to remove this type from. Each list should implementRandomAccess
. Lists may contain null values after this method.- Returns:
- Whether this whole item type could be removed (i.e. returns false if the lists didn't contain this item type completely)
-
removeFrom
@SafeVarargs public final boolean removeFrom(boolean replaceWithNull, List<org.bukkit.inventory.ItemStack>... lists) Removes this ItemType from given lists of ItemStacks. If replaceWithNull is true, then if an ItemStack is completely removed, that index in the list is set to null, instead of being removed.- Parameters:
replaceWithNull
- Whether to replace removed ItemStacks with null, or to remove them completelylists
- The lists to remove this type from. Each list should implementRandomAccess
. Lists may contain null values after this method if replaceWithNull is true.- Returns:
- Whether this whole item type could be removed (i.e. returns false if the lists didn't contain this item type completely)
-
addTo
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
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
-
isSimilar
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 theMatchQuality
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() -
toString
-
toString
-
toString
-
toString
-
toString
-
toString
-
getDebugMessage
-
serialize
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 interfaceYggdrasilSerializable.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 serializable
-
deserialize
Description copied from interface:YggdrasilSerializable.YggdrasilExtendedSerializable
Deserializes 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 interfaceYggdrasilSerializable.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 byYggdrasilSerializable.YggdrasilExtendedSerializable.serialize()
or Yggdrasil's default serialisation.NotSerializableException
-
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 @Nullable 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) Deprecated.Adds enchantments to this item type.- Parameters:
enchantments
- Enchantments.
-
getEnchantmentTypes
Gets all enchantments of this item.- Returns:
- the enchantments of this item type.
-
getEnchantmentType
@Nullable public @Nullable EnchantmentType getEnchantmentType(org.bukkit.enchantments.Enchantment enchantment) Gets theEnchantmentType
with the givenEnchantment
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
Checks whether this item type contains the given enchantments. Also checks the enchantment level.- Parameters:
enchantments
- The enchantments to be checked.
-
addEnchantments
Adds the given enchantments to the item type.- Parameters:
enchantments
- The enchantments to be added.
-
removeEnchantments
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()- Returns:
- A random Material this ItemType represents.
-
getBaseType
Returns a base item type of this. Essentially, this callsItemData.aliasCopy()
on all datas and creates a new type containing the results.- Returns:
- Base item type.
-
ItemType(BlockData)
instead