package net.glowstone.block.entity;

import org.bukkit.material.MaterialData;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.block.state.GlowFlowerPot;
import net.glowstone.constants.GlowBlockEntity;
import net.glowstone.constants.ItemIds;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.util.nbt.CompoundTag;

public class TEFlowerPot extends TileEntity {

    private MaterialData contents;

    public TEFlowerPot(GlowBlock block) {
        super(block);
        setSaveId("FlowerPot");
    }

    public MaterialData getContents() {
        return contents;
    }

    public void setContents(MaterialData contents) {
        this.contents = contents;
    }

    @Override
    public void loadNbt(CompoundTag tag) {
        super.loadNbt(tag);

        if (tag.isString("Item") && !tag.getString("Item").isEmpty() && tag.isInt("Data")) {
            contents = ItemIds.getMaterial(tag.getString("Item")).getNewData((byte) tag.getInt("Data"));
        }
    }

    @Override
    public void saveNbt(CompoundTag tag) {
        super.saveNbt(tag);

        if (contents != null) {
            tag.putString("Item", ItemIds.getName(contents.getItemType()));
            tag.putInt("Data", contents.getData());
        } else {
            // Mimics how Minecraft does it.
            tag.putString("Item", "");
            tag.putInt("Data", 0);
        }
    }

    @Override
    public GlowBlockState getState() {
        return new GlowFlowerPot(block);
    }

    @Override
    public void update(GlowPlayer player) {
        super.update(player);

        CompoundTag nbt = new CompoundTag();

        saveNbt(nbt);
        player.sendBlockEntityChange(getBlock().getLocation(), GlowBlockEntity.FLOWER_POT, nbt);
    }
}

@@ -30,16 +30,44 @@
 ntity;%0A%0A
+import org.bukkit.Material;%0A
 import o
@@ -852,194 +852,488 @@
    i
-f (tag.isString(%22Item%22) && !tag.getString(%22Item%22).isEmpty() && tag.isInt(%22Data%22)) %7B%0A            contents = ItemIds.getMaterial(tag.getString(%22Item%22)).getNewData((byte) tag.getInt(%22Data%22)
+nt contentsData = tag.isInt(%22Data%22) ? tag.getInt(%22Data%22) : 0;%0A%0A        if (tag.isString(%22Item%22) && !tag.getString(%22Item%22).isEmpty()) %7B%0A            // NBT data uses material ID names (post-1.8).%0A            contents = ItemIds.getMaterial(tag.getString(%22Item%22)).getNewData((byte) contentsData);%0A        %7D else if (tag.isInt(%22Item%22)) %7B%0A            // NBT data uses material IDs (pre-1.8).%0A            contents = Material.getMaterial(tag.getInt(%22Item%22)).getNewData((byte) contentsData
 );%0A 

Support material IDs as well (pre-1.8)