Skip to content

Commit cf82a24

Browse files
committed
Add ItemStackLike
1 parent 42dc372 commit cf82a24

File tree

3 files changed

+177
-145
lines changed

3 files changed

+177
-145
lines changed

src/main/java/org/spongepowered/api/item/inventory/ItemStack.java

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,19 @@
2424
*/
2525
package org.spongepowered.api.item.inventory;
2626

27-
import net.kyori.adventure.text.ComponentLike;
28-
import net.kyori.adventure.text.event.HoverEvent;
29-
import net.kyori.adventure.text.event.HoverEventSource;
3027
import org.spongepowered.api.Sponge;
3128
import org.spongepowered.api.block.BlockSnapshot;
3229
import org.spongepowered.api.block.BlockState;
3330
import org.spongepowered.api.block.entity.BlockEntity;
3431
import org.spongepowered.api.data.DataHolderBuilder;
35-
import org.spongepowered.api.data.Key;
3632
import org.spongepowered.api.data.SerializableDataHolder;
3733
import org.spongepowered.api.data.persistence.DataView;
38-
import org.spongepowered.api.data.value.Value;
39-
import org.spongepowered.api.data.value.ValueContainer;
4034
import org.spongepowered.api.entity.attribute.AttributeModifier;
4135
import org.spongepowered.api.entity.attribute.type.AttributeType;
4236
import org.spongepowered.api.item.ItemType;
4337
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
4438
import org.spongepowered.api.registry.DefaultedRegistryReference;
4539

46-
import java.util.Collection;
4740
import java.util.Map;
4841
import java.util.Objects;
4942
import java.util.Set;
@@ -52,14 +45,10 @@
5245
import java.util.function.Supplier;
5346

5447
/**
55-
* Represents a stack of a specific {@link ItemType}. Supports serialization and
56-
* can be compared using the comparators listed in {@link ItemStackComparators}.
57-
*
58-
* <p>{@link ItemStack}s have a variety of properties and data. It is advised to
59-
* use {@link ValueContainer#get(Key)} in order to retrieve information regarding
60-
* this item stack.</p>
48+
* Represents mutable {@link ItemStackLike}. Can be compared
49+
* using the comparators listed in {@link ItemStackComparators}.
6150
*/
62-
public interface ItemStack extends SerializableDataHolder.Mutable, ComponentLike, HoverEventSource<HoverEvent.ShowItem> {
51+
public interface ItemStack extends ItemStackLike, SerializableDataHolder.Mutable {
6352

6453
/**
6554
* Returns an empty {@link ItemStack}.
@@ -123,22 +112,6 @@ static ItemStack of(ItemType itemType) {
123112
return ItemStack.of(itemType, 1);
124113
}
125114

126-
/**
127-
* Gets the {@link ItemType} of this {@link ItemStack}.
128-
*
129-
* @return The item type
130-
*/
131-
ItemType type();
132-
133-
/**
134-
* Gets the quantity of items in this stack. This may exceed the max stack
135-
* size of the item, and if added to an inventory will then be divided by
136-
* the max stack.
137-
*
138-
* @return Quantity of items
139-
*/
140-
int quantity();
141-
142115
/**
143116
* Sets the quantity in this stack.
144117
*
@@ -148,24 +121,6 @@ static ItemStack of(ItemType itemType) {
148121
*/
149122
void setQuantity(int quantity) throws IllegalArgumentException;
150123

151-
/**
152-
* Gets the maximum quantity per stack. By default, returns
153-
* {@link ItemType#maxStackQuantity()}, unless a
154-
* different value has been set for this specific stack.
155-
*
156-
* @return Max stack quantity
157-
*/
158-
int maxStackQuantity();
159-
160-
/**
161-
* Gets the {@link ItemStackSnapshot} of this {@link ItemStack}. All
162-
* known {@link Value}s existing on this {@link ItemStack} are added
163-
* as copies to the {@link ItemStackSnapshot}.
164-
*
165-
* @return The newly created item stack snapshot
166-
*/
167-
ItemStackSnapshot createSnapshot();
168-
169124
/**
170125
* Returns true if the specified {@link ItemStack} has the same stack
171126
* size, {@link ItemType}, and data. Note that this method is not an
@@ -179,66 +134,6 @@ static ItemStack of(ItemType itemType) {
179134
*/
180135
boolean equalTo(ItemStack that);
181136

182-
/**
183-
* Returns true if {@link #quantity()} is zero and therefore this
184-
* ItemStack is empty.
185-
*
186-
* <p>In Vanilla empty ItemStacks are not rendered by the client.</p>
187-
*
188-
* @return True if this ItemStack is empty
189-
*/
190-
boolean isEmpty();
191-
192-
/**
193-
* Gets all {@link AttributeModifier}s on this item stack.
194-
*
195-
* @param attributeType The {@link AttributeType} of the modifier.
196-
* @param equipmentType The {@link EquipmentType} this modifier is applied
197-
* to.
198-
*
199-
* @return A collection of {@link AttributeModifier}s.
200-
*/
201-
default Collection<AttributeModifier> attributeModifiers(Supplier<? extends AttributeType> attributeType, DefaultedRegistryReference<? extends EquipmentType> equipmentType) {
202-
return this.attributeModifiers(attributeType.get(), equipmentType.get());
203-
}
204-
205-
/**
206-
* Gets all {@link AttributeModifier}s on this item stack.
207-
*
208-
* @param attributeType The {@link AttributeType} of the modifier.
209-
* @param equipmentType The {@link EquipmentType} this modifier is applied
210-
* to.
211-
*
212-
* @return A collection of {@link AttributeModifier}s.
213-
*/
214-
default Collection<AttributeModifier> attributeModifiers(AttributeType attributeType, DefaultedRegistryReference<? extends EquipmentType> equipmentType) {
215-
return this.attributeModifiers(attributeType, equipmentType.get());
216-
}
217-
218-
/**
219-
* Gets all {@link AttributeModifier}s on this item stack.
220-
*
221-
* @param attributeType The {@link AttributeType} of the modifier.
222-
* @param equipmentType The {@link EquipmentType} this modifier is applied
223-
* to.
224-
*
225-
* @return A collection of {@link AttributeModifier}s.
226-
*/
227-
default Collection<AttributeModifier> attributeModifiers(Supplier<? extends AttributeType> attributeType, EquipmentType equipmentType) {
228-
return this.attributeModifiers(attributeType.get(), equipmentType);
229-
}
230-
231-
/**
232-
* Gets all {@link AttributeModifier}s on this item stack.
233-
*
234-
* @param attributeType The {@link AttributeType} of the modifier.
235-
* @param equipmentType The {@link EquipmentType} this modifier is applied
236-
* to.
237-
*
238-
* @return A collection of {@link AttributeModifier}s.
239-
*/
240-
Collection<AttributeModifier> attributeModifiers(AttributeType attributeType, EquipmentType equipmentType);
241-
242137
/**
243138
* Adds an {@link AttributeModifier} to this item stack.
244139
*
@@ -386,7 +281,7 @@ default Builder fromBlockState(final Supplier<? extends BlockState> blockState)
386281
* @return This builder, for chaining
387282
*/
388283
default Builder fromSnapshot(final ItemStackSnapshot snapshot) {
389-
return this.fromItemStack(snapshot.createStack());
284+
return this.fromItemStack(snapshot.asMutable());
390285
}
391286

392287
/**
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)