Search in sources :

Example 1 with NoteBlockReceiver

use of com.denizenscript.denizen.utilities.midi.NoteBlockReceiver in project Denizen-For-Bukkit by DenizenScript.

the class MidiCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    boolean cancel = scriptEntry.hasObject("cancel");
    ElementTag filePath = scriptEntry.getElement("file");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    LocationTag location = scriptEntry.getObjectTag("location");
    float tempo = scriptEntry.getElement("tempo").asFloat();
    float volume = scriptEntry.getElement("volume").asFloat();
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), filePath, db("entities", entities), location, db("tempo", tempo), db("volume", volume));
    }
    // Play the midi
    if (!cancel) {
        String fName = scriptEntry.getElement("file").asString();
        if (!fName.endsWith(".mid")) {
            fName += ".mid";
        }
        File file = new File(Denizen.getInstance().getDataFolder(), "/midi/" + fName);
        if (!Utilities.canReadFile(file)) {
            Debug.echoError("Cannot read from that file path due to security settings in Denizen/config.yml.");
            return;
        }
        if (!file.exists()) {
            Debug.echoError(scriptEntry, "Invalid file " + filePath.asString());
            return;
        }
        NoteBlockReceiver rec;
        if (location != null) {
            rec = MidiUtil.playMidi(file, tempo, volume, location);
        } else {
            rec = MidiUtil.playMidi(file, tempo, volume, entities);
        }
        if (rec == null) {
            Debug.echoError(scriptEntry, "Something went wrong playing a midi!");
            scriptEntry.setFinished(true);
        } else {
            rec.onFinish = () -> scriptEntry.setFinished(true);
        }
    } else {
        if (location != null) {
            MidiUtil.stopMidi(location.identify());
        } else {
            MidiUtil.stopMidi(entities);
        }
        scriptEntry.setFinished(true);
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) File(java.io.File) NoteBlockReceiver(com.denizenscript.denizen.utilities.midi.NoteBlockReceiver)

Aggregations

EntityTag (com.denizenscript.denizen.objects.EntityTag)1 LocationTag (com.denizenscript.denizen.objects.LocationTag)1 NoteBlockReceiver (com.denizenscript.denizen.utilities.midi.NoteBlockReceiver)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 File (java.io.File)1 List (java.util.List)1