|
| 1 | +/* |
| 2 | + * This file is part of SpongeAPI, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.api.item.inventory; |
| 26 | + |
| 27 | +import net.kyori.adventure.text.ComponentLike; |
| 28 | +import net.kyori.adventure.text.event.HoverEvent; |
| 29 | +import net.kyori.adventure.text.event.HoverEventSource; |
| 30 | +import org.spongepowered.api.data.Key; |
| 31 | +import org.spongepowered.api.data.Keys; |
| 32 | +import org.spongepowered.api.data.SerializableDataHolder; |
| 33 | +import org.spongepowered.api.data.value.Value; |
| 34 | +import org.spongepowered.api.data.value.ValueContainer; |
| 35 | +import org.spongepowered.api.entity.attribute.AttributeModifier; |
| 36 | +import org.spongepowered.api.entity.attribute.type.AttributeType; |
| 37 | +import org.spongepowered.api.item.ItemType; |
| 38 | +import org.spongepowered.api.item.inventory.equipment.EquipmentType; |
| 39 | +import org.spongepowered.api.registry.DefaultedRegistryReference; |
| 40 | + |
| 41 | +import java.util.Collection; |
| 42 | +import java.util.function.Supplier; |
| 43 | + |
| 44 | +/** |
| 45 | + * Represents a stack of a specific {@link ItemType}. Supports serialization. |
| 46 | + * |
| 47 | + * <p>{@link ItemStackLike} have a variety of properties and data. It is advised to |
| 48 | + * use {@link ValueContainer#get(Key)} in order to retrieve information regarding |
| 49 | + * this item stack.</p> |
| 50 | + */ |
| 51 | +public interface ItemStackLike extends SerializableDataHolder, ComponentLike, HoverEventSource<HoverEvent.ShowItem> { |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the {@link ItemType} of this {@link ItemStackLike}. |
| 55 | + * |
| 56 | + * @return The item type |
| 57 | + */ |
| 58 | + ItemType type(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Gets the quantity of items in this stack. This may exceed the max stack |
| 62 | + * size of the item, and if added to an inventory will then be divided by |
| 63 | + * the max stack. |
| 64 | + * |
| 65 | + * @return Quantity of items |
| 66 | + */ |
| 67 | + int quantity(); |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the maximum quantity per stack. By default, returns |
| 71 | + * {@link ItemType#maxStackQuantity()}, unless a |
| 72 | + * different value has been set for this specific stack. |
| 73 | + * |
| 74 | + * @return Max stack quantity |
| 75 | + */ |
| 76 | + default int maxStackQuantity() { |
| 77 | + return this.require(Keys.MAX_STACK_SIZE); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns true if {@link #quantity()} is zero and therefore this |
| 82 | + * {@link ItemStackLike} is empty. |
| 83 | + * |
| 84 | + * <p>In Vanilla empty ItemStacks are not rendered by the client.</p> |
| 85 | + * |
| 86 | + * @return True if this ItemStackLike is empty |
| 87 | + */ |
| 88 | + boolean isEmpty(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets all {@link AttributeModifier}s on this {@link ItemStackLike}. |
| 92 | + * |
| 93 | + * @param attributeType The {@link AttributeType} of the modifier. |
| 94 | + * @param equipmentType The {@link EquipmentType} this modifier is applied. |
| 95 | + * to. |
| 96 | + * |
| 97 | + * @return A collection of {@link AttributeModifier}s. |
| 98 | + */ |
| 99 | + default Collection<AttributeModifier> attributeModifiers(Supplier<? extends AttributeType> attributeType, DefaultedRegistryReference<? extends EquipmentType> equipmentType) { |
| 100 | + return this.attributeModifiers(attributeType.get(), equipmentType.get()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets all {@link AttributeModifier}s on this {@link ItemStackLike}. |
| 105 | + * |
| 106 | + * @param attributeType The {@link AttributeType} of the modifier. |
| 107 | + * @param equipmentType The {@link EquipmentType} this modifier is applied. |
| 108 | + * to. |
| 109 | + * |
| 110 | + * @return A collection of {@link AttributeModifier}s. |
| 111 | + */ |
| 112 | + default Collection<AttributeModifier> attributeModifiers(AttributeType attributeType, DefaultedRegistryReference<? extends EquipmentType> equipmentType) { |
| 113 | + return this.attributeModifiers(attributeType, equipmentType.get()); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Gets all {@link AttributeModifier}s on this {@link ItemStackLike}. |
| 118 | + * |
| 119 | + * @param attributeType The {@link AttributeType} of the modifier. |
| 120 | + * @param equipmentType The {@link EquipmentType} this modifier is applied. |
| 121 | + * to. |
| 122 | + * |
| 123 | + * @return A collection of {@link AttributeModifier}s. |
| 124 | + */ |
| 125 | + default Collection<AttributeModifier> attributeModifiers(Supplier<? extends AttributeType> attributeType, EquipmentType equipmentType) { |
| 126 | + return this.attributeModifiers(attributeType.get(), equipmentType); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Gets all {@link AttributeModifier}s on this {@link ItemStackLike}. |
| 131 | + * |
| 132 | + * @param attributeType The {@link AttributeType} of the modifier. |
| 133 | + * @param equipmentType The {@link EquipmentType} this modifier is applied. |
| 134 | + * to. |
| 135 | + * |
| 136 | + * @return A collection of {@link AttributeModifier}s. |
| 137 | + */ |
| 138 | + Collection<AttributeModifier> attributeModifiers(AttributeType attributeType, EquipmentType equipmentType); |
| 139 | + |
| 140 | + /** |
| 141 | + * Retrieves a mutable form of this {@link ItemStackLike}. If this |
| 142 | + * ItemStackLike is already mutable, this would simply return itself. |
| 143 | + * In other cases, a new {@link ItemStack} is created with all the |
| 144 | + * data currently available on this {@link ItemStackLike}. |
| 145 | + * |
| 146 | + * @return An ItemStack |
| 147 | + */ |
| 148 | + ItemStack asMutable(); |
| 149 | + |
| 150 | + /** |
| 151 | + * Retrieves a copy in the mutable form of this {@link ItemStackLike}. |
| 152 | + * The new {@link ItemStack} is created with all the data currently |
| 153 | + * available on this {@link ItemStackLike}. |
| 154 | + * |
| 155 | + * @return A new ItemStack |
| 156 | + */ |
| 157 | + ItemStack asMutableCopy(); |
| 158 | + |
| 159 | + /** |
| 160 | + * Retrieves an immutable form of this {@link ItemStackLike}. If this |
| 161 | + * ItemStackLike is already immutable, this would simply return itself. |
| 162 | + * In other cases, a new {@link ItemStackSnapshot} is created with all |
| 163 | + * known {@link Value}s existing on this {@link ItemStackLike} added |
| 164 | + * as copies to the {@link ItemStackSnapshot}. |
| 165 | + * |
| 166 | + * @return An ItemStackSnapshot |
| 167 | + */ |
| 168 | + ItemStackSnapshot asImmutable(); |
| 169 | + |
| 170 | + @Override |
| 171 | + ItemStackLike copy(); |
| 172 | +} |
0 commit comments