mirror of
				https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
				synced 2025-10-26 12:59:19 +01:00 
			
		
		
		
	new: guild music player and its manager
This commit is contained in:
		
							parent
							
								
									32e78605d4
								
							
						
					
					
						commit
						e76d236278
					
				
					 2 changed files with 103 additions and 0 deletions
				
			
		|  | @ -0,0 +1,57 @@ | |||
| package codes.ztereohype.ztereomusic.audio; | ||||
| 
 | ||||
| import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; | ||||
| import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; | ||||
| import com.sedmelluq.discord.lavaplayer.track.AudioTrack; | ||||
| import lombok.Getter; | ||||
| import net.dv8tion.jda.api.entities.MessageChannel; | ||||
| 
 | ||||
| public class GuildMusicPlayer { | ||||
|     private final @Getter AudioPlayer player; | ||||
|     private final TrackManager trackManager; | ||||
|     private final MessageChannel infoChannel; | ||||
| 
 | ||||
|     public GuildMusicPlayer(AudioPlayerManager playerManager, MessageChannel infoChannel) { | ||||
|         player = playerManager.createPlayer(); | ||||
|         trackManager = new TrackManager(player, infoChannel); | ||||
|         player.addListener(trackManager); | ||||
|         this.infoChannel = infoChannel; | ||||
|     } | ||||
| 
 | ||||
|     public AudioPlayerSendHandler getAudioSendHandler() { | ||||
|         return new AudioPlayerSendHandler(this.player); | ||||
|     } | ||||
| 
 | ||||
|     public void queue(AudioTrack track) { | ||||
|         // change this to add to queue and call onTrackEnd! | ||||
|         if (player.getPlayingTrack() == null) { | ||||
|             player.playTrack(track); | ||||
|         } else { | ||||
|             trackManager.queue(track); | ||||
|             infoChannel.sendMessage("Queued " + track.getInfo().title).queue(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public void clearQueue() { | ||||
|         trackManager.trackQueue.clear(); | ||||
|     } | ||||
| 
 | ||||
|     public void pause() { | ||||
|         infoChannel.sendMessage("Pausing...").queue(); | ||||
|         player.setPaused(true); | ||||
|     } | ||||
| 
 | ||||
|     public void resume() { | ||||
|         infoChannel.sendMessage("Resuming...").queue(); | ||||
|         player.setPaused(false); | ||||
|     } | ||||
| 
 | ||||
|     public void skip() { | ||||
|         infoChannel.sendMessage("Skipping...").queue(); | ||||
|         trackManager.playNext(); | ||||
|     } | ||||
| 
 | ||||
|     public void stop() { | ||||
|         player.stopTrack(); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,46 @@ | |||
| package codes.ztereohype.ztereomusic.audio; | ||||
| 
 | ||||
| import codes.ztereohype.ztereomusic.Bot; | ||||
| import net.dv8tion.jda.api.entities.Guild; | ||||
| import net.dv8tion.jda.api.entities.MessageChannel; | ||||
| import net.dv8tion.jda.api.entities.VoiceChannel; | ||||
| 
 | ||||
| import java.util.Objects; | ||||
| 
 | ||||
| public class GuildMusicPlayers { | ||||
|     public static GuildMusicPlayer getGuildAudioPlayer(Guild guild, MessageChannel infoChannel, VoiceChannel connectedChannel, VoiceChannel requestedChannel) { | ||||
|         long guildId = guild.getIdLong(); | ||||
|         boolean isInSameVC = Objects.equals(connectedChannel, requestedChannel); | ||||
| 
 | ||||
|         // If I get called in a different vc I delete the old manager | ||||
|         if (!isInSameVC) { | ||||
|             Bot.guildMusicPlayerMap.remove(guildId); | ||||
|         } | ||||
| 
 | ||||
|         GuildMusicPlayer musicPlayer = Bot.guildMusicPlayerMap.get(guildId); | ||||
| 
 | ||||
|         if (musicPlayer == null) { | ||||
|             musicPlayer = new GuildMusicPlayer(Bot.playerManager, infoChannel); | ||||
|             Bot.guildMusicPlayerMap.put(guildId, musicPlayer); | ||||
|             guild.getAudioManager().openAudioConnection(requestedChannel); | ||||
|         } | ||||
| 
 | ||||
|         guild.getAudioManager().setSendingHandler(musicPlayer.getAudioSendHandler()); | ||||
| 
 | ||||
|         return musicPlayer; | ||||
|     } | ||||
| 
 | ||||
|     // stops player, disconnects from vc, and deletes the wrapper | ||||
|     public static void removeGuildAudioPlayer(Guild guild) { | ||||
|         long guildId = guild.getIdLong(); | ||||
|         GuildMusicPlayer musicPlayer = Bot.guildMusicPlayerMap.get(guildId); | ||||
| 
 | ||||
|         if (musicPlayer == null) return; | ||||
| 
 | ||||
|         musicPlayer.stop(); | ||||
|         guild.getAudioManager().closeAudioConnection(); | ||||
| 
 | ||||
|         //todo: remove this when will be part of disconnection listener | ||||
|         Bot.guildMusicPlayerMap.remove(guildId); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue