• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / IntegratedDynamics / 20374407946

19 Dec 2025 03:19PM UTC coverage: 53.067% (-0.005%) from 53.072%
20374407946

push

github

rubensworks
Remove deprecated IIngredientComponentValueHandler

2852 of 8730 branches covered (32.67%)

Branch coverage included in aggregate %.

17389 of 29412 relevant lines covered (59.12%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/cyclops/integrateddynamics/client/model/CableModelBase.java
1
package org.cyclops.integrateddynamics.client.model;
2

3
import com.google.common.cache.Cache;
4
import com.google.common.cache.CacheBuilder;
5
import com.google.common.collect.ImmutableMap;
6
import com.google.common.collect.Lists;
7
import com.mojang.math.Quadrant;
8
import net.minecraft.client.model.geom.builders.UVPair;
9
import net.minecraft.client.renderer.block.model.*;
10
import net.minecraft.client.renderer.chunk.ChunkSectionLayer;
11
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
12
import net.minecraft.client.resources.model.BlockModelRotation;
13
import net.minecraft.client.resources.model.ModelBaker;
14
import net.minecraft.client.resources.model.ModelState;
15
import net.minecraft.core.BlockPos;
16
import net.minecraft.core.Direction;
17
import net.minecraft.util.RandomSource;
18
import net.minecraft.world.entity.ItemOwner;
19
import net.minecraft.world.item.ItemDisplayContext;
20
import net.minecraft.world.item.ItemStack;
21
import net.minecraft.world.level.BlockAndTintGetter;
22
import net.minecraft.world.level.Level;
23
import net.minecraft.world.level.block.state.BlockState;
24
import net.neoforged.neoforge.client.model.quad.BakedColors;
25
import net.neoforged.neoforge.client.model.quad.BakedNormals;
26
import net.neoforged.neoforge.model.data.ModelData;
27
import org.apache.commons.lang3.tuple.Triple;
28
import org.cyclops.cyclopscore.client.model.DelegatingDynamicItemAndBlockModel;
29
import org.cyclops.cyclopscore.helper.IModHelpers;
30
import org.cyclops.cyclopscore.helper.ModelHelpers;
31
import org.cyclops.integrateddynamics.GeneralConfig;
32
import org.cyclops.integrateddynamics.api.part.PartRenderPosition;
33
import org.cyclops.integrateddynamics.block.BlockCableClientConfig;
34
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityMultipartTicking;
35
import org.joml.Vector3f;
36

37
import java.util.List;
38
import java.util.Optional;
39
import java.util.concurrent.TimeUnit;
40
import java.util.stream.Collectors;
41

42
/**
43
 * A base dynamic facadeModel for cables.
44
 * @author rubensworks
45
 */
46
public abstract class CableModelBase extends DelegatingDynamicItemAndBlockModel {
47

48
    public static ModelBaker MODEL_BAKER;
49
    private static final FaceBakery FACE_BAKERY = new FaceBakery();
×
50
    private static final Cache<Triple<IRenderState, Direction, ChunkSectionLayer>, List<BakedQuad>> CACHE_QUADS = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build();
×
51

52
    private static final int RADIUS = 4;
53
    private static final int TEXTURE_SIZE = 16;
54

55
    private static final int LENGTH_CONNECTION = (TEXTURE_SIZE - RADIUS) / 2;
56
    private static final int LENGTH_CONNECTION_LIMITED = 1;
57
    private static final int INV_LENGTH_CONNECTION = TEXTURE_SIZE - LENGTH_CONNECTION;
58
    public static final float MIN = (float) LENGTH_CONNECTION / (float) TEXTURE_SIZE;
59
    public static final float MAX = 1.0F - MIN;
60
    private static final PartRenderPosition CABLE_RENDERPOSITION = new PartRenderPosition(-1,
×
61
            (((float) TEXTURE_SIZE - (float) RADIUS) / 2 / (float) TEXTURE_SIZE),
62
            (float) RADIUS / (float) TEXTURE_SIZE, (float) RADIUS / (float) TEXTURE_SIZE);
63

64
    private final float[][][] quadVertexes = makeQuadVertexes(MIN, MAX, 1.00F);
×
65

66
    protected static final ItemTransforms TRANSFORMS = ModelHelpers.modifyDefaultTransforms(ImmutableMap.of(
×
67
            ItemDisplayContext.FIRST_PERSON_LEFT_HAND, new ItemTransform(
68
                    new Vector3f(0, 45, 0),
69
                    new Vector3f(0, 1f / 32, 0),
70
                    new Vector3f(0.4F, 0.4F, 0.4F)),
71
            ItemDisplayContext.FIRST_PERSON_RIGHT_HAND, new ItemTransform(
72
                    new Vector3f(0, 225, 0),
73
                    new Vector3f(0, 1f / 32, 0),
74
                    new Vector3f(0.4F, 0.4F, 0.4F))
75
    ));
76

77
    public CableModelBase(BlockAndTintGetter level, BlockState blockState, Direction facing, RandomSource rand, ModelData modelData, ChunkSectionLayer renderType) {
78
        super(level, blockState, facing, rand, modelData, renderType);
×
79
    }
×
80

81
    public CableModelBase(ItemStack itemStack, Level world, ItemOwner entity) {
82
        super(itemStack, world, entity);
×
83
    }
×
84

85
    public CableModelBase() {
86
        super();
×
87
    }
×
88

89
    protected static float[][][] makeQuadVertexes(float min, float max, float length) {
90
        return new float[][][]{
×
91
                {
92
                        {min, length, min},
93
                        {max, length, min},
94
                        {max, max   , min},
95
                        {min, max   , min},
96
                },
97
                {
98
                        {min, max   , min},
99
                        {min, max   , max},
100
                        {min, length, max},
101
                        {min, length, min},
102
                },
103
                {
104
                        {min, max   , max},
105
                        {max, max   , max},
106
                        {max, length,  max},
107
                        {min, length, max},
108
                },
109
                {
110
                        {max, length, min},
111
                        {max, length, max},
112
                        {max, max   , max},
113
                        {max, max   , min},
114
                }
115
        };
116
    }
117

118
    private Direction getSideFromVecs(Vector3f a, Vector3f b, Vector3f c) {
119
        int dir = a.y == b.y && b.y == c.y ? 0 : (a.x == b.x && b.x == c.x ? 2 : 4);
×
120
        if (dir == 0) {
×
121
            dir += (c.y >= 0.5) ? 1 : 0;
×
122
        } else if (dir == 2) {
×
123
            dir += (c.x >= 0.5) ? 1 : 0;
×
124
        } else if (dir == 4) {
×
125
            dir += (c.z >= 0.5) ? 1 : 0;
×
126
        }
127
        return Direction.from3DDataValue(dir);
×
128
    }
129

130
    public List<BakedQuad> getFacadeQuads(BlockStateModel facadeModel, BlockState blockState, Direction side, PartRenderPosition partRenderPosition, ChunkSectionLayer renderType) {
131
        List<BakedQuad> originalQuads = Lists.newArrayList();
×
132
        for (BlockModelPart collectPart : facadeModel.collectParts(level, BlockPos.ZERO, blockState, rand)) {
×
133
            if (collectPart.getRenderType(blockState) == renderType) {
×
134
                originalQuads.addAll(collectPart.getQuads(null));
×
135
                originalQuads.addAll(collectPart.getQuads(side));
×
136
            }
137
        }
×
138

139
        return originalQuads.stream()
×
140
                .flatMap(originalQuad -> {
×
141
                    List<BakedQuad> ret = Lists.newLinkedList();
×
142
                    if(partRenderPosition == PartRenderPosition.NONE) {
×
143
                        addFacadeQuad(ret, originalQuad, 0, 0, 1f, 1f, side);
×
144
                    } else {
145
                        float w = partRenderPosition.getWidthFactorSide();
×
146
                        float h = partRenderPosition.getHeightFactorSide();
×
147
                        float u0 = 0f;
×
148
                        float v0 = 0f;
×
149
                        float u1 = (1f - w) / 2;
×
150
                        float v1 = (1f - h) / 2;
×
151
                        float u2 = u1 + w;
×
152
                        float v2 = v1 + h;
×
153
                        float u3 = 1f;
×
154
                        float v3 = 1f;
×
155
                        /*
156
                         * We render the following eight boxes, excluding the part box in the middle.
157
                         * -------
158
                         * |1|2|3|
159
                         * -------
160
                         * |4|P|5|
161
                         * -------
162
                         * |6|7|8|
163
                         * -------
164
                         */
165
                        addFacadeQuad(ret, originalQuad, u0, v0, u1, v1, side); // 1
×
166
                        addFacadeQuad(ret, originalQuad, u1, v0, u2, v1, side); // 2
×
167
                        addFacadeQuad(ret, originalQuad, u2, v0, u3, v1, side); // 3
×
168
                        addFacadeQuad(ret, originalQuad, u0, v1, u1, v2, side); // 4
×
169
                        addFacadeQuad(ret, originalQuad, u2, v1, u3, v2, side); // 5
×
170
                        addFacadeQuad(ret, originalQuad, u0, v2, u1, v3, side); // 6
×
171
                        addFacadeQuad(ret, originalQuad, u1, v2, u2, v3, side); // 7
×
172
                        addFacadeQuad(ret, originalQuad, u2, v2, u3, v3, side); // 8
×
173
                    }
174
                    return ret.stream();
×
175
                })
176
                .collect(Collectors.toList());
×
177
    }
178

179
    private void addFacadeQuad(List<BakedQuad> quads, BakedQuad originalQuad, float u0, float v0, float u1, float v1, Direction side) {
180
        Vector3f from = new Vector3f(u0 * 16f, v0 * 16f, 0f);
×
181
        Vector3f to = new Vector3f(u1 * 16f, v1 * 16f, 0f);
×
182
        TextureAtlasSprite texture = originalQuad.sprite();
×
183
        BlockElementFace.UVs blockFaceUV = new BlockElementFace.UVs(16f - u1 * 16f, 16f - v1 * 16f, 16f - u0 * 16f, 16f - v0 * 16f);
×
184
        Direction NO_FACE_CULLING = null;
×
185
        String DUMMY_TEXTURE_NAME = "";
×
186
        BlockElementFace blockPartFace = new BlockElementFace(NO_FACE_CULLING, originalQuad.tintIndex(), DUMMY_TEXTURE_NAME, blockFaceUV, Quadrant.R0);
×
187
        ModelState transformation = getRotation(side);
×
188
        BlockElementRotation DEFAULT_ROTATION = null;
×
189
        boolean APPLY_SHADING = true;
×
190
        quads.add(FACE_BAKERY.bakeQuad(MODEL_BAKER.parts(), from, to, blockPartFace, texture, Direction.NORTH, transformation, DEFAULT_ROTATION, APPLY_SHADING, 0));
×
191
    }
×
192

193
    public static BlockModelRotation getRotation(Direction facing) {
194
        switch (facing) {
×
195
            case DOWN:  return BlockModelRotation.get(Quadrant.fromXYAngles(Quadrant.R90, Quadrant.R180));
×
196
            case UP:    return BlockModelRotation.get(Quadrant.fromXYAngles(Quadrant.R270, Quadrant.R180));
×
197
            case NORTH: return BlockModelRotation.get(Quadrant.fromXYAngles(Quadrant.R0, Quadrant.R0));
×
198
            case SOUTH: return BlockModelRotation.get(Quadrant.fromXYAngles(Quadrant.R0, Quadrant.R180));
×
199
            case WEST:  return BlockModelRotation.get(Quadrant.fromXYAngles(Quadrant.R0, Quadrant.R270));
×
200
            case EAST:  return BlockModelRotation.get(Quadrant.fromXYAngles(Quadrant.R0, Quadrant.R90));
×
201
        }
202
        throw new IllegalArgumentException(String.valueOf(facing));
×
203
    }
204

205
    protected abstract boolean isRealCable(ModelData modelData);
206
    protected abstract Optional<BlockState> getFacade(ModelData modelData);
207
    protected abstract boolean isConnected(ModelData modelData, Direction side);
208
    protected abstract boolean hasPart(ModelData modelData, Direction side);
209
    protected abstract PartRenderPosition getPartRenderPosition(ModelData modelData, Direction side);
210
    protected abstract boolean shouldRenderParts(ModelData modelData);
211
    protected abstract BlockStateModel getPartModel(ModelData modelData, Direction side);
212
    protected abstract IRenderState getRenderState(ModelData modelData);
213

214
    @Override
215
    public List<BakedQuad> getGeneralQuads() {
216
        Triple<IRenderState, Direction, ChunkSectionLayer> cacheKey = null;
×
217
        List<BakedQuad> cachedQuads = null;
×
218
        if (GeneralConfig.cacheCableModels) {
×
219
            IRenderState renderState = getRenderState(modelData);
×
220
            if (renderState != null) {
×
221
                cacheKey = Triple.of(renderState, this.facing, this.renderType);
×
222
                cachedQuads = CACHE_QUADS.getIfPresent(cacheKey);
×
223
            }
224
        }
225
        if (cachedQuads == null) {
×
226
            List<BakedQuad> ret = Lists.newLinkedList();
×
227
            TextureAtlasSprite texture = particleIcon();
×
228
            Optional<BlockState> blockStateHolder = getFacade(modelData);
×
229
            boolean renderCable = isItemStack() || (isRealCable(modelData) && (
×
230
                    (!blockStateHolder.isPresent() && this.renderType == ChunkSectionLayer.SOLID)
×
231
                            || (blockStateHolder.isPresent() && this.renderType == ChunkSectionLayer.TRANSLUCENT)));
×
232
            for (Direction side : Direction.values()) {
×
233
                boolean isConnected = isItemStack() ? side == Direction.EAST || side == Direction.WEST : isConnected(modelData, side);
×
234
                boolean hasPart = !isItemStack() && hasPart(modelData, side);
×
235
                if (hasPart && shouldRenderParts(modelData)) {
×
236
                    try {
237
                        for (BlockModelPart collectPart : getPartModel(modelData, side).collectParts(level, BlockPos.ZERO, blockState, rand)) {
×
238
                            ret.addAll(collectPart.getQuads(null));
×
239
                        }
×
240
                    } catch (Exception e) {
×
241
                        // Skip rendering this part, could occur when the player is still logging in.
242
                    }
×
243
                }
244
                if (renderCable) {
×
245
                    if (isConnected || hasPart) {
×
246
                        int i = 0;
×
247
                        float[][][] quadVertexes = this.quadVertexes;
×
248
                        if (hasPart) {
×
249
                            PartRenderPosition partRenderPosition = getPartRenderPosition(modelData, side);
×
250
                            float depthFactor = partRenderPosition == PartRenderPosition.NONE ? 0F : partRenderPosition.getDepthFactor();
×
251
                            quadVertexes = makeQuadVertexes(MIN, MAX, 1F - depthFactor);
×
252
                        }
253
                        for (float[][] v : quadVertexes) {
×
254
                            Vector3f v1 = rotate(new Vector3f(v[0][0] - .5f, v[0][1] - .5f, v[0][2] - .5f), side).add(.5f, .5f, .5f);
×
255
                            Vector3f v2 = rotate(new Vector3f(v[1][0] - .5f, v[1][1] - .5f, v[1][2] - .5f), side).add(.5f, .5f, .5f);
×
256
                            Vector3f v3 = rotate(new Vector3f(v[2][0] - .5f, v[2][1] - .5f, v[2][2] - .5f), side).add(.5f, .5f, .5f);
×
257
                            Vector3f v4 = rotate(new Vector3f(v[3][0] - .5f, v[3][1] - .5f, v[3][2] - .5f), side).add(.5f, .5f, .5f);
×
258
                            Direction realSide = getSideFromVecs(v1, v2, v3);
×
259

260
                            boolean invert = i == 2 || i == 1;
×
261
                            int length = hasPart ? LENGTH_CONNECTION_LIMITED : LENGTH_CONNECTION;
×
262

263
                            i++;
×
264
                            ret.add(new BakedQuad(
×
265
                                    v1,
266
                                    v2,
267
                                    v3,
268
                                    v4,
269
                                    UVPair.pack(texture.getU(LENGTH_CONNECTION / 16f), texture.getV(invert ? length / 16f : 0)),
×
270
                                    UVPair.pack(texture.getU(INV_LENGTH_CONNECTION / 16f), texture.getV(invert ? length / 16f : 0)),
×
271
                                    UVPair.pack(texture.getU(INV_LENGTH_CONNECTION / 16f), texture.getV(invert ? 0 : length / 16f)),
×
272
                                    UVPair.pack(texture.getU(LENGTH_CONNECTION / 16f), texture.getV(invert ? 0 : length / 16f)),
×
273
                                    -1,
274
                                    realSide,
275
                                    texture,
276
                                    true,
277
                                    0,
278
                                    BakedNormals.UNSPECIFIED,
279
                                    BakedColors.DEFAULT,
280
                                    true
281
                            ));
282
                        }
283
                    } else {
×
284
                        addBakedQuad(ret, MIN, MAX, MIN, MAX, MAX, texture, side);
×
285
                    }
286
                }
287
            }
288

289
            if (blockStateHolder.isPresent() && shouldRenderParts(modelData) && this.renderType != null && this.facing != null) {
×
290
                BlockStateModel facadeModel = IModHelpers.get().getRenderHelpers().getBakedModel(blockStateHolder.get());
×
291
                boolean isConnected = isItemStack() ? this.facing == Direction.EAST || this.facing == Direction.WEST : isConnected(modelData, this.facing);
×
292
                PartRenderPosition partRenderPosition = PartRenderPosition.NONE;
×
293
                boolean hasPart = !isItemStack() && hasPart(modelData, this.facing);
×
294
                if (hasPart) partRenderPosition = getPartRenderPosition(modelData, this.facing);
×
295
                else if (isConnected) partRenderPosition = CABLE_RENDERPOSITION;
×
296
                ret.addAll(getFacadeQuads(facadeModel, blockStateHolder.get(), this.facing, partRenderPosition, this.renderType));
×
297
            }
298

299
            // Close the cable connections for items
300
            if (isItemStack()) {
×
301
                addBakedQuad(ret, MIN, MAX, MIN, MAX, 1, texture, Direction.EAST);
×
302
                addBakedQuad(ret, MIN, MAX, MIN, MAX, 1, texture, Direction.WEST);
×
303
            }
304
            cachedQuads = ret;
×
305
            if (cacheKey != null) {
×
306
                CACHE_QUADS.put(cacheKey, cachedQuads);
×
307
            }
308
        }
309
        return cachedQuads;
×
310
    }
311

312
    @Override
313
    public TextureAtlasSprite particleIcon() {
314
        return BlockCableClientConfig.BLOCK_TEXTURE;
×
315
    }
316

317
    @Override
318
    public ModelData getModelData(BlockAndTintGetter world, BlockPos pos, BlockState state, ModelData tileData) {
319
        return IModHelpers.get().getBlockEntityHelpers().get(world, pos, BlockEntityMultipartTicking.class)
×
320
                .map(BlockEntityMultipartTicking::getConnectionState)
×
321
                .orElse(ModelData.EMPTY);
×
322
    }
323

324
    @Override
325
    public boolean usesBlockLight() {
326
        return false; // If false, RenderHelper.setupGuiFlatDiffuseLighting() is called
×
327
    }
328

329
    @Override
330
    public ItemTransforms getTopTransforms() {
331
        return TRANSFORMS;
×
332
    }
333

334
    @Override
335
    public List<ChunkSectionLayer> getRenderTypes(BlockState state, RandomSource rand, ModelData data) {
336
        return List.of(ChunkSectionLayer.values());
×
337
    }
338
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc