001    package org.bukkit.material;
002    
003    import org.bukkit.Material;
004    import org.bukkit.TreeSpecies;
005    import org.bukkit.block.BlockFace;
006    
007    /**
008     * Represents the different types of Trees.
009     */
010    public class Tree extends MaterialData {
011        public Tree() {
012            super(Material.LOG);
013        }
014    
015        public Tree(TreeSpecies species) {
016            this();
017            setSpecies(species);
018        }
019    
020        public Tree(TreeSpecies species, BlockFace dir) {
021            this();
022            setSpecies(species);
023            setDirection(dir);
024        }
025    
026        /**
027         *
028         * @deprecated Magic value
029         */
030        @Deprecated
031        public Tree(final int type) {
032            super(type);
033        }
034    
035        public Tree(final Material type) {
036            super(type);
037        }
038    
039        /**
040         *
041         * @deprecated Magic value
042         */
043        @Deprecated
044        public Tree(final int type, final byte data) {
045            super(type, data);
046        }
047    
048        /**
049         *
050         * @deprecated Magic value
051         */
052        @Deprecated
053        public Tree(final Material type, final byte data) {
054            super(type, data);
055        }
056    
057        /**
058         * Gets the current species of this tree
059         *
060         * @return TreeSpecies of this tree
061         */
062        public TreeSpecies getSpecies() {
063            return TreeSpecies.getByData((byte) (getData() & 0x3));
064        }
065    
066        /**
067         * Sets the species of this tree
068         *
069         * @param species New species of this tree
070         */
071        public void setSpecies(TreeSpecies species) {
072            setData((byte) ((getData() & 0xC) | species.getData()));
073        }
074    
075        /**
076         * Get direction of the log
077         *
078         * @return one of:
079         *     <ul>
080         *     <li>BlockFace.TOP for upright (default)
081         *     <li>BlockFace.NORTH (east-west)
082         *     <li>BlockFace.WEST (north-south)
083         *     <li>BlockFace.SELF (directionless)
084         *     </ul>
085         */
086        public BlockFace getDirection() {
087            switch ((getData() >> 2) & 0x3) {
088                case 0: // Up-down
089                default:
090                    return BlockFace.UP;
091                case 1: // North-south
092                    return BlockFace.WEST;
093                case 2: // East-west
094                    return BlockFace.NORTH;
095                case 3: // Directionless (bark on all sides)
096                    return BlockFace.SELF;
097            }
098        }
099        /**
100         * Set direction of the log
101         *
102         * @param dir - direction of end of log (BlockFace.SELF for no direction)
103         */
104        public void setDirection(BlockFace dir) {
105            int dat;
106            switch (dir) {
107                case UP:
108                case DOWN:
109                default:
110                    dat = 0;
111                    break;
112                case WEST:
113                case EAST:
114                    dat = 1;
115                    break;
116                case NORTH:
117                case SOUTH:
118                    dat = 2;
119                    break;
120                case SELF:
121                    dat = 3;
122                    break;
123            }
124            setData((byte) ((getData() & 0x3) | (dat << 2)));
125        }
126    
127        @Override
128        public String toString() {
129            return getSpecies() + " " + getDirection() + " " + super.toString();
130        }
131    
132        @Override
133        public Tree clone() {
134            return (Tree) super.clone();
135        }
136    }