-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
ItemStack.java
284 lines (252 loc) · 10.4 KB
/
ItemStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.item.inventory;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.tileentity.TileEntity;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataSerializable;
import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.manipulator.DataManipulator;
import org.spongepowered.api.data.manipulator.ImmutableDataManipulator;
import org.spongepowered.api.data.persistence.DataBuilder;
import org.spongepowered.api.data.value.BaseValue;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.text.translation.Translatable;
import org.spongepowered.api.util.ResettableBuilder;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* Represents a stack of a specific {@link ItemType}. Supports serialization and
* can be compared using the comparators listed in {@link ItemStackComparators}.
*
* <p>{@link ItemStack}s have varying properties and data, it is adviseable to
* use {@link DataHolder#get(Class)} to retrieve different information
* regarding this item stack.</p>
*/
public interface ItemStack extends DataHolder, DataSerializable, Translatable {
/**
* Creates a new {@link Builder} to build an {@link ItemStack}.
*
* @return The new builder
*/
static Builder builder() {
return Sponge.getRegistry().createBuilder(Builder.class);
}
/**
* Creates a new {@link ItemStack} of the provided {@link ItemType}
* and quantity.
*
* @param itemType The item type
* @param quantity The quantity
* @return The new item stack
*/
static ItemStack of(ItemType itemType, int quantity) {
return builder().itemType(itemType).quantity(quantity).build();
}
/**
* Gets the {@link ItemType} of this {@link ItemStack}.
*
* @return The item type
*/
ItemType getItem();
/**
* Gets the quantity of items in this stack. This may exceed the max stack
* size of the item, and if added to an inventory will then be divided by
* the max stack.
*
* @return Quantity of items
*/
int getQuantity();
/**
* Sets the quantity in this stack.
*
* @param quantity Quantity
* @throws IllegalArgumentException If quantity set exceeds the
* {@link ItemStack#getMaxStackQuantity()}
*/
void setQuantity(int quantity) throws IllegalArgumentException;
/**
* Get the maximum quantity per stack. By default, returns
* {@link ItemType#getMaxStackQuantity()}, unless a
* different value has been set for this specific stack.
*
* @return Max stack quantity
*/
int getMaxStackQuantity();
/**
* Gets the {@link ItemStackSnapshot} of this {@link ItemStack}. All known
* {@link DataManipulator}s existing on this {@link ItemStack} are added
* as copies to the {@link ItemStackSnapshot}.
*
* @return The newly created item stack snapshot
*/
ItemStackSnapshot createSnapshot();
/**
* Returns true if the specified {@link ItemStack} has the same stack
* size, {@link ItemType}, and data. Note that this method is not an
* overrider of {@link Object#equals(Object)} in order to maintain
* compatibility with the base game. Therefore, ItemStacks may not behave
* as expected when using them in equality based constructs such as
* {@link Map}s or {@link Set}s.
*
* @param that ItemStack to compare
* @return True if this equals the ItemStack
*/
boolean equalTo(ItemStack that);
@Override
ItemStack copy();
interface Builder extends DataBuilder<ItemStack> {
@Override
Builder from(ItemStack value);
/**
* Sets the {@link ItemType} of the item stack.
*
* @param itemType The type of item
* @return This builder, for chaining
*/
Builder itemType(ItemType itemType);
ItemType getCurrentItem();
/**
* Sets the quantity of the item stack.
*
* @param quantity The quantity of the item stack
* @return This builder, for chaining
* @throws IllegalArgumentException If the quantity is outside the allowed bounds
*/
Builder quantity(int quantity) throws IllegalArgumentException;
/**
* Adds a {@link Key} and related {@link Object} value to apply to the
* resulting {@link ItemStack}. Note that the resulting
* {@link ItemStack} may not actually accept the provided {@code Key}
* for various reasons due to support or simply that the value itself
* is not supported. Offering custom data is not supported through this,
* use {@link #itemData(DataManipulator)} instead.
*
* @param key The key to identiy the value to
* @param value The value to apply
* @param <E> The type of value
* @return This builder, for chaining
*/
<E> Builder keyValue(Key<? extends BaseValue<E>> key, E value);
/**
* Sets the {@link DataManipulator} to add to the {@link ItemStack}.
*
* @param itemData The item data to set
* @return This builder, for chaining
* @throws IllegalArgumentException If the item data is incompatible with the item
*/
Builder itemData(DataManipulator<?, ?> itemData) throws IllegalArgumentException;
/**
* Sets the {@link ImmutableDataManipulator} to add to the
* {@link ItemStack}.
*
* @param itemData The item data to set
* @return This builder, for chaining
* @throws IllegalArgumentException If the item data is incompatible
*/
Builder itemData(ImmutableDataManipulator<?, ?> itemData) throws IllegalArgumentException;
/**
* Adds the given {@link Key} with the given value.
*
* @param key The key to assign the value with
* @param value The value to assign with the key
* @param <V> The type of the value
* @return This builder, for chaining
* @throws IllegalArgumentException If the item data is incompatible
*/
<V> Builder add(Key<? extends BaseValue<V>> key, V value) throws IllegalArgumentException;
/**
* Sets all the settings in this builder from the item stack blueprint.
*
* @param itemStack The item stack to copy
* @return This builder, for chaining
*/
Builder fromItemStack(ItemStack itemStack);
/**
* Sets the data to recreate a {@link BlockState} in a held {@link ItemStack}
* state.
*
* @param blockState The block state to use
* @return This builder, for chaining
*/
default Builder fromBlockState(BlockState blockState) {
checkNotNull(blockState);
final BlockType blockType= blockState.getType();
checkArgument(blockType.getItem().isPresent(), "Missing valid ItemType for BlockType: " + blockType.getId());
itemType(blockType.getItem().get());
blockState.getContainers().forEach(this::itemData);
return this;
}
/**
* Attempts to reconstruct the builder with all of the data from
* {@link ItemStack#toContainer()} including all custom data.
*
* @param container The container to translate
* @return This bulder, for chaining
*/
Builder fromContainer(DataView container);
/**
* Reconstructs this builder to use the {@link ItemStackSnapshot}
* for all the values and data it may contain.
*
* @param snapshot The snapshot
* @return This builder, for chaining
*/
default Builder fromSnapshot(ItemStackSnapshot snapshot) {
return fromItemStack(snapshot.createStack());
}
/**
* Attempts to reconstruct a {@link BlockSnapshot} including all data
* and {@link TileEntity} related data if necessary for creating an
* {@link ItemStack} representation.
*
* @param blockSnapshot The snapshot to use
* @return This builder, for chaining
*/
Builder fromBlockSnapshot(BlockSnapshot blockSnapshot);
Builder remove(Class<? extends DataManipulator<?, ?>> manipulatorClass);
default Builder apply(Predicate<Builder> predicate, Consumer<Builder> consumer) {
if (predicate.test(this)) {
consumer.accept(this);
}
return this;
}
/**
* Builds an instance of an ItemStack.
*
* @return A new instance of an ItemStack
* @throws IllegalStateException If the item stack is not completed
*/
ItemStack build() throws IllegalStateException;
}
}