Skip to content

Commit

Permalink
Implemented ClientSoundPlayer for client-side sound playback at any p…
Browse files Browse the repository at this point in the history
…osition
  • Loading branch information
floral-qua-floral committed Oct 27, 2024
1 parent ed5ae50 commit c94d2c8
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 50 deletions.
31 changes: 18 additions & 13 deletions src/main/java/com/floralquafloral/VoiceLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.util.ClientSoundPlayer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
Expand Down Expand Up @@ -68,25 +69,29 @@ public void play(MarioData data, long seed) {
PlayerEntity mario = data.getMario();

if(mario.getWorld().isClient) {
PositionedSoundInstance prevVoiceSound = PLAYER_VOICE_LINES.get(mario);
SoundManager soundManager = MinecraftClient.getInstance().getSoundManager();
soundManager.stop(prevVoiceSound);

PositionedSoundInstance voiceSound = new PositionedSoundInstance(
ClientSoundPlayer.SOUND_MANAGER.stop(PLAYER_VOICE_LINES.get(mario));

// PositionedSoundInstance voiceSound = new PositionedSoundInstance(
// SOUND_EVENTS.get(data.getCharacter()),
// SoundCategory.VOICE,
// 1.0F,
// 1.0F,
// Random.create(seed),
// data.getMario().getX(),
// data.getMario().getY(),
// data.getMario().getZ()
// );
// ClientSoundPlayer.SOUND_MANAGER.play(voiceSound);
PLAYER_VOICE_LINES.put(mario, ClientSoundPlayer.playSound(
SOUND_EVENTS.get(data.getCharacter()),
SoundCategory.VOICE,
mario,
1.0F,
1.0F,
Random.create(seed),
data.getMario().getX(),
data.getMario().getY(),
data.getMario().getZ()
);
soundManager.play(voiceSound);
PLAYER_VOICE_LINES.put(mario, voiceSound);
seed
));
}
else {
MarioQuaMario.LOGGER.info("Send voiceline packet!!!");
MarioPackets.sendPacketToTrackers((ServerPlayerEntity) data.getMario(), new PlayVoiceLineS2CPayload(data.getMario(), this, seed));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public abstract static class AerialTransitions {
private final @NotNull OldCharaStat GRAVITY = getGravity();
private final @NotNull OldCharaStat JUMP_GRAVITY = getJumpGravity();
private final @Nullable OldCharaStat JUMP_CAP = getJumpCap();
//TODO: Make Grounded states use CharaStats as well!

protected abstract @NotNull OldCharaStat getGravity();
protected abstract @NotNull OldCharaStat getJumpGravity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.floralquafloral.mariodata.client.Input;
import com.floralquafloral.mariodata.client.MarioClientData;
import com.floralquafloral.stats.CharaStat;
import com.floralquafloral.util.ClientSoundPlayer;
import com.floralquafloral.util.MarioSFX;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.BlockPos;
import org.joml.Vector2d;

Expand All @@ -32,13 +34,10 @@ public abstract static class GroundedTransitions {
(data) -> Input.DUCK.isHeld(),
(data, isSelf, seed) -> {
// Play duck voiceline
data.getMario().playSound(MarioSFX.DUCK);
ClientSoundPlayer.playSound(MarioSFX.DUCK, data, seed);
VoiceLine.DUCK.play(data, seed);
LOGGER.info("Ducking voiceline with seed {}", seed);
},
(data, seed) -> {
LOGGER.info("Entering duck_waddle on server with seed {}", seed);
}
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public List<ActionTransitionInjection> getTransitionInjections() {
&& data.getMario().isOnGround()
&& !data.getAction().ID.equals(getID())
&& Vector2d.lengthSquared(data.getForwardVel(), data.getStrafeVel()) > threshold * threshold;
}
},
GroundedTransitions.DUCK_WADDLE.EXECUTOR_CLIENT,
GroundedTransitions.DUCK_WADDLE.EXECUTOR_SERVER
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.floralquafloral.mariodata.client.MarioClientData;
import com.floralquafloral.registries.states.action.GroundedActionDefinition;
import com.floralquafloral.stats.CharaStat;
import com.floralquafloral.util.ClientSoundPlayer;
import com.floralquafloral.util.MarioSFX;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -24,7 +27,11 @@ public class DuckWaddle extends GroundedActionDefinition {

public static final ActionTransitionDefinition UNDUCK = new ActionTransitionDefinition(
"qua_mario:basic",
(data) -> !Input.DUCK.isHeld()
(data) -> !Input.DUCK.isHeld(),
(data, isSelf, seed) -> {
ClientSoundPlayer.playSound(MarioSFX.UNDUCK, data, seed);
},
null
);

public static final CharaStat WADDLE_ACCEL = new CharaStat(0.06, DUCKING, FORWARD, ACCELERATION);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/floralquafloral/registries/stomp/ParsedStomp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.floralquafloral.MarioQuaMario;
import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.mariodata.MarioPlayerData;
import com.floralquafloral.util.ClientSoundPlayer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class ParsedStomp {
private final boolean HITS_NONLIVING_ENTITIES;

private final RegistryKey<DamageType> DAMAGE_TYPE;
private final RegistryEntry<SoundEvent> SOUND_ENTRY;
private final SoundEvent SOUND_EVENT;
private final Identifier POST_STOMP_ACTION;

public ParsedStomp(StompDefinition definition) {
Expand All @@ -51,7 +52,7 @@ public ParsedStomp(StompDefinition definition) {
this.HITS_NONLIVING_ENTITIES = definition.canHitNonLiving();

this.DAMAGE_TYPE = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, definition.getDamageType());
this.SOUND_ENTRY = Registries.SOUND_EVENT.getEntry(definition.getSoundEvent());
this.SOUND_EVENT = definition.getSoundEvent();
this.POST_STOMP_ACTION = definition.getPostStompAction();
}

Expand All @@ -71,23 +72,22 @@ public void executeServer(MarioPlayerData data, Entity target, boolean harmless,

target.damage(damageSource, damage);

this.DEFINITION.executeServer(target.getWorld(), data, target, harmless, seed);
this.DEFINITION.executeServer(data, target, harmless, seed);
}
public void executeClient(PlayerEntity hearingPlayer, MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed) {
if(this.SOUND_ENTRY != null) {
target.getWorld().playSound(
hearingPlayer,
target.getX(),
target.getY(),
target.getZ(),
this.SOUND_ENTRY,
public void executeClient(MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed) {
if(this.SOUND_EVENT != null) {
ClientSoundPlayer.playSound(
this.SOUND_EVENT,
SoundCategory.PLAYERS,
data.getMario().getX(),
target.getY() + target.getHeight(),
data.getMario().getZ(),
1.0F,
1.0F,
seed
);
}
this.DEFINITION.executeClient(hearingPlayer.getWorld(), data, isSelf, target, harmless, seed);
this.DEFINITION.executeClient(data, isSelf, target, harmless, seed);
data.applyModifiedVelocity();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -27,8 +26,8 @@ public interface StompDefinition {

float calculateDamage(MarioData data, ServerPlayerEntity mario, ItemStack equipment, double equipmentArmor, double equipmentToughness, Entity target);

void executeServer(World world, MarioPlayerData data, Entity target, boolean harmless, long seed);
void executeClient(World world, MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed);
void executeServer(MarioPlayerData data, Entity target, boolean harmless, long seed);
void executeClient(MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed);

enum PainfulStompResponse {
INJURY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static void registerReceiver() {
+ "\nareEqual2: " + (context.player().getWorld().equals(mario.getWorld()))
+ "\nmarioDataMarioWorld: " + getMarioData(mario).getMario().getWorld()
);
stompType.executeClient(context.player(), (MarioPlayerData) getMarioData(mario), mario.isMainPlayer(), target, payload.harmless, payload.seed);
stompType.executeClient((MarioPlayerData) getMarioData(mario), mario.isMainPlayer(), target, payload.harmless, payload.seed);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@
import com.floralquafloral.registries.stomp.StompDefinition;
import com.floralquafloral.registries.stomp.StompHandler;
import com.floralquafloral.util.MarioSFX;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.MovementType;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -68,7 +59,7 @@ public float calculateDamage(MarioData data, ServerPlayerEntity mario, ItemStack
}

@Override
public void executeServer(World world, MarioPlayerData data, Entity target, boolean harmless, long seed) {
public void executeServer(MarioPlayerData data, Entity target, boolean harmless, long seed) {
executeCommon(data, target);
// world.playSound(
// null,
Expand All @@ -77,7 +68,7 @@ public void executeServer(World world, MarioPlayerData data, Entity target, bool
}

@Override
public void executeClient(World world, MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed) {
public void executeClient(MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed) {
executeCommon(data, target);

// world.playSoundFromEntity(null, target, Registries.SOUND_EVENT.getEntry(MarioSFX.STOMP), SoundCategory.PLAYERS, 1.0F, 1.0F, seed);
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/floralquafloral/util/ClientSoundPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.floralquafloral.util;

import com.floralquafloral.mariodata.MarioData;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.math.random.Random;

@SuppressWarnings("UnusedReturnValue")
public abstract class ClientSoundPlayer {
public static final SoundManager SOUND_MANAGER = MinecraftClient.getInstance().getSoundManager();

public static PositionedSoundInstance playSound(SoundEvent event, SoundCategory category, double x, double y, double z, float volume, float pitch, long seed) {
PositionedSoundInstance sound = new PositionedSoundInstance(
event,
category,
volume,
pitch,
Random.create(seed),
x,
y,
z
);
SOUND_MANAGER.play(sound);
return sound;
}

public static PositionedSoundInstance playSound(SoundEvent event, SoundCategory category, Entity entity, float volume, float pitch, long seed) {
return playSound(event, category, entity.getX(), entity.getY(), entity.getZ(), volume, pitch, seed);
}

public static PositionedSoundInstance playSound(SoundEvent event, MarioData data, float volume, float pitch, long seed) {
return playSound(event, SoundCategory.PLAYERS, data.getMario(), volume, pitch, seed);
}

public static PositionedSoundInstance playSound(SoundEvent event, MarioData data, long seed) {
return playSound(event, data, 1.0F, 1.0F, seed);
}

public static void kill(PositionedSoundInstance sound) {
SOUND_MANAGER.stop(sound);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/floralquafloral/util/MarioSFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;

public abstract class MarioSFX {
public final class MarioSFX {
public static final SoundEvent JUMP = makeMovementSound("jump");
public static final SoundEvent FLIP = makeMovementSound("flip");
public static final SoundEvent SKID_BLOCK = makeMovementSound("skid");
Expand All @@ -24,7 +24,8 @@ public abstract class MarioSFX {
public static final SoundEvent STOMP_HEAVY = makeStompSound("heavy");
public static final SoundEvent STOMP_YOSHI = makeStompSound("yoshi");

public static final SoundEvent DUCK = makeAndRegisterSound("duck");
public static final SoundEvent DUCK = makeActionSound("duck");
public static final SoundEvent UNDUCK = makeActionSound("unduck");

private static SoundEvent makeMovementSound(String name) {
return makeAndRegisterSound("sfx.movement." + name);
Expand Down
Binary file modified src/main/resources/assets/qua_mario/sounds/sfx/action/unduck.ogg
Binary file not shown.

0 comments on commit c94d2c8

Please sign in to comment.