blob: 27d936efece4b9d8020b1634ac26133fd00c4b8e (
plain)
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
|
package kekztech;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.ItemStack;
public class MultiItemHandler {
private int perTypeCapacity = 0;
private boolean locked = true;
private ItemStack[] items;
public MultiItemHandler() {
}
public List<String> debugPrint() {
if(items == null) {
return new ArrayList<String>();
}
final ArrayList<String> slots = new ArrayList<>();
for(int i = 0; i < items.length; i++) {
slots.add("Slot " + i + " contains " + items[i].stackSize + " " + items[i].getDisplayName());
}
return slots;
}
/**
* Adapts the internal storage to structure changes.
* In the event of structure down-sizing, all excess items
* will be dropped on the ground.
*
* @param itemTypeCapacity
*/
public void setItemTypeCapacity(int itemTypeCapacity) {
System.out.println("Configuring type capacity");
if(items.length > itemTypeCapacity) {
// Generate new smaller backing array
final ItemStack[] newItems = new ItemStack[itemTypeCapacity];
for(int i = 0; i < newItems.length; i++) {
newItems[i] = items[i];
}
// Sort out item overflow
final ItemStack[] toDrop = new ItemStack[items.length - itemTypeCapacity];
for(int i = 0; i < toDrop.length; i++) {
toDrop[i] = items[i + newItems.length - 1];
}
// TODO drop overflow items to the ground
// Swap array
items = newItems;
} else {
// Generate new larger backing array
final ItemStack[] newItems = new ItemStack[itemTypeCapacity];
for(int i = 0; i < items.length; i++) {
newItems[i] = items[i];
}
// Swap array
items = newItems;
}
}
public void setPerTypeCapacity(int perTypeCapacity) {
this.perTypeCapacity = perTypeCapacity;
}
/**
* Lock internal storage in case Item Server is not running.
*
* @param state
* Lock state.
*/
public void setLock(boolean state) {
locked = state;
}
public int getItemTypeCapacity() {
return items != null ? items.length : 0;
}
public int getPerTypeCapacity() {
return perTypeCapacity;
}
/**
* Returns the ItemStack from the specified slot.
*
* @param slot
* Storage slot number. Zero indexed.
* @return
* ItemStack from storage or null if
* storage is locked or invalid slot parameter.
*/
public ItemStack getStackInSlot(int slot) {
System.out.println("Stack in slot " + slot + " requested");
if(locked || slot >= items.length) {
return null;
} else {
return items[slot];
}
}
/**
* Inserts a new ItemStack into storage,
* but only if the slot is still unassigned.
*
* @param slot
* Storage slot number. Zero indexed.
* @param itemStack
* ItemStack to insert.
* @return
* Operation success state.
*/
public boolean insertStackInSlot(int slot, ItemStack itemStack) {
System.out.println("Inserting " + itemStack.getDisplayName() + " into " + slot);
if(itemStack == null
|| items[slot] != null
|| locked
|| slot >= items.length) {
return false;
} else {
items[slot] = itemStack;
return true;
}
}
/**
* Tries to increase the item amount in a specified slot.
*
* @param slot
* Storage slot number. Zero indexed.
* @param amount
* Amount to increase by.
* @return
* Actual amount the item amount was increased by.
*/
public int increaseStackInSlot(int slot, int amount) {
System.out.println("Increasing item in slot " + slot + " by " + amount);
if(slot >= items.length
|| locked
|| amount <= 0) {
return 0;
} else {
final int space = perTypeCapacity - items[slot].stackSize;
final int fit = Math.min(space, amount);
items[slot].stackSize += fit;
return fit;
}
}
/**
* Tries to reduce the item amount in a specified slot.
*
* @param slot
* Storage slot number. Zero indexed.
* @param amount
* Amount to decrease by.
* @return
* Actual amount the item amount was decreased by.
*/
public int reduceStackInSlot(int slot, int amount) {
System.out.println("Reducing item in slot " + slot + " by " + amount);
if(slot >= items.length
|| locked
|| amount <= 0) {
return 0;
} else {
final int available = items[slot].stackSize;
final int take = Math.min(available, amount);
items[slot].stackSize -= take;
if(take == available) {
items[slot] = null;
}
return take;
}
}
}
|