-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathExecutingCraftingJob.java
151 lines (130 loc) · 5.86 KB
/
ExecutingCraftingJob.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
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2021, TeamAppliedEnergistics, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.crafting.execution;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import appeng.api.config.Actionable;
import appeng.api.crafting.IPatternDetails;
import appeng.api.crafting.PatternDetailsHelper;
import appeng.api.networking.IGrid;
import appeng.api.networking.crafting.ICraftingPlan;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.GenericStack;
import appeng.crafting.CraftingLink;
import appeng.crafting.inv.ListCraftingInventory;
import appeng.me.service.CraftingService;
public class ExecutingCraftingJob {
private static final String NBT_LINK = "link";
private static final String NBT_PLAYER_ID = "playerId";
private static final String NBT_FINAL_OUTPUT = "finalOutput";
private static final String NBT_WAITING_FOR = "waitingFor";
private static final String NBT_TIME_TRACKER = "timeTracker";
private static final String NBT_REMAINING_AMOUNT = "remainingAmount";
private static final String NBT_TASKS = "tasks";
private static final String NBT_CRAFTING_PROGRESS = "#craftingProgress";
final CraftingLink link;
final ListCraftingInventory waitingFor;
final Map<IPatternDetails, TaskProgress> tasks = new HashMap<>();
final ElapsedTimeTracker timeTracker;
GenericStack finalOutput;
long remainingAmount;
@Nullable
Integer playerId;
@FunctionalInterface
interface CraftingDifferenceListener {
void onCraftingDifference(AEKey what);
}
ExecutingCraftingJob(ICraftingPlan plan, CraftingDifferenceListener postCraftingDifference, CraftingLink link,
@Nullable Integer playerId) {
this.finalOutput = plan.finalOutput();
this.remainingAmount = this.finalOutput.amount();
this.waitingFor = new ListCraftingInventory(postCraftingDifference::onCraftingDifference);
// Fill waiting for and tasks
long totalPending = 0;
for (var entry : plan.emittedItems()) {
waitingFor.insert(entry.getKey(), entry.getLongValue(), Actionable.MODULATE);
totalPending += entry.getLongValue();
}
for (var entry : plan.patternTimes().entrySet()) {
tasks.computeIfAbsent(entry.getKey(), p -> new TaskProgress()).value += entry.getValue();
for (var output : entry.getKey().getOutputs()) {
totalPending += output.amount() * entry.getValue();
}
}
this.timeTracker = new ElapsedTimeTracker(totalPending);
this.link = link;
this.playerId = playerId;
}
ExecutingCraftingJob(CompoundTag data, CraftingDifferenceListener postCraftingDifference, CraftingCpuLogic cpu) {
this.link = new CraftingLink(data.getCompound(NBT_LINK), cpu.cluster);
IGrid grid = cpu.cluster.getGrid();
if (grid != null) {
((CraftingService) grid.getCraftingService()).addLink(link);
}
this.finalOutput = GenericStack.readTag(data.getCompound(NBT_FINAL_OUTPUT));
this.remainingAmount = data.getLong(NBT_REMAINING_AMOUNT);
this.waitingFor = new ListCraftingInventory(postCraftingDifference::onCraftingDifference);
this.waitingFor.readFromNBT(data.getList(NBT_WAITING_FOR, Tag.TAG_COMPOUND));
this.timeTracker = new ElapsedTimeTracker(data.getCompound(NBT_TIME_TRACKER));
if (data.contains(NBT_PLAYER_ID, Tag.TAG_INT)) {
this.playerId = data.getInt(NBT_PLAYER_ID);
} else {
this.playerId = null;
}
ListTag tasksTag = data.getList(NBT_TASKS, Tag.TAG_COMPOUND);
for (int i = 0; i < tasksTag.size(); ++i) {
final CompoundTag item = tasksTag.getCompound(i);
var pattern = AEItemKey.fromTag(item);
var details = PatternDetailsHelper.decodePattern(pattern, cpu.cluster.getLevel());
if (details != null) {
final TaskProgress tp = new TaskProgress();
tp.value = item.getLong(NBT_CRAFTING_PROGRESS);
this.tasks.put(details, tp);
}
}
}
CompoundTag writeToNBT() {
CompoundTag data = new CompoundTag();
CompoundTag linkData = new CompoundTag();
link.writeToNBT(linkData);
data.put(NBT_LINK, linkData);
data.put(NBT_FINAL_OUTPUT, GenericStack.writeTag(finalOutput));
data.put(NBT_WAITING_FOR, waitingFor.writeToNBT());
data.put(NBT_TIME_TRACKER, timeTracker.writeToNBT());
final ListTag list = new ListTag();
for (var e : this.tasks.entrySet()) {
var item = e.getKey().getDefinition().toTag();
item.putLong(NBT_CRAFTING_PROGRESS, e.getValue().value);
list.add(item);
}
data.put(NBT_TASKS, list);
data.putLong(NBT_REMAINING_AMOUNT, remainingAmount);
if (this.playerId != null) {
data.putInt(NBT_PLAYER_ID, this.playerId);
}
return data;
}
static class TaskProgress {
long value = 0;
}
}