summaryrefslogtreecommitdiff
path: root/ihl/processing/invslots/InvSlotConsumableLiquidIHL.java
blob: 7bb6eccede4fe4221fb3d02d512714f6b496dad8 (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
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
package ihl.processing.invslots;

import java.util.Iterator;

import org.apache.commons.lang3.mutable.MutableObject;

import ic2.core.block.TileEntityInventory;
import ic2.core.block.invslot.InvSlotConsumableLiquid;
import ic2.core.util.StackUtil;
import ihl.utils.IHLUtils;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;

public class InvSlotConsumableLiquidIHL extends InvSlotConsumableLiquid{

	private OpType opType;
	private String additionalInputs;

	public InvSlotConsumableLiquidIHL(TileEntityInventory base1, String name1,int oldStartIndex1, Access access1, int count,InvSide preferredSide1, OpType opType1) 
	{
		super(base1, name1, oldStartIndex1, access1, count, preferredSide1, opType1);
		opType=opType1;
	}
	
	public InvSlotConsumableLiquidIHL(TileEntityInventory base1, String name1,int oldStartIndex1, Access access1, int count,InvSide preferredSide1, OpType opType1, String additionalInputs1) 
	{
		super(base1, name1, oldStartIndex1, access1, count, preferredSide1, opType1);
		opType=opType1;
		additionalInputs=additionalInputs1;
	}
	
	@Override
    public FluidStack drain(Fluid fluid, int maxAmount, MutableObject<ItemStack> output, boolean simulate)
    {
		if(output!=null)
		{
	        output.setValue((ItemStack)null);
		}

        if (this.opType != InvSlotConsumableLiquid.OpType.Drain && this.opType != InvSlotConsumableLiquid.OpType.Both)
        {
            return null;
        }
        else
        {
            ItemStack stack = this.get();

            if (stack == null)
            {
                return null;
            }
            else if (!FluidContainerRegistry.isFilledContainer(stack))
            {
                if (stack.getItem() instanceof IFluidContainerItem)
                {
                    IFluidContainerItem var9 = (IFluidContainerItem)stack.getItem();

                    if (var9.getFluid(stack) == null)
                    {
                        return null;
                    }
                    else if (fluid != null && var9.getFluid(stack).getFluid() != fluid)
                    {
                        return null;
                    }
                    else if (!this.acceptsLiquid(var9.getFluid(stack).getFluid()))
                    {
                        return null;
                    }
                    else
                    {
                        ItemStack singleStack = StackUtil.copyWithSize(stack, 1);
                        FluidStack fluidStack = var9.drain(singleStack, maxAmount, true);

                        if (fluidStack != null && fluidStack.amount > 0)
                        {
                            if (singleStack.stackSize <= 0)
                            {
                                if (!simulate)
                                {
                                    --stack.stackSize;
                                }
                            }
                            else if (var9.getFluid(singleStack) == null)
                            {
                        		if(output!=null)
                        		{
                        			output.setValue(singleStack);
                        		}
                                if (!simulate)
                                {
                                    --stack.stackSize;
                                }
                            }
                            else
                            {
                                if (stack.stackSize > 1)
                                {
                                    return null;
                                }

                                if (!simulate)
                                {
                                    this.put(singleStack);
                                }
                            }

                            if (stack.stackSize <= 0)
                            {
                                this.put((ItemStack)null);
                            }

                            return fluidStack;
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                FluidStack container = FluidContainerRegistry.getFluidForFilledItem(stack);

                if (container != null && (fluid == null || fluid == container.getFluid()))
                {
                    if (!this.acceptsLiquid(container.getFluid()))
                    {
                        return null;
                    }
                    else if (container.amount > 0 && container.amount <= maxAmount)
                    {
                        if (stack.getItem().hasContainerItem(stack) && output!=null)
                        {
                            output.setValue(stack.getItem().getContainerItem(stack));
                        }
                        else
                        {
                        	ItemStack emptystack = FluidContainerRegistry.drainFluidContainer(stack);
                        	if(emptystack!=null && output!=null)
                        	{
                        		output.setValue(emptystack);
                        	}
                        }

                        if (!simulate)
                        {
                            --stack.stackSize;

                            if (stack.stackSize <= 0)
                            {
                                this.put((ItemStack)null);
                            }
                        }

                        return container;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
    }

	@Override
    public boolean accepts(ItemStack stack)
    {
        if (stack == null)
        {
            return true;
        }
        Item item = stack.getItem();
        if (item == null)
        {
            return false;
        }
        else
        {
        	if(this.additionalInputs!=null)
        	{
        		String fon = IHLUtils.getFirstOreDictName(stack);
        		if(fon!=null && fon.contains(additionalInputs))
        		{
        			return true;
        		}
        	}
            if (this.opType == InvSlotConsumableLiquid.OpType.Drain || this.opType == InvSlotConsumableLiquid.OpType.Both)
            {
                FluidStack containerItem = null;
                if (FluidContainerRegistry.isFilledContainer(stack))
                {
                    containerItem = FluidContainerRegistry.getFluidForFilledItem(stack);
                }
                else if (item instanceof IFluidContainerItem)
                {
                    containerItem = ((IFluidContainerItem)item).getFluid(stack);
                }

                if (containerItem != null && containerItem.amount > 0)
                {
                    return true;
                }
            }
            if (this.opType == InvSlotConsumableLiquid.OpType.Fill || this.opType == InvSlotConsumableLiquid.OpType.Both)
            {
                if (FluidContainerRegistry.isEmptyContainer(stack))
                {
                    if (this.getPossibleFluids() == null)
                    {
                        return true;
                    }

                    Iterator<Fluid> containerItem1 = this.getPossibleFluids().iterator();

                    while (containerItem1.hasNext())
                    {
                        Fluid prevFluid = containerItem1.next();

                        if (FluidContainerRegistry.fillFluidContainer(new FluidStack(prevFluid, Integer.MAX_VALUE), stack) != null)
                        {
                            return true;
                        }
                    }
                }
                else if (item instanceof IFluidContainerItem)
                {
                    IFluidContainerItem containerItem2 = (IFluidContainerItem)item;
                    FluidStack prevFluid1 = containerItem2.getFluid(stack);

                    if (prevFluid1 == null || containerItem2.getCapacity(stack) > prevFluid1.amount)
                    {
                        if (this.getPossibleFluids() == null)
                        {
                            return true;
                        }

                        ItemStack singleStack = StackUtil.copyWithSize(stack, 1);
                        Iterator<Fluid> i$ = this.getPossibleFluids().iterator();

                        while (i$.hasNext())
                        {
                            Fluid fluid = i$.next();

                            if (containerItem2.fill(singleStack, new FluidStack(fluid, Integer.MAX_VALUE), false) > 0)
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
    
}