Skip to content

Commit af73147

Browse files
committed
improve usability a ton
1 parent 29ecee5 commit af73147

File tree

5 files changed

+242
-91
lines changed

5 files changed

+242
-91
lines changed

core/src/main/java/net/labymod/serverapi/core/AbstractLabyModPlayer.java

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@
3131
import net.labymod.serverapi.core.integration.LabyModIntegrationPlayer;
3232
import net.labymod.serverapi.core.integration.LabyModProtocolIntegration;
3333
import net.labymod.serverapi.core.model.display.Subtitle;
34+
import net.labymod.serverapi.core.model.feature.DiscordRPC;
35+
import net.labymod.serverapi.core.model.feature.InteractionMenuEntry;
3436
import net.labymod.serverapi.core.model.moderation.Permission;
3537
import net.labymod.serverapi.core.model.supplement.InputPrompt;
3638
import net.labymod.serverapi.core.model.supplement.ServerSwitchPrompt;
3739
import net.labymod.serverapi.core.packet.clientbound.game.display.SubtitlePacket;
40+
import net.labymod.serverapi.core.packet.clientbound.game.display.TabListBannerPacket;
41+
import net.labymod.serverapi.core.packet.clientbound.game.feature.DiscordRPCPacket;
42+
import net.labymod.serverapi.core.packet.clientbound.game.feature.InteractionMenuPacket;
43+
import net.labymod.serverapi.core.packet.clientbound.game.feature.PlayingGameModePacket;
3844
import net.labymod.serverapi.core.packet.clientbound.game.moderation.PermissionPacket;
3945
import net.labymod.serverapi.core.packet.clientbound.game.supplement.InputPromptPacket;
4046
import net.labymod.serverapi.core.packet.clientbound.game.supplement.ServerSwitchPromptPacket;
@@ -129,7 +135,42 @@ protected AbstractLabyModPlayer(
129135
* @param component The component to set as subtitle
130136
*/
131137
public void setSubtitle(@NotNull ServerAPIComponent component) {
132-
this.setSubtitleInternal(Subtitle.create(this.uniqueId, component));
138+
this.setSubtitle(Subtitle.create(this.uniqueId, component));
139+
}
140+
141+
public void setSubtitle(@Nullable Subtitle subtitle) {
142+
if (Objects.equals(this.subtitle, subtitle)) {
143+
return;
144+
}
145+
146+
this.subtitle = subtitle;
147+
148+
List<Subtitle> subtitles = new ArrayList<>();
149+
if (subtitle == null) {
150+
subtitle = Subtitle.create(this.uniqueId, null);
151+
subtitles.add(subtitle);
152+
}
153+
154+
LabyModProtocol labyModProtocol = this.protocolService.labyModProtocol();
155+
SubtitlePacket packet = new SubtitlePacket(subtitle);
156+
for (AbstractLabyModPlayer<?> player : this.protocolService.getPlayers()) {
157+
Subtitle playerSubtitle = player.getSubtitle();
158+
if (playerSubtitle != null) {
159+
subtitles.add(playerSubtitle);
160+
}
161+
162+
if (player.equals(this)) {
163+
continue;
164+
}
165+
166+
labyModProtocol.sendPacket(player.getUniqueId(), packet);
167+
}
168+
169+
if (subtitles.isEmpty()) {
170+
return;
171+
}
172+
173+
this.sendLabyModPacket(new SubtitlePacket(subtitles));
133174
}
134175

135176
/**
@@ -139,16 +180,66 @@ public void setSubtitle(@NotNull ServerAPIComponent component) {
139180
* @param size The size of the subtitle
140181
*/
141182
public void setSubtitle(@NotNull ServerAPIComponent component, double size) {
142-
this.setSubtitleInternal(Subtitle.create(this.uniqueId, component, size));
183+
this.setSubtitle(Subtitle.create(this.uniqueId, component, size));
143184
}
144185

145186
/**
146187
* Resets the subtitle of the player and sends it to all other online players
147188
*/
148189
public void resetSubtitle() {
149-
this.setSubtitleInternal(null);
190+
this.setSubtitle((Subtitle) null);
191+
}
192+
193+
/**
194+
* Sends the provided interaction menu entries to the player
195+
*
196+
* @param entries The entries to send
197+
*/
198+
public void sendInteractionMenuEntries(@NotNull InteractionMenuEntry... entries) {
199+
this.sendLabyModPacket(new InteractionMenuPacket(entries));
200+
}
201+
202+
/**
203+
* Sends the provided interaction menu entries to the player
204+
*
205+
* @param entries The entries to send
206+
*/
207+
public void sendInteractionMenuEntries(@NotNull List<InteractionMenuEntry> entries) {
208+
this.sendLabyModPacket(new InteractionMenuPacket(entries));
209+
}
210+
211+
/**
212+
* Sends the provided game mode to the LabyMod Chat friends of the player
213+
*
214+
* @param gameMode The game mode to send or {@code null} to unset
215+
*/
216+
public void sendPlayingGameMode(@Nullable String gameMode) {
217+
this.sendLabyModPacket(new PlayingGameModePacket(gameMode));
218+
}
219+
220+
/**
221+
* Sends the provided discord rpc to the player
222+
*
223+
* @param discordRPC The discord rpc to send
224+
*/
225+
public void sendDiscordRPC(@NotNull DiscordRPC discordRPC) {
226+
this.sendLabyModPacket(new DiscordRPCPacket(discordRPC));
150227
}
151228

229+
/**
230+
* Sends the provided icon url as tab list banner to the player
231+
*
232+
* @param iconUrl The icon url to send or {@code null} to unset the banner
233+
*/
234+
public void sendTabListBanner(@Nullable String iconUrl) {
235+
this.sendLabyModPacket(new TabListBannerPacket(iconUrl));
236+
}
237+
238+
/**
239+
* Sends the provided packet to the player
240+
*
241+
* @param packet The packet to send
242+
*/
152243
public void sendPacket(@NotNull Packet packet) {
153244
Class<? extends Packet> packetClass = packet.getClass();
154245
for (Protocol protocol : this.protocolService.registry().getProtocols()) {
@@ -237,41 +328,6 @@ public void openServerSwitchPrompt(
237328
);
238329
}
239330

240-
protected void setSubtitleInternal(@Nullable Subtitle subtitle) {
241-
if (Objects.equals(this.subtitle, subtitle)) {
242-
return;
243-
}
244-
245-
this.subtitle = subtitle;
246-
247-
List<Subtitle> subtitles = new ArrayList<>();
248-
if (subtitle == null) {
249-
subtitle = Subtitle.create(this.uniqueId, null);
250-
subtitles.add(subtitle);
251-
}
252-
253-
LabyModProtocol labyModProtocol = this.protocolService.labyModProtocol();
254-
SubtitlePacket packet = new SubtitlePacket(subtitle);
255-
for (AbstractLabyModPlayer<?> player : this.protocolService.getPlayers()) {
256-
Subtitle playerSubtitle = player.getSubtitle();
257-
if (playerSubtitle != null) {
258-
subtitles.add(playerSubtitle);
259-
}
260-
261-
if (player.equals(this)) {
262-
continue;
263-
}
264-
265-
labyModProtocol.sendPacket(player.getUniqueId(), packet);
266-
}
267-
268-
if (subtitles.isEmpty()) {
269-
return;
270-
}
271-
272-
this.sendLabyModPacket(new SubtitlePacket(subtitles));
273-
}
274-
275331
private void sendLabyModPacket(@NotNull Packet packet) {
276332
this.protocolService.labyModProtocol.sendPacket(this.uniqueId, packet);
277333
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package net.labymod.serverapi.core.model.feature;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.util.Objects;
7+
8+
public class DiscordRPC {
9+
10+
private static final DiscordRPC RESET = new DiscordRPC(null, 0, 0);
11+
12+
private final String gameMode;
13+
private final long startTime;
14+
private final long endTime;
15+
16+
protected DiscordRPC(@Nullable String gameMode, long startTime, long endTime) {
17+
if (startTime < 0) {
18+
throw new IllegalArgumentException("startTime cannot be negative");
19+
}
20+
if (endTime < 0) {
21+
throw new IllegalArgumentException("endTime cannot be negative");
22+
}
23+
24+
this.gameMode = gameMode;
25+
this.startTime = startTime;
26+
this.endTime = endTime;
27+
}
28+
29+
public static DiscordRPC createReset() {
30+
return RESET;
31+
}
32+
33+
public static DiscordRPC create(@NotNull String gameMode) {
34+
Objects.requireNonNull(gameMode, "GameMode cannot be null");
35+
return new DiscordRPC(gameMode, 0, 0);
36+
}
37+
38+
public static DiscordRPC createWithStart(@NotNull String gameMode, long startTime) {
39+
Objects.requireNonNull(gameMode, "GameMode cannot be null");
40+
return new DiscordRPC(gameMode, startTime, 0);
41+
}
42+
43+
public static DiscordRPC createWithEnd(@NotNull String gameMode, long endTime) {
44+
Objects.requireNonNull(gameMode, "GameMode cannot be null");
45+
return new DiscordRPC(gameMode, 0, endTime);
46+
}
47+
48+
public @Nullable String getGameMode() {
49+
return this.gameMode;
50+
}
51+
52+
public long getStartTime() {
53+
return this.startTime;
54+
}
55+
56+
public long getEndTime() {
57+
return this.endTime;
58+
}
59+
60+
public boolean hasGameMode() {
61+
return this.gameMode != null;
62+
}
63+
64+
public boolean hasStartTime() {
65+
return this.startTime != 0;
66+
}
67+
68+
public boolean hasEndTime() {
69+
return this.endTime != 0;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "DiscordRPC{" +
75+
"gameMode='" + this.gameMode + '\'' +
76+
", startTime=" + this.startTime +
77+
", endTime=" + this.endTime +
78+
'}';
79+
}
80+
81+
@Override
82+
public boolean equals(Object o) {
83+
if (this == o) {
84+
return true;
85+
}
86+
if (!(o instanceof DiscordRPC that)) {
87+
return false;
88+
}
89+
return this.startTime == that.startTime && this.endTime == that.endTime && Objects.equals(
90+
this.gameMode, that.gameMode);
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return Objects.hash(this.gameMode, this.startTime, this.endTime);
96+
}
97+
}

core/src/main/java/net/labymod/serverapi/core/model/feature/Emote.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,27 @@ protected Emote(@NotNull UUID uniqueId, int emoteId) {
4040
this.emoteId = emoteId;
4141
}
4242

43-
public static Emote create(@NotNull UUID uniqueId, int emoteId) {
43+
/**
44+
* Plays the emote for the supplied npc.
45+
*
46+
* @param uniqueId The unique id of the npc.
47+
* @param emoteId The emote id.
48+
* @return The emote instance.
49+
*/
50+
public static Emote play(@NotNull UUID uniqueId, int emoteId) {
4451
return new Emote(uniqueId, emoteId);
4552
}
4653

54+
/**
55+
* Stops the current emote for the supplied npc.
56+
*
57+
* @param uniqueId The unique id of the npc.
58+
* @return The emote instance.
59+
*/
60+
public static Emote stop(@NotNull UUID uniqueId) {
61+
return new Emote(uniqueId, -1);
62+
}
63+
4764
public @NotNull UUID getUniqueId() {
4865
return this.uniqueId;
4966
}
@@ -52,6 +69,10 @@ public int getEmoteId() {
5269
return this.emoteId;
5370
}
5471

72+
public boolean isStop() {
73+
return this.emoteId == -1;
74+
}
75+
5576
@Override
5677
public boolean equals(Object o) {
5778
if (this == o) {

0 commit comments

Comments
 (0)