-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeItemsEmerald.java
260 lines (199 loc) · 6.65 KB
/
PipeItemsEmerald.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.pipes;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftTransport;
import buildcraft.core.GuiIds;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.inventory.InventoryWrapper;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.network.IClientState;
import buildcraft.core.network.IGuiReturnHandler;
import buildcraft.core.utils.Utils;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.PipeIconProvider;
public class PipeItemsEmerald extends PipeItemsWood implements IClientState, IGuiReturnHandler {
public enum FilterMode {
WHITE_LIST, BLACK_LIST, ROUND_ROBIN;
}
public class EmeraldPipeSettings {
private FilterMode filterMode;
public EmeraldPipeSettings() {
filterMode = FilterMode.WHITE_LIST;
}
public FilterMode getFilterMode() {
return filterMode;
}
public void setFilterMode(FilterMode mode) {
filterMode = mode;
}
public void readFromNBT(NBTTagCompound nbt) {
filterMode = FilterMode.values()[nbt.getByte("filterMode")];
}
public void writeToNBT(NBTTagCompound nbt) {
nbt.setByte("filterMode", (byte) filterMode.ordinal());
}
}
private EmeraldPipeSettings settings = new EmeraldPipeSettings();
private final SimpleInventory filters = new SimpleInventory(9, "Filters", 1);
private int currentFilter = 0;
public PipeItemsEmerald(Item item) {
super(item);
standardIconIndex = PipeIconProvider.TYPE.PipeItemsEmerald_Standard.ordinal();
solidIconIndex = PipeIconProvider.TYPE.PipeAllEmerald_Solid.ordinal();
}
@Override
public boolean blockActivated(EntityPlayer entityplayer) {
if (entityplayer.getCurrentEquippedItem() != null) {
if (Block.getBlockFromItem(entityplayer.getCurrentEquippedItem().getItem()) instanceof BlockGenericPipe) {
return false;
}
}
if (super.blockActivated(entityplayer)) {
return true;
}
if (!container.getWorldObj().isRemote) {
entityplayer.openGui(BuildCraftTransport.instance, GuiIds.PIPE_EMERALD_ITEM, container.getWorldObj(), container.xCoord, container.yCoord, container.zCoord);
}
return true;
}
@Override
public ItemStack[] checkExtract(IInventory inventory, boolean doRemove, ForgeDirection from) {
if (inventory == null) {
return null;
}
// Handle possible double chests and wrap it in the ISidedInventory interface.
ISidedInventory sidedInventory = InventoryWrapper.getWrappedInventory(InvUtils.getInventory(inventory));
if (settings.getFilterMode() == FilterMode.ROUND_ROBIN) {
return checkExtractRoundRobin(sidedInventory, doRemove, from);
}
return checkExtractFiltered(sidedInventory, doRemove, from);
}
private ItemStack[] checkExtractFiltered(ISidedInventory inventory, boolean doRemove, ForgeDirection from) {
for (int k : inventory.getAccessibleSlotsFromSide(from.ordinal())) {
ItemStack stack = inventory.getStackInSlot(k);
if (stack == null || stack.stackSize <= 0) {
continue;
}
if (!inventory.canExtractItem(k, stack, from.ordinal())) {
continue;
}
boolean matches = isFiltered(stack);
boolean isBlackList = settings.getFilterMode() == FilterMode.BLACK_LIST;
if ((isBlackList && matches) || (!isBlackList && !matches)) {
continue;
}
if (doRemove) {
int stackSize = (int) Math.floor(battery.useEnergy(10, 10 * stack.stackSize, false) / 10);
stack = inventory.decrStackSize(k, stackSize);
}
return new ItemStack[] {stack};
}
return null;
}
private ItemStack[] checkExtractRoundRobin(ISidedInventory inventory, boolean doRemove, ForgeDirection from) {
for (int i : inventory.getAccessibleSlotsFromSide(from.ordinal())) {
ItemStack stack = inventory.getStackInSlot(i);
if (stack != null && stack.stackSize > 0) {
ItemStack filter = getCurrentFilter();
if (filter == null) {
return null;
}
if (!StackHelper.isMatchingItemOrList(filter, stack)) {
continue;
}
if (!inventory.canExtractItem(i, stack, from.ordinal())) {
continue;
}
if (doRemove) {
// In Round Robin mode, extract only 1 item regardless of power level.
stack = inventory.decrStackSize(i, 1);
incrementFilter();
}
return new ItemStack[]{ stack };
}
}
return null;
}
private boolean isFiltered(ItemStack stack) {
for (int i = 0; i < filters.getSizeInventory(); i++) {
ItemStack filter = filters.getStackInSlot(i);
if (filter == null) {
return false;
}
if (StackHelper.isMatchingItemOrList(filter, stack)) {
return true;
}
}
return false;
}
private void incrementFilter() {
currentFilter++;
int count = 0;
while (filters.getStackInSlot(currentFilter % filters.getSizeInventory()) == null && count < filters.getSizeInventory()) {
currentFilter++;
count++;
}
}
private ItemStack getCurrentFilter() {
ItemStack filter = filters.getStackInSlot(currentFilter % filters.getSizeInventory());
if (filter == null) {
incrementFilter();
}
return filters.getStackInSlot(currentFilter % filters.getSizeInventory());
}
public IInventory getFilters() {
return filters;
}
public EmeraldPipeSettings getSettings() {
return settings;
}
@Override
public void writeData(ByteBuf data) {
NBTTagCompound nbt = new NBTTagCompound();
writeToNBT(nbt);
Utils.writeNBT(data, nbt);
}
@Override
public void readData(ByteBuf data) {
NBTTagCompound nbt = Utils.readNBT(data);
readFromNBT(nbt);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
filters.readFromNBT(nbt);
settings.readFromNBT(nbt);
currentFilter = nbt.getInteger("currentFilter");
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
filters.writeToNBT(nbt);
settings.writeToNBT(nbt);
nbt.setInteger("currentFilter", currentFilter);
}
@Override
public void writeGuiData(ByteBuf data) {
data.writeByte((byte) settings.getFilterMode().ordinal());
}
@Override
public void readGuiData(ByteBuf data, EntityPlayer sender) {
settings.setFilterMode(FilterMode.values()[data.readByte()]);
}
}