001    package org.bukkit.material;
002    
003    import org.bukkit.Material;
004    import org.bukkit.block.BlockFace;
005    
006    /**
007     * Represents a bed.
008     */
009    public class Bed extends MaterialData implements Directional {
010    
011        /**
012         * Default constructor for a bed.
013         */
014        public Bed() {
015            super(Material.BED_BLOCK);
016        }
017    
018        /**
019         * Instantiate a bed facing in a particular direction.
020         *
021         * @param direction the direction the bed's head is facing
022         */
023        public Bed(BlockFace direction) {
024            this();
025            setFacingDirection(direction);
026        }
027    
028        /**
029         *
030         * @deprecated Magic value
031         */
032        @Deprecated
033        public Bed(final int type) {
034            super(type);
035        }
036    
037        public Bed(final Material type) {
038            super(type);
039        }
040    
041        /**
042         *
043         * @deprecated Magic value
044         */
045        @Deprecated
046        public Bed(final int type, final byte data) {
047            super(type, data);
048        }
049    
050        /**
051         *
052         * @deprecated Magic value
053         */
054        @Deprecated
055        public Bed(final Material type, final byte data) {
056            super(type, data);
057        }
058    
059        /**
060         * Determine if this block represents the head of the bed
061         *
062         * @return true if this is the head of the bed, false if it is the foot
063         */
064        public boolean isHeadOfBed() {
065            return (getData() & 0x8) == 0x8;
066        }
067    
068        /**
069         * Configure this to be either the head or the foot of the bed
070         *
071         * @param isHeadOfBed True to make it the head.
072         */
073        public void setHeadOfBed(boolean isHeadOfBed) {
074            setData((byte) (isHeadOfBed ? (getData() | 0x8) : (getData() & ~0x8)));
075        }
076    
077        /**
078         * Set which direction the head of the bed is facing. Note that this will
079         * only affect one of the two blocks the bed is made of.
080         */
081        public void setFacingDirection(BlockFace face) {
082            byte data;
083    
084            switch (face) {
085            case SOUTH:
086                data = 0x0;
087                break;
088    
089            case WEST:
090                data = 0x1;
091                break;
092    
093            case NORTH:
094                data = 0x2;
095                break;
096    
097            case EAST:
098            default:
099                data = 0x3;
100            }
101    
102            if (isHeadOfBed()) {
103                data |= 0x8;
104            }
105    
106            setData(data);
107        }
108    
109        /**
110         * Get the direction that this bed's head is facing toward
111         *
112         * @return the direction the head of the bed is facing
113         */
114        public BlockFace getFacing() {
115            byte data = (byte) (getData() & 0x7);
116    
117            switch (data) {
118            case 0x0:
119                return BlockFace.SOUTH;
120    
121            case 0x1:
122                return BlockFace.WEST;
123    
124            case 0x2:
125                return BlockFace.NORTH;
126    
127            case 0x3:
128            default:
129                return BlockFace.EAST;
130            }
131        }
132    
133        @Override
134        public String toString() {
135            return (isHeadOfBed() ? "HEAD" : "FOOT") + " of " + super.toString() + " facing " + getFacing();
136        }
137    
138        @Override
139        public Bed clone() {
140            return (Bed) super.clone();
141        }
142    }