/*
 * Aeronica's mxTune MOD
 * Copyright {2016} Paul Boese a.k.a. Aeronica
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.aeronica.mods.mxtune.util;

import net.aeronica.mods.mxtune.blocks.IPlacedInstrument;
import net.aeronica.mods.mxtune.blocks.TileInstrument;
import net.aeronica.mods.mxtune.inventory.IInstrument;
import net.aeronica.mods.mxtune.inventory.IMusic;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.util.Constants;

public enum SheetMusicUtil
{
    ;
    public static String getMusicTitle(ItemStack stackIn)
    {
        ItemStack sheetMusic = SheetMusicUtil.getSheetMusic(stackIn);
        if (!sheetMusic.isEmpty() && sheetMusic.getTagCompound() != null)
        {
            NBTTagCompound contents = (NBTTagCompound) sheetMusic.getTagCompound().getTag("MusicBook");
            if (!contents.isEmpty())
            {
                return sheetMusic.getDisplayName();
            }
        }
        return "";
    }

    public static ItemStack getSheetMusic(BlockPos pos, EntityPlayer playerIn, boolean isPlaced)
    {
        if (isPlaced)
        {
            if (playerIn.getEntityWorld().getBlockState(pos).getBlock() instanceof IPlacedInstrument)
            {
                Block placedInst = playerIn.getEntityWorld().getBlockState(pos).getBlock();
                TileInstrument te = ((IPlacedInstrument) placedInst).getTE(playerIn.getEntityWorld(), pos);
                if(!te.getInventory().getStackInSlot(0).isEmpty())
                    return te.getInventory().getStackInSlot(0).copy();
            }
        } else
        {
            return SheetMusicUtil.getSheetMusic(playerIn.getHeldItemMainhand());
        }
        return ItemStack.EMPTY;
    }
    
    public static ItemStack getSheetMusic(ItemStack stackIn)
    {
        if (!stackIn.isEmpty() && stackIn.getTagCompound() != null && (stackIn.getItem() instanceof IInstrument))
        {
            NBTTagList items = stackIn.getTagCompound().getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);
            if (items.tagCount() == 1)
            {
                NBTTagCompound item = items.getCompoundTagAt(0);
                ItemStack sheetMusicOld = new ItemStack(item);
                if (!sheetMusicOld.isEmpty() && sheetMusicOld.getTagCompound() != null && (sheetMusicOld.getItem() instanceof IMusic))
                {
                    NBTTagCompound contents = (NBTTagCompound) sheetMusicOld.getTagCompound().getTag("MusicBook");
                    if (!contents.isEmpty())
                    {
                        return sheetMusicOld;
                    }
                }
            }
        }
        return ItemStack.EMPTY;
    }

    public static void writeSheetMusic(ItemStack sheetMusic, String musicTitle, String mml)
    {
        NBTTagCompound compound = sheetMusic.getTagCompound();
        if (compound != null && sheetMusic.getItem() instanceof IMusic)
        {
            sheetMusic.setStackDisplayName(musicTitle);
            NBTTagCompound contents = new NBTTagCompound();
            contents.setString("MML", mml);
            compound.setTag("MusicBook", contents);
        }
    }
}

@@ -1177,16 +1177,50 @@
 tants;%0A%0A
+import javax.annotation.Nonnull;%0A%0A
 public e
@@ -3512,16 +3512,25 @@
 etMusic,
+ @Nonnull
  String 
@@ -3540,16 +3540,25 @@
 icTitle,
+ @Nonnull
  String 
@@ -3560,32 +3560,84 @@
 ring mml)%0A    %7B%0A
+        sheetMusic.setStackDisplayName(musicTitle);%0A
         NBTTagCo
@@ -3769,64 +3769,8 @@
   %7B%0A
-            sheetMusic.setStackDisplayName(musicTitle);%0A
     

Fix self inflicted bug: failure to write NBT - Must set the title before checking for a tag. Add Nonnull annotations to Sting parameters.