init()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
dl 0
loc 17
rs 9.8
c 0
b 0
f 0
1
package de.pewpewproject.lasertag.client.screen;
2
3
import de.pewpewproject.lasertag.client.screen.widget.*;
4
import de.pewpewproject.lasertag.networking.NetworkingConstants;
5
import de.pewpewproject.lasertag.worldgen.chunkgen.type.ArenaType;
6
import de.pewpewproject.lasertag.worldgen.chunkgen.type.ProceduralArenaType;
7
import io.netty.buffer.Unpooled;
8
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
9
import net.minecraft.client.gui.Drawable;
10
import net.minecraft.client.gui.screen.Screen;
11
import net.minecraft.client.gui.widget.ButtonWidget;
12
import net.minecraft.entity.player.PlayerEntity;
13
import net.minecraft.network.PacketByteBuf;
14
import net.minecraft.text.Text;
15
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.List;
19
import java.util.stream.Stream;
20
21
/**
22
 * The arena management screen of the lasertag game manager
23
 *
24
 * @author Étienne Muser
25
 */
26
public class LasertaGameManagerArenaScreen extends GameManagerScreen {
27
28
    /**
29
     * Extension to the procedural arena names to distinguish them from all other arena types
30
     */
31
    private static final String PROCEDURAL_NAME_EXTENSION = "_extended";
32
33
    private ListWidget<String, String> list;
34
35
    public LasertaGameManagerArenaScreen(Screen parent, PlayerEntity player) {
36
        super(parent, "gui.game_manager.map_screen_title", player);
37
    }
38
39
    @Override
40
    public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
41
        // Forward the mouse scrolled event to the list
42
        return this.list.mouseScrolled(mouseX, mouseY, amount);
43
    }
44
45
    @Override
46
    public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
47
        // Forward the mouse dragged event to the list
48
        return this.list.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
49
    }
50
51
    @Override
52
    protected void init() {
53
        super.init();
54
55
        var columns = new ArrayList<ListColumn<String, String>>(2);
56
57
        columns.add(new ListColumn<>(this::getArenaNameColumn, s -> s, 5));
58
        columns.add(new ListColumn<>(this::getLoadArenaColumn, s -> s, 1));
59
60
        var columnsDefinition = new ListColumnsDefinition<>(columns);
61
62
        var availableHeight = this.height - (2 * verticalPadding + this.textRenderer.fontHeight + 2 * buttonPadding + buttonHeight);
63
64
        this.list = this.addDrawableChild(ListWidget.fromAvailableHeight(horizontalPadding, verticalPadding + textRenderer.fontHeight + buttonPadding,
65
                this.width - 2 * horizontalPadding, availableHeight,
66
                this::getArenaNames,
67
                columnsDefinition, this, this.textRenderer));
68
    }
69
70
    /**
71
     * Get the template for the name column of the list
72
     *
73
     * @param desc The cell description
74
     * @return The cell template
75
     */
76
    private Drawable getArenaNameColumn(ListCell<String> desc) {
77
        var startY = desc.y() + (desc.height() / 2) - (this.textRenderer.fontHeight / 2);
78
        return new LabelWidget(desc.x() + 5, startY, this.textRenderer, Text.translatable(desc.value()));
79
    }
80
81
    /**
82
     * Get the template for the load column of the list
83
     *
84
     * @param desc The cell description
85
     * @return The cell template
86
     */
87
    private Drawable getLoadArenaColumn(ListCell<String> desc) {
88
        return new ButtonWidget(desc.x() + 1, desc.y() + 1, desc.width() - 2, desc.height() - 2, Text.translatable("gui.load"), button -> {
89
90
            this.client.setScreen(null);
91
92
            // Get arena translatable name
93
            var arenaName = desc.value();
94
95
            if (arenaName.endsWith(PROCEDURAL_NAME_EXTENSION)) {
96
                arenaName = arenaName.substring(0, arenaName.length() - PROCEDURAL_NAME_EXTENSION.length());
97
            }
98
99
            // Create packet buffer
100
            var buf = new PacketByteBuf(Unpooled.buffer());
101
102
            buf.writeString(arenaName);
103
104
            ClientPlayNetworking.send(NetworkingConstants.CLIENT_TRIGGER_LOAD_MAP, buf);
105
        });
106
    }
107
108
    /**
109
     * The data source for the list
110
     *
111
     * @return The arena names
112
     */
113
    private List<String> getArenaNames() {
114
        return Arrays.stream(ArenaType.values())
115
                .flatMap(arenaType -> {
116
                    if (arenaType == ArenaType.PROCEDURAL) {
117
                        return Arrays.stream(ProceduralArenaType.values()).map(proceduralArenaType -> proceduralArenaType.translatableName + PROCEDURAL_NAME_EXTENSION);
118
                    } else {
119
                        return Stream.of(arenaType.translatableName);
120
                    }
121
                })
122
                .toList();
123
    }
124
}
125