-
Notifications
You must be signed in to change notification settings - Fork 497
/
TriggerInventory.java
140 lines (116 loc) · 3.15 KB
/
TriggerInventory.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
/**
* Copyright (c) SpaceToad, 2011
* 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.core.triggers;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
import buildcraft.api.gates.ITriggerDirectional;
import buildcraft.api.gates.ITriggerParameter;
import buildcraft.api.gates.Trigger;
import buildcraft.core.DefaultProps;
import buildcraft.core.utils.SidedInventoryAdapter;
import buildcraft.core.utils.Utils;
public class TriggerInventory extends Trigger implements ITriggerDirectional {
public enum State {
Empty, Contains, Space, Full
};
public State state;
public TriggerInventory(int id, State state) {
super(id);
this.state = state;
}
@Override
public int getIndexInTexture() {
switch (state) {
case Empty:
return 2 * 16 + 4;
case Contains:
return 2 * 16 + 5;
case Space:
return 2 * 16 + 6;
default:
return 2 * 16 + 7;
}
}
@Override
public boolean hasParameter() {
if (state == State.Contains || state == State.Space)
return true;
else
return false;
}
@Override
public String getDescription() {
switch (state) {
case Empty:
return "Inventory Empty";
case Contains:
return "Items in Inventory";
case Space:
return "Space in Inventory";
default:
return "Inventory Full";
}
}
@Override
public boolean isTriggerActive(ForgeDirection side, TileEntity tile, ITriggerParameter parameter) {
ItemStack searchedStack = null;
if (parameter != null) {
searchedStack = parameter.getItem();
}
if (tile instanceof IInventory) {
IInventory inv = Utils.getInventory(((IInventory) tile));
if (side != ForgeDirection.UNKNOWN && inv instanceof ISidedInventory) {
inv = new SidedInventoryAdapter((ISidedInventory) inv, side);
}
int invSize = inv.getSizeInventory();
if (invSize <= 0)
return false;
boolean foundItems = false;
boolean foundSpace = false;
for (int i = 0; i < invSize; ++i) {
ItemStack stack = inv.getStackInSlot(i);
boolean slotEmpty = stack == null || stack.stackSize == 0;
if (searchedStack == null) {
foundItems |= !slotEmpty;
} else if (!slotEmpty) {
foundItems |= stack.isItemEqual(searchedStack);
}
if (slotEmpty) {
foundSpace = true;
} else if (searchedStack != null) {
if (stack.stackSize < stack.getMaxStackSize() && stack.isItemEqual(searchedStack)) {
foundSpace = true;
}
}
}
switch (state) {
case Empty:
return !foundItems;
case Contains:
return foundItems;
case Space:
return foundSpace;
default:
return !foundSpace;
}
}
return false;
}
@Override
public boolean isTriggerActive(TileEntity tile, ITriggerParameter parameter) {
return isTriggerActive(ForgeDirection.UNKNOWN, tile, parameter);
}
@Override
public String getTexture() {
return DefaultProps.TEXTURE_TRIGGERS;
}
}