package org.cyclops.cyclopscore.helper;

import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;

import javax.annotation.Nullable;

/**
 * Contains helper methods for various block specific things.
 * @author rubensworks
 */
public final class BlockHelpers {

    /**
     * Safely get a block state property for a nullable state and value that may not have been set yet.
     * @param state The block state.
     * @param property The property to get the value for.
     * @param fallback The fallback value when something has failed.
     * @param <T> The type of value to fetch.
     * @return The value.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getSafeBlockStateProperty(@Nullable IBlockState state, IProperty property, T fallback) {
        if(state == null) {
            return fallback;
        }
        Comparable value = state.getValue(property);
        if(value == null) {
            return fallback;
        }
        return (T) value;
    }

    /**
     * Safely get an extended block state property for a nullable state and value that may not have been set yet.
     * @param state The block state.
     * @param property The property to get the value for.
     * @param fallback The fallback value when something has failed.
     * @param <T> The type of value to fetch.
     * @return The value.
     */
    public static <T> T getSafeBlockStateProperty(@Nullable IExtendedBlockState state, IUnlistedProperty<T> property,
                                                  T fallback) {
        if(state == null) {
            return fallback;
        }
        T value = state.getValue(property);
        if(value == null) {
            return fallback;
        }
        return value;
    }

}

@@ -34,16 +34,50 @@
 elper;%0A%0A
+import net.minecraft.block.Block;%0A
 import n
@@ -155,32 +155,138 @@
 te.IBlockState;%0A
+import net.minecraft.item.Item;%0Aimport net.minecraft.item.ItemBlock;%0Aimport net.minecraft.item.ItemStack;%0A
 import net.minec
@@ -392,16 +392,60 @@
 roperty;
+%0Aimport org.apache.commons.lang3.tuple.Pair;
 %0A%0Aimport
@@ -2080,10 +2080,1833 @@
 %0A    %7D%0A%0A
+    /**%0A     * Convert the given blockstate to a pair of blockname string and meta value.%0A     * @param blockState The blockstate to serialize.%0A     * @return The pair of the blockname and meta value.%0A     */%0A    public static Pair%3CString, Integer%3E serializeBlockState(IBlockState blockState) %7B%0A        String blockName = Block.blockRegistry.getNameForObject(blockState.getBlock()).toString();%0A        int meta = blockState.getBlock().getMetaFromState(blockState);%0A        return Pair.of(blockName, meta);%0A    %7D%0A%0A    /**%0A     * Convert the given serialized blockstate to a blockstate instance.%0A     * @param serializedBlockState The pair of the blockname and meta value.%0A     * @return The resulting blockstate. Can be null if the referred block does not exist.%0A     */%0A    public static IBlockState deserializeBlockState(Pair%3CString, Integer%3E serializedBlockState) %7B%0A        Block block = Block.getBlockFromName(serializedBlockState.getLeft());%0A        if(block != null) %7B%0A            return block.getStateFromMeta(serializedBlockState.getRight());%0A        %7D%0A        return null;%0A    %7D%0A%0A    /**%0A     * Get the blockstate from the given itemstack%0A     * @param itemStack The itemstack%0A     * @return The blockstate%0A     */%0A    public static IBlockState getBlockStateFromItemStack(ItemStack itemStack) %7B%0A        Block block = ((ItemBlock) itemStack.getItem()).getBlock();%0A        return block.getStateFromMeta(itemStack.getMetadata());%0A    %7D%0A%0A    /**%0A     * Get the itemstack from the given blockstate%0A     * @param blockState The blockstate%0A     * @return The itemstack%0A     */%0A    public static ItemStack getItemStackFromBlockState(IBlockState blockState) %7B%0A        Item item = Item.getItemFromBlock(blockState.getBlock());%0A        return new ItemStack(item, 1, blockState.getBlock().getMetaFromState(blockState));%0A    %7D%0A%0A
 %7D%0A

Add serialization methods for block states