Skip to content
Permalink
Browse files

Use the jingle note library instead of the bundled jingle note

  • Loading branch information
DarkArc committed Jan 1, 2020
1 parent 8f69ec1 commit b7c6b139954c086c688e1cb447c07393bbd8b854
@@ -74,6 +74,8 @@ allprojects {
compile 'com.destroystokyo.paper:paper-api:1.15.1-R0.1-SNAPSHOT'
compile 'com.sk89q.worldedit:worldedit-core:7.1.0-SNAPSHOT'
compile 'com.sk89q.worldedit:worldedit-bukkit:7.1.0-SNAPSHOT'
compile 'org.enginehub.jinglenote:jinglenote-core:1.0.0-SNAPSHOT'
compile 'org.enginehub.jinglenote:jinglenote-bukkit:1.0.0-SNAPSHOT'
compile 'com.zachsthings.libcomponents:libcomponents-bukkit:1.3.0-SNAPSHOT'
compile 'net.sf.opencsv:opencsv:2.0'

@@ -112,6 +114,10 @@ allprojects {
dependencies {
include(dependency('net.sf.opencsv:opencsv'))
include(dependency('com.zachsthings.libcomponents:libcomponents-bukkit'))
relocate ("org.enginehub.jinglenote", "com.sk89q.commandbook.enginehub.jinglenote") {
include(dependency("org.enginehub.jinglenote:jinglenote-core"))
include(dependency("org.enginehub.jinglenote:jinglenote-bukkit"))
}
}

exclude 'GradleStart**'
@@ -16,13 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.commandbook;
package com.sk89q.commandbook.jinglenote;

import com.google.common.collect.Lists;
import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.util.InputUtil;
import com.sk89q.commandbook.util.entity.player.PlayerUtil;
import com.sk89q.jinglenote.JingleNoteManager;
import com.sk89q.jinglenote.MidiJingleSequencer;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
@@ -36,6 +35,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.enginehub.jinglenote.sequencer.MidiJingleSequencer;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
@@ -80,24 +80,19 @@ public void onJoin(PlayerJoinEvent event) {
File file = new File(CommandBook.inst().getDataFolder(), "intro.mid");
if (file.exists()) {
sequencer = new MidiJingleSequencer(file, false);
getJingleNoteManager().play(event.getPlayer().getName(), sequencer);
getJingleNoteManager().play(event.getPlayer(), sequencer);
}
} catch (MidiUnavailableException e) {
CommandBook.logger().log(Level.WARNING, "Failed to access MIDI: "
+ e.getMessage());
} catch (InvalidMidiDataException e) {
CommandBook.logger().log(Level.WARNING, "Failed to read intro MIDI file: "
+ e.getMessage());
CommandBook.logger().log(Level.WARNING, "Failed to access MIDI: " + e.getMessage());
} catch (FileNotFoundException ignored) {
} catch (IOException e) {
CommandBook.logger().log(Level.WARNING, "Failed to read intro MIDI file: "
+ e.getMessage());
} catch (InvalidMidiDataException | IOException e) {
CommandBook.logger().log(Level.WARNING, "Failed to read intro MIDI file: " + e.getMessage());
}
}

@EventHandler
public void onQuit(PlayerQuitEvent event) {
getJingleNoteManager().stop(event.getPlayer().getName());
getJingleNoteManager().stop(event.getPlayer());
}

public class Commands {
@@ -124,20 +119,15 @@ public void intro(CommandContext args, CommandSender sender) throws CommandExcep
try {
MidiJingleSequencer sequencer = new MidiJingleSequencer(new File(CommandBook.inst().getDataFolder(), "intro.mid"), false);
for (Player player : targets) {
getJingleNoteManager().play(player.getName(), sequencer);
getJingleNoteManager().play(player, sequencer);
player.sendMessage(ChatColor.YELLOW + "Playing intro.midi...");
}
} catch (MidiUnavailableException e) {
throw new CommandException("Failed to access MIDI: "
+ e.getMessage());
} catch (InvalidMidiDataException e) {
throw new CommandException("Failed to read intro MIDI file: "
+ e.getMessage());
throw new CommandException("Failed to access MIDI: " + e.getMessage());
} catch (FileNotFoundException e) {
throw new CommandException("No intro.mid is available.");
} catch (IOException e) {
throw new CommandException("Failed to read intro MIDI file: "
+ e.getMessage());
} catch (InvalidMidiDataException | IOException e) {
throw new CommandException("Failed to read intro MIDI file: " + e.getMessage());
}
}

@@ -161,7 +151,7 @@ public void midi(CommandContext args, CommandSender sender) throws CommandExcept

if (args.argsLength() == 0) {
for (Player target : targets) {
if (getJingleNoteManager().stop(target.getName())) {
if (getJingleNoteManager().stop(target)) {
target.sendMessage(ChatColor.YELLOW + "All music stopped.");
}
}
@@ -201,21 +191,16 @@ public void midi(CommandContext args, CommandSender sender) throws CommandExcept
try {
MidiJingleSequencer sequencer = new MidiJingleSequencer(file, false);
for (Player player : targets) {
getJingleNoteManager().play(player.getName(), sequencer);
getJingleNoteManager().play(player, sequencer);
player.sendMessage(ChatColor.YELLOW + "Playing " + file.getName()
+ "... Use '/midi' to stop.");
}
} catch (MidiUnavailableException e) {
throw new CommandException("Failed to access MIDI: "
+ e.getMessage());
} catch (InvalidMidiDataException e) {
throw new CommandException("Failed to read intro MIDI file: "
+ e.getMessage());
throw new CommandException("Failed to access MIDI: " + e.getMessage());
} catch (FileNotFoundException e) {
throw new CommandException("The specified MIDI file was not found.");
} catch (IOException e) {
throw new CommandException("Failed to read intro MIDI file: "
+ e.getMessage());
} catch (InvalidMidiDataException | IOException e) {
throw new CommandException("Failed to read intro MIDI file: " + e.getMessage());
}
}
}
@@ -17,58 +17,65 @@
*/


package com.sk89q.jinglenote;
package com.sk89q.commandbook.jinglenote;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.sk89q.jinglenote.bukkit.BukkitJingleNotePlayer;
import org.bukkit.entity.Player;
import org.enginehub.jinglenote.JingleNotePlayer;
import org.enginehub.jinglenote.bukkit.BukkitJingleNotePlayer;
import org.enginehub.jinglenote.sequencer.JingleSequencer;

/**
* A manager of play instances.
*
* @author sk89q
*/
public class JingleNoteManager {

/**
* List of instances.
*/
protected final Map<String, JingleNotePlayer> instances = new HashMap<String, JingleNotePlayer>();
protected final Map<UUID, JingleNotePlayer> instances = new HashMap<>();

public void play(String player, JingleSequencer sequencer) {
public void play(Player player, JingleSequencer sequencer) {
UUID playerID = player.getUniqueId();

// Existing player found!
if (instances.containsKey(player)) {
JingleNotePlayer existing = instances.get(player);
if (instances.containsKey(playerID)) {
JingleNotePlayer existing = instances.get(playerID);
existing.stop();
instances.remove(player);
instances.remove(playerID);
}

JingleNotePlayer notePlayer = new BukkitJingleNotePlayer(player, sequencer);
Thread thread = new Thread(notePlayer);
Thread thread = new Thread(notePlayer::play);
thread.setDaemon(true);
thread.setPriority(Thread.MAX_PRIORITY);
thread.setName("JingleNotePlayer for " + player);
thread.setName("JingleNotePlayer for " + playerID);
thread.start();

instances.put(player, notePlayer);
instances.put(playerID, notePlayer);
}

public boolean stop(String player) {

public boolean stop(UUID playerID) {
// Existing player found!
if (instances.containsKey(player)) {
JingleNotePlayer existing = instances.get(player);
if (instances.containsKey(playerID)) {
JingleNotePlayer existing = instances.get(playerID);
existing.stop();
instances.remove(player);
instances.remove(playerID);
return true;
}

return false;
}

public void stopAll() {
public boolean stop(Player player) {
return stop(player.getUniqueId());
}

public void stopAll() {
for (JingleNotePlayer notePlayer : instances.values()) {
notePlayer.stop();
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit b7c6b13

Please sign in to comment.
You can’t perform that action at this time.