-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeItemsEmzuli.java
226 lines (191 loc) · 6.33 KB
/
PipeItemsEmzuli.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
/**
* Copyright (c) 2011-2015, 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 java.util.BitSet;
import java.util.Collection;
import java.util.LinkedList;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
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.api.core.EnumColor;
import buildcraft.api.statements.IActionInternal;
import buildcraft.api.statements.StatementSlot;
import buildcraft.core.GuiIds;
import buildcraft.core.lib.inventory.InvUtils;
import buildcraft.core.lib.inventory.SimpleInventory;
import buildcraft.core.lib.network.IGuiReturnHandler;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.PipeIconProvider;
import buildcraft.transport.TravelingItem;
import buildcraft.transport.statements.ActionExtractionPreset;
public class PipeItemsEmzuli extends PipeItemsWood implements IGuiReturnHandler {
public final byte[] slotColors = new byte[4];
private final SimpleInventory filters = new SimpleInventory(4, "Filters", 1);
private final BitSet activeFlags = new BitSet(4);
private final int filterCount = filters.getSizeInventory();
private int currentFilter = 0;
public PipeItemsEmzuli(Item item) {
super(item);
standardIconIndex = PipeIconProvider.TYPE.PipeItemsEmzuli_Standard.ordinal();
solidIconIndex = PipeIconProvider.TYPE.PipeAllEmzuli_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_LOGEMERALD_ITEM, container.getWorldObj(), container.xCoord, container.yCoord, container.zCoord);
}
return true;
}
@Override
protected TravelingItem makeItem(double x, double y, double z, ItemStack stack) {
TravelingItem item = super.makeItem(x, y, z, stack);
int color = slotColors[currentFilter % filterCount];
if (color > 0) {
item.color = EnumColor.fromId(color - 1);
}
return item;
}
/**
* Return the itemstack that can be if something can be extracted from this
* inventory, null if none. On certain cases, the extractable slot depends
* on the position of the pipe.
*/
@Override
public ItemStack[] checkExtract(IInventory inventory, boolean doRemove, ForgeDirection from) {
if (activeFlags.isEmpty()) {
return null;
}
incrementFilter();
if (filters.getStackInSlot(currentFilter % filterCount) == null || !activeFlags.get(currentFilter % filterCount)) {
return null;
}
IInventory inv = InvUtils.getInventory(inventory);
ItemStack result = checkExtractGeneric(inv, doRemove, from);
if (result != null) {
return new ItemStack[]{result};
}
return null;
}
@Override
public ItemStack checkExtractGeneric(net.minecraft.inventory.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 (!filter.isItemEqual(stack)) {
continue;
}
if (!inventory.canExtractItem(i, stack, from.ordinal())) {
continue;
}
if (doRemove) {
int stackSize = (int) Math.floor(battery.useEnergy(10, stack.stackSize * 10, false) / 10);
return inventory.decrStackSize(i, stackSize);
} else {
return stack;
}
}
}
return null;
}
public IInventory getFilters() {
return filters;
}
@Override
protected void actionsActivated(Collection<StatementSlot> actions) {
super.actionsActivated(actions);
activeFlags.clear();
for (StatementSlot action : actions) {
if (action.statement instanceof ActionExtractionPreset) {
setActivePreset(((ActionExtractionPreset) action.statement).color);
}
}
}
private void setActivePreset(EnumColor color) {
switch (color) {
case RED:
activeFlags.set(0);
break;
case BLUE:
activeFlags.set(1);
break;
case GREEN:
activeFlags.set(2);
break;
case YELLOW:
activeFlags.set(3);
break;
default:
break;
}
}
@Override
public LinkedList<IActionInternal> getActions() {
LinkedList<IActionInternal> result = super.getActions();
result.add(BuildCraftTransport.actionExtractionPresetRed);
result.add(BuildCraftTransport.actionExtractionPresetBlue);
result.add(BuildCraftTransport.actionExtractionPresetGreen);
result.add(BuildCraftTransport.actionExtractionPresetYellow);
return result;
}
@Override
public void writeGuiData(ByteBuf paramDataOutputStream) {
}
@Override
public void readGuiData(ByteBuf data, EntityPlayer paramEntityPlayer) {
byte slot = data.readByte();
slotColors[slot] = data.readByte();
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
filters.readFromNBT(nbt);
currentFilter = nbt.getInteger("currentFilter");
for (int slot = 0; slot < slotColors.length; slot++) {
slotColors[slot] = nbt.getByte("slotColors[" + slot + "]");
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
filters.writeToNBT(nbt);
nbt.setInteger("currentFilter", currentFilter);
for (int slot = 0; slot < slotColors.length; slot++) {
nbt.setByte("slotColors[" + slot + "]", slotColors[slot]);
}
}
private void incrementFilter() {
int count = 0;
currentFilter++;
while (!(filters.getStackInSlot(currentFilter % filterCount) != null && activeFlags.get(currentFilter % filterCount)) && count < filterCount) {
currentFilter++;
count++;
}
}
private ItemStack getCurrentFilter() {
return filters.getStackInSlot(currentFilter % filters.getSizeInventory());
}
}