Skip to content

Commit

Permalink
Slight optimization to Midi.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Sep 26, 2013
1 parent 207cc4d commit 05b4567
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
Expand Up @@ -134,15 +134,18 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
int ticks = 0;
int maxTicks = duration.getTicksAsInt();

// Track how many entities are left to rotate, and cancel
// the task if there are none
int entitiesLeft = entities.size();
// Track entities that are no longer used, to remove them from
// the regular list
Collection<dEntity> unusedEntities = new LinkedList<dEntity>();

@Override
public void run() {

if (infinite || ticks < maxTicks) {
if (entities.isEmpty()) {
this.cancel();
}

else if (infinite || ticks < maxTicks) {
for (dEntity entity : entities) {
if (entity.isSpawned() && rotatingEntities.contains(entity.getUUID())) {
Rotation.rotate(entity.getBukkitEntity(),
Expand All @@ -151,24 +154,19 @@ public void run() {
}
else {
rotatingEntities.remove(entity.getUUID());
entitiesLeft--;
unusedEntities.add(entity);
}
}

if (entitiesLeft == 0) {
this.cancel();
}
else {

// Remove any entities that are no longer spawned
if (unusedEntities.size() > 0) {
for (dEntity unusedEntity : unusedEntities)
entities.remove(unusedEntity);
unusedEntities.clear();
// Remove any entities that are no longer spawned
if (!unusedEntities.isEmpty()) {
for (dEntity unusedEntity : unusedEntities) {
entities.remove(unusedEntity);
}
ticks = (int) (ticks + frequency.getTicks());
unusedEntities.clear();
}

ticks = (int) (ticks + frequency.getTicks());
}
else this.cancel();
}
Expand Down
Expand Up @@ -48,8 +48,8 @@ public static void playMidi(File file, float tempo, List<dEntity> entities)
// If there is already a midi file being played for one of the entities,
// stop playing it
for (dEntity entity : entities) {
stopMidi(entity.identify());
receivers.put(entity.identify(), receiver);
stopMidi(entity.getUUID().toString());
receivers.put(entity.getUUID().toString(), receiver);
}

startSequencer(file, tempo, receiver);
Expand Down Expand Up @@ -80,7 +80,7 @@ public static void stopMidi(String object) {

public static void stopMidi(List<dEntity> entities) {
for (dEntity entity : entities) {
stopMidi(entity.identify());
stopMidi(entity.getUUID().toString());
}
}

Expand Down
Expand Up @@ -5,12 +5,13 @@
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.debugging.dB;

import org.bukkit.Sound;

Expand All @@ -25,20 +26,21 @@ public class NoteBlockReceiver implements Receiver
{
private static final float VOLUME_RANGE = 10.0f;

private List<dEntity> listeners;
private List<dEntity> entities;
private dLocation location;
private final Map<Integer, Integer> channelPatches;
private Collection<dEntity> unusedEntities = new LinkedList<dEntity>();

public NoteBlockReceiver(List<dEntity> entities) throws InvalidMidiDataException, IOException
{
this.listeners = entities;
this.entities = entities;
this.location = null;
this.channelPatches = Maps.newHashMap();
}

public NoteBlockReceiver(dLocation location) throws InvalidMidiDataException, IOException
{
this.listeners = null;
this.entities = null;
this.location = location;
this.channelPatches = Maps.newHashMap();
}
Expand Down Expand Up @@ -92,8 +94,8 @@ public void playNote(ShortMessage message)

location.getWorld().playSound(location, instrument, volume, pitch);
}
else if (listeners != null) {
for (dEntity entity : listeners) {
else if (entities != null && !entities.isEmpty()) {
for (dEntity entity : entities) {
if (entity.isSpawned()) {
if (entity.isPlayer()) {
entity.getPlayer().playSound(entity.getLocation(), instrument, volume, pitch);
Expand All @@ -103,17 +105,25 @@ else if (listeners != null) {
}
}
else {
dB.echoError("Cannot play midi for unspawned entity " + entity);
this.close();
unusedEntities.add(entity);
}
}

// Remove any entities that are no longer spawned
if (!unusedEntities.isEmpty()) {
for (dEntity unusedEntity : unusedEntities) {
entities.remove(unusedEntity);
}
unusedEntities.clear();
}
}
else this.close();
}

@Override
public void close()
{
if (listeners != null) listeners = null;
if (entities != null) entities = null;
if (location != null) location = null;
channelPatches.clear();
}
Expand Down

0 comments on commit 05b4567

Please sign in to comment.