diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionAccessor.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionAccessor.java new file mode 100644 index 0000000..bc030c5 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionAccessor.java @@ -0,0 +1,12 @@ +package land.chipmunk.chipmunkmod.mixin; + +import net.minecraft.network.ClientConnection; +import net.minecraft.network.listener.PacketListener; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(ClientConnection.class) +public interface ClientConnectionAccessor { + @Accessor("packetListener") + PacketListener packetListener (); +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionInvoker.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionInvoker.java new file mode 100644 index 0000000..a9800cb --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientConnectionInvoker.java @@ -0,0 +1,15 @@ +package land.chipmunk.chipmunkmod.mixin; + +import net.minecraft.network.ClientConnection; +import net.minecraft.network.listener.PacketListener; +import net.minecraft.network.packet.Packet; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(ClientConnection.class) +public interface ClientConnectionInvoker { + @Invoker("handlePacket") + static void handlePacket (Packet packet, PacketListener listener) { + throw new AssertionError(); + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java index 8e6e09f..acb74c0 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerAccessor.java @@ -1,6 +1,7 @@ package land.chipmunk.chipmunkmod.mixin; import net.minecraft.client.network.PlayerListEntry; +import net.minecraft.network.ClientConnection; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import net.minecraft.text.Text; @@ -14,6 +15,9 @@ public interface ClientPlayNetworkHandlerAccessor { @Accessor("CHAT_VALIDATION_FAILED_TEXT") static Text chatValidationFailedText () { throw new AssertionError(); } + @Accessor("connection") + ClientConnection connection(); + @Accessor("playerListEntries") Map playerListEntries(); diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java b/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java index be7cade..331edb4 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/SongPlayer.java @@ -1,5 +1,8 @@ package land.chipmunk.chipmunkmod.modules; +import land.chipmunk.chipmunkmod.mixin.ClientConnectionAccessor; +import land.chipmunk.chipmunkmod.mixin.ClientConnectionInvoker; +import land.chipmunk.chipmunkmod.mixin.ClientPlayNetworkHandlerAccessor; import land.chipmunk.chipmunkmod.song.Note; import land.chipmunk.chipmunkmod.song.Song; import land.chipmunk.chipmunkmod.song.SongLoaderException; @@ -13,15 +16,17 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket; +import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvent; import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import net.minecraft.util.math.random.Random; import java.io.File; import java.net.URL; import java.nio.file.Path; -import java.util.Arrays; import java.util.LinkedList; import java.util.Timer; import java.util.TimerTask; @@ -141,7 +146,7 @@ public class SongPlayer { } }; - playTimer.schedule(playTask, 50, 50); + playTimer.schedule(playTask, 60, 50); if (currentSong != null) currentSong.play(); } @@ -211,15 +216,31 @@ public class SongPlayer { final float floatingPitch = MathUtilities.clamp((float) (0.5 * (Math.pow(2, ((note.pitch + (pitch / 10)) / 12)))), 0F, 2F); try { - if (!useCore) { - if (floatingPitch < 0 || floatingPitch > 2) return; - + if (!useCore && client.player != null) { final String[] thing = note.instrument.sound.split(":"); if (thing[1] == null) return; // idk if this can be null but ill just protect it for now i guess - client.player.playSound(SoundEvent.of(Identifier.of(thing[0], thing[1])), SoundCategory.RECORDS, note.volume, floatingPitch); - } else CommandCore.INSTANCE.run("execute as " + SELECTOR + " at @s run playsound " + note.instrument.sound + " record @s ~ ~ ~ " + note.volume + " " + floatingPitch); + final ClientPlayNetworkHandlerAccessor networkHandlerAccessor = (ClientPlayNetworkHandlerAccessor) client.getNetworkHandler(); + + final ClientConnectionAccessor clientConnectionAccessor = (ClientConnectionAccessor) networkHandlerAccessor.connection(); + + ClientConnectionInvoker.handlePacket( + new PlaySoundS2CPacket( + RegistryEntry.of(SoundEvent.of(Identifier.of(thing[0], thing[1]))), + SoundCategory.RECORDS, + client.player.getX(), + client.player.getY(), + client.player.getZ(), + note.volume, + floatingPitch, + Random.create().nextLong() + ), + clientConnectionAccessor.packetListener() + ); + } else { + CommandCore.INSTANCE.run("execute as " + SELECTOR + " at @s run playsound " + note.instrument.sound + " record @s ~ ~ ~ " + note.volume + " " + floatingPitch); + } } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index 1e466e6..16cfcb5 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -21,7 +21,9 @@ "ElderGuardianAppearanceParticleMixin", "IdentifierMixin", "DecoratedPotBlockEntitySherdsMixin", - "TextMixin" + "TextMixin", + "ClientConnectionInvoker", + "ClientConnectionAccessor" ], "injectors": { "defaultRequire": 1