de.pewpewproject.lasertag.block.models.AbstractEmissiveBlockModel   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 303
Duplicated Lines 6.93 %

Importance

Changes 0
Metric Value
eloc 141
dl 21
loc 303
rs 10
c 0
b 0
f 0
wmc 18

20 Methods

Rating   Name   Duplication   Size   Complexity  
A addTextureROI(Direction,Sprite,Sprite,QuadEmitter,float,float,float,float,float,float,float,float,float) 0 14 1
A getOverrides() 0 3 1
A useAmbientOcclusion() 0 4 1
A isSideLit() 0 3 1
A addTextureROI(QuadEmitter,float,float,float,float) 0 9 1
A addTextureROI(Direction,Sprite,Sprite,QuadEmitter,float,float,float,float,float,int) 11 11 1
A getQuads(BlockState,Direction,Random) 0 4 1
A addTextureROI(Direction,Sprite,Sprite,QuadEmitter,float,float,float,float,float,float,float,float,float,int) 0 38 1
A isVanillaAdapter() 0 4 1
A addTextureROI(Direction,Sprite,Sprite,QuadEmitter,float,float,float,float,float) 10 10 1
A getTransformation() 0 3 1
A emitItemQuads(ItemStack,Supplier,RenderContext) 0 6 1
A isBuiltin() 0 3 1
A emitBlockQuads(BlockRenderView,BlockState,BlockPos,Supplier,RenderContext) 0 6 1
A bake(ModelLoader,Function,ModelBakeSettings,Identifier) 0 22 1
A getModelDependencies() 0 4 1
A addTexture(Direction,Sprite,Sprite,QuadEmitter,float,float,float,float,float) 0 31 1
A hasDepth() 0 3 1
getSprites(Function) 0 1 ?
addTextureToUnbakedModel(QuadEmitter) 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package de.pewpewproject.lasertag.block.models;
2
3
import net.fabricmc.api.EnvType;
4
import net.fabricmc.api.Environment;
5
import net.fabricmc.fabric.api.renderer.v1.Renderer;
6
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
7
import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
8
import net.fabricmc.fabric.api.renderer.v1.material.MaterialFinder;
9
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
10
import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh;
11
import net.fabricmc.fabric.api.renderer.v1.mesh.MeshBuilder;
12
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
13
import net.fabricmc.fabric.api.renderer.v1.mesh.QuadEmitter;
14
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
15
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;
16
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
17
import net.minecraft.block.BlockState;
18
import net.minecraft.client.render.model.*;
19
import net.minecraft.client.render.model.json.ModelOverrideList;
20
import net.minecraft.client.render.model.json.ModelTransformation;
21
import net.minecraft.client.texture.Sprite;
22
import net.minecraft.client.util.SpriteIdentifier;
23
import net.minecraft.item.ItemStack;
24
import net.minecraft.util.Identifier;
25
import net.minecraft.util.math.BlockPos;
26
import net.minecraft.util.math.Direction;
27
import net.minecraft.util.math.random.Random;
28
import net.minecraft.world.BlockRenderView;
29
30
import java.util.Collection;
31
import java.util.Collections;
32
import java.util.List;
33
import java.util.function.Function;
34
import java.util.function.Supplier;
35
36
/**
37
 * Abstract base class for all emissive custom block models
38
 *
39
 * @author Étienne Muser
40
 */
41
@Environment(EnvType.CLIENT)
42
public abstract class AbstractEmissiveBlockModel implements UnbakedModel, BakedModel, FabricBakedModel {
43
44
    private RenderMaterial emissiveMaterial;
45
46
    protected Mesh mesh;
47
48
    @Override
49
    public Collection<Identifier> getModelDependencies() {
50
        // This model does not depend on other models.
51
        return Collections.emptyList();
52
    }
53
54
    @Override
55
    public BakedModel bake(ModelLoader loader, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) {
56
57
        getSprites(textureGetter);
58
59
        // Build the mesh using the Renderer API
60
        Renderer renderer = RendererAccess.INSTANCE.getRenderer();
61
        MeshBuilder builder = renderer.meshBuilder();
62
        QuadEmitter emitter = builder.getEmitter();
63
        MaterialFinder materialFinder = renderer.materialFinder();
64
        emissiveMaterial = materialFinder.emissive(0, true)
65
                .disableDiffuse(0, true)
66
                .disableAo(0, true)
67
                .blendMode(0, BlendMode.TRANSLUCENT)
68
                .find();
69
70
        // Add texture - implemented by child class
71
        addTextureToUnbakedModel(emitter);
72
73
        mesh = builder.build();
74
75
        return this;
76
    }
77
78
    /**
79
     * Get the textures the model needs.
80
     *
81
     * @param textureGetter
82
     */
83
    protected abstract void getSprites(Function<SpriteIdentifier, Sprite> textureGetter);
84
85
    @Override
86
    public List<BakedQuad> getQuads(BlockState state, Direction face, net.minecraft.util.math.random.Random random) {
87
        // Don't need because we use FabricBakedModel instead. However, it's better to not return null in case some mod decides to call this function.
88
        return Collections.emptyList();
89
    }
90
91
    @Override
92
    public boolean useAmbientOcclusion() {
93
        // we want the block to have a shadow depending on the adjacent blocks
94
        return true;
95
    }
96
97
    @Override
98
    public boolean isBuiltin() {
99
        return false;
100
    }
101
102
    @Override
103
    public boolean hasDepth() {
104
        return false;
105
    }
106
107
    @Override
108
    public boolean isSideLit() {
109
        return false;
110
    }
111
112
    @Override
113
    public ModelTransformation getTransformation() {
114
        return ModelHelper.MODEL_TRANSFORM_BLOCK;
115
    }
116
117
    @Override
118
    public ModelOverrideList getOverrides() {
119
        return ModelOverrideList.EMPTY;
120
    }
121
122
    @Override
123
    public boolean isVanillaAdapter() {
124
        // False to trigger FabricBakedModel rendering
125
        return false;
126
    }
127
128
    @Override
129
    public void emitBlockQuads(BlockRenderView blockRenderView, BlockState blockState, BlockPos blockPos, Supplier<net.minecraft.util.math.random.Random> supplier, RenderContext renderContext) {
130
        // Render function
131
132
        // We just render the mesh
133
        renderContext.meshConsumer().accept(mesh);
134
    }
135
136
    @Override
137
    public void emitItemQuads(ItemStack itemStack, Supplier<Random> supplier, RenderContext renderContext) {
138
        // Item render function
139
140
        // We just render the mesh of the block
141
        renderContext.meshConsumer().accept(mesh);
142
    }
143
144
    /**
145
     * Adds the desired texture to the unbaked model during baking
146
     *
147
     * @param emitter
148
     */
149
    protected abstract void addTextureToUnbakedModel(QuadEmitter emitter);
150
151
    /**
152
     * Add a single glowing sprite to a texture.
153
     * <p>
154
     * If the texture is not square, it gets stretched to a square.
155
     * If you don't want this to happen, use <code>addTextureROI</code>.
156
     *
157
     * @param direction
158
     * @param textureSprite
159
     * @param glowTextureSprite
160
     * @param emitter
161
     * @param left
162
     * @param bottom
163
     * @param right
164
     * @param top
165
     * @param depth
166
     */
167
    protected void addTexture(Direction direction,
168
                              Sprite textureSprite,
169
                              Sprite glowTextureSprite,
170
                              QuadEmitter emitter,
171
                              float left,
172
                              float bottom,
173
                              float right,
174
                              float top,
175
                              float depth) {
176
177
        // Add a new face to the mesh
178
        emitter.square(direction, left, bottom, right, top, depth);
179
        // Set the sprite of the face, must be called after .square()
180
        // We haven't specified any UV coordinates, so we want to use the whole texture. BAKE_LOCK_UV does exactly that.
181
        emitter.spriteBake(0, textureSprite, MutableQuadView.BAKE_LOCK_UV);
182
        // Enable texture usage
183
        emitter.spriteColor(0, -1, -1, -1, -1);
184
        // Add the quad to the mesh
185
        emitter.emit();
186
187
        // Add a new face to the mesh
188
        emitter.square(direction, left, bottom, right, top, depth);
189
        // Set the sprite of the face, must be called after .square()
190
        // We haven't specified any UV coordinates, so we want to use the whole texture. BAKE_LOCK_UV does exactly that.
191
        emitter.spriteBake(0, glowTextureSprite, MutableQuadView.BAKE_LOCK_UV);
192
        // Enable texture usage
193
        emitter.spriteColor(0, -1, -1, -1, -1);
194
        // Add glow
195
        emitter.material(emissiveMaterial);
196
        // Add the quad to the mesh
197
        emitter.emit();
198
    }
199
200
    /**
201
     * Add a single glowing sprite to a texture with the selected UV
202
     *
203
     * @param direction
204
     * @param textureSprite
205
     * @param glowTextureSprite
206
     * @param emitter
207
     * @param left
208
     * @param bottom
209
     * @param right
210
     * @param top
211
     * @param depth
212
     */
213
    protected void addTextureROI(Direction direction,
214
                                 Sprite textureSprite,
215
                                 Sprite glowTextureSprite,
216
                                 QuadEmitter emitter,
217
                                 float left,
218
                                 float bottom,
219
                                 float right,
220
                                 float top,
221
                                 float depth,
222
                                 float textureStartX,
223
                                 float textureStartY,
224
                                 float textureEndX,
225
                                 float textureEndY,
226
                                 int additionalFlags) {
227
228
        // Add a new face to the mesh
229
        emitter.square(direction, left, bottom, right, top, depth);
230
        // Select region of interest in the texture
231
        addTextureROI(emitter, textureStartX, textureStartY, textureEndX, textureEndY);
232
        // Set the sprite of the face, must be called after .square()
233
        emitter.spriteBake(0, textureSprite, MutableQuadView.BAKE_NORMALIZED | additionalFlags);
234
        // Enable texture usage
235
        emitter.spriteColor(0, -1, -1, -1, -1);
236
        // Add the quad to the mesh
237
        emitter.emit();
238
239
        // Add a new face to the mesh
240
        emitter.square(direction, left, bottom, right, top, depth);
241
        // Select region of interest in the texture
242
        addTextureROI(emitter, textureStartX, textureStartY, textureEndX, textureEndY);
243
        // Set the sprite of the face, must be called after .square()
244
        emitter.spriteBake(0, glowTextureSprite, MutableQuadView.BAKE_NORMALIZED | additionalFlags);
245
        // Enable texture usage
246
        emitter.spriteColor(0, -1, -1, -1, -1);
247
        // Add glow
248
        emitter.material(emissiveMaterial);
249
        // Add the quad to the mesh
250
        emitter.emit();
251
    }
252
253
    /**
254
     * Add a single glowing sprite to a texture with the selected UV
255
     *
256
     * @param direction
257
     * @param textureSprite
258
     * @param glowTextureSprite
259
     * @param emitter
260
     * @param left
261
     * @param bottom
262
     * @param right
263
     * @param top
264
     * @param depth
265
     */
266
    protected void addTextureROI(Direction direction,
267
                                 Sprite textureSprite,
268
                                 Sprite glowTextureSprite,
269
                                 QuadEmitter emitter,
270
                                 float left,
271
                                 float bottom,
272
                                 float right,
273
                                 float top,
274
                                 float depth,
275
                                 float textureStartX,
276
                                 float textureStartY,
277
                                 float textureEndX,
278
                                 float textureEndY) {
279
        addTextureROI(direction, textureSprite, glowTextureSprite, emitter,left, bottom, right, top, depth, textureStartX, textureStartY, textureEndX, textureEndY, 0);
280
    }
281
282
    /**
283
     * Add a single glowing sprite to a texture with the selected UV.
284
     * Overload where the whole texture is selected.
285
     *
286
     * @param direction
287
     * @param textureSprite
288
     * @param glowTextureSprite
289
     * @param emitter
290
     * @param left
291
     * @param bottom
292
     * @param right
293
     * @param top
294
     * @param depth
295
     */
296 View Code Duplication
    protected void addTextureROI(Direction direction,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
297
                                 Sprite textureSprite,
298
                                 Sprite glowTextureSprite,
299
                                 QuadEmitter emitter,
300
                                 float left,
301
                                 float bottom,
302
                                 float right,
303
                                 float top,
304
                                 float depth,
305
                                 int additionalFlags) {
306
        addTextureROI(direction, textureSprite, glowTextureSprite, emitter, left, bottom, right, top, depth, 0.0f, 0.0f, 1.0f, 1.0f, additionalFlags);
307
    }
308
309
    /**
310
     * Add a single glowing sprite to a texture with the selected UV.
311
     * Overload where the whole texture is selected.
312
     *
313
     * @param direction
314
     * @param textureSprite
315
     * @param glowTextureSprite
316
     * @param emitter
317
     * @param left
318
     * @param bottom
319
     * @param right
320
     * @param top
321
     * @param depth
322
     */
323 View Code Duplication
    protected void addTextureROI(Direction direction,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
324
                                 Sprite textureSprite,
325
                                 Sprite glowTextureSprite,
326
                                 QuadEmitter emitter,
327
                                 float left,
328
                                 float bottom,
329
                                 float right,
330
                                 float top,
331
                                 float depth) {
332
        addTextureROI(direction, textureSprite, glowTextureSprite, emitter, left, bottom, right, top, depth, 0.0f, 0.0f, 1.0f, 1.0f, 0);
333
    }
334
335
    private void addTextureROI(QuadEmitter emitter,
336
                               float textureStartX,
337
                               float textureStartY,
338
                               float textureEndX,
339
                               float textureEndY) {
340
        emitter.sprite(0, 0, textureStartX, textureStartY);
341
        emitter.sprite(1, 0, textureStartX, textureEndY);
342
        emitter.sprite(2, 0, textureEndX, textureEndY);
343
        emitter.sprite(3, 0, textureEndX, textureStartY);
344
    }
345
}
346