-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Audience.java
504 lines (469 loc) · 15.7 KB
/
Audience.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.audience;
import java.util.Arrays;
import java.util.stream.Collector;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.pointer.Pointered;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.title.Title;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* A receiver of Minecraft media.
*
* <p><code>Audience</code> is designed to be a universal interface for any player,
* command sender, console, or otherwise who can receive text, titles,
* boss bars, and other Minecraft media. It is also designed for a group of
* receivers such as a team, server, world, or permission.</p>
* <p>In the past, Minecraft platforms have typically reserved methods such as
* <code>showTitle</code> for a <code>Player</code> interface. While this is good
* textbook object-oriented design, it presents two key drawbacks: 1) there
* is no abstraction for groups of players, such as a <code>Server</code> or a
* <code>Team</code> and 2) it add boilerplate for handling special cases like
* console or command senders.</p>
* <p>Consider the use-case of sending a message and title to every player on a
* server, and also sending a message to console. Without an <code>Audience</code>,
* the code might look like this:</p>
* <pre>
* Server server;
* for (Player player : server.getPlayers()) {
* player.sendMessage(...);
* player.showTitle(...);
* }
* server.getConsole().sendMessage(...);</pre>
* <p>Now, if <code>Server</code> implemented <code>Audience</code>, its unified interface
* would allow users to easily send media without if-guarding console or
* iterating through the list of players:</p>
* <pre>
* Server server;
* server.sendMessage(...); // Sends a message to players and console
* server.showTitle(...); // Shows a title to players, silently ignored by console</pre>
* <p>When an <code>Audience</code> is unable to perform an operation, such as sending
* a boss bar to console, it will silently fail, without logging. This
* requirement allows users to easily send media to a group of
* <code>Audience</code>s without checking each for compatibility.</p>
* <p>While the scope of <code>Audience</code> may be expanded in the future to support
* new Minecraft media such as the player list, its interface will remain stateless
* and any new methods will be stubbed by default.</p>
*
* @see ForwardingAudience
* @since 4.0.0
*/
public interface Audience extends Pointered {
/**
* Gets an audience that does nothing.
*
* @return a do-nothing audience
* @since 4.0.0
*/
static @NonNull Audience empty() {
return EmptyAudience.INSTANCE;
}
/**
* Creates an audience that forwards to many other audiences.
*
* @param audiences an array of audiences, can be empty
* @return an audience
* @see ForwardingAudience
* @since 4.0.0
*/
static @NonNull Audience audience(final @NonNull Audience@NonNull... audiences) {
final int length = audiences.length;
if(length == 0) {
return empty();
} else if(length == 1) {
return audiences[0];
}
return audience(Arrays.asList(audiences));
}
/**
* Creates an audience that forwards to many other audiences.
*
* <p>The underlying <code>Iterable</code> is not copied, therefore any changes
* made will be reflected in <code>Audience</code>.</p>
*
* @param audiences an iterable of audiences, can be empty
* @return an audience
* @see ForwardingAudience
* @since 4.0.0
*/
static @NonNull ForwardingAudience audience(final @NonNull Iterable<? extends Audience> audiences) {
return () -> audiences;
}
/**
* Provides a collector to create a forwarding audience from a stream of audiences.
*
* <p>The audience produced is immutable and can be reused as desired.</p>
*
* @return a collector to create a forwarding audience
* @since 4.0.0
*/
static @NonNull Collector<? super Audience, ?, ForwardingAudience> toAudience() {
return Audiences.COLLECTOR;
}
/**
* Sends a chat message with a {@link Identity#nil() nil} identity to this {@link Audience}.
*
* @param message a message
* @see Component
* @see #sendMessage(Identified, ComponentLike)
* @see #sendMessage(Identity, ComponentLike)
* @since 4.1.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull ComponentLike message) {
this.sendMessage(Identity.nil(), message);
}
/**
* Sends a chat message from the given {@link Identified} to this {@link Audience}.
*
* @param source the source of the message
* @param message a message
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Identified source, final @NonNull ComponentLike message) {
this.sendMessage(source, message.asComponent());
}
/**
* Sends a chat message from the entity represented by the given {@link Identity} (or the game using {@link Identity#nil()}) to this {@link Audience}.
*
* @param source the identity of the source of the message
* @param message a message
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Identity source, final @NonNull ComponentLike message) {
this.sendMessage(source, message.asComponent());
}
/**
* Sends a chat message with a {@link Identity#nil() nil} identity to this {@link Audience}.
*
* @param message a message
* @see Component
* @see #sendMessage(Identified, Component)
* @see #sendMessage(Identity, Component)
* @since 4.1.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Component message) {
this.sendMessage(Identity.nil(), message);
}
/**
* Sends a chat message from the given {@link Identified} to this {@link Audience}.
*
* @param source the source of the message
* @param message a message
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Identified source, final @NonNull Component message) {
this.sendMessage(source, message, MessageType.SYSTEM);
}
/**
* Sends a chat message from the entity represented by the given {@link Identity} (or the game using {@link Identity#nil()}) to this {@link Audience}.
*
* @param source the identity of the source of the message
* @param message a message
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Identity source, final @NonNull Component message) {
this.sendMessage(source, message, MessageType.SYSTEM);
}
/**
* Sends a chat message with a {@link Identity#nil() nil} identity to this {@link Audience}.
*
* @param message a message
* @param type the type
* @see Component
* @see #sendMessage(Identified, ComponentLike, MessageType)
* @see #sendMessage(Identity, ComponentLike, MessageType)
* @since 4.1.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull ComponentLike message, final @NonNull MessageType type) {
this.sendMessage(Identity.nil(), message, type);
}
/**
* Sends a chat message from the given {@link Identified} to this {@link Audience}.
*
* @param source the source of the message
* @param message a message
* @param type the type
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Identified source, final @NonNull ComponentLike message, final @NonNull MessageType type) {
this.sendMessage(source, message.asComponent(), type);
}
/**
* Sends a chat message from the entity represented by the given {@link Identity} (or the game using {@link Identity#nil()}) to this {@link Audience}.
*
* @param source the identity of the source of the message
* @param message a message
* @param type the type
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Identity source, final @NonNull ComponentLike message, final @NonNull MessageType type) {
this.sendMessage(source, message.asComponent(), type);
}
/**
* Sends a chat message with a {@link Identity#nil() nil} identity to this {@link Audience}.
*
* @param message a message
* @param type the type
* @see Component
* @see #sendMessage(Identified, Component, MessageType)
* @see #sendMessage(Identity, Component, MessageType)
* @since 4.1.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendMessage(final @NonNull Component message, final @NonNull MessageType type) {
this.sendMessage(Identity.nil(), message, type);
}
/**
* Sends a chat message.
*
* @param source the source of the message
* @param message a message
* @param type the type
* @see Component
* @since 4.0.0
*/
default void sendMessage(final @NonNull Identified source, final @NonNull Component message, final @NonNull MessageType type) {
this.sendMessage(source.identity(), message, type);
}
/**
* Sends a chat message.
*
* @param source the identity of the source of the message
* @param message a message
* @param type the type
* @see Component
* @since 4.0.0
*/
default void sendMessage(final @NonNull Identity source, final @NonNull Component message, final @NonNull MessageType type) {
}
/**
* Sends a message on the action bar.
*
* @param message a message
* @see Component
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendActionBar(final @NonNull ComponentLike message) {
this.sendActionBar(message.asComponent());
}
/**
* Sends a message on the action bar.
*
* @param message a message
* @see Component
* @since 4.0.0
*/
default void sendActionBar(final @NonNull Component message) {
}
/**
* Sends the player list header.
*
* <p>Depending on the implementation of this {@code Audience}, an existing footer may be displayed. If you wish
* to set both the header and the footer, please use {@link #sendPlayerListHeaderAndFooter(ComponentLike, ComponentLike)}.</p>
*
* @param header the header
* @since 4.3.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendPlayerListHeader(final @NonNull ComponentLike header) {
this.sendPlayerListHeader(header.asComponent());
}
/**
* Sends the player list header.
*
* <p>Depending on the implementation of this {@code Audience}, an existing footer may be displayed. If you wish
* to set both the header and the footer, please use {@link #sendPlayerListHeaderAndFooter(Component, Component)}.</p>
*
* @param header the header
* @since 4.3.0
*/
default void sendPlayerListHeader(final @NonNull Component header) {
this.sendPlayerListHeaderAndFooter(header, Component.empty());
}
/**
* Sends the player list footer.
*
* <p>Depending on the implementation of this {@code Audience}, an existing footer may be displayed. If you wish
* to set both the header and the footer, please use {@link #sendPlayerListHeaderAndFooter(ComponentLike, ComponentLike)}.</p>
*
* @param footer the footer
* @since 4.3.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendPlayerListFooter(final @NonNull ComponentLike footer) {
this.sendPlayerListFooter(footer.asComponent());
}
/**
* Sends the player list footer.
*
* <p>Depending on the implementation of this {@code Audience}, an existing footer may be displayed. If you wish
* to set both the header and the footer, please use {@link #sendPlayerListHeaderAndFooter(Component, Component)}.</p>
*
* @param footer the footer
* @since 4.3.0
*/
default void sendPlayerListFooter(final @NonNull Component footer) {
this.sendPlayerListHeaderAndFooter(Component.empty(), footer);
}
/**
* Sends the player list header and footer.
*
* @param header the header
* @param footer the footer
* @since 4.3.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendPlayerListHeaderAndFooter(final @NonNull ComponentLike header, final @NonNull ComponentLike footer) {
this.sendPlayerListHeaderAndFooter(header.asComponent(), footer.asComponent());
}
/**
* Sends the player list header and footer.
*
* @param header the header
* @param footer the footer
* @since 4.3.0
*/
default void sendPlayerListHeaderAndFooter(final @NonNull Component header, final @NonNull Component footer) {
}
/**
* Shows a title.
*
* @param title a title
* @see Title
* @since 4.0.0
*/
default void showTitle(final @NonNull Title title) {
}
/**
* Clears the title, if one is being displayed.
*
* @see Title
* @since 4.0.0
*/
default void clearTitle() {
}
/**
* Resets the title and timings back to their default.
*
* @see Title
* @since 4.0.0
*/
default void resetTitle() {
}
/**
* Shows a boss bar.
*
* @param bar a boss bar
* @see BossBar
* @since 4.0.0
*/
default void showBossBar(final @NonNull BossBar bar) {
}
/**
* Hides a boss bar.
*
* @param bar a boss bar
* @see BossBar
* @since 4.0.0
*/
default void hideBossBar(final @NonNull BossBar bar) {
}
/**
* Plays a sound.
*
* @param sound a sound
* @see Sound
* @since 4.0.0
*/
default void playSound(final @NonNull Sound sound) {
}
/**
* Plays a sound at a location.
*
* @param sound a sound
* @param x x coordinate
* @param y y coordinate
* @param z z coordinate
* @see Sound
* @since 4.0.0
*/
default void playSound(final @NonNull Sound sound, final double x, final double y, final double z) {
}
/**
* Stops a sound, or many sounds.
*
* @param stop a sound stop
* @see SoundStop
* @since 4.0.0
*/
default void stopSound(final @NonNull SoundStop stop) {
}
/**
* Opens a book.
*
* <p>When possible, no item should persist after closing the book.</p>
*
* @param book a book
* @see Book
* @since 4.0.0
*/
@ForwardingAudienceOverrideNotRequired
default void openBook(final Book.@NonNull Builder book) {
this.openBook(book.build());
}
/**
* Opens a book.
*
* <p>When possible, no item should persist after closing the book.</p>
*
* @param book a book
* @see Book
* @since 4.0.0
*/
default void openBook(final @NonNull Book book) {
}
}