001 package org.bukkit.material;
002
003 import org.bukkit.Material;
004 import org.bukkit.block.BlockFace;
005
006 /**
007 * Represents minecart rails.
008 */
009 public class Rails extends MaterialData {
010
011 public Rails() {
012 super(Material.RAILS);
013 }
014
015 /**
016 *
017 * @deprecated Magic value
018 */
019 @Deprecated
020 public Rails(final int type) {
021 super(type);
022 }
023
024 public Rails(final Material type) {
025 super(type);
026 }
027
028 /**
029 *
030 * @deprecated Magic value
031 */
032 @Deprecated
033 public Rails(final int type, final byte data) {
034 super(type, data);
035 }
036
037 /**
038 *
039 * @deprecated Magic value
040 */
041 @Deprecated
042 public Rails(final Material type, final byte data) {
043 super(type, data);
044 }
045
046 /**
047 * @return the whether this track is set on a slope
048 */
049 public boolean isOnSlope() {
050 byte d = getConvertedData();
051
052 return (d == 0x2 || d == 0x3 || d == 0x4 || d == 0x5);
053 }
054
055 /**
056 * @return the whether this track is set as a curve
057 */
058 public boolean isCurve() {
059 byte d = getConvertedData();
060
061 return (d == 0x6 || d == 0x7 || d == 0x8 || d == 0x9);
062 }
063
064 /**
065 * @return the direction these tracks are set
066 * <p>
067 * Note that tracks are bidirectional and that the direction returned
068 * is the ascending direction if the track is set on a slope. If it is
069 * set as a curve, the corner of the track is returned.
070 */
071 public BlockFace getDirection() {
072 byte d = getConvertedData();
073
074 switch (d) {
075 case 0x0:
076 default:
077 return BlockFace.SOUTH;
078
079 case 0x1:
080 return BlockFace.EAST;
081
082 case 0x2:
083 return BlockFace.EAST;
084
085 case 0x3:
086 return BlockFace.WEST;
087
088 case 0x4:
089 return BlockFace.NORTH;
090
091 case 0x5:
092 return BlockFace.SOUTH;
093
094 case 0x6:
095 return BlockFace.NORTH_WEST;
096
097 case 0x7:
098 return BlockFace.NORTH_EAST;
099
100 case 0x8:
101 return BlockFace.SOUTH_EAST;
102
103 case 0x9:
104 return BlockFace.SOUTH_WEST;
105 }
106 }
107
108 @Override
109 public String toString() {
110 return super.toString() + " facing " + getDirection() + (isCurve() ? " on a curve" : (isOnSlope() ? " on a slope" : ""));
111 }
112
113 /**
114 * Return the data without the extended properties used by {@link
115 * PoweredRail} and {@link DetectorRail}. Overridden in {@link
116 * ExtendedRails}
117 *
118 * @return the data without the extended part
119 * @deprecated Magic value
120 */
121 @Deprecated
122 protected byte getConvertedData() {
123 return getData();
124 }
125
126 /**
127 * Set the direction of these tracks
128 * <p>
129 * Note that tracks are bidirectional and that the direction returned is
130 * the ascending direction if the track is set on a slope. If it is set as
131 * a curve, the corner of the track should be supplied.
132 *
133 * @param face the direction the track should be facing
134 * @param isOnSlope whether or not the track should be on a slope
135 */
136 public void setDirection(BlockFace face, boolean isOnSlope) {
137 switch (face) {
138 case EAST:
139 setData((byte) (isOnSlope ? 0x2 : 0x1));
140 break;
141
142 case WEST:
143 setData((byte) (isOnSlope ? 0x3 : 0x1));
144 break;
145
146 case NORTH:
147 setData((byte) (isOnSlope ? 0x4 : 0x0));
148 break;
149
150 case SOUTH:
151 setData((byte) (isOnSlope ? 0x5 : 0x0));
152 break;
153
154 case NORTH_WEST:
155 setData((byte) 0x6);
156 break;
157
158 case NORTH_EAST:
159 setData((byte) 0x7);
160 break;
161
162 case SOUTH_EAST:
163 setData((byte) 0x8);
164 break;
165
166 case SOUTH_WEST:
167 setData((byte) 0x9);
168 break;
169 }
170 }
171
172 @Override
173 public Rails clone() {
174 return (Rails) super.clone();
175 }
176 }