Skip to content

Commit 36fb26d

Browse files
committed
feat: migration to Velocity's new immutable packets model (PaperMC/Velocity#1672)
version might change depending on when Velocity PR is merged
1 parent 1deb4a0 commit 36fb26d

File tree

3 files changed

+77
-65
lines changed

3 files changed

+77
-65
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.timongcraft</groupId>
88
<artifactId>VeloBoard</artifactId>
9-
<version>1.6.1</version>
9+
<version>1.6.2-SNAPSHOT</version>
1010

1111
<properties>
1212
<java.version>17</java.version>
@@ -105,14 +105,14 @@
105105
</dependency>
106106
<dependency>
107107
<groupId>com.velocitypowered</groupId>
108-
<artifactId>velocity-proxy</artifactId>
108+
<artifactId>local-velocity-immutable-packets-proxy</artifactId>
109109
<version>3.4.0-SNAPSHOT</version>
110110
<scope>provided</scope>
111111
</dependency>
112112
<dependency>
113113
<groupId>de.timongcraft</groupId>
114114
<artifactId>VeloPacketImpl</artifactId>
115-
<version>3.1.5</version>
115+
<version>4.0.0-SNAPSHOT</version>
116116
</dependency>
117117
</dependencies>
118118
</project>

src/main/java/de/timongcraft/veloboard/SimpleBoard.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
import de.timongcraft.velopacketimpl.network.protocol.packets.UpdateScorePacket;
1010
import de.timongcraft.velopacketimpl.utils.ComponentUtils;
1111
import de.timongcraft.velopacketimpl.utils.annotations.Since;
12-
import net.kyori.adventure.text.Component;
13-
import org.jetbrains.annotations.Nullable;
14-
import org.jetbrains.annotations.Unmodifiable;
15-
1612
import java.util.ArrayList;
1713
import java.util.Arrays;
1814
import java.util.Collection;
1915
import java.util.List;
2016
import java.util.Objects;
17+
import net.kyori.adventure.text.Component;
18+
import org.jetbrains.annotations.Nullable;
19+
import org.jetbrains.annotations.Unmodifiable;
2120

2221
import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_20_3;
2322

@@ -52,7 +51,7 @@ public SimpleBoard(Player player, Component title, @Nullable ComponentUtils.Numb
5251
public void initialize() {
5352
withLock(() -> {
5453
sendObjectivePacket(UpdateObjectivesPacket.Mode.CREATE_SCOREBOARD);
55-
sendPacket(new DisplayObjectivePacket(1, id));
54+
sendPacket(DisplayObjectivePacket.of(1, id));
5655
});
5756
}
5857

@@ -307,7 +306,7 @@ private void sendLineChangeUnsafe(int score, UpdateScorePacket.Action action) {
307306
if (action == UpdateScorePacket.Action.CREATE_OR_UPDATE_SCORE) {
308307
LinesEntry line = getLineByScore(lines, score);
309308
sendPacket(
310-
new UpdateScorePacket(
309+
UpdateScorePacket.of(
311310
String.valueOf(score),
312311
id,
313312
score,
@@ -320,7 +319,7 @@ private void sendLineChangeUnsafe(int score, UpdateScorePacket.Action action) {
320319
)
321320
);
322321
} else {
323-
sendPacket(new ResetScorePacket(
322+
sendPacket(ResetScorePacket.of(
324323
String.valueOf(score),
325324
id
326325
));
@@ -329,9 +328,11 @@ private void sendLineChangeUnsafe(int score, UpdateScorePacket.Action action) {
329328

330329
private void sendObjectivePacket(UpdateObjectivesPacket.Mode mode) {
331330
sendPacket(
332-
new UpdateObjectivesPacket(
331+
mode == UpdateObjectivesPacket.Mode.REMOVE_SCOREBOARD
332+
? UpdateObjectivesPacket.ofRemove(id)
333+
: UpdateObjectivesPacket.of(
333334
id,
334-
mode,
335+
mode == UpdateObjectivesPacket.Mode.CREATE_SCOREBOARD,
335336
title,
336337
UpdateObjectivesPacket.Type.INTEGER,
337338
defaultNumberFormat

src/main/java/de/timongcraft/veloboard/VeloBoard.java

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.timongcraft.veloboard;
22

3+
import com.google.common.collect.ImmutableList;
4+
import com.google.common.collect.ImmutableSet;
35
import com.velocitypowered.api.proxy.Player;
46
import de.timongcraft.veloboard.utils.ListUtils;
57
import de.timongcraft.velopacketimpl.network.protocol.packets.DisplayObjectivePacket;
@@ -9,18 +11,15 @@
911
import de.timongcraft.velopacketimpl.network.protocol.packets.UpdateTeamsPacket;
1012
import de.timongcraft.velopacketimpl.utils.ComponentUtils;
1113
import de.timongcraft.velopacketimpl.utils.annotations.Since;
12-
import net.kyori.adventure.text.Component;
13-
import net.kyori.adventure.text.format.NamedTextColor;
14-
import org.jetbrains.annotations.Nullable;
15-
import org.jetbrains.annotations.Unmodifiable;
16-
1714
import java.util.ArrayList;
1815
import java.util.Arrays;
1916
import java.util.Collection;
20-
import java.util.Collections;
21-
import java.util.EnumSet;
2217
import java.util.List;
2318
import java.util.Objects;
19+
import net.kyori.adventure.text.Component;
20+
import net.kyori.adventure.text.format.NamedTextColor;
21+
import org.jetbrains.annotations.Nullable;
22+
import org.jetbrains.annotations.Unmodifiable;
2423

2524
import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_20_3;
2625

@@ -53,7 +52,7 @@ public VeloBoard(Player player, Component title, @Nullable ComponentUtils.Number
5352
public void initialize() {
5453
withLock(() -> {
5554
sendObjectivePacket(UpdateObjectivesPacket.Mode.CREATE_SCOREBOARD);
56-
sendPacket(new DisplayObjectivePacket(1, id));
55+
sendPacket(DisplayObjectivePacket.of(1, id));
5756
});
5857
}
5958

@@ -64,7 +63,7 @@ public void resend() {
6463

6564
for (int i = 0; i < lines.size(); ++i) {
6665
sendScorePacketUnchecked(i, UpdateScorePacket.Action.CREATE_OR_UPDATE_SCORE);
67-
sendTeamPacketUnchecked(i, UpdateTeamsPacket.Mode.CREATE_TEAM, getLineByScore(lines, i));
66+
sendTeamPacketUnchecked(i, true, getLineByScoreUnsafe(i));
6867
}
6968
});
7069
}
@@ -73,7 +72,7 @@ public void resend() {
7372
public void clear() {
7473
withLock(() -> {
7574
for (int i = 0; i < this.lines.size(); ++i) {
76-
sendTeamPacketUnchecked(i, UpdateTeamsPacket.Mode.REMOVE_TEAM);
75+
sendRemoveTeamUnchecked(i);
7776
}
7877

7978
sendObjectivePacket(UpdateObjectivesPacket.Mode.REMOVE_SCOREBOARD);
@@ -169,19 +168,20 @@ public void updateLines(Collection<Component> lines) {
169168
for (int i = 0; i < this.lines.size(); i++) {
170169
if (i >= oldLines.size()) {
171170
sendScorePacketUnchecked(i, UpdateScorePacket.Action.CREATE_OR_UPDATE_SCORE);
172-
sendTeamPacketUnchecked(i, UpdateTeamsPacket.Mode.CREATE_TEAM);
171+
sendTeamPacketUnchecked(i, true, getLineByScoreUnsafe(i));
172+
} else {
173+
sendLineChangeUnsafe(i);
173174
}
174-
sendLineChangeUnsafe(i);
175175
}
176176

177177
for (int i = this.lines.size(); i < oldLines.size(); i++) {
178-
sendTeamPacketUnchecked(i, UpdateTeamsPacket.Mode.REMOVE_TEAM);
178+
sendRemoveTeamUnchecked(i);
179179
sendScorePacketUnchecked(i, UpdateScorePacket.Action.REMOVE_SCORE);
180180
}
181181
}
182182

183183
for (int i = 0; i < this.lines.size(); ++i) {
184-
if (!Objects.equals(getLineByScore(oldLines, i), getLineByScore(this.lines, i))) {
184+
if (!Objects.equals(getLineByScore(i, oldLines), getLineByScoreUnsafe(i))) {
185185
sendLineChangeUnsafe(i);
186186
}
187187
}
@@ -264,26 +264,33 @@ private void checkLineIndexUnsafe(int lineIndex, boolean checkInRange, boolean c
264264
}
265265

266266
private int getScoreByLineUnsafe(int lineIndex) {
267+
return getScoreByLine(lineIndex, lines);
268+
}
269+
270+
private int getScoreByLine(int lineIndex, List<Component> lines) {
267271
return lines.size() - lineIndex - 1;
268272
}
269273

270-
private static @Nullable Component getLineByScore(List<Component> lines, int score) {
271-
if (score < lines.size()) {
272-
return lines.get(lines.size() - score - 1);
273-
} else {
274-
return null;
275-
}
274+
private @Nullable Component getLineByScoreUnsafe(int score) {
275+
return getLineByScore(score, lines);
276+
}
277+
278+
private @Nullable Component getLineByScore(int score, List<Component> lines) {
279+
int index = lines.size() - score - 1;
280+
if (index < 0 || index >= lines.size()) return null;
281+
return lines.get(index);
276282
}
277283

278284
private void sendLineChangeUnsafe(int score) {
279-
sendTeamPacketUnchecked(score, UpdateTeamsPacket.Mode.UPDATE_TEAM_INFO, getLineByScore(lines, score));
285+
sendTeamPacketUnchecked(score, false, getLineByScoreUnsafe(score));
280286
}
281287

282288
private void sendObjectivePacket(UpdateObjectivesPacket.Mode mode) {
283289
sendPacket(
284-
new UpdateObjectivesPacket(
285-
id,
286-
mode,
290+
mode == UpdateObjectivesPacket.Mode.REMOVE_SCOREBOARD
291+
? UpdateObjectivesPacket.ofRemove(id)
292+
: UpdateObjectivesPacket.of(id,
293+
mode == UpdateObjectivesPacket.Mode.CREATE_SCOREBOARD,
287294
player.translateMessage(title),
288295
UpdateObjectivesPacket.Type.INTEGER,
289296
defaultNumberFormat
@@ -293,40 +300,44 @@ private void sendObjectivePacket(UpdateObjectivesPacket.Mode mode) {
293300

294301
private void sendScorePacketUnchecked(int score, UpdateScorePacket.Action action) {
295302
sendPacket(
296-
action != UpdateScorePacket.Action.REMOVE_SCORE || player.getProtocolVersion().getProtocol() < MINECRAFT_1_20_3.getProtocol() ?
297-
new UpdateScorePacket(
298-
COLOR_CODES[score],
299-
action,
300-
id,
301-
score
302-
)
303-
:
304-
new ResetScorePacket(
305-
COLOR_CODES[score],
306-
id
307-
)
303+
action == UpdateScorePacket.Action.CREATE_OR_UPDATE_SCORE
304+
? UpdateScorePacket.of(COLOR_CODES[score], id, score)
305+
306+
: player.getProtocolVersion().lessThan(MINECRAFT_1_20_3)
307+
? UpdateScorePacket.ofLegacyRemove(COLOR_CODES[score], id)
308+
: ResetScorePacket.of(COLOR_CODES[score], id)
308309
);
309310
}
310311

311-
private void sendTeamPacketUnchecked(int score, UpdateTeamsPacket.Mode mode) {
312-
sendTeamPacketUnchecked(score, mode, Component.empty());
312+
private void sendRemoveTeamUnchecked(int score) {
313+
sendPacket(UpdateTeamsPacket.ofRemove(id + ':' + score));
313314
}
314315

315-
private void sendTeamPacketUnchecked(int score, UpdateTeamsPacket.Mode mode, Component teamPrefix) {
316-
sendPacket(
317-
new UpdateTeamsPacket(
318-
id + ':' + score,
319-
mode,
320-
Component.empty(),
321-
EnumSet.noneOf(UpdateTeamsPacket.FriendlyFlag.class),
322-
UpdateTeamsPacket.NameTagVisibility.ALWAYS,
323-
UpdateTeamsPacket.CollisionRule.ALWAYS,
324-
NamedTextColor.BLACK,
325-
player.translateMessage(teamPrefix),
326-
Component.empty(),
327-
Collections.singletonList(COLOR_CODES[score])
328-
)
329-
);
316+
private void sendTeamPacketUnchecked(int score, boolean create, Component teamPrefix) {
317+
if (create) {
318+
sendPacket(UpdateTeamsPacket.ofCreate(
319+
id + ':' + score,
320+
Component.empty(),
321+
ImmutableSet.of(),
322+
UpdateTeamsPacket.NameTagVisibility.ALWAYS,
323+
UpdateTeamsPacket.CollisionRule.ALWAYS,
324+
NamedTextColor.BLACK,
325+
player.translateMessage(teamPrefix),
326+
Component.empty(),
327+
ImmutableList.of(COLOR_CODES[score])
328+
));
329+
} else {
330+
sendPacket(UpdateTeamsPacket.ofUpdateInfo(
331+
id + ':' + score,
332+
Component.empty(),
333+
ImmutableSet.of(),
334+
UpdateTeamsPacket.NameTagVisibility.ALWAYS,
335+
UpdateTeamsPacket.CollisionRule.ALWAYS,
336+
NamedTextColor.BLACK,
337+
player.translateMessage(teamPrefix),
338+
Component.empty()
339+
));
340+
}
330341
}
331342

332343
}

0 commit comments

Comments
 (0)