-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathWorkableTieredMachine.java
More file actions
222 lines (195 loc) · 8.3 KB
/
WorkableTieredMachine.java
File metadata and controls
222 lines (195 loc) · 8.3 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
package com.gregtechceu.gtceu.api.machine;
import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.blockentity.BlockEntityCreationInfo;
import com.gregtechceu.gtceu.api.capability.recipe.*;
import com.gregtechceu.gtceu.api.machine.feature.*;
import com.gregtechceu.gtceu.api.machine.trait.*;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.sync_system.annotations.SaveField;
import com.gregtechceu.gtceu.api.sync_system.annotations.SyncToClient;
import com.gregtechceu.gtceu.utils.GTUtil;
import com.gregtechceu.gtceu.utils.ISubscription;
import com.mojang.blaze3d.MethodsReturnNonnullByDefault;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;
import java.util.*;
import java.util.function.Function;
import javax.annotation.ParametersAreNonnullByDefault;
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public abstract class WorkableTieredMachine extends TieredEnergyMachine implements IRecipeLogicMachine,
IMachineLife, IMufflableMachine, IOverclockMachine {
@Getter
@SaveField
@SyncToClient
public final RecipeLogic recipeLogic;
@Getter
public final GTRecipeType[] recipeTypes;
@Getter
@Setter
@SaveField
public int activeRecipeType;
@Getter
protected final CleanroomReceiverTrait cleanroomReceiver;
@SaveField
public final NotifiableItemStackHandler importItems, exportItems;
@SaveField
public final NotifiableFluidTank importFluids, exportFluids;
@SaveField
public final NotifiableComputationContainer importComputation, exportComputation;
@Getter
protected final Map<IO, List<RecipeHandlerList>> capabilitiesProxy;
@Getter
protected final Map<IO, Map<RecipeCapability<?>, List<IRecipeHandler<?>>>> capabilitiesFlat;
@SaveField
@Getter
protected int overclockTier;
protected final List<ISubscription> traitSubscriptions;
@SaveField
@SyncToClient
@Getter
protected boolean isMuffled;
protected boolean previouslyMuffled = true;
public WorkableTieredMachine(BlockEntityCreationInfo info, int tier,
Function<WorkableTieredMachine, RecipeLogic> recipeLogicSupplier, int importSlots,
int exportSlots,
int fluidImportSlots, int fluidExportSlots, Int2IntFunction tankScalingFunction) {
super(info, tier);
this.overclockTier = getMaxOverclockTier();
this.recipeTypes = getDefinition().getRecipeTypes();
this.activeRecipeType = 0;
this.capabilitiesProxy = new EnumMap<>(IO.class);
this.capabilitiesFlat = new EnumMap<>(IO.class);
this.traitSubscriptions = new ArrayList<>();
this.cleanroomReceiver = new CleanroomReceiverTrait(this);
this.recipeLogic = recipeLogicSupplier.apply(this);
this.importItems = new NotifiableItemStackHandler(this, importSlots, IO.IN, IO.BOTH);
this.exportItems = new NotifiableItemStackHandler(this, exportSlots, IO.OUT);
this.importFluids = new NotifiableFluidTank(this, fluidImportSlots, tankScalingFunction.applyAsInt(getTier()),
IO.IN, IO.BOTH);
this.exportFluids = new NotifiableFluidTank(this, fluidExportSlots, tankScalingFunction.applyAsInt(getTier()),
IO.OUT);
this.importComputation = new NotifiableComputationContainer(this, IO.IN, true);
this.exportComputation = new NotifiableComputationContainer(this, IO.OUT, false);
}
public WorkableTieredMachine(BlockEntityCreationInfo info, int tier, Int2IntFunction tankScalingFunction) {
super(info, tier);
this.overclockTier = getMaxOverclockTier();
this.recipeTypes = getDefinition().getRecipeTypes();
this.activeRecipeType = 0;
this.capabilitiesProxy = new EnumMap<>(IO.class);
this.capabilitiesFlat = new EnumMap<>(IO.class);
this.traitSubscriptions = new ArrayList<>();
this.cleanroomReceiver = new CleanroomReceiverTrait(this);
this.recipeLogic = new RecipeLogic(this);
this.importItems = new NotifiableItemStackHandler(this, getRecipeType().getMaxInputs(ItemRecipeCapability.CAP),
IO.IN);
this.exportItems = new NotifiableItemStackHandler(this, getRecipeType().getMaxOutputs(ItemRecipeCapability.CAP),
IO.OUT);
this.importFluids = new NotifiableFluidTank(this, getRecipeType().getMaxInputs(FluidRecipeCapability.CAP),
tankScalingFunction.applyAsInt(getTier()), IO.IN);
this.exportFluids = new NotifiableFluidTank(this, getRecipeType().getMaxOutputs(FluidRecipeCapability.CAP),
tankScalingFunction.applyAsInt(getTier()), IO.OUT);
this.importComputation = new NotifiableComputationContainer(this, IO.IN, true);
this.exportComputation = new NotifiableComputationContainer(this, IO.OUT, false);
}
//////////////////////////////////////
// ***** Initialization ******//
//////////////////////////////////////
@Override
public void onLoad() {
super.onLoad();
// attach self traits
Map<IO, List<IRecipeHandler<?>>> ioTraits = new EnumMap<>(IO.class);
for (MachineTrait trait : traitHolder.getAllTraits()) {
if (trait instanceof IRecipeHandlerTrait<?> handlerTrait) {
ioTraits.computeIfAbsent(handlerTrait.getHandlerIO(), i -> new ArrayList<>()).add(handlerTrait);
}
}
for (var entry : ioTraits.entrySet()) {
var handlerList = RecipeHandlerList.of(entry.getKey(), entry.getValue());
this.addHandlerList(handlerList);
traitSubscriptions.add(handlerList.subscribe(recipeLogic::updateTickSubscription));
}
}
@Override
public void onUnload() {
super.onUnload();
traitSubscriptions.forEach(ISubscription::unsubscribe);
traitSubscriptions.clear();
capabilitiesProxy.clear();
capabilitiesFlat.clear();
recipeLogic.inValid();
}
//////////////////////////////////////
// ********** MISC ***********//
//////////////////////////////////////
@Override
public void onMachineRemoved() {
clearInventory(importItems.storage);
clearInventory(exportItems.storage);
}
public void setMuffled(boolean muffled) {
isMuffled = muffled;
syncDataHolder.markClientSyncFieldDirty("isMuffled");
}
//////////////////////////////////////
// ******** OVERCLOCK *********//
//////////////////////////////////////
@Override
public int getMaxOverclockTier() {
return GTUtil.getTierByVoltage(Math.max(energyContainer.getInputVoltage(), energyContainer.getOutputVoltage()));
}
@Override
public int getMinOverclockTier() {
return 0;
}
@Override
public void setOverclockTier(int tier) {
if (!isRemote() && tier >= getMinOverclockTier() && tier <= getMaxOverclockTier()) {
this.overclockTier = tier;
this.recipeLogic.markLastRecipeDirty();
}
}
@Override
public long getOverclockVoltage() {
return Math.min(GTValues.V[getOverclockTier()],
Math.max(energyContainer.getInputVoltage(), energyContainer.getOutputVoltage()));
}
//////////////////////////////////////
// ****** RECIPE LOGIC *******//
//////////////////////////////////////
@Override
public void clientTick() {
super.clientTick();
if (previouslyMuffled != isMuffled) {
previouslyMuffled = isMuffled;
if (recipeLogic != null)
recipeLogic.updateSound();
}
}
@Override
public boolean keepSubscribing() {
return false;
}
@NotNull
public GTRecipeType getRecipeType() {
return recipeTypes[activeRecipeType];
}
/**
* Sets a recipe type of the machine.
* FOR INTERNAL / TESTING USE ONLY!
* NOT SUPPORTED FOR PRODUCTION USE!
*
* @param newType The new recipe type
*/
@ApiStatus.Internal
@VisibleForTesting
public void setRecipeType(GTRecipeType newType) {
recipeTypes[activeRecipeType] = newType;
}
}