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

CyclopsMC / IntegratedDynamics / 19267223570

11 Nov 2025 01:30PM UTC coverage: 53.046% (+0.04%) from 53.006%
19267223570

push

github

rubensworks
Merge remote-tracking branch 'origin/master-1.21-lts' into master-1.21

2875 of 8772 branches covered (32.77%)

Branch coverage included in aggregate %.

17354 of 29363 relevant lines covered (59.1%)

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.google.common.primitives.Ints;
8
import com.mojang.math.Quadrant;
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.ModelState;
14
import net.minecraft.core.BlockPos;
15
import net.minecraft.core.Direction;
16
import net.minecraft.util.RandomSource;
17
import net.minecraft.world.entity.LivingEntity;
18
import net.minecraft.world.item.ItemDisplayContext;
19
import net.minecraft.world.item.ItemStack;
20
import net.minecraft.world.level.BlockAndTintGetter;
21
import net.minecraft.world.level.Level;
22
import net.minecraft.world.level.block.state.BlockState;
23
import net.minecraft.world.phys.Vec3;
24
import net.neoforged.neoforge.client.ClientHooks;
25
import net.neoforged.neoforge.model.data.ModelData;
26
import org.apache.commons.lang3.tuple.Triple;
27
import org.cyclops.cyclopscore.client.model.DelegatingDynamicItemAndBlockModel;
28
import org.cyclops.cyclopscore.helper.IModHelpers;
29
import org.cyclops.cyclopscore.helper.ModelHelpers;
30
import org.cyclops.integrateddynamics.GeneralConfig;
31
import org.cyclops.integrateddynamics.api.part.PartRenderPosition;
32
import org.cyclops.integrateddynamics.block.BlockCableClientConfig;
33
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityMultipartTicking;
34
import org.joml.Vector3f;
35

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

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

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

50
    private static final int RADIUS = 4;
51
    private static final int TEXTURE_SIZE = 16;
52

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

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

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

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

79
    public CableModelBase(ItemStack itemStack, Level world, LivingEntity entity) {
80
        super(itemStack, world, entity);
×
81
    }
×
82

83
    public CableModelBase() {
84
        super();
×
85
    }
×
86

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

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

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

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

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

191
    public static BlockModelRotation getRotation(Direction facing) {
192
        switch (facing) {
×
193
            case DOWN:  return BlockModelRotation.X90_Y180;
×
194
            case UP:    return BlockModelRotation.X270_Y180;
×
195
            case NORTH: return BlockModelRotation.X0_Y0;
×
196
            case SOUTH: return BlockModelRotation.X0_Y180;
×
197
            case WEST:  return BlockModelRotation.X0_Y270;
×
198
            case EAST:  return BlockModelRotation.X0_Y90;
×
199
        }
200
        throw new IllegalArgumentException(String.valueOf(facing));
×
201
    }
202

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

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

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

261
                            int[] data = Ints.concat(
×
262
                                    vertexToInts((float) v1.x, (float) v1.y, (float) v1.z, -1, texture,
×
263
                                            LENGTH_CONNECTION, invert ? length : 0),
×
264
                                    vertexToInts((float) v2.x, (float) v2.y, (float) v2.z, -1, texture,
×
265
                                            INV_LENGTH_CONNECTION, invert ? length : 0),
×
266
                                    vertexToInts((float) v3.x, (float) v3.y, (float) v3.z, -1, texture,
×
267
                                            INV_LENGTH_CONNECTION, invert ? 0 : length),
×
268
                                    vertexToInts((float) v4.x, (float) v4.y, (float) v4.z, -1, texture,
×
269
                                            LENGTH_CONNECTION, invert ? 0 : length)
×
270
                            );
271
                            i++;
×
272
                            ClientHooks.fillNormal(data); // This fixes lighting issues when item is rendered in hand/inventory
×
273
                            ret.add(new BakedQuad(data, -1, realSide, texture, true, 0, true));
×
274
                        }
275
                    } else {
×
276
                        addBakedQuad(ret, MIN, MAX, MIN, MAX, MAX, texture, side);
×
277
                    }
278
                }
279
            }
280

281
            if (blockStateHolder.isPresent() && shouldRenderParts(modelData) && this.renderType != null && this.facing != null) {
×
282
                BlockStateModel facadeModel = IModHelpers.get().getRenderHelpers().getBakedModel(blockStateHolder.get());
×
283
                boolean isConnected = isItemStack() ? this.facing == Direction.EAST || this.facing == Direction.WEST : isConnected(modelData, this.facing);
×
284
                PartRenderPosition partRenderPosition = PartRenderPosition.NONE;
×
285
                boolean hasPart = !isItemStack() && hasPart(modelData, this.facing);
×
286
                if (hasPart) partRenderPosition = getPartRenderPosition(modelData, this.facing);
×
287
                else if (isConnected) partRenderPosition = CABLE_RENDERPOSITION;
×
288
                ret.addAll(getFacadeQuads(facadeModel, blockStateHolder.get(), this.facing, partRenderPosition, this.renderType));
×
289
            }
290

291
            // Close the cable connections for items
292
            if (isItemStack()) {
×
293
                addBakedQuad(ret, MIN, MAX, MIN, MAX, 1, texture, Direction.EAST);
×
294
                addBakedQuad(ret, MIN, MAX, MIN, MAX, 1, texture, Direction.WEST);
×
295
            }
296
            cachedQuads = ret;
×
297
            if (cacheKey != null) {
×
298
                CACHE_QUADS.put(cacheKey, cachedQuads);
×
299
            }
300
        }
301
        return cachedQuads;
×
302
    }
303

304
    @Override
305
    public TextureAtlasSprite particleIcon() {
306
        return BlockCableClientConfig.BLOCK_TEXTURE;
×
307
    }
308

309
    @Override
310
    public ModelData getModelData(BlockAndTintGetter world, BlockPos pos, BlockState state, ModelData tileData) {
311
        return IModHelpers.get().getBlockEntityHelpers().get(world, pos, BlockEntityMultipartTicking.class)
×
312
                .map(BlockEntityMultipartTicking::getConnectionState)
×
313
                .orElse(ModelData.EMPTY);
×
314
    }
315

316
    @Override
317
    public boolean usesBlockLight() {
318
        return false; // If false, RenderHelper.setupGuiFlatDiffuseLighting() is called
×
319
    }
320

321
    @Override
322
    public ItemTransforms getTopTransforms() {
323
        return TRANSFORMS;
×
324
    }
325

326
    @Override
327
    public List<ChunkSectionLayer> getRenderTypes(BlockState state, RandomSource rand, ModelData data) {
328
        return List.of(ChunkSectionLayer.values());
×
329
    }
330
}
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