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 bed.
8 */
9public class Bed extends MaterialData implements Directional {
10
11 /**
12 * Default constructor for a bed.
13 */
14 public Bed() {
15 super(Material.BED_BLOCK);
16 }
17
18 /**
19 * Instantiate a bed facing in a particular direction.
20 *
21 * @param direction the direction the bed's head is facing
22 */
23 public Bed(BlockFace direction) {
24 this();
25 setFacingDirection(direction);
26 }
27
28 public Bed(final int type) {
29 super(type);
30 }
31
32 public Bed(final Material type) {
33 super(type);
34 }
35
36 public Bed(final int type, final byte data) {
37 super(type, data);
38 }
39
40 public Bed(final Material type, final byte data) {
41 super(type, data);
42 }
43
44 /**
45 * Determine if this block represents the head of the bed
46 *
47 * @return true if this is the head of the bed, false if it is the foot
48 */
49 public boolean isHeadOfBed() {
50 return (getData() & 0x8) == 0x8;
51 }
52
53 /**
54 * Configure this to be either the head or the foot of the bed
55 *
56 * @param isHeadOfBed
57 */
58 public void setHeadOfBed(boolean isHeadOfBed) {
59 setData((byte) (isHeadOfBed ? (getData() | 0x8) : (getData() & ~0x8)));
60 }
61
62 /**
63 * Set which direction the head of the bed is facing. Note that this will
64 * only affect one of the two blocks the bed is made of.
65 */
66 public void setFacingDirection(BlockFace face) {
67 byte data;
68
69 switch (face) {
70 case WEST:
71 data = 0x0;
72 break;
73
74 case NORTH:
75 data = 0x1;
76 break;
77
78 case EAST:
79 data = 0x2;
80 break;
81
82 case SOUTH:
83 default:
84 data = 0x3;
85 }
86
87 if (isHeadOfBed()) {
88 data |= 0x8;
89 }
90
91 setData(data);
92 }
93
94 /**
95 * Get the direction that this bed's head is facing toward
96 *
97 * @return the direction the head of the bed is facing
98 */
99 public BlockFace getFacing() {
100 byte data = (byte) (getData() & 0x7);
101
102 switch (data) {
103 case 0x0:
104 return BlockFace.WEST;
105
106 case 0x1:
107 return BlockFace.NORTH;
108
109 case 0x2:
110 return BlockFace.EAST;
111
112 case 0x3:
113 default:
114 return BlockFace.SOUTH;
115 }
116 }
117
118 @Override
119 public String toString() {
120 return (isHeadOfBed() ? "HEAD" : "FOOT") + " of " + super.toString() + " facing " + getFacing();
121 }
122}