001 package org.bukkit.material; 002 003 import org.bukkit.Material; 004 import org.bukkit.block.BlockFace; 005 006 /** 007 * Represents a fence gate 008 */ 009 public class Gate extends MaterialData implements Directional, Openable { 010 private static final byte OPEN_BIT = 0x4; 011 private static final byte DIR_BIT = 0x3; 012 private static final byte GATE_SOUTH = 0x0; 013 private static final byte GATE_WEST = 0x1; 014 private static final byte GATE_NORTH = 0x2; 015 private static final byte GATE_EAST = 0x3; 016 017 public Gate() { 018 super(Material.FENCE_GATE); 019 } 020 021 public Gate(int type, byte data){ 022 super(type, data); 023 } 024 025 public Gate(byte data) { 026 super(Material.FENCE_GATE, data); 027 } 028 029 public void setFacingDirection(BlockFace face) { 030 byte data = (byte) (getData() &~ DIR_BIT); 031 032 switch (face) { 033 default: 034 case EAST: 035 data |= GATE_SOUTH; 036 break; 037 case SOUTH: 038 data |= GATE_WEST; 039 break; 040 case WEST: 041 data |= GATE_NORTH; 042 break; 043 case NORTH: 044 data |= GATE_EAST; 045 break; 046 } 047 048 setData(data); 049 } 050 051 public BlockFace getFacing() { 052 switch (getData() & DIR_BIT) { 053 case GATE_SOUTH: 054 return BlockFace.EAST; 055 case GATE_WEST: 056 return BlockFace.SOUTH; 057 case GATE_NORTH: 058 return BlockFace.WEST; 059 case GATE_EAST: 060 return BlockFace.NORTH; 061 } 062 063 return BlockFace.EAST; 064 } 065 066 public boolean isOpen() { 067 return (getData() & OPEN_BIT) > 0; 068 } 069 070 public void setOpen(boolean isOpen) { 071 byte data = getData(); 072 073 if (isOpen) { 074 data |= OPEN_BIT; 075 } else { 076 data &= ~OPEN_BIT; 077 } 078 079 setData(data); 080 } 081 082 @Override 083 public String toString() { 084 return (isOpen() ? "OPEN " : "CLOSED ") + " facing and opening " + getFacing(); 085 } 086 087 @Override 088 public Gate clone() { 089 return (Gate) super.clone(); 090 } 091 }