/
NoteBlockAPISongEndsScriptEvent.java
85 lines (74 loc) · 2.57 KB
/
NoteBlockAPISongEndsScriptEvent.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
package com.denizenscript.depenizen.bukkit.events.noteblockapi;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.xxmicloxx.NoteBlockAPI.event.SongEndEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.nio.file.Path;
public class NoteBlockAPISongEndsScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// noteblockapi song (<'song'>) ends
//
// @Cancellable true
//
// @Triggers when a song playing through NoteBlockAPI ends.
//
// @Context
// <context.song> returns the file name of the song.
//
// @Plugin Depenizen, NoteBlockAPI
//
// @Group Depenizen
//
// -->
public NoteBlockAPISongEndsScriptEvent() {
registerCouldMatcher("noteblockapi song (<'song'>) ends");
}
public SongEndEvent event;
@Override
public boolean matches(ScriptPath path) {
String songName = path.eventArgLowerAt(2);
if (!songName.equals("ends") && !songName.equals(CoreUtilities.toLowerCase(getSongFileName()))) {
return false;
}
return true;
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(null, null);
}
public String getSongFileName() {
try {
Path songPath = event.getSongPlayer().getSong().getPath().toPath();
Path denizenPath = Denizen.getInstance().getDataFolder().toPath();
String finalPath = denizenPath.relativize(songPath).toString();
if (finalPath.endsWith(".nbs")) {
finalPath = finalPath.substring(0, finalPath.length() - ".nbs".length());
}
return finalPath;
}
catch (Throwable ex) {
Debug.echoError(ex);
return null;
}
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("song")) {
return new ElementTag(getSongFileName());
}
return super.getContext(name);
}
@EventHandler
public void onSongEndEvent(SongEndEvent event) {
this.event = event;
fire(event);
}
}