package net.glowstone.block.blocktype;

import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Gate;
import org.bukkit.material.MaterialData;
import org.bukkit.util.Vector;

import java.util.Arrays;
import java.util.Collection;

public class BlockFenceGate extends BlockOpenable {

    private static BlockFace blockFaceFromYaw(float yaw) {
        // nb: opposite from getOppositeBlockFace in BlockType
        yaw = yaw % 360;
        if (yaw < 0) {
            yaw += 360.0;
        }
        if (0 <= yaw && yaw < 45) {
            return BlockFace.SOUTH;
        } else if (45 <= yaw && yaw < 135) {
            return BlockFace.WEST;
        } else if (135 <= yaw && yaw < 225) {
            return BlockFace.NORTH;
        } else if (225 <= yaw && yaw < 315) {
            return BlockFace.EAST;
        } else if (315 <= yaw && yaw < 360.0) {
            return BlockFace.SOUTH;
        } else {
            return BlockFace.EAST;
        }
    }

    private static BlockFace getOpenDirection(float yaw, BlockFace oldFacing) {
        BlockFace facingDirection = blockFaceFromYaw(yaw);

        if (facingDirection == oldFacing.getOppositeFace()) {
            return facingDirection;
        } else {
            return oldFacing;
        }
    }

    @Override
    public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) {
        super.placeBlock(player, state, face, holding, clickedLoc);

        MaterialData materialData = state.getData();
        if (materialData instanceof Gate) {
            Gate gate = (Gate) materialData;
            float yaw = player.getLocation().getYaw();
            gate.setFacingDirection(blockFaceFromYaw(yaw));
            state.update(true);
        } else {
            warnMaterialData(Gate.class, materialData);
        }
    }

    @Override
    protected void onOpened(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc, GlowBlockState state, MaterialData materialData) {
        if (materialData instanceof Gate) {
            Gate gate = (Gate) materialData;
            gate.setFacingDirection(getOpenDirection(player.getLocation().getYaw(), gate.getFacing()));
        } else {
            warnMaterialData(Gate.class, materialData);
        }
    }

    @Override
    public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) {
        return Arrays.asList(new ItemStack(block.getType()));
    }

    @Override
    public void onRedstoneUpdate(GlowBlock block) {
        GlowBlockState state = block.getState();
        Gate gate = (Gate) state.getData();

        boolean powered = block.isBlockIndirectlyPowered();
        if (powered != gate.isOpen()) {
            gate.setOpen(powered);
            state.update();
        }
    }
}

@@ -660,31 +660,66 @@
     
-if (0 %3C= yaw && yaw %3C 4
+System.out.println(yaw);%0A        if (yaw %3C 45 %7C%7C yaw %3E= 31
 5) %7B
@@ -740,37 +740,36 @@
 eturn BlockFace.
-SOUTH
+EAST
 ;%0A        %7D else
@@ -832,12 +832,13 @@
 ace.
-WEST
+SOUTH
 ;%0A  
@@ -914,230 +914,65 @@
 ace.
-NORTH;%0A        %7D else if (225 %3C= yaw && yaw %3C 315) %7B%0A            return BlockFace.EAST;%0A        %7D else if (315 %3C= yaw && yaw %3C 360.0) %7B%0A            return BlockFace.SOUTH;%0A        %7D else %7B%0A            return BlockFace.EAST
+WEST;%0A        %7D else %7B%0A            return BlockFace.NORTH
 ;%0A  

Fix fence yaw stuff