-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
MinecraftInventoryAdapter.java
268 lines (228 loc) · 9.12 KB
/
MinecraftInventoryAdapter.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
/*
* This file is part of Sponge, 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.common.item.inventory.adapter.impl;
import net.minecraft.inventory.IInventory;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryArchetype;
import org.spongepowered.api.item.inventory.InventoryArchetypes;
import org.spongepowered.api.item.inventory.InventoryProperty;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.property.AbstractInventoryProperty;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
import org.spongepowered.api.text.translation.Translation;
import org.spongepowered.common.item.inventory.InventoryIterator;
import org.spongepowered.common.item.inventory.adapter.InventoryAdapter;
import org.spongepowered.common.item.inventory.custom.CustomInventory;
import org.spongepowered.common.item.inventory.query.Query;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Optional;
import java.util.stream.Collectors;
public interface MinecraftInventoryAdapter extends InventoryAdapter<IInventory, net.minecraft.item.ItemStack> {
@Override
default Translation getName() {
return this.getRootLens().getName(this.getInventory());
}
@Override
default Inventory root() {
return this.parent() == this ? this : this.parent().root();
}
@Override
default Optional<ItemStack> poll() {
return Adapter.Logic.pollSequential(this);
}
@Override
default Optional<ItemStack> poll(int limit) {
return Adapter.Logic.pollSequential(this, limit);
}
@Override
default Optional<ItemStack> peek() {
return Adapter.Logic.peekSequential(this);
}
@Override
default Optional<ItemStack> peek(int limit) {
return Adapter.Logic.peekSequential(this, limit);
}
@Override
default InventoryTransactionResult offer(ItemStack stack) {
// try {
return Adapter.Logic.appendSequential(this, stack);
// } catch (Exception ex) {
// return false;
// }
}
@Override
default InventoryTransactionResult set(ItemStack stack) {
return Adapter.Logic.insertSequential(this, stack);
}
@Override
default int size() {
return Adapter.Logic.countStacks(this);
}
@Override
default int totalItems() {
return Adapter.Logic.countItems(this);
}
@Override
default int capacity() {
return Adapter.Logic.getCapacity(this);
}
@Override
default boolean hasChildren() {
return this.getRootLens().getChildren().size() != 0;
}
@Override
default boolean contains(ItemStack stack) {
return Adapter.Logic.contains(this, stack);
}
@Override
default boolean containsAny(ItemStack stack) {
return Adapter.Logic.contains(this, stack, 1);
}
@Override
default boolean contains(ItemType type) {
return Adapter.Logic.contains(this, type);
}
@Override
default int getMaxStackSize() {
return this.getRootLens().getMaxStackSize(this.getInventory());
}
@Override
default void setMaxStackSize(int size) {
throw new UnsupportedOperationException("This inventory does not support stack limit adjustment");
}
@SuppressWarnings("unchecked")
@Override
default <T extends InventoryProperty<?, ?>> Collection<T> getProperties(Inventory child, Class<T> property) {
return (Collection<T>) Adapter.Logic.getProperties(this, child, property);
}
@Override
default <T extends InventoryProperty<?, ?>> Collection<T> getProperties(Class<T> property) {
if (this.parent() == this) {
if (this instanceof CustomInventory) {
return ((CustomInventory) this).getProperties().values().stream().filter(p -> property.equals(p.getClass()))
.map(p -> property.cast(p)).collect(Collectors.toList());
}
return Collections.emptyList(); // TODO properties of top level inventory?
}
return this.parent().getProperties(this, property);
}
@SuppressWarnings("unchecked")
@Override
default <T extends InventoryProperty<?, ?>> Optional<T> getProperty(Inventory child, Class<T> property, Object key) {
for (InventoryProperty<?, ?> prop : Adapter.Logic.getProperties(this, child, property)) {
if (key.equals(prop.getKey())) {
return Optional.of((T)prop);
}
}
return Optional.empty();
}
@Override
default <T extends InventoryProperty<?, ?>> Optional<T> getProperty(Class<T> property, Object key) {
if (this.parent() == this) {
if (this instanceof CustomInventory) {
InventoryProperty forKey = ((CustomInventory) this).getProperties().get(key);
if (property.equals(forKey.getClass())) {
return Optional.of((T) forKey);
}
}
return Optional.empty(); // TODO properties of top level inventory?
}
return this.parent().getProperty(this, property, key);
}
@Override
default <T extends InventoryProperty<?, ?>> Optional<T> getInventoryProperty(Inventory child, Class<T> property) {
Object key = AbstractInventoryProperty.getDefaultKey(property);
return this.getProperty(child, property, key);
}
@Override
default <T extends InventoryProperty<?, ?>> Optional<T> getInventoryProperty(Class<T> property) {
Object key = AbstractInventoryProperty.getDefaultKey(property);
return this.getProperty(property, key);
}
@Override
default Iterator<Inventory> iterator() {
return new InventoryIterator<>(this.getRootLens(), this.getInventory(), this);
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(Class<?>... types) {
return (T) Query.compile(this, types).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(ItemType... types) {
return (T) Query.compile(this, types).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(ItemStack... types) {
return (T) Query.compileExact(this, types).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T queryAny(ItemStack... types) {
return (T) Query.compile(this, types).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(InventoryProperty<?, ?>... props) {
return (T) Query.compile(this, props).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(Translation... names) {
return (T) Query.compile(this, names).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(String... args) {
return (T) Query.compile(this, args).execute();
}
@SuppressWarnings("unchecked")
@Override
default <T extends Inventory> T query(Object... args) {
return (T) Query.compile(this, args).execute();
}
@Override
default Inventory intersect(Inventory inventory) {
return Query.intersect(this, inventory).execute();
}
@Override
default Inventory union(Inventory inventory) {
return Query.union(this, inventory).execute();
}
@Override
default boolean containsInventory(Inventory inventory) {
Inventory result = Query.compile(this, ((InventoryAdapter) inventory).getRootLens()).execute();
return result.capacity() == inventory.capacity() && ((InventoryAdapter) result).getRootLens() == ((InventoryAdapter) inventory).getRootLens();
}
@Override
default InventoryArchetype getArchetype() {
return InventoryArchetypes.UNKNOWN;
}
}