001package net.minecraft.tileentity;
002
003import net.minecraft.block.Block;
004import net.minecraft.block.material.Material;
005import net.minecraft.nbt.NBTTagCompound;
006import net.minecraft.world.World;
007
008public class TileEntityNote extends TileEntity
009{
010    /** Note to play */
011    public byte note = 0;
012
013    /** stores the latest redstone state */
014    public boolean previousRedstoneState = false;
015
016    /**
017     * Writes a tile entity to NBT.
018     */
019    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
020    {
021        super.writeToNBT(par1NBTTagCompound);
022        par1NBTTagCompound.setByte("note", this.note);
023    }
024
025    /**
026     * Reads a tile entity from NBT.
027     */
028    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
029    {
030        super.readFromNBT(par1NBTTagCompound);
031        this.note = par1NBTTagCompound.getByte("note");
032
033        if (this.note < 0)
034        {
035            this.note = 0;
036        }
037
038        if (this.note > 24)
039        {
040            this.note = 24;
041        }
042    }
043
044    /**
045     * change pitch by -> (currentPitch + 1) % 25
046     */
047    public void changePitch()
048    {
049        this.note = (byte)((this.note + 1) % 25);
050        this.onInventoryChanged();
051    }
052
053    /**
054     * plays the stored note
055     */
056    public void triggerNote(World par1World, int par2, int par3, int par4)
057    {
058        if (par1World.getBlockMaterial(par2, par3 + 1, par4) == Material.air)
059        {
060            Material material = par1World.getBlockMaterial(par2, par3 - 1, par4);
061            byte b0 = 0;
062
063            if (material == Material.rock)
064            {
065                b0 = 1;
066            }
067
068            if (material == Material.sand)
069            {
070                b0 = 2;
071            }
072
073            if (material == Material.glass)
074            {
075                b0 = 3;
076            }
077
078            if (material == Material.wood)
079            {
080                b0 = 4;
081            }
082
083            par1World.addBlockEvent(par2, par3, par4, Block.music.blockID, b0, this.note);
084        }
085    }
086}