-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
NBSCommand.java
140 lines (115 loc) · 4.97 KB
/
NBSCommand.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
package com.denizenscript.depenizen.bukkit.commands.noteblockapi;
import com.xxmicloxx.NoteBlockAPI.NoteBlockAPI;
import com.xxmicloxx.NoteBlockAPI.model.Song;
import com.xxmicloxx.NoteBlockAPI.songplayer.RadioSongPlayer;
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
import com.xxmicloxx.NoteBlockAPI.utils.NBSDecoder;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.Utilities;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.debugging.dB;
import java.io.File;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.List;
public class NBSCommand extends AbstractCommand {
// <--[command]
// @Name nbs
// @Syntax nbs [play/stop] (file:<file path>) [targets:<entity>|...]
// @Group Depenizen
// @Plugin Depenizen, NoteBlockAPI
// @Required 2
// @Short Plays or stops a noteblock song.
// @Description
// Plays a .nbs file for the targets, being players specified.
// If no targets are specified, the target will be the player in the queue.
// Note block song files are created using NoteBlockStudio or other programs.
// The file path starts in the denizen folder: /plugins/Denizen/
// @Tags
// <p@player.nbs_is_playing>
// @Usage
// Use to play a song to the linked player in a queue.
// - nbs play file:MySong
// @Usage
// Use to play a song to everyone online.
// - nbs play file:MySong targets:<player.list_online_players>
// @Usage
// Use to stop the current song playing for the linked player in a queue.
// - nbs stop
// @Usage
// Use to stop the current song playing for all online players.
// - nbs stop targets:<player.list_online_players>
// -->
private enum Action {PLAY, STOP}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("targets")
&& arg.matchesPrefix("targets", "targets")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("targets", arg.asType(dList.class).filter(dPlayer.class));
}
else if (!scriptEntry.hasObject("file")
&& arg.matchesPrefix("file")) {
scriptEntry.addObject("file", arg.asElement());
}
else if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Action not specified! (play/stop)");
}
else if (!scriptEntry.hasObject("targets")) {
if (Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("targets", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
}
else {
throw new InvalidArgumentsException("Must specify players to add, remove or spectate!");
}
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) {
Element file = scriptEntry.getdObject("file");
Element action = scriptEntry.getdObject("action");
List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
// Report to dB
dB.report(scriptEntry, getName(), action.debug()
+ aH.debugList("targets", targets)
+ (file != null ? file.debug() : ""));
if (targets == null || targets.isEmpty()) {
dB.echoError(scriptEntry.getResidingQueue(), "Targets not found!");
return;
}
if (action.asString().equalsIgnoreCase("play")) {
if (file == null) {
dB.echoError(scriptEntry.getResidingQueue(), "File not specified!");
return;
}
String directory = URLDecoder.decode(System.getProperty("user.dir"));
Song s = NBSDecoder.parse(new File(directory + "/plugins/Denizen/" + file + ".nbs"));
SongPlayer sp = new RadioSongPlayer(s);
sp.setAutoDestroy(true);
for (dPlayer p : targets) {
sp.addPlayer(p.getPlayerEntity());
}
sp.setPlaying(true);
}
else if (action.asString().equalsIgnoreCase("stop")) {
for (dPlayer p : targets) {
NoteBlockAPI.stopPlaying(p.getPlayerEntity());
}
}
}
}