Newer
Older
ultramine_bukkit / src / test / java / org / ultramine / libtest / UBlockState.java
package org.ultramine.libtest;

import net.minecraft.block.Block;
import net.minecraftforge.oredict.OreDictionary;

import javax.annotation.Nonnull;
import java.util.Objects;

public class UBlockState
{
	@Nonnull private final Block block;
	@Nonnull private final int meta;

	public UBlockState(@Nonnull Block block, int meta)
	{
		if(meta != OreDictionary.WILDCARD_VALUE) //allow wildcard value
			checkBlockMeta(meta);
		this.block = block;
		this.meta = meta;
	}

	public UBlockState(Block block)
	{
		this(block, 0);
	}

	@Nonnull
	public Block getType()
	{
		return block;
	}

	public int getMeta()
	{
		return meta;
	}

	public boolean isType(Block block)
	{
		return Block.isEqualTo(getType(), block);
	}

	public boolean isType(Block block, int meta)
	{
		return isType(block) && (this.meta == meta || meta == OreDictionary.WILDCARD_VALUE || this.meta == OreDictionary.WILDCARD_VALUE);
	}

	public boolean isType(UBlockState other)
	{
		return isType(other.getType()) && (this.meta == other.meta || this.meta == OreDictionary.WILDCARD_VALUE || other.meta == OreDictionary.WILDCARD_VALUE);
	}

	@Nonnull
	public UBlockState withMeta(int meta)
	{
		return new UBlockState(block, meta);
	}

	@Override
	public int hashCode()
	{
		return Block.getIdFromBlock(block); //ignoring metadata in hash
	}

	@Override
	public boolean equals(Object o)
	{
		if(this == o)
			return true;
		if(o == null || getClass() != o.getClass())
			return false;
		UBlockState other = (UBlockState) o;
		return Objects.equals(this.block, other.block) && (this.meta == other.meta
				|| this.meta == OreDictionary.WILDCARD_VALUE || other.meta == OreDictionary.WILDCARD_VALUE);
	}

	public static UBlockState of(Object o)
	{
		if (o == null)
			throw new NullPointerException();

		if (o instanceof UBlockState)
			return (UBlockState) o;
		else if (o instanceof Block)
			return new UBlockState((Block) o, 0);

		throw new IllegalArgumentException("Unknown block type: " + o.getClass());
	}

	private static void checkBlockMeta(int meta) throws IllegalArgumentException
	{
		if(meta > 15 || meta < 0)
			throw new IllegalArgumentException("Block meta cat't be more then 15 or less then 0. Given: " + meta);
	}
}