|
5 | 5 | import org.spongepowered.api.command.CommandResult;
|
6 | 6 | import org.spongepowered.api.command.CommandSource;
|
7 | 7 | import org.spongepowered.api.command.args.CommandContext;
|
8 |
| -import org.spongepowered.api.command.args.GenericArguments; |
9 | 8 | import org.spongepowered.api.command.spec.CommandSpec;
|
10 | 9 | import org.spongepowered.api.entity.living.player.Player;
|
11 |
| -import org.spongepowered.api.service.pagination.PaginationService; |
12 | 10 | import org.spongepowered.api.text.Text;
|
13 |
| -import org.spongepowered.api.text.action.TextActions; |
14 |
| -import org.spongepowered.api.text.format.TextColors; |
15 | 11 | import se.walkercrou.composer.Composer;
|
16 |
| -import se.walkercrou.composer.Score; |
17 |
| -import se.walkercrou.composer.nbs.NoteBlockStudioSong; |
| 12 | +import se.walkercrou.composer.util.TextUtil; |
| 13 | +import se.walkercrou.composer.nbs.MusicPlayer; |
18 | 14 |
|
19 |
| -import java.util.ArrayList; |
20 |
| -import java.util.List; |
21 |
| -import java.util.Map; |
22 |
| -import java.util.UUID; |
| 15 | +import static org.spongepowered.api.command.args.GenericArguments.*; |
23 | 16 |
|
24 | 17 | /**
|
25 | 18 | * Main commands for plugin.
|
26 | 19 | */
|
27 | 20 | public class ComposerCommands {
|
28 | 21 | private final Composer plugin;
|
29 | 22 | private final CommandSpec list = CommandSpec.builder()
|
30 |
| - .arguments(GenericArguments.optional(GenericArguments.integer(Text.of("page")))) |
| 23 | + .arguments(optional(integer(Text.of("page")))) |
31 | 24 | .description(Text.of("Lists the currently loaded tracks."))
|
32 | 25 | .executor(this::listTracks)
|
33 | 26 | .build();
|
34 |
| - private final CommandSpec main = CommandSpec.builder() |
| 27 | + private final CommandSpec play = CommandSpec.builder() |
| 28 | + .arguments(flags() |
| 29 | + .valueFlag(player(Text.of("player")), "p") |
| 30 | + .buildWith(integer(Text.of("trackNumber"))) |
| 31 | + ) |
| 32 | + .description(Text.of("Plays the specified track.")) |
| 33 | + .executor(this::playTrack) |
| 34 | + .build(); |
| 35 | + private final CommandSpec pause = CommandSpec.builder() |
| 36 | + .arguments(optional(player(Text.of("player")))) |
| 37 | + .description(Text.of("Pauses the track that is currently playing.")) |
| 38 | + .executor(this::pauseTrack) |
| 39 | + .build(); |
| 40 | + private final CommandSpec resume = CommandSpec.builder() |
| 41 | + .arguments(optional(player(Text.of("player")))) |
| 42 | + .description(Text.of("Resumes playing or starts playing the first track.")) |
| 43 | + .executor(this::resumeTrack) |
| 44 | + .build(); |
| 45 | + private final CommandSpec shuffle = CommandSpec.builder() |
| 46 | + .arguments(optional(player(Text.of("player")))) |
| 47 | + .description(Text.of("Shuffles the tracks and starts playing.")) |
| 48 | + .executor(this::shuffleTracks) |
| 49 | + .build(); |
| 50 | + private final CommandSpec queue = CommandSpec.builder() |
| 51 | + .arguments(optional(player(Text.of("player")))) |
| 52 | + .description(Text.of("Prints the specified player's play queue.")) |
| 53 | + .executor(this::printPlayQueue) |
| 54 | + .build(); |
| 55 | + private final CommandSpec next = CommandSpec.builder() |
| 56 | + .arguments(optional(player(Text.of("player")))) |
| 57 | + .description(Text.of("Starts the next song in the queue.")) |
| 58 | + .executor(this::nextTrack) |
| 59 | + .build(); |
| 60 | + private final CommandSpec previous = CommandSpec.builder() |
| 61 | + .arguments(optional(player(Text.of("player")))) |
| 62 | + .description(Text.of("Goes back to the previous song in the queue.")) |
| 63 | + .executor(this::previousTrack) |
| 64 | + .build(); |
| 65 | + private final CommandSpec base = CommandSpec.builder() |
35 | 66 | .description(Text.of("Main parent command for plugin."))
|
| 67 | + .executor(this::listTracks) |
36 | 68 | .child(list, "list", "list-tracks", "tracks", "track-list")
|
| 69 | + .child(play, "play", "start", ">") |
| 70 | + .child(pause, "pause", "stop", "||") |
| 71 | + .child(resume, "resume") |
| 72 | + .child(shuffle, "shuffle") |
| 73 | + .child(queue, "queue", "order") |
| 74 | + .child(next, "next", "skip", ">|") |
| 75 | + .child(previous, "previous", "back", "|<") |
37 | 76 | .build();
|
38 | 77 |
|
39 | 78 | public ComposerCommands(Composer plugin) {
|
40 | 79 | this.plugin = plugin;
|
41 | 80 | }
|
42 | 81 |
|
43 | 82 | public void register() {
|
44 |
| - Sponge.getCommandManager().register(plugin, main, "music", "composer"); |
| 83 | + Sponge.getCommandManager().register(plugin, base, "music", "composer"); |
45 | 84 | }
|
46 | 85 |
|
47 |
| - /** |
48 |
| - * Lists the currently loaded tracks. |
49 |
| - * |
50 |
| - * @param source command source |
51 |
| - * @param context command context |
52 |
| - * @return result |
53 |
| - */ |
54 |
| - public CommandResult listTracks(CommandSource source, CommandContext context) throws CommandException { |
55 |
| - List<NoteBlockStudioSong> tracks = plugin.getNbsTracks(); |
56 |
| - List<Text> trackListings = new ArrayList<>(tracks.size()); |
| 86 | + public CommandResult previousTrack(CommandSource src, CommandContext context) throws CommandException { |
| 87 | + Player player = getPlayer(src, context); |
| 88 | + plugin.getMusicPlayer(player).previous(player); |
| 89 | + return CommandResult.success(); |
| 90 | + } |
57 | 91 |
|
58 |
| - final Player player = source instanceof Player ? (Player) source : null; |
59 |
| - for (int i = 0; i < tracks.size(); i++) { |
60 |
| - final int index = i; |
61 |
| - NoteBlockStudioSong track = tracks.get(i); |
62 |
| - trackListings.add(trackText(track) |
63 |
| - .onClick(TextActions.executeCallback(src -> playSong(player, index))) |
64 |
| - .build()); |
65 |
| - } |
| 92 | + public CommandResult nextTrack(CommandSource src, CommandContext context) throws CommandException { |
| 93 | + Player player = getPlayer(src, context); |
| 94 | + plugin.getMusicPlayer(player).next(player); |
| 95 | + return CommandResult.success(); |
| 96 | + } |
| 97 | + |
| 98 | + public CommandResult printPlayQueue(CommandSource src, CommandContext context) throws CommandException { |
| 99 | + Player player = getPlayer(src, context); |
| 100 | + TextUtil.trackList(plugin.getMusicPlayer(player).getTracks()).sendTo(src); |
| 101 | + return CommandResult.success(); |
| 102 | + } |
66 | 103 |
|
67 |
| - if (trackListings.isEmpty()) |
68 |
| - throw new CommandException(Text.of("There are no tracks currently loaded.")); |
| 104 | + public CommandResult shuffleTracks(CommandSource src, CommandContext context) throws CommandException { |
| 105 | + Player player = getPlayer(src, context); |
| 106 | + plugin.getMusicPlayer(player).shuffle(player); |
| 107 | + return CommandResult.success(); |
| 108 | + } |
69 | 109 |
|
70 |
| - Sponge.getServiceManager().provide(PaginationService.class).get().builder() |
71 |
| - .contents(trackListings) |
72 |
| - .title(Text.builder("Tracks").color(TextColors.GOLD).build()) |
73 |
| - .footer(Text.builder("Click a track to play it").color(TextColors.GRAY).build()) |
74 |
| - .paddingString("-") |
75 |
| - .sendTo(source); |
| 110 | + public CommandResult resumeTrack(CommandSource src, CommandContext context) throws CommandException { |
| 111 | + Player player = getPlayer(src, context); |
| 112 | + MusicPlayer mp = plugin.getMusicPlayer(player); |
| 113 | + if (mp.isPlaying()) |
| 114 | + throw new CommandException(Text.of("Music already playing.")); |
| 115 | + mp.play(player); |
| 116 | + return CommandResult.success(); |
| 117 | + } |
76 | 118 |
|
| 119 | + public CommandResult pauseTrack(CommandSource src, CommandContext context) throws CommandException { |
| 120 | + Player player = getPlayer(src, context); |
| 121 | + MusicPlayer mp = plugin.getMusicPlayer(player); |
| 122 | + if (!mp.isPlaying()) |
| 123 | + throw new CommandException(Text.of("No music playing.")); |
| 124 | + mp.pause(); |
77 | 125 | return CommandResult.success();
|
78 | 126 | }
|
79 | 127 |
|
80 |
| - private Text.Builder trackText(NoteBlockStudioSong track) { |
81 |
| - return Text.builder(strOrUnknown(track.name)) |
82 |
| - .color(TextColors.GREEN) |
83 |
| - .append(Text.builder(" by ").color(TextColors.GRAY).build()) |
84 |
| - .append(Text.builder(strOrUnknown(track.ogAuthor).equals("Unknown") |
85 |
| - ? strOrUnknown(track.author) : track.ogAuthor) |
86 |
| - .color(TextColors.GREEN) |
87 |
| - .build()); |
| 128 | + public CommandResult playTrack(CommandSource src, CommandContext context) throws CommandException { |
| 129 | + int trackIndex = context.<Integer>getOne("trackNumber").get() - 1; |
| 130 | + Player player = getPlayer(src, context); |
| 131 | + plugin.getMusicPlayer(player).play(player, trackIndex); |
| 132 | + return CommandResult.success(); |
88 | 133 | }
|
89 | 134 |
|
90 |
| - private String strOrUnknown(String str) { |
91 |
| - return str == null || str.isEmpty() ? "Unknown" : str; |
| 135 | + public CommandResult listTracks(CommandSource source, CommandContext context) throws CommandException { |
| 136 | + TextUtil.trackList(plugin.getNbsTracks()).sendTo(source); |
| 137 | + return CommandResult.success(); |
92 | 138 | }
|
93 | 139 |
|
94 |
| - private void playSong(Player player, int index) { |
95 |
| - Map<UUID, Score> nowPlaying = plugin.nowPlaying(); |
96 |
| - UUID id = player.getUniqueId(); |
97 |
| - Score currentSong = nowPlaying.get(id); |
98 |
| - NoteBlockStudioSong track = plugin.getNbsTracks().get(index); |
99 |
| - Score newSong = track.toScore().onFinish(() -> nowPlaying.put(id, null)); |
100 |
| - if (currentSong != null) |
101 |
| - currentSong.finish(); |
102 |
| - newSong.play(plugin, player, player.getLocation().getPosition()); |
103 |
| - nowPlaying.put(id, newSong); |
104 |
| - player.sendMessage(Text.builder("Now playing: ") |
105 |
| - .color(TextColors.GOLD) |
106 |
| - .append(trackText(track).build()) |
107 |
| - .build()); |
| 140 | + private Player getPlayer(CommandSource src, CommandContext context) throws CommandException { |
| 141 | + Player player = context.<Player>getOne("player").orElse(null); |
| 142 | + if (player == null) { |
| 143 | + if (!(src instanceof Player)) |
| 144 | + throw new CommandException(Text.of("Cannot run command as non-player without player argument.")); |
| 145 | + player = (Player) src; |
| 146 | + } |
| 147 | + return player; |
108 | 148 | }
|
109 | 149 | }
|
0 commit comments