package common.packet;

import java.io.IOException;

import common.init.SoundEvent;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;

public class S29PacketSoundEffect implements Packet<IClientPlayer>
{
    private SoundEvent sound;
    private int posX;
    private int posY = Integer.MAX_VALUE;
    private int posZ;
    private float soundVolume;

    public S29PacketSoundEffect()
    {
    	this.sound = null;
    }

    public S29PacketSoundEffect(SoundEvent sound, double soundX, double soundY, double soundZ, float volume)
    {
        this.sound = sound;
        if(this.sound == null) {
        	return;
//			throw new NullPointerException("Name is null");
		}
        this.posX = (int)(soundX * 8.0D);
        this.posY = (int)(soundY * 8.0D);
        this.posZ = (int)(soundZ * 8.0D);
        this.soundVolume = volume;
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.sound = buf.readEnumOrNull(SoundEvent.class);
        if(this.sound != null) {
	        this.posX = buf.readInt();
	        this.posY = buf.readInt();
	        this.posZ = buf.readInt();
	        this.soundVolume = buf.readFloat();
        }
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeEnumOrNull(this.sound);
        if(this.sound != null) {
	        buf.writeInt(this.posX);
	        buf.writeInt(this.posY);
	        buf.writeInt(this.posZ);
	        buf.writeFloat(this.soundVolume);
        }
    }

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

    public double getX()
    {
        return (double)((float)this.posX / 8.0F);
    }

    public double getY()
    {
        return (double)((float)this.posY / 8.0F);
    }

    public double getZ()
    {
        return (double)((float)this.posZ / 8.0F);
    }

    public float getVolume()
    {
        return this.soundVolume;
    }
    
    public void processPacket(IClientPlayer handler)
    {
        handler.handleSoundEffect(this);
    }
}
