Skip to content

Commit

Permalink
Changes ServerSoundtrackPlayer Management
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Dec 26, 2024
1 parent d16362a commit a659915
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static <T extends NetworkSupport> void register() {
// Client Operations Networking
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.CONFIGS, (client, handler, buf, sender) -> ClientOperations.receiveConfigOnClient(buf));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.STELLAR_STATUS, (client, handler, buf, sender) -> ClientOperations.receiveStellarStatusOnClient(handler, buf));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.SEND_SOUNDTRACKS, (client, handler, buf, sender) -> ClientOperations.receiveSentSoundtrackActivity(client, buf));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.CLEAR_SOUNDTRACKS, (client, handler, buf, sender) -> ClientOperations.receiveClearedSoundtrackActivity(client, buf));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.SEND_SOUNDTRACKS, (client, handler, buf, sender) -> ClientOperations.receiveSentSoundtrack(client, buf));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.SKIP_SOUNDTRACKS, (client, handler, buf, sender) -> ClientOperations.receiveSoundtrackSkip(client));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.SKIP_TO_PART_SOUNDTRACKS, (client, handler, buf, sender) -> ClientOperations.receiveSoundtrackSkipToPart(client, buf));
ClientPlayNetworking.registerGlobalReceiver(MModdingPackets.CLEAR_SOUNDTRACKS, (client, handler, buf, sender) -> ClientOperations.receiveSoundtrackDeletion(client));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mmodding.mmodding_lib.library.soundtracks;

import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;

import java.util.List;
Expand Down Expand Up @@ -30,12 +31,12 @@ public Part getPart(int part) {

public static class Part {

private final Identifier path;
private final SoundEvent sound;
private final boolean looping;
private final int iterations;

private Part(Identifier path, boolean looping, int iterations) {
this.path = path;
this.sound = new SoundEvent(path);
this.looping = looping;
this.iterations = iterations;
}
Expand All @@ -48,8 +49,8 @@ public static Part iterations(Identifier path, int iterations) {
return new Part(path, false, iterations);
}

public Identifier getPath() {
return this.path;
public SoundEvent getSound() {
return this.sound;
}

public boolean isLooping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import com.mmodding.mmodding_lib.library.soundtracks.SoundtrackPlayer;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.sound.EntityTrackingSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.client.util.ClientPlayerTickable;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.loader.api.minecraft.ClientOnly;
Expand All @@ -20,7 +18,7 @@ public class ClientSoundtrackPlayer implements ClientPlayerTickable, SoundtrackP
private final SoundManager soundManager;

@Nullable
private SoundInstance instance = null;
private EntityTrackingSoundInstance instance = null;

@Nullable
private Soundtrack currentSoundtrack = null;
Expand Down Expand Up @@ -62,6 +60,9 @@ public void skip(int part) {
this.currentPart = part;
this.instance = null;
}
else {
this.stop();
}
}
}

Expand All @@ -77,7 +78,7 @@ public void tick() {
if (this.currentSoundtrack != null && this.currentPart != -1) {
if (this.instance == null) {
this.instance = new EntityTrackingSoundInstance(
new SoundEvent(this.currentSoundtrack.getPart(this.currentPart).getPath()),
this.currentSoundtrack.getPart(this.currentPart).getSound(),
SoundCategory.MUSIC,
1.0f,
1.0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,37 @@
import com.mmodding.mmodding_lib.library.soundtracks.SoundtrackPlayer;
import com.mmodding.mmodding_lib.networking.CommonOperations;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;

public class ServerSoundtrackPlayer implements SoundtrackPlayer {

private final ServerPlayerEntity player;

@Nullable
private Soundtrack currentSoundtrack;

private int currentPart;

public ServerSoundtrackPlayer(ServerPlayerEntity player) {
this.player = player;
this.currentSoundtrack = null;
this.currentPart = -1;
}

@Override
public void play(Soundtrack soundtrack, int part) {
this.currentSoundtrack = soundtrack;
this.currentPart = part;
this.send();
CommonOperations.playSoundtrackForPlayer(this.player, soundtrack, part, true);
}

@Override
public void playOnce(Soundtrack soundtrack, int part) {
if (this.currentSoundtrack != soundtrack) {
this.play(soundtrack, part);
}
CommonOperations.playSoundtrackForPlayer(this.player, soundtrack, part, false);
}

@Override
public void skip() {
if (this.currentSoundtrack != null && this.currentPart != -1) {
if (this.currentPart == this.currentSoundtrack.getPartsCount() - 1) {
this.stop();
}
}
CommonOperations.skipSoundtrackPartForPlayer(this.player);
}

@Override
public void skip(int part) {
if (this.currentSoundtrack != null) {
if (part < this.currentSoundtrack.getPartsCount()) {
this.currentPart = part;
this.send();
}
}
CommonOperations.skipToPartForPlayer(this.player, part);
}

@Override
public void stop() {
this.currentSoundtrack = null;
this.currentPart = -1;
this.send();
}

private void send() {
if (this.currentSoundtrack != null && this.currentPart != -1) {
CommonOperations.sendSoundtrackActivity(this.player, this.currentSoundtrack, this.currentPart);
}
else {
CommonOperations.clearSoundtrackActivity(this.player);
}
CommonOperations.clearSoundtrackForPlayer(this.player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void sendStellarStatusesToClient(ServerPlayerEntity player) {
StellarStatusNetworkingEvents.AFTER_ALL.invoker().afterAllStellarStatusSent(stellarStatus);
}

public static void sendSoundtrackActivity(ServerPlayerEntity player, Soundtrack soundtrack, int part) {
public static void playSoundtrackForPlayer(ServerPlayerEntity player, Soundtrack soundtrack, int part, boolean override) {
PacketByteBuf packet = PacketByteBufs.create();

List<Soundtrack.Part> parts = new ArrayList<>();
Expand All @@ -51,17 +51,29 @@ public static void sendSoundtrackActivity(ServerPlayerEntity player, Soundtrack
}

packet.writeCollection(parts, (buf, current) -> {
buf.writeIdentifier(current.getPath());
buf.writeIdentifier(current.getSound().getId());
buf.writeBoolean(current.isLooping());
buf.writeVarInt(current.getIterations());
});

packet.writeVarInt(part);

packet.writeBoolean(override);

ServerPlayNetworking.send(player, MModdingPackets.SEND_SOUNDTRACKS, packet);
}

public static void clearSoundtrackActivity(ServerPlayerEntity player) {
public static void skipSoundtrackPartForPlayer(ServerPlayerEntity player) {
ServerPlayNetworking.send(player, MModdingPackets.SKIP_SOUNDTRACKS, PacketByteBufs.create());
}

public static void skipToPartForPlayer(ServerPlayerEntity player, int part) {
PacketByteBuf packet = PacketByteBufs.create();
packet.writeVarInt(part);
ServerPlayNetworking.send(player, MModdingPackets.SKIP_TO_PART_SOUNDTRACKS, packet);
}

public static void clearSoundtrackForPlayer(ServerPlayerEntity player) {
ServerPlayNetworking.send(player, MModdingPackets.CLEAR_SOUNDTRACKS, PacketByteBufs.create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ public class MModdingPackets {

public static final MModdingIdentifier SEND_SOUNDTRACKS = new MModdingIdentifier("networking/soundtracks/send");

public static final MModdingIdentifier SKIP_SOUNDTRACKS = new MModdingIdentifier("networking/soundtracks/skip");

public static final MModdingIdentifier SKIP_TO_PART_SOUNDTRACKS = new MModdingIdentifier("networking/soundtracks/skip_to_part");

public static final MModdingIdentifier CLEAR_SOUNDTRACKS = new MModdingIdentifier("networking/soundtracks/clear");
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void receiveStellarStatusOnClient(ClientPlayNetworkHandler handler
ClientStellarStatusNetworkingEvents.AFTER.invoker().afterStellarStatusReceived(identifier, status);
}

public static void receiveSentSoundtrackActivity(MinecraftClient client, PacketByteBuf packet) {
public static void receiveSentSoundtrack(MinecraftClient client, PacketByteBuf packet) {
if (client.player != null) {
List<Soundtrack.Part> parts = packet.readList(current -> {
Identifier path = current.readIdentifier();
Expand All @@ -65,11 +65,28 @@ public static void receiveSentSoundtrackActivity(MinecraftClient client, PacketB
});
Soundtrack soundtrack = Soundtrack.create(parts);
int part = packet.readVarInt();
client.player.getSoundtrackPlayer().play(soundtrack, part);
if (packet.readBoolean()) {
client.player.getSoundtrackPlayer().playOnce(soundtrack, part);
}
else {
client.player.getSoundtrackPlayer().play(soundtrack, part);
}
}
}

public static void receiveClearedSoundtrackActivity(MinecraftClient client, PacketByteBuf packet) {
public static void receiveSoundtrackSkip(MinecraftClient client) {
if (client.player != null) {
client.player.getSoundtrackPlayer().skip();
}
}

public static void receiveSoundtrackSkipToPart(MinecraftClient client, PacketByteBuf packet) {
if (client.player != null) {
client.player.getSoundtrackPlayer().skip(packet.readVarInt());
}
}

public static void receiveSoundtrackDeletion(MinecraftClient client) {
if (client.player != null) {
client.player.getSoundtrackPlayer().stop();
}
Expand Down

0 comments on commit a659915

Please sign in to comment.