1
|
|
|
package de.pewpewproject.lasertag.client.screen; |
2
|
|
|
|
3
|
|
|
import de.pewpewproject.lasertag.client.screen.widget.*; |
4
|
|
|
import de.pewpewproject.lasertag.common.types.Tuple; |
5
|
|
|
import de.pewpewproject.lasertag.lasertaggame.team.TeamDto; |
6
|
|
|
import de.pewpewproject.lasertag.networking.NetworkingConstants; |
7
|
|
|
import io.netty.buffer.Unpooled; |
8
|
|
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; |
9
|
|
|
import net.minecraft.client.MinecraftClient; |
10
|
|
|
import net.minecraft.client.gui.Drawable; |
11
|
|
|
import net.minecraft.client.gui.screen.Screen; |
12
|
|
|
import net.minecraft.client.gui.widget.ButtonWidget; |
13
|
|
|
import net.minecraft.entity.player.PlayerEntity; |
14
|
|
|
import net.minecraft.network.PacketByteBuf; |
15
|
|
|
import net.minecraft.text.Text; |
16
|
|
|
|
17
|
|
|
import java.util.ArrayList; |
18
|
|
|
import java.util.Comparator; |
19
|
|
|
import java.util.List; |
20
|
|
|
import java.util.UUID; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The teams screen of the lasertag game manager |
24
|
|
|
* |
25
|
|
|
* @author Étienne Muser |
26
|
|
|
*/ |
27
|
|
|
public class LasertagGameManagerTeamsScreen extends GameManagerScreen { |
28
|
|
|
|
29
|
|
|
private ListWidget<Tuple<TeamDto, UUID>, UUID> list; |
30
|
|
|
|
31
|
|
|
public LasertagGameManagerTeamsScreen(Screen parent, PlayerEntity player) { |
32
|
|
|
super(parent, "gui.game_manager.teams_screen_title", player); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Reload the lists data source |
37
|
|
|
*/ |
38
|
|
|
public void resetList() { |
39
|
|
|
this.list.refreshDataSource(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
@Override |
43
|
|
|
public boolean mouseScrolled(double mouseX, double mouseY, double amount) { |
44
|
|
|
// Forward the mouse scrolled event to the list |
45
|
|
|
return this.list.mouseScrolled(mouseX, mouseY, amount); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
@Override |
49
|
|
|
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { |
50
|
|
|
// Forward the mouse dragged event to the list |
51
|
|
|
return this.list.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
@Override |
|
|
|
|
55
|
|
|
protected void init() { |
56
|
|
|
super.init(); |
57
|
|
|
|
58
|
|
|
var columns = new ArrayList<ListColumn<Tuple<TeamDto, UUID>, UUID>>(); |
59
|
|
|
|
60
|
|
|
columns.add(new ListColumn<>(this::getTeamNameColumn, Tuple::y, 2)); |
61
|
|
|
columns.add(new ListColumn<>(this::getPlayerNameColumn, Tuple::y, 2)); |
62
|
|
|
columns.add(new ListColumn<>(this::getKickPlayerColumn, Tuple::y, 1)); |
63
|
|
|
|
64
|
|
|
var columnsDefinition = new ListColumnsDefinition<>(columns); |
65
|
|
|
|
66
|
|
|
var availableHeight = this.height - (2 * verticalPadding + this.textRenderer.fontHeight + 2 * buttonPadding + buttonHeight); |
67
|
|
|
|
68
|
|
|
this.list = this.addDrawableChild(ListWidget.fromAvailableHeight(horizontalPadding, verticalPadding + textRenderer.fontHeight + buttonPadding, |
69
|
|
|
this.width - 2 * horizontalPadding, availableHeight, |
70
|
|
|
this::getPlayers, |
71
|
|
|
columnsDefinition, this, this.textRenderer)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the template for the team name column of the list |
76
|
|
|
* |
77
|
|
|
* @param desc The cell description |
78
|
|
|
* @return The cell template |
79
|
|
|
*/ |
80
|
|
|
private Drawable getTeamNameColumn(ListCell<Tuple<TeamDto, UUID>> desc) { |
81
|
|
|
var teamDto = desc.value().x(); |
82
|
|
|
var teamName = this.getTeamName(teamDto); |
83
|
|
|
var teamColor = teamDto != null ? teamDto.color().getValue() : 0xFFFFFFFF; |
84
|
|
|
var startY = desc.y() + (desc.height() / 2) - (this.textRenderer.fontHeight / 2); |
85
|
|
|
return new LabelWidget(desc.x() + 5, startY, this.textRenderer, Text.literal(teamName), teamColor); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the template for the player name column of the list |
90
|
|
|
* |
91
|
|
|
* @param desc The cell description |
92
|
|
|
* @return The cell template |
93
|
|
|
*/ |
94
|
|
|
private Drawable getPlayerNameColumn(ListCell<Tuple<TeamDto, UUID>> desc) { |
95
|
|
|
|
96
|
|
|
// Get the game managers |
97
|
|
|
var gameManager = MinecraftClient.getInstance().world.getClientLasertagManager(); |
98
|
|
|
var playerUsernamesState = gameManager.getSyncedState().getPlayerNamesState(); |
99
|
|
|
|
100
|
|
|
var playerName = playerUsernamesState.getPlayerUsername(desc.value().y()); |
101
|
|
|
var startY = desc.y() + (desc.height() / 2) - (this.textRenderer.fontHeight / 2); |
102
|
|
|
var networkPlayer = this.client.getNetworkHandler().getPlayerListEntry(desc.value().y()); |
103
|
|
|
var playerColor = networkPlayer != null ? 0xFFFFFFFF : 0xFF808080; |
104
|
|
|
return new LabelWidget(desc.x(), startY, this.textRenderer, Text.literal(playerName), playerColor); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the template for the kick player button column of the list |
109
|
|
|
* |
110
|
|
|
* @param desc The cell description |
111
|
|
|
* @return The cell template |
112
|
|
|
*/ |
113
|
|
|
private Drawable getKickPlayerColumn(ListCell<Tuple<TeamDto, UUID>> desc) { |
114
|
|
|
var buttonWidget = new ButtonWidget(desc.x() + 1, desc.y() + 1, desc.width() - 2, desc.height() - 2, Text.translatable("gui.kick"), button -> { |
115
|
|
|
// Create packet buffer |
116
|
|
|
var buf = new PacketByteBuf(Unpooled.buffer()); |
117
|
|
|
|
118
|
|
|
buf.writeUuid(desc.value().y()); |
119
|
|
|
|
120
|
|
|
ClientPlayNetworking.send(NetworkingConstants.CLIENT_TRIGGER_PLAYER_KICK, buf); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
buttonWidget.active = desc.value().x() != null; |
124
|
|
|
|
125
|
|
|
return buttonWidget; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* The data source for the list |
130
|
|
|
* |
131
|
|
|
* @return The teams and players |
132
|
|
|
*/ |
133
|
|
|
private List<Tuple<TeamDto, UUID>> getPlayers() { |
134
|
|
|
|
135
|
|
|
// Get the game managers |
136
|
|
|
var gameManager = MinecraftClient.getInstance().world.getClientLasertagManager(); |
137
|
|
|
var playerNamesState = gameManager.getSyncedState().getPlayerNamesState(); |
138
|
|
|
var teamsManager = gameManager.getTeamsManager(); |
139
|
|
|
var teamsConfigState = gameManager.getSyncedState().getTeamsConfigState(); |
140
|
|
|
|
141
|
|
|
var players = new ArrayList<Tuple<TeamDto, UUID>>(); |
142
|
|
|
|
143
|
|
|
playerNamesState.forEachPlayer(playerUuid -> { |
144
|
|
|
|
145
|
|
|
var teamIdOptional = teamsManager.getTeamOfPlayer(playerUuid); |
146
|
|
|
TeamDto team = null; |
147
|
|
|
if (teamIdOptional.isPresent()) { |
148
|
|
|
var teamId = teamIdOptional.get(); |
149
|
|
|
team = teamsConfigState.getTeamOfId(teamId).orElseThrow(); |
150
|
|
|
} |
151
|
|
|
players.add(new Tuple<>(team, playerUuid)); |
152
|
|
|
}); |
153
|
|
|
|
154
|
|
|
// All those Z's are to make sure that players without teams get sorted to the bottom of the list |
155
|
|
|
return players.stream().sorted(Comparator.comparing(t -> t.x() != null ? t.x().name() : "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ")).toList(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private String getTeamName(TeamDto teamDto) { |
159
|
|
|
return teamDto != null ? teamDto.name() : ""; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|