/
NoteBlockPlaysNoteScriptEvent.java
138 lines (127 loc) · 4.98 KB
/
NoteBlockPlaysNoteScriptEvent.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
package com.denizenscript.denizen.events.block;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.Sound;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.NotePlayEvent;
public class NoteBlockPlaysNoteScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// noteblock plays note
//
// @Regex ^on noteblock plays note$
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
// @Switch instrument:<instrument> to only process the event if a specific instrument was played.
//
// @Cancellable true
//
// @Triggers when a NoteBlock plays a note.
//
// @Context
// <context.location> returns the LocationTag of the note block.
// <context.instrument> returns the name of the instrument played, see list at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Instrument.html>.
// <context.sound> returns the name of the sound (that fits into <@link command playsound>) represented by the instrument.
// <context.tone> returns the the note tone played (A to G).
// <context.octave> returns the octave the note is played at (as a number).
// <context.sharp> returns a boolean indicating whether the note is sharp.
// <context.pitch> returns the computed pitch value (that fits into <@link command playsound>). Note that volume is always 3.
//
// -->
public NoteBlockPlaysNoteScriptEvent() {
instance = this;
}
public static NoteBlockPlaysNoteScriptEvent instance;
public NotePlayEvent event;
public LocationTag location;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("noteblock plays note");
}
@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, location)) {
return false;
}
if (!runGenericSwitchCheck(path, "instrument", event.getInstrument().name())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "NoteBlockPlaysNote";
}
public Sound getSound() {
switch (event.getInstrument()) {
case PIANO:
return Sound.BLOCK_NOTE_BLOCK_HARP;
case BASS_DRUM:
return Sound.BLOCK_NOTE_BLOCK_BASEDRUM;
case SNARE_DRUM:
return Sound.BLOCK_NOTE_BLOCK_SNARE;
case STICKS:
return Sound.BLOCK_NOTE_BLOCK_HAT;
case BASS_GUITAR:
return Sound.BLOCK_NOTE_BLOCK_BASS;
case FLUTE:
return Sound.BLOCK_NOTE_BLOCK_FLUTE;
case BELL:
return Sound.BLOCK_NOTE_BLOCK_BELL;
case GUITAR:
return Sound.BLOCK_NOTE_BLOCK_GUITAR;
case CHIME:
return Sound.BLOCK_NOTE_BLOCK_CHIME;
case XYLOPHONE:
return Sound.BLOCK_NOTE_BLOCK_XYLOPHONE;
case IRON_XYLOPHONE:
return Sound.BLOCK_NOTE_BLOCK_IRON_XYLOPHONE;
case COW_BELL:
return Sound.BLOCK_NOTE_BLOCK_COW_BELL;
case DIDGERIDOO:
return Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO;
case BIT:
return Sound.BLOCK_NOTE_BLOCK_BIT;
case BANJO:
return Sound.BLOCK_NOTE_BLOCK_BANJO;
case PLING:
return Sound.BLOCK_NOTE_BLOCK_PLING;
}
return null;
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("location")) {
return location;
}
else if (name.equals("instrument")) {
return new ElementTag(event.getInstrument().name());
}
else if (name.equals("sound")) {
return new ElementTag(getSound().name());
}
else if (name.equals("tone")) {
return new ElementTag(event.getNote().getTone().name());
}
else if (name.equals("octave")) {
return new ElementTag(event.getNote().getOctave());
}
else if (name.equals("sharp")) {
return new ElementTag(event.getNote().isSharped());
}
else if (name.equals("pitch")) {
double pitch = Math.pow(2.0, (double)(event.getNote().getId() - 12) / 12.0); // based on minecraft source
return new ElementTag(pitch);
}
return super.getContext(name);
}
@EventHandler
public void onNotePlay(NotePlayEvent event) {
location = new LocationTag(event.getBlock().getLocation());
this.event = event;
fire(event);
}
}