de.pewpewproject.lasertag.client.screen.LasertagTeamZoneGeneratorScreen   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 7.37 %

Importance

Changes 0
Metric Value
eloc 54
dl 7
loc 95
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 28 1
A triggerGenerateTeamZone(String) 0 15 1
A drawBackground(MatrixStack) 0 2 1
A render(MatrixStack,int,int,float) 7 7 1
A LasertagTeamZoneGeneratorScreen(LasertagTeamZoneGeneratorBlockEntity) 0 4 1
A drawExitButton() 0 3 1
A keyPressed(int,int,int) 0 9 2

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.client.screen;
2
3
import de.pewpewproject.lasertag.block.entity.LasertagTeamZoneGeneratorBlockEntity;
4
import de.pewpewproject.lasertag.client.screen.widget.LabelWidget;
5
import de.pewpewproject.lasertag.networking.NetworkingConstants;
6
import io.netty.buffer.Unpooled;
7
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
8
import net.minecraft.client.gui.DrawableHelper;
9
import net.minecraft.client.gui.screen.Screen;
10
import net.minecraft.client.gui.widget.ButtonWidget;
11
import net.minecraft.client.gui.widget.TextFieldWidget;
12
import net.minecraft.client.util.math.MatrixStack;
13
import net.minecraft.network.PacketByteBuf;
14
import net.minecraft.text.Text;
15
import net.minecraft.util.Formatting;
16
import org.lwjgl.glfw.GLFW;
17
18
/**
19
 * @author Étienne Muser
20
 */
21
public class LasertagTeamZoneGeneratorScreen extends Screen {
22
23
    private static final Text TITLE = Text.translatable("gui.team_zone_generator.title");
24
    private static final int VERTICAL_PADDING = 15;
25
    private static final int BUTTON_HEIGHT = 20;
26
    private static final int EXIT_BUTTON_WIDTH = 100;
27
    private static final int HORIZONTAL_PADDING = 20;
28
    private static final int BUTTON_PADDING = 10;
29
    private static final int BUTTON_WIDTH = 200;
30
31
    private final LasertagTeamZoneGeneratorBlockEntity teamZoneGenerator;
32
33
    private TextFieldWidget textFieldWidget;
34
35
    public LasertagTeamZoneGeneratorScreen(LasertagTeamZoneGeneratorBlockEntity teamZoneGenerator) {
36
        super(TITLE);
37
38
        this.teamZoneGenerator = teamZoneGenerator;
39
    }
40
41
    @Override
42
    protected void init() {
43
        super.init();
44
45
        // Calculate start pos
46
        var startX = HORIZONTAL_PADDING;
47
        var startY = VERTICAL_PADDING + textRenderer.fontHeight + BUTTON_PADDING;
48
49
        // Create the team name label
50
        var labelStartY = startY + (this.textRenderer.fontHeight / 2);
51
        var labelText = Text.translatable("gui.team_zone_generator.team_name");
52
        var labelTextWidth = this.textRenderer.getWidth(labelText);
53
        this.addDrawableChild(new LabelWidget(startX, labelStartY, this.textRenderer, labelText));
54
55
        // Create the text input for the team name
56
        this.textFieldWidget = this.addDrawableChild(new TextFieldWidget(this.textRenderer, startX + labelTextWidth + BUTTON_PADDING, startY, BUTTON_WIDTH, BUTTON_HEIGHT, Text.literal(this.teamZoneGenerator.getTeamName())));
57
        this.textFieldWidget.setText(this.teamZoneGenerator.getTeamName());
58
        this.focusOn(this.textFieldWidget);
59
        this.textFieldWidget.setTextFieldFocused(true);
60
61
        // Add the "generate" button
62
        this.addDrawableChild(new ButtonWidget(startX + labelTextWidth + BUTTON_PADDING + BUTTON_WIDTH + BUTTON_PADDING, startY, BUTTON_WIDTH / 2, BUTTON_HEIGHT, Text.translatable("gui.team_zone_generator.generate"), button -> {
63
            this.triggerGenerateTeamZone(this.textFieldWidget.getText());
64
        }));
65
66
        // Add warning label
67
        this.addDrawableChild(new LabelWidget(startX, startY + BUTTON_HEIGHT + BUTTON_PADDING, this.textRenderer, Text.translatable("gui.team_zone_generator.warning_label_text").formatted(Formatting.RED)));
68
        this.addDrawableChild(new LabelWidget(startX, startY + BUTTON_HEIGHT + BUTTON_PADDING + this.textRenderer.fontHeight + 2, this.textRenderer, Text.translatable("gui.team_zone_generator.change_info_text").formatted(Formatting.RED)));
69
    }
70
71
    @Override
72
    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
73
        if (keyCode == GLFW.GLFW_KEY_ENTER) {
74
            this.triggerGenerateTeamZone(this.textFieldWidget.getText());
75
            this.close();
76
            return true;
77
        }
78
79
        return super.keyPressed(keyCode, scanCode, modifiers);
80
    }
81
82 View Code Duplication
    @Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
83
    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
84
        this.drawBackground(matrices);
85
        DrawableHelper.drawCenteredText(matrices, this.textRenderer, TITLE.getString(), this.width / 2, VERTICAL_PADDING + (this.textRenderer.fontHeight / 2), 0xFFFFFFFF);
86
        drawExitButton();
87
88
        super.render(matrices, mouseX, mouseY, delta);
89
    }
90
91
    private void drawBackground(MatrixStack matrices) {
92
        DrawableHelper.fill(matrices, 0, 0, this.width, this.height, 0x80000000);
93
    }
94
95
    private void drawExitButton() {
96
        this.addDrawableChild(new ButtonWidget(this.width - HORIZONTAL_PADDING - EXIT_BUTTON_WIDTH, this.height - VERTICAL_PADDING - BUTTON_HEIGHT, EXIT_BUTTON_WIDTH, BUTTON_HEIGHT, Text.translatable("gui.exit"), button -> {
97
            this.close();
98
        }));
99
    }
100
101
    private void triggerGenerateTeamZone(String teamName) {
102
103
        // Create buffer
104
        var buf = new PacketByteBuf(Unpooled.buffer());
105
106
        // Get the block pos
107
        var pos = this.teamZoneGenerator.getPos();
108
109
        // Write the pos
110
        buf.writeBlockPos(pos);
111
112
        // Write the team name
113
        buf.writeString(teamName);
114
115
        ClientPlayNetworking.send(NetworkingConstants.CLIENT_TRIGGER_GENERATE_ZONE, buf);
116
    }
117
}
118