package com.khorn.terraincontrol.forge;

import net.minecraft.block.Block;
import net.minecraft.block.BlockFalling;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;

import com.khorn.terraincontrol.LocalMaterialData;
import com.khorn.terraincontrol.TerrainControl;
import com.khorn.terraincontrol.util.helpers.BlockHelper;
import com.khorn.terraincontrol.util.minecraftTypes.DefaultMaterial;

/**
 * Implementation of LocalMaterial that wraps one of Minecraft's Blocks.
 * 
 */
public class ForgeMaterialData implements LocalMaterialData
{

    /**
     * Gets a {@code BukkitMaterialData} of the given id and data.
     * @param id   The block id.
     * @param data The block data.
     * @return The {@code BukkitMateialData} instance.
     */
    public static ForgeMaterialData ofIds(int id, int data)
    {
        Block block = Block.getBlockById(id);
        IBlockState blockData = block.getStateFromMeta(data);
        return ofMinecraftBlockState(blockData);
    }

    /**
     * Gets a {@code BukkitMaterialData} of the given material and data.
     * @param material The material.
     * @param data     The block data.
     * @return The {@code BukkitMateialData} instance.
     */
    public static ForgeMaterialData ofDefaultMaterial(DefaultMaterial material, int data)
    {
        return ofIds(material.id, data);
    }

    /**
     * Gets a {@code BukkitMaterialData} of the given Minecraft block. The
     * default block data (usually 0) will be used.
     * @param block The material.
     * @return The {@code BukkitMateialData} instance.
     */
    public static ForgeMaterialData ofMinecraftBlock(Block block)
    {
        return ofMinecraftBlockState(block.getDefaultState());
    }

    /**
     * Gets a {@code BukkitMaterialData} of the given Minecraft blockData.
     * @param blockData The material an data.
     * @return The {@code BukkitMateialData} instance.
     */
    public static ForgeMaterialData ofMinecraftBlockState(IBlockState blockData)
    {
        return new ForgeMaterialData(blockData);
    }

    private final IBlockState blockData;

    private ForgeMaterialData(IBlockState blockData)
    {
        this.blockData = blockData;
    }

    @Override
    public boolean canSnowFallOn()
    {
        return toDefaultMaterial().canSnowFallOn();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (!(obj instanceof ForgeMaterialData))
        {
            return false;
        }
        ForgeMaterialData other = (ForgeMaterialData) obj;
        if (!blockData.equals(other.blockData))
        {
            return false;
        }
        return true;
    }

    @Override
    public byte getBlockData()
    {
        return (byte) blockData.getBlock().getMetaFromState(blockData);
    }

    @Override
    public int getBlockId()
    {
        return Block.getIdFromBlock(blockData.getBlock());
    }

    @Override
    public String getName()
    {
        Block block = blockData.getBlock();
        DefaultMaterial defaultMaterial = toDefaultMaterial();

        byte data = getBlockData();
        boolean nonDefaultData = !block.getBlockState().equals(this.blockData);
        // Note that the above line is not equivalent to data != 0, as for
        // example pumpkins have a default data value of 2

        if (defaultMaterial == DefaultMaterial.UNKNOWN_BLOCK)
        {
            // Use Minecraft's name
            if (nonDefaultData)
            {
                return Block.blockRegistry.getNameForObject(blockData.getBlock()) + ":" + data;
            }
            return Block.blockRegistry.getNameForObject(blockData.getBlock()).toString();
        } else
        {
            // Use our name
            if (nonDefaultData)
            {
                return defaultMaterial.name() + ":" + getBlockData();
            }
            return defaultMaterial.name();
        }
    }

    @Override
    public int hashCode()
    {
        // From 4096 to 69632 when there are 4096 block ids
        return TerrainControl.SUPPORTED_BLOCK_IDS + getBlockId() * 16 + getBlockData();
    }

    @Override
    public int hashCodeWithoutBlockData()
    {
        // From 0 to 4095 when there are 4096 block ids
        return getBlockId();
    }

    @Override
    public boolean isLiquid()
    {
        return blockData.getBlock().getMaterial().isLiquid();
    }

    @Override
    public boolean isMaterial(DefaultMaterial material)
    {
        return material.id == getBlockId();
    }

    @Override
    public boolean isSolid()
    {
        // Let us override whether materials are solid
        DefaultMaterial defaultMaterial = toDefaultMaterial();
        if (defaultMaterial != DefaultMaterial.UNKNOWN_BLOCK)
        {
            return defaultMaterial.isSolid();
        }

        return blockData.getBlock().getMaterial().isSolid();
    }

    @Override
    public DefaultMaterial toDefaultMaterial()
    {
        return DefaultMaterial.getMaterial(getBlockId());
    }

    @Override
    public String toString()
    {
        return getName();
    }

    @Override
    public LocalMaterialData withBlockData(int i)
    {
        if (i == getBlockData())
        {
            return this;
        }

        Block block = this.blockData.getBlock();
        return ofMinecraftBlockState(block.getStateFromMeta(i));
    }

    public IBlockState internalBlock()
    {
        return blockData;
    }

    @Override
    public LocalMaterialData rotate()
    {
        // Try to rotate
        DefaultMaterial defaultMaterial = toDefaultMaterial();
        if (defaultMaterial != DefaultMaterial.UNKNOWN_BLOCK)
        {
            // We only know how to rotate vanilla blocks
            byte blockDataByte = getBlockData();
            int newData = BlockHelper.rotateData(defaultMaterial, blockDataByte);
            if (newData != blockDataByte)
            {
                return ofMinecraftBlockState(blockData.getBlock().getStateFromMeta(newData));
            }
        }

        // No changes, return object itself
        return this;
    }

    @Override
    public boolean isAir() {
        return blockData.getBlock().getMaterial() == Material.air;
    }

    @Override
    public boolean canFall()
    {
        return blockData.getBlock() instanceof BlockFalling;
    }

}

@@ -38,176 +38,8 @@
 e;%0A%0A
-import net.minecraft.block.Block;%0Aimport net.minecraft.block.BlockFalling;%0Aimport net.minecraft.block.material.Material;%0Aimport net.minecraft.block.state.IBlockState;%0A%0A
 impo
@@ -259,16 +259,183 @@
 aterial;
+%0Aimport net.minecraft.block.Block;%0Aimport net.minecraft.block.BlockFalling;%0Aimport net.minecraft.block.material.Material;%0Aimport net.minecraft.block.state.IBlockState;
 %0A%0A/**%0A *
@@ -3226,21 +3226,23 @@
 lock.get
-Block
+Default
 State().

Fix material writing on Forge