From 0079cf3a59de99361ad42a6632d1e84ff41eba7b Mon Sep 17 00:00:00 2001 From: Chayapak Supasakul Date: Sat, 19 Oct 2024 18:43:49 +0700 Subject: [PATCH] Update to 1.21(.1) --- .../songplayer/conversion/TXTConverter.java | 87 ----------------- .../songplayer/conversion/TxtConverter.java | 93 ++++++++++++++----- .../mixin/ClientConnectionMixin.java | 47 ---------- .../songplayer/playing/SongHandler.java | 2 +- .../hhhzzzsss/songplayer/playing/Stage.java | 2 + .../songplayer/song/SongLoaderThread.java | 2 +- src/main/resources/songplayer.mixins.json | 1 - 7 files changed, 74 insertions(+), 160 deletions(-) delete mode 100644 src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java delete mode 100644 src/main/java/com/github/hhhzzzsss/songplayer/mixin/ClientConnectionMixin.java diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java deleted file mode 100644 index 707ed22..0000000 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TXTConverter.java +++ /dev/null @@ -1,87 +0,0 @@ -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; - -public class TXTConverter { - public static Instrument[] instrumentIndex = new Instrument[] { - Instrument.HARP, - Instrument.BASS, - Instrument.BASEDRUM, - Instrument.SNARE, - Instrument.HAT, - Instrument.GUITAR, - Instrument.FLUTE, - Instrument.BELL, - Instrument.CHIME, - Instrument.XYLOPHONE, - Instrument.IRON_XYLOPHONE, - Instrument.COW_BELL, - Instrument.DIDGERIDOO, - Instrument.BIT, - Instrument.BANJO, - Instrument.PLING, - }; - - public static Song getSongFromBytes (byte[] bytes, String fileName) { - return getSong(new String(bytes), fileName); - } - - public static Song getSong (String data, String fileName) { - if (!data.contains(":")) return null; - - int length = 0; - - final Song song = new Song(fileName); - - for (String line : data.split("\r\n|\r|\n")) { - if (line.isBlank()) continue; - - if (line.startsWith("title:")) { - song.name = line.substring("title:".length()); - continue; - } - - final String[] split = line.split(":"); - - final int tick = Integer.parseInt(split[0]); - int pitch = (int) Float.parseFloat(split[1]) + 33 + SongHandler.getInstance().pitch; - final String instrument = split[2]; - - int intInstrument = -1; - try { - intInstrument = Integer.parseInt(instrument); - } catch (NumberFormatException ignored) {} - - if (intInstrument == -1) continue; - - if (intInstrument > instrumentIndex.length - 1) continue; - - final Instrument instrumentClass = instrumentIndex[intInstrument]; - - if (instrumentClass == null) continue; - - final int time = tick * 50; - - length = Math.max(length, time); - - while (pitch < 33) pitch += 12; - while (pitch > 57) pitch -= 12; - - pitch -= 33; - - song.add( - new Note( - pitch + (instrumentClass.instrumentId * 25), - time - ) - ); - } - - song.length = song.get(song.size() - 1).time + 50; - - return song; - } -} diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TxtConverter.java b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TxtConverter.java index 2257e67..233dced 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TxtConverter.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/conversion/TxtConverter.java @@ -1,39 +1,86 @@ 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; -import java.io.IOException; -import java.nio.charset.StandardCharsets; - public class TxtConverter { - public static Song getSongFromBytes(byte[] bytes, String fileName) throws IOException { - Song song = new Song(fileName); - String strContent = new String(bytes, StandardCharsets.UTF_8); + public static Instrument[] instrumentIndex = new Instrument[] { + Instrument.HARP, + Instrument.BASS, + Instrument.BASEDRUM, + Instrument.SNARE, + Instrument.HAT, + Instrument.GUITAR, + Instrument.FLUTE, + Instrument.BELL, + Instrument.CHIME, + Instrument.XYLOPHONE, + Instrument.IRON_XYLOPHONE, + Instrument.COW_BELL, + Instrument.DIDGERIDOO, + Instrument.BIT, + Instrument.BANJO, + Instrument.PLING, + }; - String[] lines = strContent.split("\\r?\\n"); - for (int lineNum = 1; lineNum <= lines.length; lineNum++) { - String line = lines[lineNum-1].strip(); + public static Song getSongFromBytes (byte[] bytes, String fileName) { + return getSong(new String(bytes), fileName); + } - if (line.startsWith("#")) continue; + public static Song getSong (String data, String fileName) { + if (!data.contains(":")) return null; - String[] split = line.split(":"); - if (split.length != 3) throw new IOException("Invalid format at line " + lineNum); - int tick, pitch, instrument; - try { - tick = Integer.parseInt(split[0]); - pitch = Integer.parseInt(split[1]); - instrument = Integer.parseInt(split[2]); - } catch (NumberFormatException e) { - throw new IOException("Invalid format at line " + lineNum); + int length = 0; + + final Song song = new Song(fileName); + + for (String line : data.split("\r\n|\r|\n")) { + if (line.isBlank()) continue; + + if (line.startsWith("title:")) { + song.name = line.substring("title:".length()); + continue; } - int noteId = pitch + instrument*25; - song.add(new Note(noteId, tick*50)); - song.length = song.get(song.size()-1).time + 50; + final String[] split = line.split(":"); + + final int tick = Integer.parseInt(split[0]); + int pitch = (int) Float.parseFloat(split[1]) + 33 + SongHandler.getInstance().pitch; + final String instrument = split[2]; + + int intInstrument = -1; + try { + intInstrument = Integer.parseInt(instrument); + } catch (NumberFormatException ignored) {} + + if (intInstrument == -1) continue; + + if (intInstrument > instrumentIndex.length - 1) continue; + + final Instrument instrumentClass = instrumentIndex[intInstrument]; + + if (instrumentClass == null) continue; + + final int time = tick * 50; + + length = Math.max(length, time); + + while (pitch < 33) pitch += 12; + while (pitch > 57) pitch -= 12; + + pitch -= 33; + + song.add( + new Note( + pitch + (instrumentClass.instrumentId * 25), + time + ) + ); } - song.sort(); + song.length = song.get(song.size() - 1).time + 50; return song; } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ClientConnectionMixin.java b/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ClientConnectionMixin.java deleted file mode 100644 index f7a969a..0000000 --- a/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ClientConnectionMixin.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.github.hhhzzzsss.songplayer.mixin; - -import com.github.hhhzzzsss.songplayer.Config; -import com.github.hhhzzzsss.songplayer.SongPlayer; -import com.github.hhhzzzsss.songplayer.playing.SongHandler; -import com.github.hhhzzzsss.songplayer.playing.Stage; -import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.EntityPose; -import net.minecraft.network.ClientConnection; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket; -import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(ClientConnection.class) -public class ClientConnectionMixin { - @Inject(at = @At("HEAD"), method = "send(Lnet/minecraft/network/packet/Packet;)V", cancellable = true) - private void sendPacket(Packet packet, CallbackInfo ci) { - Stage stage = SongHandler.getInstance().stage; - - if (stage != null && packet instanceof PlayerMoveC2SPacket) { - if (!Config.getConfig().rotate) { - MinecraftClient.getInstance().getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.Full(stage.position.getX() + 0.5, stage.position.getY(), stage.position.getZ() + 0.5, SongPlayer.MC.player.getYaw(), SongPlayer.MC.player.getPitch(), true)); - if (SongPlayer.fakePlayer != null) { - SongPlayer.fakePlayer.copyStagePosAndPlayerLook(); - } - } - ci.cancel(); - } - else if (packet instanceof ClientCommandC2SPacket) { - ClientCommandC2SPacket.Mode mode = ((ClientCommandC2SPacket) packet).getMode(); - if (SongPlayer.fakePlayer != null) { - if (mode == ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY) { - SongPlayer.fakePlayer.setSneaking(true); - SongPlayer.fakePlayer.setPose(EntityPose.CROUCHING); - } - else if (mode == ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY) { - SongPlayer.fakePlayer.setSneaking(false); - SongPlayer.fakePlayer.setPose(EntityPose.STANDING); - } - } - } - } -} 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 cff80f1..2250678 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/SongHandler.java @@ -713,7 +713,7 @@ public class SongHandler { private void sendGamemodeCommand(String command) { cachedCommand = command; } - private void sendMessage(String message) { + public void sendMessage(String message) { cachedMessage = message; } private void checkCommandCache() { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java b/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java index e134146..caa61c8 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/playing/Stage.java @@ -46,6 +46,8 @@ public class Stage { } public void sendMovementPacketToStagePosition() { + if (SongPlayer.MC.getNetworkHandler() == null) return; + if (SongPlayer.fakePlayer != null) { SongPlayer.MC.getNetworkHandler().sendPacket(new PlayerMoveC2SPacket.Full( position.getX() + 0.5, position.getY(), position.getZ() + 0.5, 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 7717d84..b07492f 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/song/SongLoaderThread.java @@ -56,7 +56,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 { diff --git a/src/main/resources/songplayer.mixins.json b/src/main/resources/songplayer.mixins.json index a79454d..23d100f 100644 --- a/src/main/resources/songplayer.mixins.json +++ b/src/main/resources/songplayer.mixins.json @@ -12,7 +12,6 @@ "ClientPlayerInteractionManagerAccessor", "ClientPlayNetworkHandlerAccessor", "ClientPlayNetworkHandlerMixin", - "ClientConnectionMixin", "InGameHudMixin", "MinecraftClientMixin" ],