forked from wormzjl/ModularMachinery
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTileItemOutputBus.java
More file actions
161 lines (137 loc) · 5.75 KB
/
Copy pathTileItemOutputBus.java
File metadata and controls
161 lines (137 loc) · 5.75 KB
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
/*******************************************************************************
* HellFirePvP / Modular Machinery 2019
*
* This project is licensed under GNU GENERAL PUBLIC LICENSE Version 3.
* The source code is available on github: https://github.com/HellFirePvP/ModularMachinery
* For further details, see the License file there.
******************************************************************************/
package hellfirepvp.modularmachinery.common.tiles;
import github.kasuminova.mmce.common.tile.MEPatternProvider;
import github.kasuminova.mmce.common.tile.base.MEItemBus;
import hellfirepvp.modularmachinery.ModularMachinery;
import hellfirepvp.modularmachinery.common.base.Mods;
import hellfirepvp.modularmachinery.common.block.prop.ItemBusSize;
import hellfirepvp.modularmachinery.common.machine.IOType;
import hellfirepvp.modularmachinery.common.machine.MachineComponent;
import hellfirepvp.modularmachinery.common.tiles.base.MachineComponentTile;
import hellfirepvp.modularmachinery.common.tiles.base.TileInventory;
import hellfirepvp.modularmachinery.common.tiles.base.TileItemBus;
import hellfirepvp.modularmachinery.common.util.IOInventory;
import hellfirepvp.modularmachinery.common.util.ItemUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nullable;
/**
* This class is part of the Modular Machinery Mod
* The complete source code for this mod can be found on github.
* Class: TileItemOutputBus
* Created by HellFirePvP
* Date: 07.07.2017 / 18:41
*/
public class TileItemOutputBus extends TileItemBus implements MachineComponentTile {
public static int minWorkDelay = 5;
public static int maxWorkDelay = 60;
public TileItemOutputBus() {
}
public TileItemOutputBus(ItemBusSize type) {
super(type);
}
@Override
public void doRestrictedTick() {
if (getWorld().isRemote || isExternalIODisabled() || !canWork(minWorkDelay, maxWorkDelay)) {
return;
}
for (EnumFacing facing : EnumFacing.VALUES) {
BlockPos offset = getPos().offset(facing);
TileEntity te = getWorld().getTileEntity(offset);
if (te == null || te instanceof TileItemBus) {
continue;
}
if (Mods.AE2.isPresent() && ((te instanceof MEItemBus) || (te instanceof MEPatternProvider))) {
continue;
}
EnumFacing accessingSide = facing.getOpposite();
IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, accessingSide);
if (itemHandler == null) {
continue;
}
try {
outputToExternal(itemHandler);
} catch (Exception e) {
ModularMachinery.log.error("Exception when insert item: ", e);
}
}
}
private synchronized void outputToExternal(IItemHandler external) {
boolean successAtLeastOnce = false;
for (int externalSlotId = 0; externalSlotId < external.getSlots(); externalSlotId++) {
ItemStack externalStack = external.getStackInSlot(externalSlotId);
int slotLimit = external.getSlotLimit(externalSlotId);
if (!externalStack.isEmpty() && externalStack.getCount() >= slotLimit) {
continue;
}
for (int internalSlotId = 0; internalSlotId < inventory.getSlots(); internalSlotId++) {
ItemStack internalStack = inventory.getStackInSlot(internalSlotId);
if (internalStack.isEmpty()) {
continue;
}
if (externalStack.isEmpty()) {
ItemStack notInserted = external.insertItem(externalSlotId, internalStack, false);
// Safeguard against Storage Drawers virtual slot
if (notInserted.getCount() == internalStack.getCount()) {
break;
}
inventory.setStackInSlot(internalSlotId, notInserted);
successAtLeastOnce = true;
if (notInserted.isEmpty()) {
break;
}
continue;
}
if (!ItemUtils.matchStacks(internalStack, externalStack)) {
continue;
}
// Extract internal item to external.
ItemStack notInserted = external.insertItem(externalSlotId, internalStack, false);
inventory.setStackInSlot(internalSlotId, notInserted);
successAtLeastOnce = true;
if (notInserted.isEmpty()) {
break;
}
}
}
if (successAtLeastOnce) {
incrementSuccessCounter(maxWorkDelay, minWorkDelay);
super.markNoUpdate();
} else {
decrementSuccessCounter();
}
}
@Override
public void markNoUpdate() {
super.markNoUpdate();
inventoryChanged = true;
}
@Override
public IOInventory buildInventory(TileInventory tile, int size) {
int[] slots = new int[size];
for (int i = 0; i < size; i++) {
slots[i] = i;
}
return new IOInventory(tile, new int[]{}, slots);
}
@Nullable
@Override
public MachineComponent.ItemBus provideComponent() {
return new MachineComponent.ItemBus(IOType.OUTPUT) {
@Override
public IOInventory getContainerProvider() {
return TileItemOutputBus.this.inventory;
}
};
}
}