/
TileEntityFilter.java
662 lines (588 loc) · 18.7 KB
/
TileEntityFilter.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package com.draco18s.industry.entities;
import javax.annotation.Nullable;
import com.draco18s.industry.ExpandedIndustryBase;
import com.draco18s.industry.world.FilterDimension;
import com.google.common.collect.ImmutableList;
import net.minecraft.block.BlockHopper;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.IHopper;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.tileentity.TileEntityHopper;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
public class TileEntityFilter extends TileEntityHopper {
private ItemStack[] filters = new ItemStack[6];
private IBlockState[] filterStates = new IBlockState[6];
private EnumAcceptType acceptRule = EnumAcceptType.OR;
// can I get away with using the origin for ALL instances of the filter?
// would need to save the exact blockstate being placed though...
// that would just be the metadata, which could be displayed in the item...
// use the TE's local position as the location in the void dim to set all of
// its filters
// it would overwrite itself if it has more than one filter rule
// but the odds of a collision between Filters reduces dramatically
public TileEntityFilter() {
super();
setCustomName("container.expindustry:filter");
}
@Override
public void update() {
super.update();
}
@Override
public boolean updateHopper() {
if (worldObj != null && !worldObj.isRemote) {
if (!isOnTransferCooldown() && BlockHopper.isEnabled(getBlockMetadata())) {
boolean flag = false;
if (!isEmpty()) {
flag = transferItemsOut();
}
if (!isFull()) {
flag = captureDroppedItems(this) || flag;
}
if (flag) {
setTransferCooldown(8);
markDirty();
return true;
}
}
return false;
}
else {
return false;
}
}
@Override
public ItemStack getStackInSlot(int slot) {
if (slot < 5) {
return super.getStackInSlot(slot);
}
else {
return filters[slot - 5];
}
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if (stack == null) {
return true;
}
if (slot < 5) {
if (doIHaveFilters()) {
switch (acceptRule) {
case OR:
return acceptOr(slot, stack);
case AND:
return acceptAnd(slot, stack);
case NONE:
return !acceptOr(slot, stack);
case SOME:
return acceptOr(slot, stack) && !acceptAnd(slot, stack);
}
}
}
else if (stack.getItem() instanceof ItemBlock) {
World w = getFilterWorld();
if(w == null) return false;
boolean ret = createAndCheckTE(w, pos, stack, slot - 5);
worldObj.notifyBlockUpdate(pos, getState(), getState(), 3);
worldObj.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
markDirty();
return ret;
}
return false;
}
private World getFilterWorld() {
return ExpandedIndustryBase.proxy.getFilterWorld();
}
public boolean doIHaveFilters() {
for (ItemStack ii : filters) {
if (ii != null) {
return true;
}
}
return false;
}
private boolean acceptOr(int slot, ItemStack stack) {
boolean ret = false;
for (int i = filters.length - 1; i >= 0; i--) {
if (filters[i] != null) {
World w = getFilterWorld();
createAndCheckTE(w, pos, filters[i], i);
TileEntity te = w.getTileEntity(pos.up(i));
EnumFacing side = getFacingForSlot(i);
IItemHandler inven = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
for (int fakeSlot = 0; inven != null && fakeSlot < inven.getSlots(); fakeSlot++) {
ItemStack input = stack.copy();
input.stackSize = 1;
int numSlots = inven.getSlots();
ret |= doesAccept(te, inven, fakeSlot, input, i);
}
}
}
return ret;
}
private boolean acceptAnd(int slot, ItemStack stack) {
boolean ret = true;
for (int i = filters.length - 1; i >= 0; i--) {
if (filters[i] != null) {
boolean anySlot = false;
World w = getFilterWorld();
createAndCheckTE(w, pos, filters[i], slot);
TileEntity te = w.getTileEntity(pos.up(i));
EnumFacing side = getFacingForSlot(i);
IItemHandler inven = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
for (int fakeSlot = 0; inven != null && fakeSlot < inven.getSlots(); fakeSlot++) {
ItemStack input = stack.copy();
input.stackSize = 1;
anySlot = anySlot || doesAccept(te, inven, fakeSlot, input, i);
}
ret &= anySlot;
}
}
return ret;
}
private boolean doesAccept(TileEntity te, IItemHandler inven, int slot, ItemStack stack, int filterSlot) {
if (te instanceof TileEntityFurnace) {
// have to check filterSlot because the Furnace slots aren't
// restricted
if (/* slot == 1 && */(filterSlot == 2 || filterSlot == 3)) {
int t = TileEntityFurnace.getItemBurnTime(stack);
return t > 0;
}
else if (/* slot == 0 && */(filterSlot == 0 || filterSlot == 1)) {
ItemStack s = FurnaceRecipes.instance().getSmeltingResult(stack);
return s != null;
}
return false;
}
else {
ItemStack remain = inven.insertItem(slot, stack, true);
return (remain == null);
}
}
@Override
public void setInventorySlotContents(int slot, @Nullable ItemStack stack) {
if (slot < 5) {
super.setInventorySlotContents(slot, stack);
}
else if (stack == null) {
filters[slot - 5] = null;
filterStates[slot - 5] = null;
if(!this.worldObj.isRemote) {
World w = getFilterWorld();
if(w == null) return;
w.setBlockToAir(pos);
}
}
else if (stack.getItem() instanceof ItemBlock) {
filters[slot - 5] = stack;
}
if (worldObj != null) {
worldObj.notifyBlockUpdate(pos, getState(), getState(), 3);
}
worldObj.notifyBlockUpdate(pos, getState(), getState(), 3);
worldObj.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
markDirty();
}
@Override
public ItemStack decrStackSize(int slot, int num) {
if (slot < 5) {
return super.decrStackSize(slot, num);
}
if (filters[slot - 5] != null) {
//ItemStack itemstack;
if (filters[slot - 5].stackSize <= num) {
//itemstack = filters[slot - 5];
filters[slot - 5] = null;
filterStates[slot - 5] = null;
if(!this.worldObj.isRemote) {
World w = getFilterWorld();
if(w == null) return null;
w.setBlockToAir(pos);
}
//return itemstack;
}
else {
//itemstack = filters[slot - 5].splitStack(num);
if (filters[slot - 5].stackSize == 0) {
filters[slot - 5] = null;
filterStates[slot - 5] = null;
if(!this.worldObj.isRemote) {
World w = getFilterWorld();
if(w == null) return null;
w.setBlockToAir(pos);
}
}
//return itemstack;
}
worldObj.notifyBlockUpdate(pos, getState(), getState(), 3);
worldObj.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
markDirty();
return null;
}
else {
return null;
}
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
return oldState.getBlock() != newState.getBlock();
}
private IBlockState getState() {
return worldObj.getBlockState(pos);
}
public EnumAcceptType getEnumType() {
return acceptRule;
}
public void setEnumType(EnumAcceptType enumAcceptType) {
acceptRule = enumAcceptType;
}
@Override
@Nullable
public SPacketUpdateTileEntity getUpdatePacket() {
return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
}
@Override
public NBTTagCompound getUpdateTag() {
return writeToNBT(new NBTTagCompound());
}
@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
NBTTagList nbttaglist = tag.getTagList("expindustry:filters", 10);
filters = new ItemStack[6];
for (int i = 0; i < nbttaglist.tagCount(); ++i) {
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("expindustry:fslot") & 255;
if (j >= 0 && j < filters.length) {
filters[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
acceptRule = EnumAcceptType.values()[tag.getInteger("expindustry:accepttype")];
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < filters.length; ++i) {
if (filters[i] != null) {
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("expindustry:fslot", (byte) i);
filters[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
tag.setTag("expindustry:filters", nbttaglist);
tag.setInteger("expindustry:accepttype", acceptRule.ordinal());
return tag;
}
@Override
public void validate() {
if(!this.worldObj.isRemote && getFilterWorld() != null)
ExpandedIndustryBase.forceChunkLoad(getFilterWorld(), new ChunkPos(pos));
super.validate();
}
@Override
public void invalidate() {
if(!this.worldObj.isRemote && getFilterWorld() != null)
ExpandedIndustryBase.releaseChunkLoad(getFilterWorld(), new ChunkPos(pos));
super.invalidate();
}
private boolean createAndCheckTE(World world, BlockPos pos, ItemStack stack, int slot) {
if(world == null) return false;
if (pos.getY() + slot < 255) {
pos = pos.up(slot);
}
ItemBlock ib = (ItemBlock) stack.getItem();
IBlockState state = ib.block.getStateFromMeta(ib.getMetadata(stack));
// World w =
// FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(FilterDimension.DIMENSION_ID);
if (world.getBlockState(pos) == filterStates[slot]) {
return true;
}
if (filterStates[slot] != null) {
world.setBlockState(pos, filterStates[slot], 2);
TileEntity te = world.getTileEntity(pos);
if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
filterStates[slot] = state;
return true;
}
}
if (ib.block.hasTileEntity(state)) {
world.setBlockState(pos, state, 2);
IBlockState somestate = world.getBlockState(pos);
TileEntity te = world.getTileEntity(pos);
if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
filterStates[slot] = state;
return true;
}
else if (ib.getMetadata(stack) == 0) {
BlockStateContainer bsc = ib.block.getBlockState();
ImmutableList<IBlockState> values = bsc.getValidStates();
EnumFacing side = getFacingForSlot(slot);
for (IBlockState s : values) {
world.setBlockState(pos, s, 2);
System.out.println(world.getBlockState(pos));
te = world.getTileEntity(pos);
if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)) {
filterStates[slot] = s;
return true;
}
}
}
}
else {
BlockStateContainer bsc = ib.block.getBlockState();
ImmutableList<IBlockState> values = bsc.getValidStates();
EnumFacing side = getFacingForSlot(slot - 5);
for (IBlockState s : values) {
world.setBlockState(pos, s, 2);
// System.out.println("Checking..." + world.getBlockState(pos));
TileEntity te = world.getTileEntity(pos);
if (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)) {
filterStates[slot] = s;
return true;
}
}
}
filterStates[slot] = null;
world.setBlockToAir(pos);
return false;
}
private EnumFacing getFacingForSlot(int slot) {
EnumFacing side = EnumFacing.DOWN;
switch (slot) {
case 0:
case 1:
side = EnumFacing.UP;
break;
case 2:
case 3:
side = EnumFacing.EAST;
break;
case 4:
case 5:
side = EnumFacing.DOWN;
break;
}
return side;
}
public static enum EnumAcceptType {
OR, AND, NONE, SOME;
}
/*====================================== Overrides for Filtering ======================================*/
/*============================================ Hopper Stuff ===========================================*/
private boolean isEmpty() {
for (int i = 0; i < getSizeInventory(); i++) {
ItemStack itemstack = getStackInSlot(i);
if (itemstack != null) {
return false;
}
}
return true;
}
private boolean isFull() {
for (int i = 0; i < getSizeInventory(); i++) {
ItemStack itemstack = getStackInSlot(i);
if (itemstack == null || itemstack.stackSize != itemstack.getMaxStackSize()) {
return false;
}
}
return true;
}
private boolean transferItemsOut() {
if (net.minecraftforge.items.VanillaInventoryCodeHooks.insertHook(this)) {
return true;
}
IInventory iinventory = getInventoryForHopperTransfer();
if (iinventory == null) {
return false;
}
else {
EnumFacing enumfacing = BlockHopper.getFacing(getBlockMetadata()).getOpposite();
if (isInventoryFull(iinventory, enumfacing)) {
return false;
}
else {
for (int i = 0; i < getSizeInventory(); ++i) {
if (getStackInSlot(i) != null) {
ItemStack itemstack = getStackInSlot(i).copy();
ItemStack itemstack1 = putStackInInventoryAllSlots(iinventory, decrStackSize(i, 1), enumfacing);
if (itemstack1 == null || itemstack1.stackSize == 0) {
iinventory.markDirty();
return true;
}
setInventorySlotContents(i, itemstack);
}
}
return false;
}
}
}
public static boolean captureDroppedItems(TileEntityFilter hopper) {
Boolean ret = extractHook(hopper);
if (ret != null) {
return ret;
}
IInventory iinventory = getHopperInventory(hopper);
if (iinventory != null) {
EnumFacing enumfacing = EnumFacing.DOWN;
if (isInventoryEmpty(iinventory, enumfacing)) {
return false;
}
if (iinventory instanceof ISidedInventory) {
ISidedInventory isidedinventory = (ISidedInventory) iinventory;
int[] aint = isidedinventory.getSlotsForFace(enumfacing);
for (int i : aint) {
if (pullItemFromSlot(hopper, iinventory, i, enumfacing)) {
return true;
}
}
}
else {
int j = iinventory.getSizeInventory();
for (int k = 0; k < j; ++k) {
if (pullItemFromSlot(hopper, iinventory, k, enumfacing)) {
return true;
}
}
}
}
else {
for (EntityItem entityitem : getCaptureItems(hopper.getWorld(), hopper.getXPos(), hopper.getYPos(),
hopper.getZPos())) {
if (putDropInInventoryAllSlots(hopper, entityitem)) {
return true;
}
}
}
return false;
}
private boolean isInventoryFull(IInventory inventoryIn, EnumFacing side) {
if (inventoryIn instanceof ISidedInventory) {
ISidedInventory isidedinventory = (ISidedInventory) inventoryIn;
int[] aint = isidedinventory.getSlotsForFace(side);
for (int k : aint) {
ItemStack itemstack1 = isidedinventory.getStackInSlot(k);
if (itemstack1 == null || itemstack1.stackSize != itemstack1.getMaxStackSize()) {
return false;
}
}
}
else {
int i = inventoryIn.getSizeInventory();
for (int j = 0; j < i; ++j) {
ItemStack itemstack = inventoryIn.getStackInSlot(j);
if (itemstack == null || itemstack.stackSize != itemstack.getMaxStackSize()) {
return false;
}
}
}
return true;
}
/**
* Returns false if the specified IInventory contains any items
*/
private static boolean isInventoryEmpty(IInventory inventoryIn, EnumFacing side) {
if (inventoryIn instanceof ISidedInventory) {
ISidedInventory isidedinventory = (ISidedInventory) inventoryIn;
int[] aint = isidedinventory.getSlotsForFace(side);
for (int i : aint) {
if (isidedinventory.getStackInSlot(i) != null) {
return false;
}
}
}
else {
int j = inventoryIn.getSizeInventory();
for (int k = 0; k < j; ++k) {
if (inventoryIn.getStackInSlot(k) != null) {
return false;
}
}
}
return true;
}
private IInventory getInventoryForHopperTransfer() {
EnumFacing enumfacing = BlockHopper.getFacing(getBlockMetadata());
/**
* Returns the IInventory (if applicable) of the TileEntity at the
* specified position
*/
return getInventoryAtPosition(getWorld(), getXPos() + enumfacing.getFrontOffsetX(),
getYPos() + enumfacing.getFrontOffsetY(), getZPos() + enumfacing.getFrontOffsetZ());
}
private static boolean pullItemFromSlot(IHopper hopper, IInventory inventoryIn, int index, EnumFacing direction) {
ItemStack itemstack = inventoryIn.getStackInSlot(index);
if (itemstack != null && canExtractItemFromSlot(inventoryIn, itemstack, index, direction)) {
ItemStack itemstack1 = itemstack.copy();
ItemStack itemstack2 = putStackInInventoryAllSlots(hopper, inventoryIn.decrStackSize(index, 1),
(EnumFacing) null);
if (itemstack2 == null || itemstack2.stackSize == 0) {
inventoryIn.markDirty();
return true;
}
inventoryIn.setInventorySlotContents(index, itemstack1);
}
return false;
}
private static boolean canExtractItemFromSlot(IInventory inventoryIn, ItemStack stack, int index, EnumFacing side) {
return !(inventoryIn instanceof ISidedInventory)
|| ((ISidedInventory) inventoryIn).canExtractItem(index, stack, side);
}
/*======================================= Vanilla Hooks Override ======================================*/
private static Boolean extractHook(IHopper dest) {
//IHopper l;
TileEntity tileEntity = dest.getWorld()
.getTileEntity(new BlockPos(dest.getXPos(), dest.getYPos() + 1, dest.getZPos()));
if (tileEntity == null
|| !tileEntity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN)) {
return null;
}
IItemHandler handler = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack extractItem = handler.extractItem(i, 1, true);
if (extractItem != null) {
for (int j = 0; j < dest.getSizeInventory(); j++) {
ItemStack destStack = dest.getStackInSlot(j);
if (destStack == null || destStack.stackSize < destStack.getMaxStackSize()
&& destStack.stackSize < dest.getInventoryStackLimit()
&& ItemHandlerHelper.canItemStacksStack(extractItem, destStack)) {
// override! we need to check that the insert is valid!
if (dest.isItemValidForSlot(j, extractItem)) {
extractItem = handler.extractItem(i, 1, false);
if (destStack == null) {
dest.setInventorySlotContents(j, extractItem);
}
else {
destStack.stackSize++;
dest.setInventorySlotContents(j, destStack);
}
dest.markDirty();
return true;
}
}
}
}
}
return false;
}
}