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

CyclopsMC / IntegratedDynamics / 19266698686

11 Nov 2025 01:11PM UTC coverage: 44.848% (-0.006%) from 44.854%
19266698686

push

github

rubensworks
Bump mod version

2582 of 8548 branches covered (30.21%)

Branch coverage included in aggregate %.

11789 of 23496 relevant lines covered (50.17%)

2.38 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.Transformation;
9
import net.minecraft.client.renderer.RenderType;
10
import net.minecraft.client.renderer.block.model.*;
11
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
12
import net.minecraft.client.resources.model.BakedModel;
13
import net.minecraft.client.resources.model.BlockModelRotation;
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.LivingEntity;
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.minecraft.world.phys.Vec3;
25
import net.neoforged.neoforge.client.ChunkRenderTypeSet;
26
import net.neoforged.neoforge.client.ClientHooks;
27
import net.neoforged.neoforge.client.model.SimpleModelState;
28
import net.neoforged.neoforge.client.model.data.ModelData;
29
import org.apache.commons.lang3.tuple.Triple;
30
import org.cyclops.cyclopscore.client.model.DelegatingDynamicItemAndBlockModel;
31
import org.cyclops.cyclopscore.helper.BlockEntityHelpers;
32
import org.cyclops.cyclopscore.helper.ModelHelpers;
33
import org.cyclops.cyclopscore.helper.RenderHelpers;
34
import org.cyclops.integrateddynamics.GeneralConfig;
35
import org.cyclops.integrateddynamics.api.part.PartRenderPosition;
36
import org.cyclops.integrateddynamics.block.BlockCableClientConfig;
37
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityMultipartTicking;
38
import org.jetbrains.annotations.NotNull;
39
import org.joml.Vector3f;
40

41
import javax.annotation.Nonnull;
42
import java.util.List;
43
import java.util.Optional;
44
import java.util.concurrent.TimeUnit;
45
import java.util.stream.Collectors;
46

47
/**
48
 * A base dynamic model for cables.
49
 * @author rubensworks
50
 */
51
public abstract class CableModelBase extends DelegatingDynamicItemAndBlockModel {
52

53
    private static final FaceBakery FACE_BAKERY = new FaceBakery();
×
54
    private static final Cache<Triple<IRenderState, Direction, RenderType>, List<BakedQuad>> CACHE_QUADS = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build();
×
55

56
    private static final int RADIUS = 4;
57
    private static final int TEXTURE_SIZE = 16;
58

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

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

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

81
    public CableModelBase(BlockState blockState, Direction facing, RandomSource rand, ModelData modelData, RenderType renderType) {
82
        super(blockState, facing, rand, modelData, renderType);
×
83
    }
×
84

85
    public CableModelBase(ItemStack itemStack, Level world, LivingEntity entity) {
86
        super(itemStack, world, entity);
×
87
    }
×
88

89
    public CableModelBase() {
90
        super();
×
91
    }
×
92

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

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

134
    public List<BakedQuad> getFacadeQuads(BakedModel facadeModel, BlockState blockState, Direction side, PartRenderPosition partRenderPosition) {
135
        RandomSource rand = RandomSource.create();
×
136
        List<BakedQuad> originalQuads = facadeModel.getQuads(blockState, side, rand);
×
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.getSprite();
×
181
        float[] uvArray = { 16f - u1 * 16f, 16f - v1 * 16f, 16f - u0 * 16f, 16f - v0 * 16f };
×
182
        int ROTATION_NONE = 0;
×
183
        BlockFaceUV blockFaceUV = new BlockFaceUV(uvArray, ROTATION_NONE);
×
184
        Direction NO_FACE_CULLING = null;
×
185
        String DUMMY_TEXTURE_NAME = "";
×
186
        BlockElementFace blockPartFace = new BlockElementFace(NO_FACE_CULLING, originalQuad.getTintIndex(), DUMMY_TEXTURE_NAME, blockFaceUV);
×
187
        ModelState transformation = new SimpleModelState(getMatrix(getRotation(side)));
×
188
        BlockElementRotation DEFAULT_ROTATION = null;
×
189
        boolean APPLY_SHADING = true;
×
190
        quads.add(FACE_BAKERY.bakeQuad(from, to, blockPartFace, texture, Direction.NORTH, transformation, DEFAULT_ROTATION, APPLY_SHADING));
×
191
    }
×
192

193
    public static Transformation getMatrix(BlockModelRotation modelRotation) {
194
        return modelRotation.getRotation();
×
195
    }
196

197
    public static BlockModelRotation getRotation(Direction facing) {
198
        switch (facing) {
×
199
            case DOWN:  return BlockModelRotation.X90_Y180;
×
200
            case UP:    return BlockModelRotation.X270_Y180;
×
201
            case NORTH: return BlockModelRotation.X0_Y0;
×
202
            case SOUTH: return BlockModelRotation.X0_Y180;
×
203
            case WEST:  return BlockModelRotation.X0_Y270;
×
204
            case EAST:  return BlockModelRotation.X0_Y90;
×
205
        }
206
        throw new IllegalArgumentException(String.valueOf(facing));
×
207
    }
208

209
    protected abstract boolean isRealCable(ModelData modelData);
210
    protected abstract Optional<BlockState> getFacade(ModelData modelData);
211
    protected abstract boolean isConnected(ModelData modelData, Direction side);
212
    protected abstract boolean hasPart(ModelData modelData, Direction side);
213
    protected abstract PartRenderPosition getPartRenderPosition(ModelData modelData, Direction side);
214
    protected abstract boolean shouldRenderParts(ModelData modelData);
215
    protected abstract BakedModel getPartModel(ModelData modelData, Direction side);
216
    protected abstract IRenderState getRenderState(ModelData modelData);
217

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

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

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

285
            if (blockStateHolder.isPresent() && shouldRenderParts(modelData)
×
286
                    && this.renderType != null) {
287
                BakedModel facadeModel = RenderHelpers.getBakedModel(blockStateHolder.get());
×
288
                if (facadeModel.getRenderTypes(blockStateHolder.get(), rand, ModelData.EMPTY)
×
289
                        .contains(this.renderType)
×
290
                        && this.facing != null) {
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));
×
297
                }
298
            }
299

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

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

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

327
    @Override
328
    public boolean usesBlockLight() {
329
        return false; // If false, RenderHelper.setupGuiFlatDiffuseLighting() is called
×
330
    }
331

332
    @Override
333
    public ItemTransforms getTransforms() {
334
        return TRANSFORMS;
×
335
    }
336

337
    @Override
338
    public ChunkRenderTypeSet getRenderTypes(@NotNull BlockState state, @NotNull RandomSource rand, @NotNull ModelData data) {
339
        return ChunkRenderTypeSet.all();
×
340
    }
341
}
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