001    package org.bukkit.material;
002    
003    import org.bukkit.block.BlockFace;
004    import org.bukkit.Material;
005    
006    /**
007     * Represents a button
008     */
009    public class Button extends SimpleAttachableMaterialData implements Redstone {
010        public Button() {
011            super(Material.STONE_BUTTON);
012        }
013    
014        /**
015         *
016         * @deprecated Magic value
017         */
018        @Deprecated
019        public Button(final int type) {
020            super(type);
021        }
022    
023        public Button(final Material type) {
024            super(type);
025        }
026    
027        /**
028         *
029         * @deprecated Magic value
030         */
031        @Deprecated
032        public Button(final int type, final byte data) {
033            super(type, data);
034        }
035    
036        /**
037         *
038         * @deprecated Magic value
039         */
040        @Deprecated
041        public Button(final Material type, final byte data) {
042            super(type, data);
043        }
044    
045        /**
046         * Gets the current state of this Material, indicating if it's powered or
047         * unpowered
048         *
049         * @return true if powered, otherwise false
050         */
051        public boolean isPowered() {
052            return (getData() & 0x8) == 0x8;
053        }
054    
055        /**
056         * Sets the current state of this button
057         *
058         * @param bool
059         *            whether or not the button is powered
060         */
061        public void setPowered(boolean bool) {
062            setData((byte) (bool ? (getData() | 0x8) : (getData() & ~0x8)));
063        }
064    
065        /**
066         * Gets the face that this block is attached on
067         *
068         * @return BlockFace attached to
069         */
070        public BlockFace getAttachedFace() {
071            byte data = (byte) (getData() & 0x7);
072    
073            switch (data) {
074            case 0x1:
075                return BlockFace.WEST;
076    
077            case 0x2:
078                return BlockFace.EAST;
079    
080            case 0x3:
081                return BlockFace.NORTH;
082    
083            case 0x4:
084                return BlockFace.SOUTH;
085            }
086    
087            return null;
088        }
089    
090        /**
091         * Sets the direction this button is pointing toward
092         */
093        public void setFacingDirection(BlockFace face) {
094            byte data = (byte) (getData() & 0x8);
095    
096            switch (face) {
097            case EAST:
098                data |= 0x1;
099                break;
100    
101            case WEST:
102                data |= 0x2;
103                break;
104    
105            case SOUTH:
106                data |= 0x3;
107                break;
108    
109            case NORTH:
110                data |= 0x4;
111                break;
112            }
113    
114            setData(data);
115        }
116    
117        @Override
118        public String toString() {
119            return super.toString() + " " + (isPowered() ? "" : "NOT ") + "POWERED";
120        }
121    
122        @Override
123        public Button clone() {
124            return (Button) super.clone();
125        }
126    }