-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
ForwardingAudience.java
283 lines (241 loc) · 9.15 KB
/
ForwardingAudience.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
/*
* 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.Collections;
import java.util.Optional;
import java.util.function.Supplier;
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.Pointer;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
/**
* A receiver that wraps one or more receivers.
*
* <p><code>ForwardingAudience</code> is designed to easily allow users or
* implementations wrap an existing (collection of) <code>Audience</code>(s).</p>
*
* @see Audience
* @since 4.0.0
*/
@FunctionalInterface
public interface ForwardingAudience extends Audience {
/**
* Gets the audiences.
*
* @return the audiences
* @since 4.0.0
*/
@ApiStatus.OverrideOnly
@NotNull Iterable<? extends Audience> audiences();
@Override
default <T> @NotNull Optional<T> get(final @NotNull Pointer<T> pointer) {
return Optional.empty(); // unsupported
}
@Contract("_, null -> null; _, !null -> !null")
@Override
default <T> @Nullable T getOrDefault(final @NotNull Pointer<T> pointer, final @Nullable T defaultValue) {
return defaultValue; // unsupported
}
@Override
default <T> @UnknownNullability T getOrDefaultFrom(final @NotNull Pointer<T> pointer, final @NotNull Supplier<? extends T> defaultValue) {
return defaultValue.get(); // unsupported
}
@Override
default void sendMessage(final @NotNull Identified source, final @NotNull Component message, final @NotNull MessageType type) {
for (final Audience audience : this.audiences()) audience.sendMessage(source, message, type);
}
@Override
default void sendMessage(final @NotNull Identity source, final @NotNull Component message, final @NotNull MessageType type) {
for (final Audience audience : this.audiences()) audience.sendMessage(source, message, type);
}
@Override
default void sendActionBar(final @NotNull Component message) {
for (final Audience audience : this.audiences()) audience.sendActionBar(message);
}
@Override
default void sendPlayerListHeader(final @NotNull Component header) {
for (final Audience audience : this.audiences()) audience.sendPlayerListHeader(header);
}
@Override
default void sendPlayerListFooter(final @NotNull Component footer) {
for (final Audience audience : this.audiences()) audience.sendPlayerListFooter(footer);
}
@Override
default void sendPlayerListHeaderAndFooter(final @NotNull Component header, final @NotNull Component footer) {
for (final Audience audience : this.audiences()) audience.sendPlayerListHeaderAndFooter(header, footer);
}
@Override
default void showTitle(final @NotNull Title title) {
for (final Audience audience : this.audiences()) audience.showTitle(title);
}
@Override
default void clearTitle() {
for (final Audience audience : this.audiences()) audience.clearTitle();
}
@Override
default void resetTitle() {
for (final Audience audience : this.audiences()) audience.resetTitle();
}
@Override
default void showBossBar(final @NotNull BossBar bar) {
for (final Audience audience : this.audiences()) audience.showBossBar(bar);
}
@Override
default void hideBossBar(final @NotNull BossBar bar) {
for (final Audience audience : this.audiences()) audience.hideBossBar(bar);
}
@Override
default void playSound(final @NotNull Sound sound) {
for (final Audience audience : this.audiences()) audience.playSound(sound);
}
@Override
default void playSound(final @NotNull Sound sound, final double x, final double y, final double z) {
for (final Audience audience : this.audiences()) audience.playSound(sound, x, y, z);
}
@Override
default void playSound(final @NotNull Sound sound, final Sound.@NotNull Emitter emitter) {
for (final Audience audience : this.audiences()) audience.playSound(sound, emitter);
}
@Override
default void stopSound(final @NotNull SoundStop stop) {
for (final Audience audience : this.audiences()) audience.stopSound(stop);
}
@Override
default void openBook(final @NotNull Book book) {
for (final Audience audience : this.audiences()) audience.openBook(book);
}
/**
* An audience that forwards everything to a single other audience.
*
* @since 4.0.0
*/
interface Single extends ForwardingAudience {
/**
* Gets the audience.
*
* @return the audience
* @since 4.0.0
*/
@ApiStatus.OverrideOnly
@NotNull Audience audience();
/**
* {@inheritDoc}
*
* @return {@link #audience()}
* @deprecated this audience only supports forwarding to a single audience
*/
@Deprecated(/* forRemoval = false */)
@Override
default @NotNull Iterable<? extends Audience> audiences() {
return Collections.singleton(this.audience());
}
@Override
default <T> @NotNull Optional<T> get(final @NotNull Pointer<T> pointer) {
return this.audience().get(pointer);
}
@Contract("_, null -> null; _, !null -> !null")
@Override
default <T> @Nullable T getOrDefault(final @NotNull Pointer<T> pointer, final @Nullable T defaultValue) {
return this.audience().getOrDefault(pointer, defaultValue);
}
@Override
default <T> @UnknownNullability T getOrDefaultFrom(final @NotNull Pointer<T> pointer, final @NotNull Supplier<? extends T> defaultValue) {
return this.audience().getOrDefaultFrom(pointer, defaultValue);
}
@Override
default void sendMessage(final @NotNull Identified source, final @NotNull Component message, final @NotNull MessageType type) {
this.audience().sendMessage(source, message, type);
}
@Override
default void sendMessage(final @NotNull Identity source, final @NotNull Component message, final @NotNull MessageType type) {
this.audience().sendMessage(source, message, type);
}
@Override
default void sendActionBar(final @NotNull Component message) {
this.audience().sendActionBar(message);
}
@Override
default void sendPlayerListHeader(final @NotNull Component header) {
this.audience().sendPlayerListHeader(header);
}
@Override
default void sendPlayerListFooter(final @NotNull Component footer) {
this.audience().sendPlayerListFooter(footer);
}
@Override
default void sendPlayerListHeaderAndFooter(final @NotNull Component header, final @NotNull Component footer) {
this.audience().sendPlayerListHeaderAndFooter(header, footer);
}
@Override
default void showTitle(final @NotNull Title title) {
this.audience().showTitle(title);
}
@Override
default void clearTitle() {
this.audience().clearTitle();
}
@Override
default void resetTitle() {
this.audience().resetTitle();
}
@Override
default void showBossBar(final @NotNull BossBar bar) {
this.audience().showBossBar(bar);
}
@Override
default void hideBossBar(final @NotNull BossBar bar) {
this.audience().hideBossBar(bar);
}
@Override
default void playSound(final @NotNull Sound sound) {
this.audience().playSound(sound);
}
@Override
default void playSound(final @NotNull Sound sound, final double x, final double y, final double z) {
this.audience().playSound(sound, x, y, z);
}
@Override
default void playSound(final @NotNull Sound sound, final Sound.@NotNull Emitter emitter) {
this.audience().playSound(sound, emitter);
}
@Override
default void stopSound(final @NotNull SoundStop stop) {
this.audience().stopSound(stop);
}
@Override
default void openBook(final @NotNull Book book) {
this.audience().openBook(book);
}
}
}