-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for new custom nbs player
- Loading branch information
1 parent
97dd58b
commit be02364
Showing
5 changed files
with
34 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 15 additions & 140 deletions
155
src/main/java/net/kyrptonaught/serverutils/noteblockMusic/NoteblockMusicMod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,34 @@ | ||
package net.kyrptonaught.serverutils.noteblockMusic; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.arguments.BoolArgumentType; | ||
import com.mojang.brigadier.arguments.IntegerArgumentType; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import net.kyrptonaught.noteblockplayer.Commands; | ||
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.event.SongEndEvent; | ||
import nota.model.RepeatMode; | ||
import nota.model.Song; | ||
import nota.player.EntitySongPlayer; | ||
import nota.player.PositionSongPlayer; | ||
import nota.player.RadioSongPlayer; | ||
import nota.player.SongPlayer; | ||
import nota.utils.NBSDecoder; | ||
import xyz.nucleoid.fantasy.mixin.MinecraftServerAccess; | ||
import net.raphimc.noteblocklib.format.nbs.NbsSong; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Stream; | ||
|
||
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") | ||
.requires((source) -> source.hasPermissionLevel(2)) | ||
.then(CommandManager.argument("song", StringArgumentType.word()) | ||
.suggests(NoteblockMusicMod::getAvailableNBSFiles) | ||
.then(CommandManager.argument("looping", BoolArgumentType.bool()) | ||
.then(CommandManager.argument("listeners", EntityArgumentType.players()) | ||
.then(CommandManager.literal("GLOBAL") | ||
.executes(context -> { | ||
Song song = getSong(context); | ||
RadioSongPlayer songPlayer = new RadioSongPlayer(song); | ||
|
||
primeSongPlayer(songPlayer, context); | ||
songPlayer.setPlaying(true); | ||
return 1; | ||
})) | ||
.then(CommandManager.literal("BLOCKPOS") | ||
.then(CommandManager.argument("blockpos", BlockPosArgumentType.blockPos()) | ||
.then(CommandManager.argument("distance", IntegerArgumentType.integer(1)) | ||
.executes(context -> { | ||
BlockPos blockPos = BlockPosArgumentType.getBlockPos(context, "blockpos"); | ||
int distance = IntegerArgumentType.getInteger(context, "distance"); | ||
|
||
Song song = getSong(context); | ||
PositionSongPlayer songPlayer = new PositionSongPlayer(song, context.getSource().getWorld()); | ||
songPlayer.setBlockPos(blockPos); | ||
songPlayer.setDistance(distance); | ||
|
||
primeSongPlayer(songPlayer, context); | ||
songPlayer.setPlaying(true); | ||
return 1; | ||
})))) | ||
.then(CommandManager.literal("ENTITY") | ||
.then(CommandManager.argument("entity", EntityArgumentType.entity()) | ||
.then(CommandManager.argument("distance", IntegerArgumentType.integer(1)) | ||
.executes(context -> { | ||
Entity entity = EntityArgumentType.getEntity(context, "entity"); | ||
int distance = IntegerArgumentType.getInteger(context, "distance"); | ||
|
||
Song song = getSong(context); | ||
EntitySongPlayer songPlayer = new EntitySongPlayer(song); | ||
songPlayer.setEntity(entity); | ||
songPlayer.setDistance(distance); | ||
|
||
primeSongPlayer(songPlayer, context); | ||
songPlayer.setPlaying(true); | ||
return 1; | ||
})))))))); | ||
dispatcher.register(CommandManager.literal("stopnbs") | ||
.requires((source) -> source.hasPermissionLevel(2)) | ||
.then(CommandManager.literal("ALL") | ||
.executes(context -> { | ||
songPlayers.values().forEach(SongPlayer::destroy); | ||
songPlayers.clear(); | ||
return 1; | ||
})) | ||
.then(CommandManager.argument("song", StringArgumentType.word()) | ||
.suggests((context, builder) -> { | ||
songPlayers.keySet().forEach(builder::suggest); | ||
return builder.buildFuture(); | ||
}) | ||
.executes(context -> { | ||
String songFile = StringArgumentType.getString(context, "song"); | ||
songPlayers.remove(songFile).destroy(); | ||
return 1; | ||
}))); | ||
public static void cacheSong(String id, Path songPath) { | ||
NbsSong song = Commands.loadSong(songPath); | ||
Commands.songCache.put(id, song); | ||
} | ||
|
||
private static Song getSong(CommandContext<ServerCommandSource> context) { | ||
String songFile = StringArgumentType.getString(context, "song"); | ||
ServerWorld world = context.getSource().getWorld(); | ||
LevelStorage.Session session = ((MinecraftServerAccess) context.getSource().getServer()).getSession(); | ||
Path path = session.getWorldDirectory(world.getRegistryKey()).resolve("nbs").resolve(songFile + ".nbs"); | ||
|
||
return NBSDecoder.parse(path.toFile()); | ||
public static void clearCache() { | ||
Commands.songCache.clear(); | ||
} | ||
|
||
private static void primeSongPlayer(SongPlayer songPlayer, CommandContext<ServerCommandSource> context) throws CommandSyntaxException { | ||
Collection<ServerPlayerEntity> players = EntityArgumentType.getPlayers(context, "listeners"); | ||
boolean looping = BoolArgumentType.getBool(context, "looping"); | ||
String songFile = StringArgumentType.getString(context, "song").toLowerCase().replaceAll("[^a-z0-9_.-]", ""); | ||
|
||
songPlayer.setEnable10Octave(true); | ||
songPlayer.setAutoDestroy(true); | ||
songPlayer.setRepeatMode(looping ? RepeatMode.ALL : RepeatMode.NONE); | ||
songPlayer.setId(Identifier.of(ServerUtilsMod.MOD_ID, songFile)); | ||
players.forEach(songPlayer::addPlayer); | ||
|
||
if (songPlayers.containsKey(songFile)) songPlayers.remove(songFile).destroy(); | ||
songPlayers.put(songFile, songPlayer); | ||
} | ||
|
||
private static CompletableFuture<Suggestions> getAvailableNBSFiles(CommandContext<ServerCommandSource> context, final SuggestionsBuilder builder) { | ||
ServerWorld world = context.getSource().getWorld(); | ||
LevelStorage.Session session = ((MinecraftServerAccess) context.getSource().getServer()).getSession(); | ||
Path path = session.getWorldDirectory(world.getRegistryKey()).resolve("nbs"); | ||
|
||
try (Stream<Path> files = Files.walk(path)) { | ||
files.forEach(path1 -> { | ||
if (path1.getFileName().toString().endsWith(".nbs")) | ||
builder.suggest(path1.getFileName().toString().replace(".nbs", "")); | ||
public static void cacheAll(Path input) { | ||
clearCache(); | ||
try (Stream<Path> files = Files.walk(input)) { | ||
files.forEach(path -> { | ||
if (path.getFileName().toString().endsWith(".nbs")) { | ||
String id = path.getFileName().toString().replace(".nbs", ""); | ||
cacheSong(id, path); | ||
} | ||
}); | ||
} catch (Exception ignored) { | ||
} | ||
return builder.buildFuture(); | ||
} | ||
} |