package common.block.tech;

import java.util.List;

import common.block.Block;
import common.block.Material;
import common.entity.Entity;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.Items;
import common.item.CheatTab;
import common.item.Item;
import common.item.block.ItemSmallBlock;
import common.item.tool.ItemShears;
import common.model.BlockLayer;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.properties.Property;
import common.properties.PropertyBool;
import common.rng.Random;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.Facing;
import common.world.IBlockAccess;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockTripWire extends Block
{
    public static final PropertyBool POWERED = PropertyBool.create("powered");
    public static final PropertyBool SUSPENDED = PropertyBool.create("suspended");
    public static final PropertyBool ATTACHED = PropertyBool.create("attached");
    public static final PropertyBool DISARMED = PropertyBool.create("disarmed");
    public static final PropertyBool NORTH = PropertyBool.create("north");
    public static final PropertyBool EAST = PropertyBool.create("east");
    public static final PropertyBool SOUTH = PropertyBool.create("south");
    public static final PropertyBool WEST = PropertyBool.create("west");

    public BlockTripWire()
    {
        super(Material.SMALL);
        this.setDefaultState(this.getBaseState().withProperty(POWERED, Boolean.valueOf(false)).withProperty(SUSPENDED, Boolean.valueOf(false)).withProperty(ATTACHED, Boolean.valueOf(false)).withProperty(DISARMED, Boolean.valueOf(false)).withProperty(NORTH, Boolean.valueOf(false)).withProperty(EAST, Boolean.valueOf(false)).withProperty(SOUTH, Boolean.valueOf(false)).withProperty(WEST, Boolean.valueOf(false)));
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.15625F, 1.0F);
//        this.setTickRandomly(true);
    }

    /**
     * Get the actual Block state of this Block at the given position. This applies properties not visible in the
     * metadata, such as fence connections.
     */
    public State getActualState(State state, IWorldAccess worldIn, BlockPos pos)
    {
        return state.withProperty(NORTH, Boolean.valueOf(isConnectedTo(worldIn, pos, state, Facing.NORTH))).withProperty(EAST, Boolean.valueOf(isConnectedTo(worldIn, pos, state, Facing.EAST))).withProperty(SOUTH, Boolean.valueOf(isConnectedTo(worldIn, pos, state, Facing.SOUTH))).withProperty(WEST, Boolean.valueOf(isConnectedTo(worldIn, pos, state, Facing.WEST)));
    }

    public BoundingBox getCollisionBoundingBox(World worldIn, BlockPos pos, State state)
    {
        return null;
    }

    /**
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
     */
    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean isFullCube()
    {
        return false;
    }

    public BlockLayer getBlockLayer()
    {
        return BlockLayer.TRANSLUCENT;
    }

    /**
     * Get the Item that this Block should drop when harvested.
     */
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return Items.string;
    }

    public Item getItem(World worldIn, BlockPos pos)
    {
        return Items.string;
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
    {
        boolean flag = ((Boolean)state.getValue(SUSPENDED)).booleanValue();
        boolean flag1 = !worldIn.isBlockSolid(pos.down());

        if (flag != flag1)
        {
            this.dropBlockAsItem(worldIn, pos, state, 0);
            worldIn.setBlockToAir(pos);
        }
    }

    public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
    {
        State iblockstate = worldIn.getState(pos);
        boolean flag = ((Boolean)iblockstate.getValue(ATTACHED)).booleanValue();
        boolean flag1 = ((Boolean)iblockstate.getValue(SUSPENDED)).booleanValue();

        if (!flag1)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.09375F, 1.0F);
        }
        else if (!flag)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
        }
        else
        {
            this.setBlockBounds(0.0F, 0.0625F, 0.0F, 1.0F, 0.15625F, 1.0F);
        }
    }

    public void onBlockAdded(AWorldServer worldIn, BlockPos pos, State state)
    {
        state = state.withProperty(SUSPENDED, Boolean.valueOf(!worldIn.isBlockSolid(pos.down())));
        worldIn.setState(pos, state, 3);
        this.notifyHook(worldIn, pos, state);
    }

    public void onBlockRemoved(AWorldServer worldIn, BlockPos pos, State state)
    {
        this.notifyHook(worldIn, pos, state.withProperty(POWERED, Boolean.valueOf(true)));
    }

    public void onBlockHarvested(World worldIn, BlockPos pos, State state, EntityNPC player)
    {
        if (!worldIn.client)
        {
            if (player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof ItemShears)
            {
                worldIn.setState(pos, state.withProperty(DISARMED, Boolean.valueOf(true)), 4);
            }
        }
    }

    private void notifyHook(World worldIn, BlockPos pos, State state)
    {
        for (Facing enumfacing : new Facing[] {Facing.SOUTH, Facing.WEST})
        {
            for (int i = 1; i < 42; ++i)
            {
                BlockPos blockpos = pos.offset(enumfacing, i);
                State iblockstate = worldIn.getState(blockpos);

                if (iblockstate.getBlock() == Blocks.tripwire_hook)
                {
                    if (iblockstate.getValue(BlockTripWireHook.FACING) == enumfacing.getOpposite())
                    {
                        Blocks.tripwire_hook.triggerHookAt(worldIn, blockpos, iblockstate, false, true, i, state);
                    }

                    break;
                }

                if (iblockstate.getBlock() != Blocks.string)
                {
                    break;
                }
            }
        }
    }

    /**
     * Called When an Entity Collided with the Block
     */
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, State state, Entity entityIn)
    {
        if (!worldIn.client)
        {
            if (!((Boolean)state.getValue(POWERED)).booleanValue())
            {
                this.updateState(worldIn, pos);
            }
        }
    }

    /**
     * Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
     */
    public void randomTick(AWorldServer worldIn, BlockPos pos, State state, Random random)
    {
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
//        if (!worldIn.client)
//        {
        if (((Boolean)worldIn.getState(pos).getValue(POWERED)).booleanValue())
        {
            this.updateState(worldIn, pos);
        }
//        }
    }

    private void updateState(World worldIn, BlockPos pos)
    {
        State iblockstate = worldIn.getState(pos);
        boolean flag = ((Boolean)iblockstate.getValue(POWERED)).booleanValue();
        boolean flag1 = false;
        List <? extends Entity > list = worldIn.getEntitiesWithinAABBExcludingEntity((Entity)null, new BoundingBox((double)pos.getX() + this.minX, (double)pos.getY() + this.minY, (double)pos.getZ() + this.minZ, (double)pos.getX() + this.maxX, (double)pos.getY() + this.maxY, (double)pos.getZ() + this.maxZ));

        if (!list.isEmpty())
        {
            for (Entity entity : list)
            {
                if (!entity.doesEntityNotTriggerPressurePlate())
                {
                    flag1 = true;
                    break;
                }
            }
        }

        if (flag1 != flag)
        {
            iblockstate = iblockstate.withProperty(POWERED, Boolean.valueOf(flag1));
            worldIn.setState(pos, iblockstate, 3);
            this.notifyHook(worldIn, pos, iblockstate);
        }

        if (flag1)
        {
            worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn, pos));
        }
    }

    public static boolean isConnectedTo(IBlockAccess worldIn, BlockPos pos, State state, Facing direction)
    {
        BlockPos blockpos = pos.offset(direction);
        State iblockstate = worldIn.getState(blockpos);
        Block block = iblockstate.getBlock();

        if (block == Blocks.tripwire_hook)
        {
            Facing enumfacing = direction.getOpposite();
            return iblockstate.getValue(BlockTripWireHook.FACING) == enumfacing;
        }
        else if (block == Blocks.string)
        {
            boolean flag = ((Boolean)state.getValue(SUSPENDED)).booleanValue();
            boolean flag1 = ((Boolean)iblockstate.getValue(SUSPENDED)).booleanValue();
            return flag == flag1;
        }
        else
        {
            return false;
        }
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(POWERED, Boolean.valueOf((meta & 1) > 0)).withProperty(SUSPENDED, Boolean.valueOf((meta & 2) > 0)).withProperty(ATTACHED, Boolean.valueOf((meta & 4) > 0)).withProperty(DISARMED, Boolean.valueOf((meta & 8) > 0));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        int i = 0;

        if (((Boolean)state.getValue(POWERED)).booleanValue())
        {
            i |= 1;
        }

        if (((Boolean)state.getValue(SUSPENDED)).booleanValue())
        {
            i |= 2;
        }

        if (((Boolean)state.getValue(ATTACHED)).booleanValue())
        {
            i |= 4;
        }

        if (((Boolean)state.getValue(DISARMED)).booleanValue())
        {
            i |= 8;
        }

        return i;
    }

    protected Property[] getProperties()
    {
        return new Property[] {POWERED, SUSPENDED, ATTACHED, DISARMED, NORTH, EAST, WEST, SOUTH};
    }
    
    private static Model getModelDetached(boolean n, boolean s, boolean w, boolean e, int sides) {
    	if(sides == 1)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : (s ? ModelRotation.X0_Y180 : (w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2 && ((e != w) || (n != s)))
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(8, 1.5f, 7.75f, 12, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(12, 1.5f, 7.75f, 16, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.rotate(n && e ? ModelRotation.X0_Y0 : (s && w ? ModelRotation.X0_Y180 : 
    			(n && w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 12, 8.25f, 1.5f, 16).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : ModelRotation.X0_Y90);
    	else if(sides == 3)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 12, 8.25f, 1.5f, 16).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(8, 1.5f, 7.75f, 12, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(12, 1.5f, 7.75f, 16, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.rotate(!w ? ModelRotation.X0_Y0 : (!e ? ModelRotation.X0_Y180 : (!s ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 1.5f, 12, 8.25f, 1.5f, 16).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(0, 1.5f, 7.75f, 4, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(4, 1.5f, 7.75f, 8, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(8, 1.5f, 7.75f, 12, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(12, 1.5f, 7.75f, 16, 1.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull();
    }
    
    private static Model getModelAttached(boolean n, boolean s, boolean w, boolean e, int sides) {
    	if(sides == 1)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : (s ? ModelRotation.X0_Y180 : (w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2 && ((e != w) || (n != s)))
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(8, 1.5f, 7.75f, 12, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(12, 1.5f, 7.75f, 16, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.rotate(n && e ? ModelRotation.X0_Y0 : (s && w ? ModelRotation.X0_Y180 : 
    			(n && w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 12, 8.25f, 1.5f, 16).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : ModelRotation.X0_Y90);
    	else if(sides == 3)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 12, 8.25f, 1.5f, 16).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(8, 1.5f, 7.75f, 12, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(12, 1.5f, 7.75f, 16, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.rotate(!w ? ModelRotation.X0_Y0 : (!e ? ModelRotation.X0_Y180 : (!s ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 1.5f, 0, 8.25f, 1.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 4, 8.25f, 1.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 8, 8.25f, 1.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 1.5f, 12, 8.25f, 1.5f, 16).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(0, 1.5f, 7.75f, 4, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(4, 1.5f, 7.75f, 8, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(8, 1.5f, 7.75f, 12, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(12, 1.5f, 7.75f, 16, 1.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull();
    }
    
    private static Model getModelDetSuspend(boolean n, boolean s, boolean w, boolean e, int sides) {
    	if(sides == 1)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : (s ? ModelRotation.X0_Y180 : (w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2 && ((e != w) || (n != s)))
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(8, 3.5f, 7.75f, 12, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(12, 3.5f, 7.75f, 16, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.rotate(n && e ? ModelRotation.X0_Y0 : (s && w ? ModelRotation.X0_Y180 : 
    			(n && w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 12, 8.25f, 3.5f, 16).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : ModelRotation.X0_Y90);
    	else if(sides == 3)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 12, 8.25f, 3.5f, 16).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(8, 3.5f, 7.75f, 12, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(12, 3.5f, 7.75f, 16, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.rotate(!w ? ModelRotation.X0_Y0 : (!e ? ModelRotation.X0_Y180 : (!s ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(7.75f, 3.5f, 12, 8.25f, 3.5f, 16).noShade()
    				.d().uv(0, 2, 16, 0).rot(90).noCull()
    				.u().uv(0, 0, 16, 2).rot(90).noCull()
    			.add(0, 3.5f, 7.75f, 4, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(4, 3.5f, 7.75f, 8, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(8, 3.5f, 7.75f, 12, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull()
    			.add(12, 3.5f, 7.75f, 16, 3.5f, 8.25f).noShade()
    				.d().uv(0, 2, 16, 0).noCull()
    				.u().uv(0, 0, 16, 2).noCull();
    }
    
    private static Model getModelAttSuspend(boolean n, boolean s, boolean w, boolean e, int sides) {
    	if(sides == 1)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : (s ? ModelRotation.X0_Y180 : (w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2 && ((e != w) || (n != s)))
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(8, 3.5f, 7.75f, 12, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(12, 3.5f, 7.75f, 16, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.rotate(n && e ? ModelRotation.X0_Y0 : (s && w ? ModelRotation.X0_Y180 : 
    			(n && w ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else if(sides == 2)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 12, 8.25f, 3.5f, 16).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.rotate(n ? ModelRotation.X0_Y0 : ModelRotation.X0_Y90);
    	else if(sides == 3)
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 12, 8.25f, 3.5f, 16).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(8, 3.5f, 7.75f, 12, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(12, 3.5f, 7.75f, 16, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.rotate(!w ? ModelRotation.X0_Y0 : (!e ? ModelRotation.X0_Y180 : (!s ? ModelRotation.X0_Y270 : ModelRotation.X0_Y90)));
    	else
    		return ModelProvider.getModelProvider().getModel("trip_wire")
    				.add(7.75f, 3.5f, 0, 8.25f, 3.5f, 4).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 4, 8.25f, 3.5f, 8).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 8, 8.25f, 3.5f, 12).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(7.75f, 3.5f, 12, 8.25f, 3.5f, 16).noShade()
    				.d().uv(0, 4, 16, 2).rot(90).noCull()
    				.u().uv(0, 2, 16, 4).rot(90).noCull()
    			.add(0, 3.5f, 7.75f, 4, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(4, 3.5f, 7.75f, 8, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(8, 3.5f, 7.75f, 12, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull()
    			.add(12, 3.5f, 7.75f, 16, 3.5f, 8.25f).noShade()
    				.d().uv(0, 4, 16, 2).noCull()
    				.u().uv(0, 2, 16, 4).noCull();
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	boolean n = state.getValue(NORTH);
    	boolean s = state.getValue(SOUTH);
    	boolean w = state.getValue(WEST);
    	boolean e = state.getValue(EAST);
    	int sides = (n ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0) + (e ? 1 : 0);
    	if(sides == 0) {
    		n = true;
    		s = true;
    		sides = 2;
    	}
    	return (state.getValue(SUSPENDED) ? ((state.getValue(ATTACHED) ? getModelAttSuspend(n, s, w, e, sides) : 
    			getModelDetSuspend(n, s, w, e, sides))) : (state.getValue(ATTACHED) ? getModelAttached(n, s, w, e, sides) : 
    			getModelDetached(n, s, w, e, sides)));
    }

    public Property<?>[] getIgnoredProperties() {
    	return new Property[] {DISARMED, POWERED};
    }
	
	protected Item getItemToRegister() {
		return new ItemSmallBlock(this).setDisplay("Faden").setTab(CheatTab.TECHNOLOGY).setMaxAmount(1024);
	}
}
