-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
BukkitFacet.java
421 lines (369 loc) · 16.2 KB
/
BukkitFacet.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
/*
* This file is part of adventure-platform, licensed under the MIT License.
*
* Copyright (c) 2018-2020 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.platform.bukkit;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import java.util.Collection;
import java.util.Set;
import java.util.function.Function;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.permission.PermissionChecker;
import net.kyori.adventure.platform.facet.Facet;
import net.kyori.adventure.platform.facet.FacetBase;
import net.kyori.adventure.platform.facet.FacetPointers;
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.TriState;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.SoundCategory;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import static net.kyori.adventure.platform.bukkit.BukkitComponentSerializer.legacy;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findClass;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.hasClass;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.hasMethod;
import static net.kyori.adventure.platform.facet.Knob.logUnsupported;
class BukkitFacet<V extends CommandSender> extends FacetBase<V> {
protected BukkitFacet(final @Nullable Class<? extends V> viewerClass) {
super(viewerClass);
}
static class Message<V extends CommandSender> extends BukkitFacet<V> implements Facet.Message<V, String> {
protected Message(final @Nullable Class<? extends V> viewerClass) {
super(viewerClass);
}
@Override
public @NotNull String createMessage(final @NotNull V viewer, final @NotNull Component message) {
return legacy().serialize(message);
}
}
static class Chat extends Message<CommandSender> implements Facet.Chat<CommandSender, String> {
protected Chat() {
super(CommandSender.class);
}
@Override
public void sendMessage(final @NotNull CommandSender viewer, final @NotNull Identity source, final @NotNull String message, final @NotNull Object type) {
viewer.sendMessage(message);
}
}
static class Position extends BukkitFacet<Player> implements Facet.Position<Player, Vector> {
protected Position() {
super(Player.class);
}
@Override
public @NotNull Vector createPosition(final @NotNull Player viewer) {
return viewer.getLocation().toVector();
}
@Override
public @NotNull Vector createPosition(final double x, final double y, final double z) {
return new Vector(x, y, z);
}
}
static class Sound extends Position implements Facet.Sound<Player, Vector> {
private static final boolean KEY_SUPPORTED = hasClass("org.bukkit.NamespacedKey"); // Added MC 1.13
private static final boolean STOP_SUPPORTED = hasMethod(Player.class, "stopSound", String.class); // Added MC 1.9
@Override
public void playSound(final @NotNull Player viewer, final net.kyori.adventure.sound.@NotNull Sound sound, final @NotNull Vector vector) {
final String name = name(sound.name());
final Location location = vector.toLocation(viewer.getWorld());
viewer.playSound(location, name, sound.volume(), sound.pitch());
}
@Override
public void stopSound(final @NotNull Player viewer, final @NotNull SoundStop stop) {
if (STOP_SUPPORTED) {
final String name = name(stop.sound());
viewer.stopSound(name);
}
}
protected static @NotNull String name(final @Nullable Key name) {
if (name == null) {
return "";
}
if (KEY_SUPPORTED) { // Sound format changed to use identifiers
return name.asString();
} else {
return name.value();
}
}
}
static class SoundWithCategory extends Sound {
private static final boolean SUPPORTED = hasMethod(Player.class, "stopSound", String.class, findClass("org.bukkit.SoundCategory")); // Added MC 1.11
@Override
public boolean isSupported() {
return super.isSupported() && SUPPORTED;
}
@Override
public void playSound(final @NotNull Player viewer, final net.kyori.adventure.sound.@NotNull Sound sound, final @NotNull Vector vector) {
final SoundCategory category = this.category(sound.source());
if (category == null) {
super.playSound(viewer, sound, vector);
} else {
final String name = name(sound.name());
viewer.playSound(vector.toLocation(viewer.getWorld()), name, category, sound.volume(), sound.pitch());
}
}
@Override
public void stopSound(final @NotNull Player viewer, final @NotNull SoundStop stop) {
final SoundCategory category = this.category(stop.source());
if (category == null) {
super.stopSound(viewer, stop);
} else {
final String name = name(stop.sound());
viewer.stopSound(name, category);
}
}
private @Nullable SoundCategory category(final net.kyori.adventure.sound.Sound.@Nullable Source source) {
if (source == null) {
return null;
}
if (source == net.kyori.adventure.sound.Sound.Source.MASTER) {
return SoundCategory.MASTER;
} else if (source == net.kyori.adventure.sound.Sound.Source.MUSIC) {
return SoundCategory.MUSIC;
} else if (source == net.kyori.adventure.sound.Sound.Source.RECORD) {
return SoundCategory.RECORDS;
} else if (source == net.kyori.adventure.sound.Sound.Source.WEATHER) {
return SoundCategory.WEATHER;
} else if (source == net.kyori.adventure.sound.Sound.Source.BLOCK) {
return SoundCategory.BLOCKS;
} else if (source == net.kyori.adventure.sound.Sound.Source.HOSTILE) {
return SoundCategory.HOSTILE;
} else if (source == net.kyori.adventure.sound.Sound.Source.NEUTRAL) {
return SoundCategory.NEUTRAL;
} else if (source == net.kyori.adventure.sound.Sound.Source.PLAYER) {
return SoundCategory.PLAYERS;
} else if (source == net.kyori.adventure.sound.Sound.Source.AMBIENT) {
return SoundCategory.AMBIENT;
} else if (source == net.kyori.adventure.sound.Sound.Source.VOICE) {
return SoundCategory.VOICE;
}
logUnsupported(this, source);
return null;
}
}
static class BossBarBuilder extends BukkitFacet<Player> implements Facet.BossBar.Builder<Player, net.kyori.adventure.platform.bukkit.BukkitFacet.BossBar> {
private static final boolean SUPPORTED = hasClass("org.bukkit.boss.BossBar"); // Added MC 1.9
protected BossBarBuilder() {
super(Player.class);
}
@Override
public boolean isSupported() {
return super.isSupported() && SUPPORTED;
}
@Override
public net.kyori.adventure.platform.bukkit.BukkitFacet.@NotNull BossBar createBossBar(final @NotNull Collection<Player> viewers) {
return new net.kyori.adventure.platform.bukkit.BukkitFacet.BossBar(viewers);
}
}
static class BossBar extends Message<Player> implements Facet.BossBar<Player> {
protected final org.bukkit.boss.BossBar bar;
protected BossBar(final @NotNull Collection<Player> viewers) {
super(Player.class);
this.bar = Bukkit.createBossBar("", BarColor.PINK, BarStyle.SOLID);
this.bar.setVisible(false);
for (final Player viewer : viewers) {
this.bar.addPlayer(viewer);
}
}
@Override
public void bossBarInitialized(final net.kyori.adventure.bossbar.@NotNull BossBar bar) {
Facet.BossBar.super.bossBarInitialized(bar);
this.bar.setVisible(true);
}
@Override
public void bossBarNameChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final @NotNull Component oldName, final @NotNull Component newName) {
if (!this.bar.getPlayers().isEmpty()) {
this.bar.setTitle(this.createMessage(this.bar.getPlayers().get(0), newName));
}
}
@Override
public void bossBarProgressChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final float oldPercent, final float newPercent) {
this.bar.setProgress(newPercent);
}
@Override
public void bossBarColorChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final net.kyori.adventure.bossbar.BossBar.@NotNull Color oldColor, final net.kyori.adventure.bossbar.BossBar.@NotNull Color newColor) {
final BarColor color = this.color(newColor);
if (color != null) {
this.bar.setColor(color);
}
}
private @Nullable BarColor color(final net.kyori.adventure.bossbar.BossBar.@NotNull Color color) {
if (color == net.kyori.adventure.bossbar.BossBar.Color.PINK) {
return BarColor.PINK;
} else if (color == net.kyori.adventure.bossbar.BossBar.Color.BLUE) {
return BarColor.BLUE;
} else if (color == net.kyori.adventure.bossbar.BossBar.Color.RED) {
return BarColor.RED;
} else if (color == net.kyori.adventure.bossbar.BossBar.Color.GREEN) {
return BarColor.GREEN;
} else if (color == net.kyori.adventure.bossbar.BossBar.Color.YELLOW) {
return BarColor.YELLOW;
} else if (color == net.kyori.adventure.bossbar.BossBar.Color.PURPLE) {
return BarColor.PURPLE;
} else if (color == net.kyori.adventure.bossbar.BossBar.Color.WHITE) {
return BarColor.WHITE;
}
logUnsupported(this, color);
return null;
}
@Override
public void bossBarOverlayChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final net.kyori.adventure.bossbar.BossBar.@NotNull Overlay oldOverlay, final net.kyori.adventure.bossbar.BossBar.@NotNull Overlay newOverlay) {
final BarStyle style = this.style(newOverlay);
if (style != null) {
this.bar.setStyle(style);
}
}
private @Nullable BarStyle style(final net.kyori.adventure.bossbar.BossBar.@NotNull Overlay overlay) {
if (overlay == net.kyori.adventure.bossbar.BossBar.Overlay.PROGRESS) {
return BarStyle.SOLID;
} else if (overlay == net.kyori.adventure.bossbar.BossBar.Overlay.NOTCHED_6) {
return BarStyle.SEGMENTED_6;
} else if (overlay == net.kyori.adventure.bossbar.BossBar.Overlay.NOTCHED_10) {
return BarStyle.SEGMENTED_10;
} else if (overlay == net.kyori.adventure.bossbar.BossBar.Overlay.NOTCHED_12) {
return BarStyle.SEGMENTED_12;
} else if (overlay == net.kyori.adventure.bossbar.BossBar.Overlay.NOTCHED_20) {
return BarStyle.SEGMENTED_20;
}
logUnsupported(this, overlay);
return null;
}
@Override
public void bossBarFlagsChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final @NotNull Set<net.kyori.adventure.bossbar.BossBar.Flag> flagsAdded, final @NotNull Set<net.kyori.adventure.bossbar.BossBar.Flag> flagsRemoved) {
for (final net.kyori.adventure.bossbar.BossBar.Flag removeFlag : flagsRemoved) {
final BarFlag flag = this.flag(removeFlag);
if (flag != null) {
this.bar.removeFlag(flag);
}
}
for (final net.kyori.adventure.bossbar.BossBar.Flag addFlag : flagsAdded) {
final BarFlag flag = this.flag(addFlag);
if (flag != null) {
this.bar.addFlag(flag);
}
}
}
private @Nullable BarFlag flag(final net.kyori.adventure.bossbar.BossBar.@NotNull Flag flag) {
if (flag == net.kyori.adventure.bossbar.BossBar.Flag.DARKEN_SCREEN) {
return BarFlag.DARKEN_SKY;
} else if (flag == net.kyori.adventure.bossbar.BossBar.Flag.PLAY_BOSS_MUSIC) {
return BarFlag.PLAY_BOSS_MUSIC;
} else if (flag == net.kyori.adventure.bossbar.BossBar.Flag.CREATE_WORLD_FOG) {
return BarFlag.CREATE_FOG;
}
logUnsupported(this, flag);
return null;
}
@Override
public void addViewer(final @NotNull Player viewer) {
this.bar.addPlayer(viewer);
}
@Override
public void removeViewer(final @NotNull Player viewer) {
this.bar.removePlayer(viewer);
}
@Override
public boolean isEmpty() {
return !this.bar.isVisible() || this.bar.getPlayers().isEmpty();
}
@Override
public void close() {
this.bar.removeAll();
}
}
static final class ViaHook implements Function<Player, UserConnection> {
@Override
public UserConnection apply(final @NotNull Player player) {
return Via.getManager().getConnectionManager().getConnectedClient(player.getUniqueId());
}
}
static final class TabList extends Message<Player> implements Facet.TabList<Player, String> {
// All methods added at the same time
private static final boolean SUPPORTED = hasMethod(Player.class, "setPlayerListHeader", String.class);
TabList() {
super(Player.class);
}
@Override
public boolean isSupported() {
return SUPPORTED && super.isSupported();
}
@Override
public void send(final Player viewer, final @Nullable String header, final @Nullable String footer) {
if (header != null && footer != null) {
viewer.setPlayerListHeaderFooter(header, footer);
} else if (header != null) {
viewer.setPlayerListHeader(header);
} else if (footer != null) {
viewer.setPlayerListFooter(footer);
}
}
}
static final class CommandSenderPointers extends BukkitFacet<CommandSender> implements Facet.Pointers<CommandSender> {
CommandSenderPointers() {
super(CommandSender.class);
}
@Override
public void contributePointers(final CommandSender viewer, final net.kyori.adventure.pointer.Pointers.Builder builder) {
// Name
builder.withDynamic(Identity.NAME, viewer::getName);
// Permission (technically up in Permissible but *shrug*)
builder.withStatic(PermissionChecker.POINTER, perm -> {
if (viewer.isPermissionSet(perm)) {
return viewer.hasPermission(perm) ? TriState.TRUE : TriState.FALSE;
} else {
return TriState.NOT_SET;
}
});
}
}
static final class ConsoleCommandSenderPointers extends BukkitFacet<ConsoleCommandSender> implements Facet.Pointers<ConsoleCommandSender> {
ConsoleCommandSenderPointers() {
super(ConsoleCommandSender.class);
}
@Override
public void contributePointers(final ConsoleCommandSender viewer, final net.kyori.adventure.pointer.Pointers.Builder builder) {
builder.withStatic(FacetPointers.TYPE, FacetPointers.Type.CONSOLE);
}
}
static final class PlayerPointers extends BukkitFacet<Player> implements Facet.Pointers<Player> {
PlayerPointers() {
super(Player.class);
}
@Override
public void contributePointers(final Player viewer, final net.kyori.adventure.pointer.Pointers.Builder builder) {
builder.withDynamic(Identity.UUID, viewer::getUniqueId);
builder.withDynamic(Identity.DISPLAY_NAME, () -> BukkitComponentSerializer.legacy().deserializeOrNull(viewer.getDisplayName()));
builder.withStatic(FacetPointers.TYPE, FacetPointers.Type.PLAYER);
builder.withDynamic(FacetPointers.WORLD, () -> Key.key(viewer.getWorld().getName())); // :(
}
}
}