-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathIMachineBlock.java
More file actions
335 lines (318 loc) · 15.2 KB
/
IMachineBlock.java
File metadata and controls
335 lines (318 loc) · 15.2 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
package com.gregtechceu.gtceu.api.block;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.capability.*;
import com.gregtechceu.gtceu.api.capability.GTCapability;
import com.gregtechceu.gtceu.api.capability.compat.EnergyStorageList;
import com.gregtechceu.gtceu.api.data.RotationState;
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
import com.gregtechceu.gtceu.api.machine.MachineDefinition;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMaintenanceMachine;
import com.gregtechceu.gtceu.api.machine.trait.MachineTrait;
import com.gregtechceu.gtceu.api.machine.trait.RecipeLogic;
import com.gregtechceu.gtceu.api.misc.EnergyContainerList;
import com.gregtechceu.gtceu.api.misc.EnergyInfoProviderList;
import com.gregtechceu.gtceu.api.misc.LaserContainerList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.energy.IEnergyStorage;
import appeng.api.AECapabilities;
import appeng.api.networking.IInWorldGridNodeHost;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public interface IMachineBlock extends EntityBlock {
default Block self() {
return (Block) this;
}
MachineDefinition getDefinition();
default RotationState getRotationState() {
return getDefinition().getRotationState();
}
default Direction getFrontFacing(BlockState state) {
return getRotationState() == RotationState.NONE ? Direction.NORTH : state.getValue(getRotationState().property);
}
@Nullable
default MetaMachine getMachine(BlockGetter level, BlockPos pos) {
return MetaMachine.getMachine(level, pos);
}
static int colorTinted(BlockState blockState, @Nullable BlockAndTintGetter level, @Nullable BlockPos pos,
int index) {
if (level != null && pos != null) {
var machine = MetaMachine.getMachine(level, pos);
if (machine != null) {
return machine.tintColor(index);
}
}
return -1;
}
@Nullable
@Override
default BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return getDefinition().getBlockEntityType().create(pos, state);
}
@Nullable
@Override
default <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state,
BlockEntityType<T> blockEntityType) {
if (blockEntityType == getDefinition().getBlockEntityType()) {
if (!level.isClientSide) {
return (pLevel, pPos, pState, pTile) -> {
if (pTile instanceof IMachineBlockEntity metaMachine) {
metaMachine.getMetaMachine().serverTick();
}
};
} else {
return (pLevel, pPos, pState, pTile) -> {
if (pTile instanceof IMachineBlockEntity metaMachine) {
metaMachine.getMetaMachine().clientTick();
}
};
}
}
return null;
}
default boolean canConnectRedstone(BlockGetter level, BlockPos pos, Direction side) {
return getMachine(level, pos).canConnectRedstone(side);
}
default void attachCapabilities(RegisterCapabilitiesEvent event) {
event.registerBlock(GTCapability.CAPABILITY_COVERABLE, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
return machine.getMetaMachine().getCoverContainer();
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_TOOLABLE, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
return machine.getMetaMachine();
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_WORKABLE, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IWorkable workable) {
return workable;
}
for (MachineTrait trait : machine.getMetaMachine().getTraits()) {
if (trait instanceof IWorkable workable) {
return workable;
}
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_CONTROLLABLE, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IControllable controllable) {
return controllable;
}
for (MachineTrait trait : machine.getMetaMachine().getTraits()) {
if (trait instanceof IControllable controllable) {
return controllable;
}
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_RECIPE_LOGIC, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
for (MachineTrait trait : machine.getMetaMachine().getTraits()) {
if (trait instanceof RecipeLogic recipeLogic) {
return recipeLogic;
}
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_ENERGY_CONTAINER, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IEnergyContainer energyContainer) {
return energyContainer;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side,
IEnergyContainer.class);
if (!list.isEmpty()) {
return list.size() == 1 ? list.getFirst() : new EnergyContainerList(list);
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_ENERGY_INFO_PROVIDER, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IEnergyInfoProvider energyInfoProvider) {
return energyInfoProvider;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side,
IEnergyInfoProvider.class);
if (!list.isEmpty()) {
return list.size() == 1 ? list.getFirst() : new EnergyInfoProviderList(list);
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_CLEANROOM_RECEIVER, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof ICleanroomReceiver cleanroomReceiver) {
return cleanroomReceiver;
}
for (MachineTrait trait : machine.getMetaMachine().getTraits()) {
if (trait instanceof ICleanroomReceiver cleanroomReceiver) {
return cleanroomReceiver;
}
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_CLEANROOM_RECEIVER, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof ICleanroomReceiver workable) {
return workable;
}
for (MachineTrait trait : machine.getMetaMachine().getTraits()) {
if (trait instanceof ICleanroomReceiver workable) {
return workable;
}
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_MAINTENANCE_MACHINE, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IMaintenanceMachine maintenance) {
return maintenance;
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_TURBINE_MACHINE, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof ITurbineMachine turbine) {
return turbine;
}
}
return null;
}, this.self());
event.registerBlock(Capabilities.ItemHandler.BLOCK, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
return machine.getMetaMachine().getItemHandlerCap(side, true);
}
return null;
}, this.self());
event.registerBlock(Capabilities.FluidHandler.BLOCK, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
return machine.getMetaMachine().getFluidHandlerCap(side, true);
}
return null;
}, this.self());
event.registerBlock(Capabilities.EnergyStorage.BLOCK, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IEnergyStorage energyStorage) {
return energyStorage;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side, IEnergyStorage.class);
if (!list.isEmpty()) {
return list.size() == 1 ? list.getFirst() : new EnergyStorageList(list);
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_LASER, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof ILaserContainer laserContainer) {
return laserContainer;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side, ILaserContainer.class);
if (!list.isEmpty()) {
return list.size() == 1 ? list.getFirst() : new LaserContainerList(list);
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_COMPUTATION_PROVIDER, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IOpticalComputationProvider computationProvider) {
return computationProvider;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side,
IOpticalComputationProvider.class);
if (!list.isEmpty()) {
return list.getFirst();
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_DATA_ACCESS, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IDataAccessHatch dataAccess) {
return dataAccess;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side,
IDataAccessHatch.class);
if (!list.isEmpty()) {
return list.getFirst();
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_MONITOR_COMPONENT, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IMonitorComponent monitorComponent) {
return monitorComponent;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), side,
IMonitorComponent.class);
if (!list.isEmpty()) {
return list.getFirst();
}
}
return null;
}, this.self());
event.registerBlock(GTCapability.CAPABILITY_CENTRAL_MONITOR, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof ICentralMonitor centralMonitor) {
return centralMonitor;
}
}
return null;
}, this.self());
if (GTCEu.Mods.isAE2Loaded()) {
event.registerBlock(AECapabilities.IN_WORLD_GRID_NODE_HOST, (level, pos, state, blockEntity, side) -> {
if (blockEntity instanceof IMachineBlockEntity machine) {
if (machine.getMetaMachine() instanceof IInWorldGridNodeHost nodeHost) {
return nodeHost;
}
var list = getCapabilitiesFromTraits(machine.getMetaMachine().getTraits(), null,
IInWorldGridNodeHost.class);
if (!list.isEmpty()) {
// TODO wrap list in the future (or not.)
return list.getFirst();
}
}
return null;
}, this.self());
}
}
static <T> List<T> getCapabilitiesFromTraits(List<MachineTrait> traits, @Nullable Direction accessSide,
Class<T> capability) {
if (traits.isEmpty()) return Collections.emptyList();
List<T> list = new ArrayList<>();
for (MachineTrait trait : traits) {
if (trait.hasCapability(accessSide) && capability.isInstance(trait)) {
list.add(capability.cast(trait));
}
}
return list;
}
}