From d3feb98040cd9cd2af5502c7f83ae86bbb2a3631 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 27 Aug 2023 19:30:48 +0700 Subject: [PATCH] add all stuff i have made too lazy to list them lol --- .../songplayer/CommandProcessor.java | 68 +++++++++++-------- .../github/hhhzzzsss/songplayer/Config.java | 1 - .../hhhzzzsss/songplayer/SongPlayer.java | 13 +++- .../songplayer/conversion/MidiConverter.java | 5 +- .../songplayer/conversion/NBSConverter.java | 14 ++-- .../songplayer/playing/SongHandler.java | 53 ++++++++------- .../songplayer/song/DownloadUtils.java | 11 +-- .../songplayer/song/SongLoaderThread.java | 2 +- 8 files changed, 90 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java index 953b483..f462de7 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java @@ -53,6 +53,7 @@ public class CommandProcessor { commands.add(new announcementCommand()); commands.add(new songItemCommand()); commands.add(new testSongCommand()); + commands.add(new pitchCommand()); for (Command command : commands) { commandMap.put(command.getName().toLowerCase(Locale.ROOT), command); @@ -129,7 +130,7 @@ public class CommandProcessor { if (args.length() == 0) { StringBuilder helpMessage = new StringBuilder("§6Commands -"); for (Command c : commands) { - helpMessage.append(" " + Config.getConfig().prefix + c.getName()); + helpMessage.append(" ").append(Config.getConfig().prefix).append(c.getName()); } SongPlayer.addChatMessage(helpMessage.toString()); } @@ -239,7 +240,7 @@ public class CommandProcessor { SongHandler.getInstance().stage.movePlayerToStagePosition(); } SongHandler.getInstance().restoreStateAndCleanUp(); - SongPlayer.addChatMessage("§6Stopped playing"); + SongPlayer.addChatMessage("§6Stopped playing", true); return true; } else { @@ -293,7 +294,7 @@ public class CommandProcessor { try { long time = Util.parseTime(args); SongHandler.getInstance().currentSong.setTime(time); - SongPlayer.addChatMessage("§6Set song time to §3" + Util.formatTime(time)); + SongPlayer.addChatMessage("§6Set song time to §3" + Util.formatTime(time), true); return true; } catch (IOException e) { SongPlayer.addChatMessage("§cNot a valid time stamp"); @@ -325,10 +326,10 @@ public class CommandProcessor { SongHandler.getInstance().currentSong.looping = !SongHandler.getInstance().currentSong.looping; SongHandler.getInstance().currentSong.loopCount = 0; if (SongHandler.getInstance().currentSong.looping) { - SongPlayer.addChatMessage("§6Enabled looping"); + SongPlayer.addChatMessage("§6Enabled looping", true); } else { - SongPlayer.addChatMessage("§6Disabled looping"); + SongPlayer.addChatMessage("§6Disabled looping", true); } return true; } @@ -432,8 +433,8 @@ public class CommandProcessor { } } - List subdirectories = null; - List songs = null; + List subdirectories; + List songs; try { subdirectories = Files.list(dir) .filter(Files::isDirectory) @@ -592,9 +593,9 @@ public class CommandProcessor { Config.getConfig().loopPlaylists = !Config.getConfig().loopPlaylists; SongHandler.getInstance().setPlaylistLoop(Config.getConfig().loopPlaylists); if (Config.getConfig().loopPlaylists) { - SongPlayer.addChatMessage("§6Enabled playlist looping"); + SongPlayer.addChatMessage("§6Enabled playlist looping", true); } else { - SongPlayer.addChatMessage("§6Disabled playlist looping"); + SongPlayer.addChatMessage("§6Disabled playlist looping", true); } Config.saveConfigWithErrorHandling(); return true; @@ -604,9 +605,9 @@ public class CommandProcessor { Config.getConfig().shufflePlaylists = !Config.getConfig().shufflePlaylists; SongHandler.getInstance().setPlaylistShuffle(Config.getConfig().shufflePlaylists); if (Config.getConfig().shufflePlaylists) { - SongPlayer.addChatMessage("§6Enabled playlist shuffling"); + SongPlayer.addChatMessage("§6Enabled playlist shuffling", true); } else { - SongPlayer.addChatMessage("§6Disabled playlist shuffling"); + SongPlayer.addChatMessage("§6Disabled playlist shuffling", true); } Config.saveConfigWithErrorHandling(); return true; @@ -685,7 +686,7 @@ public class CommandProcessor { if (playlistFiles == null) { return null; } - int max = playlistFiles.collect(Collectors.toList()).size(); + int max = (int) playlistFiles.count(); Stream suggestions = IntStream.range(1, max+1).mapToObj(Integer::toString); return CommandSource.suggestMatching(suggestions, suggestionsBuilder); } @@ -858,7 +859,7 @@ public class CommandProcessor { try { Stage.StageType stageType = Stage.StageType.valueOf(args.toUpperCase(Locale.ROOT)); Config.getConfig().stageType = stageType; - SongPlayer.addChatMessage("§6Set stage type to §3" + stageType.name()); + SongPlayer.addChatMessage("§6Set stage type to §3" + stageType.name(), true); Config.saveConfigWithErrorHandling(); } catch (IllegalArgumentException e) { @@ -936,13 +937,11 @@ public class CommandProcessor { public String[] getSyntax() { return new String[] { "enable", - "disable", - "getMessage", - "setMessage ", + "disable" }; } public String getDescription() { - return "Set an announcement message that is sent when you start playing a song. With setMessage, write [name] where the song name should go."; + return "Set an announcement message that is sent when you start playing a song."; } public boolean processCommand(String args) { String[] split = args.split(" ", 2); @@ -950,23 +949,13 @@ public class CommandProcessor { case "enable": if (split.length != 1) return false; Config.getConfig().doAnnouncement = true; - SongPlayer.addChatMessage("§6Enabled song announcements"); + SongPlayer.addChatMessage("§6Enabled song announcements", true); Config.saveConfigWithErrorHandling(); return true; case "disable": if (split.length != 1) return false; Config.getConfig().doAnnouncement = false; - SongPlayer.addChatMessage("§6Disabled song announcements"); - Config.saveConfigWithErrorHandling(); - return true; - case "getmessage": - if (split.length != 1) return false; - SongPlayer.addChatMessage("§6Current announcement message is §r" + Config.getConfig().announcementMessage); - return true; - case "setmessage": - if (split.length != 2) return false; - Config.getConfig().announcementMessage = split[1]; - SongPlayer.addChatMessage("§6Set announcement message to §r" + split[1]); + SongPlayer.addChatMessage("§6Disabled song announcements", true); Config.saveConfigWithErrorHandling(); return true; default: @@ -975,7 +964,7 @@ public class CommandProcessor { } public CompletableFuture getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) { if (!args.contains(" ")) { - return CommandSource.suggestMatching(new String[]{"enable", "disable", "getMessage", "setMessage"}, suggestionsBuilder); + return CommandSource.suggestMatching(new String[]{"enable", "disable"}, suggestionsBuilder); } else { return null; @@ -1087,6 +1076,25 @@ public class CommandProcessor { } } + private static class pitchCommand extends Command { + public String getName() { return "pitch"; } + public String[] getSyntax() { return new String[] { "" }; } + public String getDescription() { return "Sets the pitch of the song"; } + public boolean processCommand(String args) { + if (args.length() == 0) return false; + + try { + SongHandler.getInstance().pitch = Integer.parseInt(args); + + SongPlayer.addChatMessage("§6Set the pitch to §3" + args, true); + } catch (NumberFormatException e) { + return false; + } + + return true; + } + } + public static CompletableFuture handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) { if (!text.contains(" ")) { List names = commandCompletions diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/Config.java b/src/main/java/com/github/hhhzzzsss/songplayer/Config.java index fab497c..9f58abf 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/Config.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/Config.java @@ -25,7 +25,6 @@ public class Config { public boolean swing = false; public boolean rotate = false; public boolean doAnnouncement = false; - public String announcementMessage = "&6Now playing: &3[name]"; public static Config getConfig() { if (config == null) { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/SongPlayer.java b/src/main/java/com/github/hhhzzzsss/songplayer/SongPlayer.java index 9864171..34d10c1 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/SongPlayer.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/SongPlayer.java @@ -1,5 +1,6 @@ package com.github.hhhzzzsss.songplayer; +import com.github.hhhzzzsss.songplayer.playing.SongHandler; import net.fabricmc.api.ModInitializer; import net.minecraft.block.Block; import net.minecraft.block.Blocks; @@ -35,12 +36,18 @@ public class SongPlayer implements ModInitializer { CommandProcessor.initCommands(); } - public static void addChatMessage(String message) { - MC.player.sendMessage(Text.of(message), false); + public static void addChatMessage(String message) { addChatMessage(message, false); } + public static void addChatMessage(String message, boolean broadcast) { + MC.player.sendMessage(Text.literal(message), false); + + if (broadcast && Config.getConfig().doAnnouncement) SongHandler.getInstance().sendMessage(message.replace('§', '&')); } - public static void addChatMessage(Text text) { + public static void addChatMessage(Text text) { addChatMessage(text, false); } + public static void addChatMessage(Text text, boolean broadcast) { MC.player.sendMessage(text, false); + + if (broadcast && Config.getConfig().doAnnouncement) SongHandler.getInstance().sendMessage(text.getString().replace('§', '&')); // Maybe parse the colors } public static void removeFakePlayer() { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java index f36b74a..cf97b2a 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/MidiConverter.java @@ -1,5 +1,6 @@ package com.github.hhhzzzsss.songplayer.conversion; +import com.github.hhhzzzsss.songplayer.playing.SongHandler; import com.github.hhhzzzsss.songplayer.song.DownloadUtils; import com.github.hhhzzzsss.songplayer.song.Instrument; import com.github.hhhzzzsss.songplayer.song.Note; @@ -25,7 +26,7 @@ public class MidiConverter { public static final int NOTE_OFF = 0x80; public static Song getSongFromUrl(URL url) throws IOException, InvalidMidiDataException, URISyntaxException, NoSuchAlgorithmException, KeyManagementException { - Sequence sequence = MidiSystem.getSequence(DownloadUtils.DownloadToInputStream(url, 5*1024*1024)); + Sequence sequence = MidiSystem.getSequence(DownloadUtils.DownloadToInputStream(url)); return getSong(sequence, Paths.get(url.toURI().getPath()).getFileName().toString()); } @@ -146,7 +147,7 @@ public class MidiConverter { return null; } - int pitch = midiPitch-instrument.offset; + int pitch = (midiPitch-instrument.offset) + SongHandler.getInstance().pitch; int noteId = pitch + instrument.instrumentId*25; long time = microTime / 1000L; diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java index dad97c8..26a1776 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/NBSConverter.java @@ -1,5 +1,6 @@ package com.github.hhhzzzsss.songplayer.conversion; +import com.github.hhhzzzsss.songplayer.playing.SongHandler; import com.github.hhhzzzsss.songplayer.song.Instrument; import com.github.hhhzzzsss.songplayer.song.Note; import com.github.hhhzzzsss.songplayer.song.Song; @@ -147,17 +148,18 @@ public class NBSConverter { continue; } - if (note.key < 33 || note.key > 57) { - continue; - } - byte layerVolume = 100; if (nbsLayers.size() > note.layer) { layerVolume = nbsLayers.get(note.layer).volume; } - int pitch = note.key-33; - int noteId = pitch + instrument.instrumentId*25; + int key = note.key; + + while (key < 33) key += 12; + while (key > 57) key -= 12; + + int pitch = key-33; + int noteId = (pitch + instrument.instrumentId*25) + SongHandler.getInstance().pitch; song.add(new Note(noteId, getMilliTime(note.tick, tempo))); } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java index cbb5d16..ff45981 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java @@ -25,7 +25,9 @@ import net.minecraft.world.GameMode; import java.io.IOException; import java.nio.file.Path; +import java.util.ArrayList; import java.util.LinkedList; +import java.util.List; public class SongHandler { private static SongHandler instance = null; @@ -47,6 +49,8 @@ public class SongHandler { public boolean wasFlying = false; public GameMode originalGamemode = GameMode.CREATIVE; + public int pitch = 0; + boolean playlistChecked = false; public void onUpdate(boolean tick) { @@ -81,7 +85,7 @@ public class SongHandler { // Check if loader thread is finished and handle accordingly if (loaderThread != null && !loaderThread.isAlive()) { if (loaderThread.exception != null) { - SongPlayer.addChatMessage("§cFailed to load song: §4" + loaderThread.exception.getMessage()); + SongPlayer.addChatMessage("§cFailed to load song: §4" + loaderThread.exception.getMessage(), true); } else { if (currentSong == null) { setSong(loaderThread.song); @@ -144,10 +148,10 @@ public class SongHandler { else { try { loaderThread = new SongLoaderThread(location); - SongPlayer.addChatMessage("§6Loading §3" + location); + SongPlayer.addChatMessage("§6Loading §3" + location, true); loaderThread.start(); } catch (IOException e) { - SongPlayer.addChatMessage("§cFailed to load song: §4" + e.getMessage()); + SongPlayer.addChatMessage("§cFailed to load song: §4" + e.getMessage(), true); } } } @@ -168,9 +172,6 @@ public class SongHandler { currentSong = song; building = true; setCreativeIfNeeded(); - if (Config.getConfig().doAnnouncement) { - sendMessage(Config.getConfig().announcementMessage.replaceAll("\\[name\\]", song.name)); - } if (stage == null) { stage = new Stage(); stage.movePlayerToStagePosition(); @@ -179,13 +180,13 @@ public class SongHandler { stage.sendMovementPacketToStagePosition(); } getAndSaveBuildSlot(); - SongPlayer.addChatMessage("§6Building noteblocks"); + SongPlayer.addChatMessage("§6Building noteblocks", true); } private void queueSong(Song song) { songQueue.add(song); - SongPlayer.addChatMessage("§6Added song to queue: §3" + song.name); + SongPlayer.addChatMessage("§6Added song to queue: §3" + song.name, true); } public void setPlaylist(Path playlist) { @@ -270,7 +271,7 @@ public class SongHandler { building = false; setSurvivalIfNeeded(); stage.sendMovementPacketToStagePosition(); - SongPlayer.addChatMessage("§6Now playing §3" + currentSong.name); + SongPlayer.addChatMessage("§6Now playing §3" + currentSong.name, true); } } private void setBuildProgressDisplay() { @@ -321,7 +322,7 @@ public class SongHandler { System.out.println("Missing note: " + Instrument.getInstrumentFromId(instrumentId).name() + ":" + pitch); } getAndSaveBuildSlot(); - SongPlayer.addChatMessage("§6Stage was altered. Rebuilding!"); + SongPlayer.addChatMessage("§6Stage was altered. Rebuilding!", true); return; } } @@ -343,7 +344,7 @@ public class SongHandler { } if (currentSong.finished()) { - SongPlayer.addChatMessage("§6Done playing §3" + currentSong.name); + SongPlayer.addChatMessage("§6Done playing §3" + currentSong.name, true); currentSong = null; } } @@ -411,40 +412,40 @@ public class SongHandler { } private long lastCommandTime = System.currentTimeMillis(); - private String cachedCommand = null; - private String cachedMessage = null; + private List cachedCommands = new ArrayList<>(); + private List cachedMessages = new ArrayList<>(); private void sendGamemodeCommand(String command) { - cachedCommand = command; + cachedCommands.add(command); } - private void sendMessage(String message) { - cachedMessage = message; + public void sendMessage(String message) { + cachedMessages.add(message); } private void checkCommandCache() { long currentTime = System.currentTimeMillis(); - if (currentTime >= lastCommandTime + 1500 && cachedCommand != null) { - SongPlayer.MC.getNetworkHandler().sendCommand(cachedCommand); - cachedCommand = null; + if (currentTime >= lastCommandTime + 1500 && !cachedCommands.isEmpty()) { + SongPlayer.MC.getNetworkHandler().sendCommand(cachedCommands.get(0)); + cachedCommands.remove(0); lastCommandTime = currentTime; } - else if (currentTime >= lastCommandTime + 500 && cachedMessage != null) { - if (cachedMessage.startsWith("/")) { - SongPlayer.MC.getNetworkHandler().sendCommand(cachedMessage.substring(1)); + else if (currentTime >= lastCommandTime + 500 && !cachedMessages.isEmpty()) { + if (cachedMessages.get(0).startsWith("/")) { + SongPlayer.MC.getNetworkHandler().sendCommand(cachedMessages.get(0).substring(1)); } else { - SongPlayer.MC.getNetworkHandler().sendChatMessage(cachedMessage); + SongPlayer.MC.getNetworkHandler().sendChatMessage(cachedMessages.get(0)); } - cachedMessage = null; + cachedMessages.remove(0); lastCommandTime = currentTime; } } private void setCreativeIfNeeded() { - cachedCommand = null; + cachedCommands.clear(); if (SongPlayer.MC.interactionManager.getCurrentGameMode() != GameMode.CREATIVE) { sendGamemodeCommand(Config.getConfig().creativeCommand); } } private void setSurvivalIfNeeded() { - cachedCommand = null; + cachedCommands.clear(); if (SongPlayer.MC.interactionManager.getCurrentGameMode() != GameMode.SURVIVAL) { sendGamemodeCommand(Config.getConfig().survivalCommand); } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/DownloadUtils.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/DownloadUtils.java index 6bc366a..d417f50 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/DownloadUtils.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/DownloadUtils.java @@ -29,7 +29,7 @@ public class DownloadUtils { } } - public static byte[] DownloadToByteArray(URL url, int maxSize) throws IOException, KeyManagementException, NoSuchAlgorithmException { + public static byte[] DownloadToByteArray(URL url) throws IOException, KeyManagementException, NoSuchAlgorithmException { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); SSLContext.setDefault(ctx); @@ -43,13 +43,8 @@ public class DownloadUtils { try { byte[] buf = new byte[1024]; int n; - int tot = 0; while ((n = downloadStream.read(buf)) > 0) { byteArrayStream.write(buf, 0, n); - tot += n; - if (tot > maxSize) { - throw new IOException("File is too large"); - } if (Thread.interrupted()) { return null; } @@ -61,7 +56,7 @@ public class DownloadUtils { } } - public static InputStream DownloadToInputStream(URL url, int maxSize) throws KeyManagementException, NoSuchAlgorithmException, IOException { - return new ByteArrayInputStream(DownloadToByteArray(url, maxSize)); + public static InputStream DownloadToInputStream(URL url) throws KeyManagementException, NoSuchAlgorithmException, IOException { + return new ByteArrayInputStream(DownloadToByteArray(url)); } } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java index cc89583..1294a40 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java @@ -55,7 +55,7 @@ public class SongLoaderThread extends Thread{ try { byte[] bytes; if (isUrl) { - bytes = DownloadUtils.DownloadToByteArray(songUrl, 10*1024*1024); + bytes = DownloadUtils.DownloadToByteArray(songUrl); filename = Paths.get(songUrl.toURI().getPath()).getFileName().toString(); } else {