-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathPipeModel.java
More file actions
435 lines (399 loc) · 20 KB
/
PipeModel.java
File metadata and controls
435 lines (399 loc) · 20 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package com.gregtechceu.gtceu.client.model.pipe;
import com.gregtechceu.gtceu.api.block.PipeBlock;
import com.gregtechceu.gtceu.api.block.property.GTBlockStateProperties;
import com.gregtechceu.gtceu.api.registry.registrate.GTBlockBuilder;
import com.gregtechceu.gtceu.api.registry.registrate.provider.GTBlockstateProvider;
import com.gregtechceu.gtceu.data.model.builder.PipeModelBuilder;
import com.gregtechceu.gtceu.data.pack.event.RegisterDynamicResourcesEvent;
import com.gregtechceu.gtceu.utils.GTMath;
import com.gregtechceu.gtceu.utils.GTUtil;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.models.blockstates.MultiVariantGenerator;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.model.generators.*;
import com.tterrag.registrate.util.nullness.NonNullBiConsumer;
import it.unimi.dsi.fastutil.objects.Reference2FloatMap;
import it.unimi.dsi.fastutil.objects.Reference2FloatOpenHashMap;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* This is an automatic pipe model generator.
*
* <h2>For material pipes</h2>
* If the pipe this model belongs to is generated from a material property (or equivalent),
* you should call {@link #dynamicModel()}, which adds the model to {@link #DYNAMIC_MODELS}
* and automatically processes it as a part of runtime asset generation.
* <p>
* <strong style="font-size:17">NOTE:</strong><br>
* You must also initialize the models in an {@link RegisterDynamicResourcesEvent} listener as such:
*
* <pre>
* {@code
*
* // in a @EventBusSubscriber-annotated class
* @SubscribeEvent
* public static void registerDynamicAssets(RegisterDynamicResourcesEvent event) {
* for (var block : YourBlocks.YOUR_PIPE_BLOCKS.values()) {
* if (block == null) continue;
* block.get().createPipeModel(RuntimeExistingFileHelper.INSTANCE).dynamicModel();
* }
* }
* }
* </pre>
*
* Remember to replace {@code YourBlocks.YOUR_PIPE_BLOCKS.values()} with a reference to your pipe block collection!
* </p>
*
* <h2>For non-material pipes</h2>
* Conversely, if the pipe is <strong>not</strong> generated, but has a constant set of variants (such as optical fiber
* cables),
* you should <strong>NOT</strong> use {@link #dynamicModel()} and instead set the model with
* {@link GTBlockBuilder#gtBlockstate(NonNullBiConsumer)} as such:
*
* <pre>
* {@code
* // on your pipe block builder
* ... = REGISTRATE.block(...)
* .properties(...)
* .gtBlockstate(GTModels::createPipeBlockModel)
* ...more builder things...
* .item(...)
* .model(NonNullBiConsumer.noop())
* ...more builder things...
* }
* </pre>
*
* This makes the pipe model(s) be generated for you without having to process them at runtime.
*
*/
public class PipeModel {
// spotless:off
public static final String
SIDE_KEY = "side",
END_KEY = "end",
SIDE_SECONDARY_KEY = "side_secondary",
END_SECONDARY_KEY = "end_secondary",
SIDE_OVERLAY_KEY = "side_overlay",
END_OVERLAY_KEY = "end_overlay";
// spotless:on
protected static final ResourceLocation RENDERTYPE_CUTOUT_MIPPED = new ResourceLocation("cutout_mipped");
public static final Set<PipeModel> DYNAMIC_MODELS = new HashSet<>();
public static void initDynamicModels() {
for (PipeModel generator : DYNAMIC_MODELS) {
generator.initModels();
}
}
@Getter
protected final PipeBlock<?, ?, ?> block;
public final @NotNull ResourceLocation blockId;
protected final GTBlockstateProvider provider;
/**
* The pipe's "thickness" in the (0,16] voxel range, where 1 is 1 voxel and 16 is a full block thick
*/
protected final float thickness;
/**
* The pipe model's 'minimum' coordinate in the (0,16] voxel range.<br>
* This is ex. the height of the center part's bottom edge.
*/
protected final float minCoord;
/**
* The pipe model's 'maximum' coordinate in the (0,16] voxel range.<br>
* This is ex. the height of the center part's top edge.
*/
protected final float maxCoord;
@Setter
public ResourceLocation side, end;
@Setter
public @Nullable ResourceLocation sideSecondary, endSecondary;
@Setter
public @Nullable ResourceLocation sideOverlay, endOverlay;
/// Use {@link #getOrCreateBlockModel()} instead of referencing this field directly.
private BlockModelBuilder blockModel;
/// Use {@link #getOrCreateItemModel()} instead of referencing this field directly.
private ItemModelBuilder itemModel;
/// Use {@link #getOrCreateCenterElement()} instead of referencing this field directly.
private BlockModelBuilder centerElement;
/// Use {@link #getOrCreateConnectionElement()} instead of referencing this field directly.
private BlockModelBuilder connectionElement;
public PipeModel(PipeBlock<?, ?, ?> block, GTBlockstateProvider provider,
float thickness, ResourceLocation side, ResourceLocation end) {
this.block = block;
this.blockId = BuiltInRegistries.BLOCK.getKey(this.block);
this.provider = provider;
// assume thickness is in the 0-1 range
this.thickness = thickness * 16.0f;
this.side = side;
this.end = end;
this.minCoord = (16.0f - this.thickness) / 2.0f;
this.maxCoord = this.minCoord + this.thickness;
}
public final void dynamicModel() {
DYNAMIC_MODELS.add(this);
}
/**
* Initialize all models that are required for this block model to exist.<br>
* <i>Order is important!</i> Dependent models must be initialized <strong>after</strong> their dependencies.
*
* @see #getOrCreateBlockModel()
* @see #getOrCreateCenterElement()
* @see #getOrCreateConnectionElement()
*/
@MustBeInvokedByOverriders
public void initModels() {
getOrCreateCenterElement();
getOrCreateConnectionElement();
getOrCreateBlockModel();
createBlockState();
getOrCreateItemModel();
}
/**
* Override this to change the actual model {@link #block this.block} will use.
*
* @return A model builder for the block's actual model.
* @see #getOrCreateCenterElement()
* @see #getOrCreateConnectionElement()
*/
@ApiStatus.OverrideOnly
protected BlockModelBuilder getOrCreateBlockModel() {
if (this.blockModel == null) {
// spotless:off
this.blockModel = this.provider.models().getBuilder(this.blockId.toString())
// make the "default" model be based on the center part's model
.parent(this.getOrCreateCenterElement())
.customLoader(PipeModelBuilder.begin(this.thickness, this.provider))
.centerModels(this.getOrCreateCenterElement().getLocation())
.connectionModels(this.getOrCreateConnectionElement().getLocation())
.end();
// spotless:on
}
return this.blockModel;
}
/**
* Override this to change the center element's model.
*
* @return A model builder for the center element's model.
* @see #getOrCreateBlockModel()
* @see #getOrCreateConnectionElement()
*/
@ApiStatus.OverrideOnly
protected BlockModelBuilder getOrCreateCenterElement() {
if (this.centerElement == null) {
this.centerElement = makeElementModel(this.blockId.withPath(path -> "block/pipe/" + path + "/center"),
null, minCoord, minCoord, minCoord, maxCoord, maxCoord, maxCoord);
}
return this.centerElement;
}
/**
* Override this to change the 'connection' element's model.<br>
* By default, this is rotated & used for all connected sides of the pipe.<br>
* Note that that is not a hard requirement, and that you may set a model per side in
* {@link #getOrCreateBlockModel()}.
*
* @return A model builder for the connection element's model.
* @see #getOrCreateBlockModel()
* @see #getOrCreateCenterElement()
*/
@ApiStatus.OverrideOnly
protected BlockModelBuilder getOrCreateConnectionElement() {
if (this.connectionElement == null) {
this.connectionElement = makeElementModel(
this.blockId.withPath(path -> "block/pipe/" + path + "/connection"),
Direction.DOWN, minCoord, 0, minCoord, maxCoord, minCoord, maxCoord);
}
return this.connectionElement;
}
/**
* Override this to change the item model.<br>
* By default, this creates a version of the pipe block model with the north & south sides 'connected'.
*
* @return The item model builder.
* @see #getOrCreateBlockModel()
*/
@ApiStatus.OverrideOnly
protected ItemModelBuilder getOrCreateItemModel() {
if (this.itemModel == null) {
this.itemModel = createItemModel(this.blockId, this.minCoord, this.maxCoord);
}
return this.itemModel;
}
/**
* Override this to change the block state set {@link #block this.block} will use.<br>
* By default, this creates a simple block state with no properties.<br>
* The activable pipes (laser & optical) use this to add a model for the
* {@link GTBlockStateProperties#ACTIVE "active"} state of the blocks.
*
* @return The block state generator, usually a {@link MultiVariantGenerator}.
* @see #getOrCreateBlockModel()
* @see ActivablePipeModel#createBlockState()
*/
@ApiStatus.OverrideOnly
public IGeneratedBlockState createBlockState() {
// spotless:off
return this.provider.getVariantBuilder(this.getBlock())
.partialState()
.modelForState()
.modelFile(this.provider.models().getExistingFile(this.blockId))
.addModel();
// spotless:on
}
/**
* Creates an item model based on the block model that extends to the north/south end of the block space.
*
* @param name The resulting model's path.
* @param min The minimum X/Y coordinate.
* @param max The maximum X/Y coordinate.
* @return An item model builder.
*/
protected ItemModelBuilder createItemModel(ResourceLocation name, float min, float max) {
ItemModelBuilder model = this.provider.itemModels().getBuilder(name.toString())
.parent(this.getOrCreateCenterElement());
makePartModelElement(model, Direction.NORTH, false, true, 0.0f, 0, 1,
min, min, 0, max, max, 16, this.side, this.end, SIDE_KEY, END_KEY);
makePartModelElement(model, Direction.NORTH, true, true, 0.001f, 0, 1,
min, min, 0, max, max, 16, this.sideSecondary, this.endSecondary, SIDE_SECONDARY_KEY,
END_SECONDARY_KEY);
makePartModelElement(model, Direction.NORTH, true, true, 0.002f, 2, 2,
min, min, 0, max, max, 16, this.sideOverlay, this.endOverlay, SIDE_OVERLAY_KEY, END_OVERLAY_KEY);
return model;
}
/**
* Fills out a model builder with applicable pipe model elements and returns it for further use
*
* @param name the resulting model's path
* @param endFace the model face that's being created
* @param x1 min X coordinate in the range [-16,32]
* @param y1 min Y coordinate in the range [-16,32]
* @param z1 min Z coordinate in the range [-16,32]
* @param x2 max X coordinate in the range [-16,32]
* @param y2 max Y coordinate in the range [-16,32]
* @param z2 max Z coordinate in the range [-16,32]
* @implNote The coordinates must be in the correct order or the resulting model's cubes will be inside out!
*/
protected BlockModelBuilder makeElementModel(ResourceLocation name, @Nullable Direction endFace,
final float x1, final float y1, final float z1,
final float x2, final float y2, final float z2) {
BlockModelBuilder model = this.provider.models().getBuilder(name.toString())
.parent(new ModelFile.UncheckedModelFile("block/block"))
.texture("particle", "#" + (this.side != null ? SIDE_KEY : END_KEY))
.renderType(RENDERTYPE_CUTOUT_MIPPED);
makePartModelElement(model, endFace, false, 0.0f, 0, 1,
x1, y1, z1, x2, y2, z2, this.side, this.end, SIDE_KEY, END_KEY);
makePartModelElement(model, endFace, true, 0.001f, 0, 1,
x1, y1, z1, x2, y2, z2, this.sideSecondary, this.endSecondary, "side_secondary", "end_secondary");
makePartModelElement(model, endFace, true, 0.002f, 2, 2,
x1, y1, z1, x2, y2, z2, this.sideOverlay, this.endOverlay, "side_overlay", "end_overlay");
return model;
}
protected <T extends ModelBuilder<T>> void makePartModelElement(T model, @Nullable Direction endFace,
boolean useEndWithFullCube,
float offset, int sideTintIndex, int endTintIndex,
final float x1, final float y1, final float z1,
final float x2, final float y2, final float z2,
@Nullable ResourceLocation sideTexture,
@Nullable ResourceLocation endTexture,
String sideKey, String endKey) {
makePartModelElement(model, endFace, useEndWithFullCube, false,
offset, sideTintIndex, endTintIndex, x1, y1, z1, x2, y2, z2,
sideTexture, endTexture, sideKey, endKey);
}
protected <T extends ModelBuilder<T>> void makePartModelElement(T model, @Nullable Direction endFace,
boolean useEndWithFullCube, boolean alwaysAddEnd,
float offset, int sideTintIndex, int endTintIndex,
final float x1, final float y1, final float z1,
final float x2, final float y2, final float z2,
@Nullable ResourceLocation sideTexture,
@Nullable ResourceLocation endTexture,
String sideKey, String endKey) {
makePartModelElement(model, endFace, useEndWithFullCube, alwaysAddEnd,
offset, sideTintIndex, endTintIndex, x1, y1, z1, x2, y2, z2,
sideTexture, endTexture, sideKey, endKey, (face, texture, builder) -> {});
}
protected <T extends ModelBuilder<T>> void makePartModelElement(T model, @Nullable Direction endFace,
boolean useEndWithFullCube, boolean alwaysAddEnd,
float offset, int sideTintIndex, int endTintIndex,
final float x1, final float y1, final float z1,
final float x2, final float y2, final float z2,
@Nullable ResourceLocation sideTexture,
@Nullable ResourceLocation endTexture,
String sideKey, String endKey,
FaceConfigurator<T> faceConfigurator) {
if (sideTexture == null && (endFace == null || endTexture == null)) {
return;
}
if (sideTexture != null) model.texture(sideKey, sideTexture);
if (endFace != null && endTexture != null) model.texture(endKey, endTexture);
boolean fullCube = !useEndWithFullCube &&
(x1 == y1 && x1 == z1 && x1 <= 0.0f) &&
(x2 == y2 && x2 == z2 && x2 >= 16.0f);
ModelBuilder<T>.ElementBuilder element = model.element()
.from(x1 - offset, y1 - offset, z1 - offset)
.to(x2 + offset, y2 + offset, z2 + offset);
for (Direction dir : GTUtil.DIRECTIONS) {
if (!alwaysAddEnd && endFace == dir.getOpposite()) {
// skip the inside face
continue;
}
boolean isEnd = !fullCube && endFace == dir;
if (isEnd && endTexture != null) {
var face = element.face(dir).cullface(dir).texture("#" + endKey).tintindex(endTintIndex);
faceConfigurator.accept(dir, endKey, face);
} else if (!isEnd && sideTexture != null) {
var face = element.face(dir).texture("#" + sideKey).tintindex(sideTintIndex);
faceConfigurator.accept(dir, sideKey, face);
}
}
}
protected final Reference2FloatMap<Direction> makeFaceEndpointMap(final float x1, final float y1, final float z1,
final float x2, final float y2, final float z2) {
Reference2FloatMap<Direction> faceEndpoints = new Reference2FloatOpenHashMap<>();
faceEndpoints.defaultReturnValue(GTMath.max(x1, y1, z1, x2, y2, z2));
for (Direction dir : GTUtil.DIRECTIONS) {
faceEndpoints.put(dir, switch (dir) {
case DOWN -> Math.min(y1, y2);
case UP -> Math.max(y1, y2);
case NORTH -> Math.min(z1, z2);
case SOUTH -> Math.max(z1, z2);
case WEST -> Math.min(x1, x2);
case EAST -> Math.max(x1, x2);
});
}
return faceEndpoints;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof PipeModel pipeModel)) return false;
return block == pipeModel.block &&
Objects.equals(side, pipeModel.side) &&
Objects.equals(end, pipeModel.end) &&
Objects.equals(sideSecondary, pipeModel.sideSecondary) &&
Objects.equals(endSecondary, pipeModel.endSecondary) &&
Objects.equals(sideOverlay, pipeModel.sideOverlay) &&
Objects.equals(endOverlay, pipeModel.endOverlay);
}
@Override
public int hashCode() {
return Objects.hash(block, side, end, sideSecondary, endSecondary, sideOverlay, endOverlay);
}
@FunctionalInterface
public interface FaceConfigurator<T extends ModelBuilder<T>> {
/**
* This is a callback for modifying a block element face builder in ways not supported by "basic" API.<br>
* For example, you can make faces emissive, like {@link ActivablePipeModel#makePartModelElement}.
*
* @param face The normal direction of this face.
* @param texture The texture of the face, usually in {@code #reference} format.
* <b>Note that the String does NOT begin with {@code #}</b>.
* @param builder The face builder.
* @see #makePartModelElement(ModelBuilder, Direction, boolean, float, int, int, float, float, float, float,
* float, float, ResourceLocation, ResourceLocation, String, String)
* ActivablePipeModel.makePartModelElement
*/
void accept(Direction face, String texture, ModelBuilder<T>.ElementBuilder.FaceBuilder builder);
}
}