Skip to content

Commit

Permalink
enter/exit vehicle event: fix entity objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 14, 2022
1 parent c248731 commit 79adb59
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
Expand Up @@ -88,10 +88,10 @@ public ScriptEntryData getScriptEntryData() {
@Override
public ObjectTag getContext(String name) {
if (name.equals("vehicle")) {
return vehicle;
return vehicle.getDenizenObject();
}
else if (name.equals("entity")) {
return entity;
return entity.getDenizenObject();
}
return super.getContext(name);
}
Expand Down
Expand Up @@ -72,10 +72,10 @@ public ScriptEntryData getScriptEntryData() {
@Override
public ObjectTag getContext(String name) {
if (name.equals("vehicle")) {
return vehicle;
return vehicle.getDenizenObject();
}
else if (name.equals("entity")) {
return entity;
return entity.getDenizenObject();
}
return super.getContext(name);
}
Expand Down
Expand Up @@ -39,6 +39,10 @@ public class ProjectileHitsBlockScriptEvent extends BukkitScriptEvent implements
// <context.location> returns a LocationTag of the block that was hit.
// <context.hit_face> returns a LocationTag vector of the hit normal (like '0,1,0' if the projectile hit the top of the block).
//
// @Player when the entity that shot the projectile is a player.
//
// @NPC when the entity that shot the projectile is an NPC.
//
// -->

// <--[event]
Expand Down Expand Up @@ -66,6 +70,10 @@ public class ProjectileHitsBlockScriptEvent extends BukkitScriptEvent implements
// <context.location> returns the LocationTag of the block that was hit.
// <context.hit_face> returns a LocationTag vector of the hit normal (like '0,1,0' if the projectile hit the top of the block).
//
// @Player when the entity that shot the projectile is a player.
//
// @NPC when the entity that shot the projectile is an NPC.
//
// -->
public ProjectileHitsBlockScriptEvent() {
instance = this;
Expand Down
Expand Up @@ -83,60 +83,53 @@ public void send(MidiMessage m, long time) {
}
}

// Note that this may run async
public void playNote(ShortMessage message) {
// if this isn't a NOTE_ON message, we can't play it
if (ShortMessage.NOTE_ON != message.getCommand()) {
return;
}

int channel = message.getChannel();

// If this is a percussion channel, return
if (channel == 9) {
return;
}

if (channelPatches == null) {
Debug.echoError("Trying to play notes on closed midi NoteBlockReceiver!");
return;
}

// get the correct instrument
Integer patch = channelPatches.get(channel);

// get pitch and volume from the midi message
float pitch = (float) ToneUtil.midiToPitch(message);
float volume = VOLUME_RANGE * (message.getData2() / 127.0f);

SoundHelper soundHelper = NMSHandler.getSoundHelper();
Sound instrument = soundHelper.getDefaultMidiInstrument();
if (patch != null) {
instrument = soundHelper.getMidiInstrumentFromPatch(patch);
}

if (location != null) {
location.getWorld().playSound(location, instrument, volume, pitch);
}
else if (entities != null && !entities.isEmpty()) {
for (int i = 0; i < entities.size(); i++) {
EntityTag entity = entities.get(i);
if (entity.isSpawned()) {
if (entity.isPlayer()) {
NMSHandler.getSoundHelper().playSound(entity.getPlayer(), entity.getLocation(), instrument, volume, pitch, "RECORDS");
Sound instrument = patch == null ? soundHelper.getDefaultMidiInstrument() : soundHelper.getMidiInstrumentFromPatch(patch);
Runnable actualPlay = () -> {
if (location != null) {
location.getWorld().playSound(location, instrument, volume, pitch);
}
else if (entities != null && !entities.isEmpty()) {
for (int i = 0; i < entities.size(); i++) {
EntityTag entity = entities.get(i);
if (entity.isSpawned()) {
if (entity.isPlayer()) {
NMSHandler.getSoundHelper().playSound(entity.getPlayer(), entity.getLocation(), instrument, volume, pitch, "RECORDS");
}
else {
NMSHandler.getSoundHelper().playSound(null, entity.getLocation(), instrument, volume, pitch, "RECORDS");
}
}
else {
NMSHandler.getSoundHelper().playSound(null, entity.getLocation(), instrument, volume, pitch, "RECORDS");
entities.remove(i);
i--;
}
}
else {
entities.remove(i);
i--;
}
}
else {
this.close();
}
};
if (Bukkit.isPrimaryThread()) {
actualPlay.run();
}
else {
this.close();
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), actualPlay);
}
}

Expand Down

0 comments on commit 79adb59

Please sign in to comment.