Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.material;
2
3import org.bukkit.Material;
4import org.bukkit.block.BlockFace;
5
6/**
7 * Represents a lever
8 */
9public class Lever extends SimpleAttachableMaterialData implements Redstone {
10 public Lever() {
11 super(Material.LEVER);
12 }
13
14 public Lever(final int type) {
15 super(type);
16 }
17
18 public Lever(final Material type) {
19 super(type);
20 }
21
22 public Lever(final int type, final byte data) {
23 super(type, data);
24 }
25
26 public Lever(final Material type, final byte data) {
27 super(type, data);
28 }
29
30 /**
31 * Gets the current state of this Material, indicating if it's powered or
32 * unpowered
33 *
34 * @return true if powered, otherwise false
35 */
36 public boolean isPowered() {
37 return (getData() & 0x8) == 0x8;
38 }
39
40 /**
41 * Set this lever to be powered or not.
42 *
43 * @param isPowered whether the lever should be powered or not
44 */
45 public void setPowered(boolean isPowered) {
46 setData((byte) (isPowered ? (getData() | 0x8) : (getData() & ~0x8)));
47 }
48
49 /**
50 * Gets the face that this block is attached on
51 *
52 * @return BlockFace attached to
53 */
54 public BlockFace getAttachedFace() {
55 byte data = (byte) (getData() & 0x7);
56
57 switch (data) {
58 case 0x1:
59 return BlockFace.NORTH;
60
61 case 0x2:
62 return BlockFace.SOUTH;
63
64 case 0x3:
65 return BlockFace.EAST;
66
67 case 0x4:
68 return BlockFace.WEST;
69
70 case 0x5:
71 case 0x6:
72 return BlockFace.DOWN;
73 }
74
75 return null;
76 }
77
78 /**
79 * Sets the direction this lever is pointing in
80 */
81 public void setFacingDirection(BlockFace face) {
82 byte data = (byte) (getData() & 0x8);
83
84 if (getAttachedFace() == BlockFace.DOWN) {
85 switch (face) {
86 case WEST:
87 case EAST:
88 data |= 0x5;
89 break;
90
91 case SOUTH:
92 case NORTH:
93 data |= 0x6;
94 break;
95 }
96 } else {
97 switch (face) {
98 case SOUTH:
99 data |= 0x1;
100 break;
101
102 case NORTH:
103 data |= 0x2;
104 break;
105
106 case WEST:
107 data |= 0x3;
108 break;
109
110 case EAST:
111 data |= 0x4;
112 break;
113 }
114 }
115 setData(data);
116 }
117
118 @Override
119 public String toString() {
120 return super.toString() + " facing " + getFacing() + " " + (isPowered() ? "" : "NOT ") + "POWERED";
121 }
122}