-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCraftingJobDependencyGraph.java
More file actions
356 lines (311 loc) · 15.5 KB
/
CraftingJobDependencyGraph.java
File metadata and controls
356 lines (311 loc) · 15.5 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
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
package org.cyclops.integratedcrafting.api.crafting;
import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntCollection;
import it.unimi.dsi.fastutil.ints.IntIterator;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import org.cyclops.commoncapabilities.api.capability.recipehandler.IRecipeDefinition;
import org.cyclops.integratedcrafting.core.CraftingHelpers;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* A CraftingJobDependencyGraph stores dependencies between crafting jobs based on their unique ID.
* @author rubensworks
*/
public class CraftingJobDependencyGraph {
private final Int2ObjectMap<CraftingJob> craftingJobs;
private final Int2ObjectMap<IntCollection> dependencies;
private final Int2ObjectMap<IntCollection> dependents;
public CraftingJobDependencyGraph() {
this(new Int2ObjectOpenHashMap<>(), new Int2ObjectOpenHashMap<>(), new Int2ObjectOpenHashMap<>());
}
public CraftingJobDependencyGraph(Int2ObjectMap<CraftingJob> craftingJobs,
Int2ObjectMap<IntCollection> dependencies,
Int2ObjectMap<IntCollection> dependents) {
this.craftingJobs = craftingJobs;
this.dependencies = dependencies;
this.dependents = dependents;
}
public Collection<CraftingJob> getCraftingJobs() {
return craftingJobs.values();
}
@Nullable
public CraftingJob getCraftingJob(int id) {
return craftingJobs.get(id);
}
public Collection<CraftingJob> getDependencies(CraftingJob craftingJob) {
return dependencies.getOrDefault(craftingJob.getId(), new IntArrayList())
.stream()
.map(craftingJobs::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
public boolean hasDependencies(CraftingJob craftingJob) {
return hasDependencies(craftingJob.getId());
}
public boolean hasDependencies(int craftingJobId) {
IntCollection deps = dependencies.get(craftingJobId);
if (deps != null) {
IntIterator it = deps.iterator();
while (it.hasNext()) {
if (craftingJobs.get(it.next()) != null) {
return true;
}
}
}
return false;
}
public Collection<CraftingJob> getDependents(CraftingJob craftingJob) {
return dependents.getOrDefault(craftingJob.getId(), new IntArrayList())
.stream()
.map(craftingJobs::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
public void addCraftingJobId(CraftingJob craftingJob) {
craftingJobs.put(craftingJob.getId(), craftingJob);
}
public void removeCraftingJobId(CraftingJob craftingJob) {
craftingJobs.remove(craftingJob.getId());
}
public void onCraftingJobFinished(CraftingJob craftingJob) {
this.onCraftingJobFinished(craftingJob, false);
}
public void onCraftingJobFinished(CraftingJob craftingJob, boolean finishInvalidDependencies) {
// Remove the job instance reference
removeCraftingJobId(craftingJob);
// Remove the dependents
IntCollection removed = dependents.remove(craftingJob.getId());
craftingJob.getDependentCraftingJobs().clear();
// Remove all backwards dependency links
if (removed != null) {
IntIterator removedIt = removed.iterator();
while (removedIt.hasNext()) {
int dependent = removedIt.nextInt();
IntCollection dependentDependencies = dependencies.get(dependent);
dependentDependencies.rem(craftingJob.getId());
CraftingJob dependentJob = craftingJobs.get(dependent);
if (dependentJob != null) {
dependentJob.getDependencyCraftingJobs().rem(craftingJob.getId());
if (dependentDependencies.isEmpty()) {
dependencies.remove(dependent);
if (!dependents.containsKey(dependent)) {
craftingJobs.remove(dependent);
}
}
}
}
}
// Remove invalid dependencies that are not present in craftingJobs
IntCollection removedDependencies = dependencies.remove(craftingJob.getId());
if (removedDependencies != null) {
IntIterator removedDependenciesIt = removedDependencies.iterator();
while (removedDependenciesIt.hasNext()) {
int dependency = removedDependenciesIt.nextInt();
dependents.remove(dependency);
if (finishInvalidDependencies) {
onCraftingJobFinished(craftingJobs.get(dependency), true);
}
}
}
}
public void addDependency(CraftingJob craftingJob, CraftingJob dependency) {
// Store id's of the edge
addCraftingJobId(dependency);
addDependency(craftingJob, dependency.getId());
}
public void addDependency(CraftingJob craftingJob, int dependency) {
// Store id's of the edge
addCraftingJobId(craftingJob);
// Save dependency link
IntCollection jobDependencies = dependencies.get(craftingJob.getId());
if (jobDependencies == null) {
jobDependencies = new IntArrayList();
dependencies.put(craftingJob.getId(), jobDependencies);
}
jobDependencies.add(dependency);
// Save reverse link
IntCollection jobDependents = dependents.get(dependency);
if (jobDependents == null) {
jobDependents = new IntArrayList();
dependents.put(dependency, jobDependents);
}
jobDependents.add(craftingJob.getId());
}
public void removeDependency(int craftingJob, int dependency) {
// Remove dependency link
IntCollection jobDependencies = dependencies.get(craftingJob);
if (jobDependencies != null) {
jobDependencies.rem(dependency);
if (jobDependencies.isEmpty()) {
dependencies.remove(craftingJob);
if (!dependents.containsKey(craftingJob)) {
craftingJobs.remove(craftingJob);
}
}
}
// Remove reverse link
IntCollection jobDependents = dependents.get(dependency);
if (jobDependents != null) {
jobDependents.rem(craftingJob);
if (jobDependents.isEmpty()) {
dependents.remove(dependency);
if (!dependencies.containsKey(dependency)) {
craftingJobs.remove(dependency);
}
}
}
}
public void importDependencies(CraftingJobDependencyGraph craftingJobsGraph) {
for (CraftingJob craftingJob : craftingJobsGraph.getCraftingJobs()) {
for (CraftingJob dependency : craftingJobsGraph.getDependencies(craftingJob)) {
this.addDependency(craftingJob, dependency);
}
}
}
/**
* Merge the two crafting jobs by adding the second job's amount into the first job's amount.
* Furthermore, all dependencies of the second job will be merged into the dependencies of the first job as well.
* @param target The job that should be merged into.
* @param mergee The job that should be removed and merged into the target job.
* @param markMergeeAsFinished If the mergee job should be marked as finished.
*/
public void mergeCraftingJobs(CraftingJob target, CraftingJob mergee, boolean markMergeeAsFinished) {
target.setAmount(target.getAmount() + mergee.getAmount());
target.setIngredientsStorage(CraftingHelpers.mergeMixedIngredients(
target.getIngredientsStorage(), mergee.getIngredientsStorage()));
// If the existing job had dependencies, batch the dependencies as well
// First, collect all dependency crafting jobs for the target job
Map<IRecipeDefinition, CraftingJob> dependencyRecipeJobs = Maps.newHashMap();
for (Integer dependencyCraftingJobId : target.getDependencyCraftingJobs()) {
CraftingJob dependencyCraftingJob = this.getCraftingJob(dependencyCraftingJobId);
dependencyRecipeJobs.put(dependencyCraftingJob.getRecipe(), dependencyCraftingJob);
}
// Next, try merging the mergee's jobs into the target dependency jobs
// If no corresponding target dependency job exists, just add the dependency directly to target as dependency.
for (Integer dependencyCraftingJobId : mergee.getDependencyCraftingJobs()) {
CraftingJob dependencyCraftingJob = this.getCraftingJob(dependencyCraftingJobId);
CraftingJob existingDependencyJob = dependencyRecipeJobs.get(dependencyCraftingJob.getRecipe());
if (existingDependencyJob != null) {
mergeCraftingJobs(existingDependencyJob, dependencyCraftingJob, false);
} else {
// Update dependency links
mergee.removeDependency(dependencyCraftingJob);
target.addDependency(dependencyCraftingJob);
this.removeDependency(mergee.getId(), dependencyCraftingJobId);
this.addDependency(target, dependencyCraftingJob);
// Add to our available jobs
dependencyRecipeJobs.put(dependencyCraftingJob.getRecipe(), dependencyCraftingJob);
}
}
if (markMergeeAsFinished) {
// Transfer all dependents of the mergee to the target before removing the mergee.
// This ensures that jobs which depended on the mergee now correctly depend on the target,
// so that crafted outputs are properly routed to those dependent jobs.
IntCollection mergeeDependents = dependents.get(mergee.getId());
if (mergeeDependents != null) {
for (int dependentId : mergeeDependents.toIntArray()) {
CraftingJob dependentJob = craftingJobs.get(dependentId);
// Update graph-level dependency map: replace mergee with target for this dependent
IntCollection depDependencies = dependencies.get(dependentId);
if (depDependencies != null) {
depDependencies.rem(mergee.getId());
if (!depDependencies.contains(target.getId())) {
depDependencies.add(target.getId());
}
}
// Update graph-level dependent map: add this dependent to target
IntCollection targetDependents = dependents.get(target.getId());
if (targetDependents == null) {
targetDependents = new IntArrayList();
dependents.put(target.getId(), targetDependents);
}
if (!targetDependents.contains(dependentId)) {
targetDependents.add(dependentId);
}
// Update job-level lists if the dependent job is available
if (dependentJob != null) {
dependentJob.getDependencyCraftingJobs().rem(mergee.getId());
if (!dependentJob.getDependencyCraftingJobs().contains(target.getId())) {
dependentJob.getDependencyCraftingJobs().add(target.getId());
}
if (!target.getDependentCraftingJobs().contains(dependentId)) {
target.getDependentCraftingJobs().add(dependentId);
}
}
}
// Clear mergee's dependents so onCraftingJobFinished doesn't process them
dependents.remove(mergee.getId());
mergee.getDependentCraftingJobs().clear();
}
// Remove the crafting job from the graph
this.onCraftingJobFinished(mergee, true);
}
}
public static CompoundTag serialize(HolderLookup.Provider lookupProvider, CraftingJobDependencyGraph graph) {
CompoundTag tag = new CompoundTag();
ListTag craftingJobs = new ListTag();
for (CraftingJob craftingJob : graph.getCraftingJobs()) {
craftingJobs.add(CraftingJob.serialize(lookupProvider, craftingJob));
}
tag.put("craftingJobs", craftingJobs);
CompoundTag dependencies = new CompoundTag();
for (CraftingJob craftingJob : graph.getCraftingJobs()) {
IntCollection intCollection = graph.dependencies.get(craftingJob.getId());
if (intCollection != null) {
dependencies.put(Integer.toString(craftingJob.getId()), new IntArrayTag(intCollection.toIntArray()));
}
}
tag.put("dependencies", dependencies);
CompoundTag dependents = new CompoundTag();
for (CraftingJob craftingJob : graph.getCraftingJobs()) {
IntCollection intCollection = graph.dependents.get(craftingJob.getId());
if (intCollection != null) {
dependents.put(Integer.toString(craftingJob.getId()), new IntArrayTag(intCollection.toIntArray()));
}
}
tag.put("dependents", dependents);
return tag;
}
public static CraftingJobDependencyGraph deserialize(HolderLookup.Provider lookupProvider, CompoundTag tag) {
if (!tag.contains("craftingJobs", Tag.TAG_LIST)) {
throw new IllegalArgumentException("Could not find a craftingJobs entry in the given tag");
}
if (!tag.contains("dependencies", Tag.TAG_COMPOUND)) {
throw new IllegalArgumentException("Could not find a dependencies entry in the given tag");
}
if (!tag.contains("dependents", Tag.TAG_COMPOUND)) {
throw new IllegalArgumentException("Could not find a dependents entry in the given tag");
}
Int2ObjectMap<CraftingJob> craftingJobs = new Int2ObjectOpenHashMap<>();
ListTag craftingJobsTag = tag.getList("craftingJobs", Tag.TAG_COMPOUND);
for (int i = 0; i < craftingJobsTag.size(); i++) {
CraftingJob craftingJob = CraftingJob.deserialize(lookupProvider, craftingJobsTag.getCompound(i));
craftingJobs.put(craftingJob.getId(), craftingJob);
}
Int2ObjectMap<IntCollection> dependencies = new Int2ObjectOpenHashMap<>();
CompoundTag dependenciesTag = tag.getCompound("dependencies");
for (String key : dependenciesTag.getAllKeys()) {
int id = Integer.parseInt(key);
int[] value = dependenciesTag.getIntArray(key);
dependencies.put(id, new IntArrayList(value));
}
Int2ObjectMap<IntCollection> dependents = new Int2ObjectOpenHashMap<>();
CompoundTag dependentsTag = tag.getCompound("dependents");
for (String key : dependentsTag.getAllKeys()) {
int id = Integer.parseInt(key);
int[] value = dependentsTag.getIntArray(key);
dependents.put(id, new IntArrayList(value));
}
return new CraftingJobDependencyGraph(craftingJobs, dependencies, dependents);
}
}