Skip to content

Commit

Permalink
Auto stop NBS songs when they are complete
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Nov 6, 2023
1 parent 814a292 commit 4fae6af
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.kyrptonaught.serverutils.Module;
import net.kyrptonaught.serverutils.ServerUtilsMod;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.level.storage.LevelStorage;
import nota.Nota;
import nota.event.SongEndEvent;
import nota.model.RepeatMode;
import nota.model.Song;
import nota.player.EntitySongPlayer;
Expand All @@ -38,6 +42,14 @@ public class NoteblockMusicMod extends Module {

private static final HashMap<String, SongPlayer> songPlayers = new HashMap<>();

@Override
public void onInitialize() {
SongEndEvent.EVENT.register(songPlayer -> {
String song = songPlayer.getId().getPath();
songPlayers.remove(song).destroy();
});
}

@Override
public void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("playnbs")
Expand Down Expand Up @@ -124,6 +136,7 @@ private static void primeSongPlayer(SongPlayer songPlayer, CommandContext<Server
songPlayer.setEnable10Octave(true);
songPlayer.setAutoDestroy(true);
songPlayer.setRepeatMode(looping ? RepeatMode.ALL : RepeatMode.NONE);
songPlayer.setId(new Identifier(ServerUtilsMod.MOD_ID, songFile));
players.forEach(songPlayer::addPlayer);

if (songPlayers.containsKey(songFile)) songPlayers.remove(songFile).destroy();
Expand Down

0 comments on commit 4fae6af

Please sign in to comment.