-
Notifications
You must be signed in to change notification settings - Fork 497
/
BoardRobotCrafter.java
executable file
·205 lines (173 loc) · 5.79 KB
/
BoardRobotCrafter.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
/**
* Copyright (c) 2011-2015, 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.robotics.boards;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import buildcraft.api.boards.RedstoneBoardRobot;
import buildcraft.api.boards.RedstoneBoardRobotNBT;
import buildcraft.api.recipes.CraftingResult;
import buildcraft.api.recipes.IFlexibleRecipe;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.IDockingStation;
import buildcraft.api.robots.StackRequest;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.recipes.AssemblyRecipeManager;
import buildcraft.robotics.ai.AIRobotCraftAssemblyTable;
import buildcraft.robotics.ai.AIRobotCraftFurnace;
import buildcraft.robotics.ai.AIRobotCraftGeneric;
import buildcraft.robotics.ai.AIRobotCraftWorkbench;
import buildcraft.robotics.ai.AIRobotDeliverRequested;
import buildcraft.robotics.ai.AIRobotDisposeItems;
import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotGotoStationToUnload;
import buildcraft.robotics.ai.AIRobotSearchStackRequest;
import buildcraft.robotics.ai.AIRobotUnload;
import buildcraft.robotics.statements.ActionRobotFilter;
public class BoardRobotCrafter extends RedstoneBoardRobot {
private ItemStack order;
private ArrayList<ItemStack> craftingBlacklist = new ArrayList<ItemStack>();
private HashSet<IDockingStation> reservedStations = new HashSet<IDockingStation>();
private StackRequest currentRequest = null;
public BoardRobotCrafter(EntityRobotBase iRobot) {
super(iRobot);
}
@Override
public RedstoneBoardRobotNBT getNBTHandler() {
return BoardRobotCrafterNBT.instance;
}
@Override
public void update() {
if (robot.containsItems()) {
// Always makes sure that when starting a craft, the inventory is
// clean.
startDelegateAI(new AIRobotDisposeItems(robot));
return;
}
if (currentRequest != null) {
order = currentRequest.stack;
} else {
order = null;
}
if (order == null) {
startDelegateAI(new AIRobotSearchStackRequest(robot, ActionRobotFilter.getGateFilter(robot
.getLinkedStation()), craftingBlacklist));
return;
}
IRecipe recipe = lookForWorkbenchRecipe(order);
if (recipe != null) {
startDelegateAI(new AIRobotCraftWorkbench(robot, recipe));
return;
}
ItemStack furnaceInput = lookForFurnaceRecipe(order);
if (furnaceInput != null) {
startDelegateAI(new AIRobotCraftFurnace(robot, furnaceInput, order));
return;
}
CraftingResult craftingResult = lookForAssemblyTableRecipe(order);
if (craftingResult != null) {
startDelegateAI(new AIRobotCraftAssemblyTable(robot, craftingResult));
return;
}
craftingBlacklist.add(order);
}
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotCraftGeneric) {
if (!ai.success()) {
robot.releaseResources();
currentRequest = null;
craftingBlacklist.add(order);
} else {
if (currentRequest != null) {
startDelegateAI(new AIRobotDeliverRequested(robot, currentRequest));
} else {
robot.releaseResources();
// The extra crafted items may make some crafting possible
craftingBlacklist.clear();
}
}
} else if (ai instanceof AIRobotGotoStationToUnload) {
if (ai.success()) {
startDelegateAI(new AIRobotUnload(robot));
} else {
robot.releaseResources();
currentRequest = null;
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotSearchStackRequest) {
if (!ai.success()) {
craftingBlacklist.clear();
currentRequest = null;
startDelegateAI(new AIRobotGotoSleep(robot));
} else {
currentRequest = ((AIRobotSearchStackRequest) ai).request;
if (!currentRequest.station.take(robot)) {
currentRequest = null;
}
}
} else if (ai instanceof AIRobotDeliverRequested) {
currentRequest = null;
robot.releaseResources();
// The extra crafted items may make some crafting possible
craftingBlacklist.clear();
}
}
private IRecipe lookForWorkbenchRecipe(ItemStack order) {
for (Object o : CraftingManager.getInstance().getRecipeList()) {
IRecipe r = (IRecipe) o;
if (r instanceof ShapedRecipes
|| r instanceof ShapelessRecipes
|| r instanceof ShapedOreRecipe
|| r instanceof ShapelessOreRecipe) {
if (StackHelper.isMatchingItem(r.getRecipeOutput(), order)) {
return r;
}
}
}
return null;
}
private ItemStack lookForFurnaceRecipe(ItemStack order) {
for (Object o : FurnaceRecipes.smelting().getSmeltingList().entrySet()) {
Map.Entry e = (Map.Entry) o;
ItemStack input = (ItemStack) e.getKey();
ItemStack output = (ItemStack) e.getValue();
if (StackHelper.isMatchingItem(output, order)) {
return input;
}
}
return null;
}
private CraftingResult<?> lookForAssemblyTableRecipe(ItemStack order) {
for (IFlexibleRecipe r : AssemblyRecipeManager.INSTANCE.getRecipes()) {
CraftingResult<?> result = r.canCraft(order);
if (result != null) {
return result;
}
}
return null;
}
private boolean isBlacklisted(ItemStack stack) {
for (ItemStack black : craftingBlacklist) {
if (StackHelper.isMatchingItem(stack, black)) {
return true;
}
}
return false;
}
}