|
| 1 | +package com.xxmicloxx.NoteBlockAPI.songplayer; |
| 2 | + |
| 3 | +import com.xxmicloxx.NoteBlockAPI.NoteBlockAPI; |
| 4 | +import com.xxmicloxx.NoteBlockAPI.event.PlayerRangeStateChangeEvent; |
| 5 | +import com.xxmicloxx.NoteBlockAPI.model.*; |
| 6 | +import com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils; |
| 7 | +import com.xxmicloxx.NoteBlockAPI.utils.InstrumentUtils; |
| 8 | +import org.bukkit.Bukkit; |
| 9 | +import org.bukkit.entity.Entity; |
| 10 | +import org.bukkit.entity.Player; |
| 11 | + |
| 12 | +public class EntitySongPlayer extends RangeSongPlayer { |
| 13 | + |
| 14 | + private Entity entity; |
| 15 | + |
| 16 | + public EntitySongPlayer(Song song) { |
| 17 | + super(song); |
| 18 | + } |
| 19 | + |
| 20 | + public EntitySongPlayer(Song song, SoundCategory soundCategory) { |
| 21 | + super(song, soundCategory); |
| 22 | + } |
| 23 | + |
| 24 | + public EntitySongPlayer(Playlist playlist, SoundCategory soundCategory) { |
| 25 | + super(playlist, soundCategory); |
| 26 | + } |
| 27 | + |
| 28 | + public EntitySongPlayer(Playlist playlist) { |
| 29 | + super(playlist); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns true if the Player is able to hear the current {@link EntitySongPlayer} |
| 34 | + * @param player in range |
| 35 | + * @return ability to hear the current {@link EntitySongPlayer} |
| 36 | + */ |
| 37 | + @Override |
| 38 | + public boolean isInRange(Player player) { |
| 39 | + return player.getLocation().distance(entity.getLocation()) <= getDistance(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Set entity associated with this {@link EntitySongPlayer} |
| 44 | + * @param entity |
| 45 | + */ |
| 46 | + public void setEntity(Entity entity){ |
| 47 | + this.entity = entity; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get {@link Entity} associated with this {@link EntitySongPlayer} |
| 52 | + * @return |
| 53 | + */ |
| 54 | + public Entity getEntity() { |
| 55 | + return entity; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void playTick(Player player, int tick) { |
| 60 | + if (entity.isDead()){ |
| 61 | + if (autoDestroy){ |
| 62 | + destroy(); |
| 63 | + } else { |
| 64 | + setPlaying(false); |
| 65 | + } |
| 66 | + } |
| 67 | + if (!player.getWorld().getName().equals(entity.getWorld().getName())) { |
| 68 | + return; // not in same world |
| 69 | + } |
| 70 | + |
| 71 | + byte playerVolume = NoteBlockAPI.getPlayerVolume(player); |
| 72 | + |
| 73 | + for (Layer layer : song.getLayerHashMap().values()) { |
| 74 | + Note note = layer.getNote(tick); |
| 75 | + if (note == null) continue; |
| 76 | + |
| 77 | + float volume = ((layer.getVolume() * (int) this.volume * (int) playerVolume) / 1000000F) |
| 78 | + * ((1F / 16F) * getDistance()); |
| 79 | + float pitch = NotePitch.getPitch(note.getKey() - 33); |
| 80 | + |
| 81 | + if (InstrumentUtils.isCustomInstrument(note.getInstrument())) { |
| 82 | + CustomInstrument instrument = song.getCustomInstruments() |
| 83 | + [note.getInstrument() - InstrumentUtils.getCustomInstrumentFirstIndex()]; |
| 84 | + |
| 85 | + if (instrument.getSound() != null) { |
| 86 | + CompatibilityUtils.playSound(player, entity.getLocation(), instrument.getSound(), |
| 87 | + this.soundCategory, volume, pitch, false); |
| 88 | + } else { |
| 89 | + CompatibilityUtils.playSound(player, entity.getLocation(), instrument.getSoundFileName(), |
| 90 | + this.soundCategory, volume, pitch, false); |
| 91 | + } |
| 92 | + } else { |
| 93 | + CompatibilityUtils.playSound(player, entity.getLocation(), |
| 94 | + InstrumentUtils.getInstrument(note.getInstrument()), this.soundCategory, |
| 95 | + volume, pitch, false); |
| 96 | + } |
| 97 | + |
| 98 | + if (isInRange(player)) { |
| 99 | + if (!this.playerList.get(player.getUniqueId())) { |
| 100 | + playerList.put(player.getUniqueId(), true); |
| 101 | + Bukkit.getPluginManager().callEvent(new PlayerRangeStateChangeEvent(this, player, true)); |
| 102 | + } |
| 103 | + } else { |
| 104 | + if (this.playerList.get(player.getUniqueId())) { |
| 105 | + playerList.put(player.getUniqueId(), false); |
| 106 | + Bukkit.getPluginManager().callEvent(new PlayerRangeStateChangeEvent(this, player, false)); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments