-
Notifications
You must be signed in to change notification settings - Fork 497
/
TriggerInventory.java
113 lines (92 loc) · 3.13 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
/**
* Copyright (c) 2011-2014, 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.core.statements;
import java.util.Locale;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.gates.IStatementParameter;
import buildcraft.core.ItemList;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.utils.StringUtils;
public class TriggerInventory extends BCTrigger {
public enum State {
Empty, Contains, Space, Full
};
public State state;
public TriggerInventory(State state) {
super("buildcraft:inventory." + state.name().toLowerCase(Locale.ENGLISH), "buildcraft.inventory." + state.name().toLowerCase(Locale.ENGLISH));
this.state = state;
}
@Override
public int maxParameters() {
return state == State.Contains || state == State.Space ? 1 : 0;
}
@Override
public String getDescription() {
return StringUtils.localize("gate.trigger.inventory." + state.name().toLowerCase(Locale.ENGLISH));
}
@Override
public boolean isTriggerActive(ForgeDirection side, TileEntity tile, IStatementParameter parameter) {
ItemStack searchedStack = null;
if (parameter != null) {
searchedStack = parameter.getItemStack();
}
if (tile instanceof IInventory) {
boolean hasSlots = false;
boolean foundItems = false;
boolean foundSpace = false;
for (IInvSlot slot : InventoryIterator.getIterable((IInventory) tile, side.getOpposite())) {
hasSlots = true;
ItemStack stack = slot.getStackInSlot();
foundItems |= stack != null
&& (searchedStack == null || StackHelper.canStacksOrListsMerge(stack, searchedStack));
foundSpace |= (stack == null
|| (StackHelper.canStacksOrListsMerge(stack, searchedStack) && stack.stackSize < stack
.getMaxStackSize()))
&& (searchedStack == null || searchedStack.getItem() instanceof ItemList || slot
.canPutStackInSlot(searchedStack));
// On the test above, we deactivate item list as inventories
// typically don't check for lists possibility. This is a
// heuristic which is more desirable than expensive computation
// of list components or possibility of extension
}
if (!hasSlots) {
return false;
}
switch (state) {
case Empty:
return !foundItems;
case Contains:
return foundItems;
case Space:
return foundSpace;
default:
return !foundSpace;
}
}
return false;
}
@Override
public int getIconIndex() {
switch (state) {
case Empty:
return StatementIconProvider.Trigger_Inventory_Empty;
case Contains:
return StatementIconProvider.Trigger_Inventory_Contains;
case Space:
return StatementIconProvider.Trigger_Inventory_Space;
default:
return StatementIconProvider.Trigger_Inventory_Full;
}
}
}