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 door.
8 */
9public class Door extends MaterialData implements Directional {
10 public Door() {
11 super(Material.WOODEN_DOOR);
12 }
13
14 public Door(final int type) {
15 super(type);
16 }
17
18 public Door(final Material type) {
19 super(type);
20 }
21
22 public Door(final int type, final byte data) {
23 super(type, data);
24 }
25
26 public Door(final Material type, final byte data) {
27 super(type, data);
28 }
29
30 /**
31 * Check to see if the door is open.
32 *
33 * @return true if the door has swung counterclockwise around its hinge.
34 */
35 public boolean isOpen() {
36 return ((getData() & 0x4) == 0x4);
37 }
38
39 /**
40 * Configure this door to be either open or closed;
41 *
42 * @param isOpen
43 */
44 public void setOpen(boolean isOpen) {
45 setData((byte) (isOpen ? (getData() | 0x4) : (getData() & ~0x4)));
46 }
47
48 /**
49 * @return whether this is the top half of the door
50 */
51 public boolean isTopHalf() {
52 return ((getData() & 0x8) == 0x8);
53 }
54
55 /**
56 * Configure this part of the door to be either the top or the bottom half;
57 *
58 * @param isTopHalf
59 */
60 public void setTopHalf(boolean isTopHalf) {
61 setData((byte) (isTopHalf ? (getData() | 0x8) : (getData() & ~0x8)));
62 }
63
64 /**
65 * @return the location of the hinges
66 */
67 public BlockFace getHingeCorner() {
68 byte d = getData();
69
70 if ((d & 0x3) == 0x3) {
71 return BlockFace.NORTH_WEST;
72 } else if ((d & 0x1) == 0x1) {
73 return BlockFace.SOUTH_EAST;
74 } else if ((d & 0x2) == 0x2) {
75 return BlockFace.SOUTH_WEST;
76 }
77
78 return BlockFace.NORTH_EAST;
79 }
80
81 @Override
82 public String toString() {
83 return (isTopHalf() ? "TOP" : "BOTTOM") + " half of " + (isOpen() ? "an OPEN " : "a CLOSED ") + super.toString() + " with hinges " + getHingeCorner();
84 }
85
86 /**
87 * Set the direction that this door should is facing.
88 *
89 * @param face the direction
90 */
91 public void setFacingDirection(BlockFace face) {
92 byte data = (byte) (getData() & 0x12);
93 switch (face) {
94 case EAST:
95 data |= 0x1;
96 break;
97
98 case SOUTH:
99 data |= 0x2;
100 break;
101
102 case WEST:
103 data |= 0x3;
104 break;
105 }
106 setData(data);
107 }
108
109 /**
110 * Get the direction that this door is facing.
111 *
112 * @return the direction
113 */
114 public BlockFace getFacing() {
115 byte data = (byte) (getData() & 0x3);
116 switch (data) {
117 case 0:
118 return BlockFace.NORTH;
119
120 case 1:
121 return BlockFace.EAST;
122
123 case 2:
124 return BlockFace.SOUTH;
125
126 case 3:
127 return BlockFace.WEST;
128 }
129 return null; // shouldn't happen
130 }
131}