#Custom sound for generic.scale command

2 messages · Page 1 of 1 (latest)

random grail
#

i have made a mod but it didnt work here is the code i am trying to work it thru a resource pack because the custom sound is on the resource pack that i want to use when walking

#

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;

public class GiantWalkingSoundMod implements ModInitializer {
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
server.getPlayerManager().getPlayerList().forEach(player -> {
player.getAttributeInstance(EntityAttributes.GENERIC_SCALE).setOnAttributeModifiedCallback((attribute, value) -> {
double size = player.getAttributeInstance(EntityAttributes.GENERIC_SCALE).getValue();
// Check if the player's size is within the specified range
if (size >= 5.0 && size <= 16.0) {
// Check if the player is moving
if (player.horizontalSpeed > 0) {
// Play custom sound
playCustomSound(player, CustomSoundEvents.GIANT_WALKING);
}
}
return value;
});
});
});
}

private void playCustomSound(ServerPlayerEntity player, SoundEvent soundEvent) {
    player.playSound(soundEvent, 1.0f, 1.0f);
}

// Define custom sound event
public static class CustomSoundEvents {
    // Ensure these identifiers match the sounds in your resource pack
    public static final SoundEvent GIANT_WALKING = register("yourmodid:giant_walking");

    private static SoundEvent register(String name) {
        return new SoundEvent(new Identifier(name));
    }
}

}