Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
Mushroom.java
1 package org.bukkit.material;
2 
3 import java.util.EnumSet;
4 import java.util.Set;
5 
6 import org.apache.commons.lang.Validate;
7 import org.bukkit.Material;
9 
10 /**
11  * Represents a huge mushroom block
12  */
13 public class Mushroom extends MaterialData {
14  private static final byte SHROOM_NONE = 0;
15  private static final byte SHROOM_STEM = 10;
16  private static final byte NORTH_LIMIT = 4;
17  private static final byte SOUTH_LIMIT = 6;
18  private static final byte EAST_WEST_LIMIT = 3;
19  private static final byte EAST_REMAINDER = 0;
20  private static final byte WEST_REMAINDER = 1;
21  private static final byte NORTH_SOUTH_MOD = 3;
22  private static final byte EAST_WEST_MOD = 1;
23 
24  public Mushroom(Material shroom) {
25  super(shroom);
26  Validate.isTrue(shroom == Material.HUGE_MUSHROOM_1 || shroom == Material.HUGE_MUSHROOM_2, "Not a mushroom!");
27  }
28 
29  /**
30  *
31  * @deprecated Magic value
32  */
33  @Deprecated
34  public Mushroom(Material shroom, byte data) {
35  super(shroom, data);
36  Validate.isTrue(shroom == Material.HUGE_MUSHROOM_1 || shroom == Material.HUGE_MUSHROOM_2, "Not a mushroom!");
37  }
38 
39  /**
40  *
41  * @deprecated Magic value
42  */
43  @Deprecated
44  public Mushroom(int type, byte data){
45  super(type, data);
46  Validate.isTrue(type == Material.HUGE_MUSHROOM_1.getId() || type == Material.HUGE_MUSHROOM_2.getId(), "Not a mushroom!");
47  }
48 
49  /**
50  * @return Whether this is a mushroom stem.
51  */
52  public boolean isStem() {
53  return getData() == SHROOM_STEM;
54  }
55 
56  /**
57  * Sets this to be a mushroom stem.
58  */
59  public void setStem() {
60  setData((byte) 10);
61  }
62 
63  /**
64  * Checks whether a face of the block is painted.
65  *
66  * @param face The face to check.
67  * @return True if it is painted.
68  */
69  public boolean isFacePainted(BlockFace face) {
70  byte data = getData();
71 
72  if (data == SHROOM_NONE || data == SHROOM_STEM) {
73  return false;
74  }
75 
76  switch (face) {
77  case WEST:
78  return data < NORTH_LIMIT;
79  case EAST:
80  return data > SOUTH_LIMIT;
81  case NORTH:
82  return data % EAST_WEST_LIMIT == EAST_REMAINDER;
83  case SOUTH:
84  return data % EAST_WEST_LIMIT == WEST_REMAINDER;
85  case UP:
86  return true;
87  default:
88  return false;
89  }
90  }
91 
92  /**
93  * Set a face of the block to be painted or not. Note that due to the
94  * nature of how the data is stored, setting a face painted or not is not
95  * guaranteed to leave the other faces unchanged.
96  *
97  * @param face The face to paint or unpaint.
98  * @param painted True if you want to paint it, false if you want the
99  * pores to show.
100  */
101  public void setFacePainted(BlockFace face, boolean painted) {
102  if (painted == isFacePainted(face)) {
103  return;
104  }
105 
106  byte data = getData();
107 
108  if (data == SHROOM_STEM) {
109  data = 5;
110  }
111 
112  switch (face) {
113  case WEST:
114  if (painted) {
115  data -= NORTH_SOUTH_MOD;
116  } else {
117  data += NORTH_SOUTH_MOD;
118  }
119 
120  break;
121  case EAST:
122  if (painted) {
123  data += NORTH_SOUTH_MOD;
124  } else {
125  data -= NORTH_SOUTH_MOD;
126  }
127 
128  break;
129  case NORTH:
130  if (painted) {
131  data += EAST_WEST_MOD;
132  } else {
133  data -= EAST_WEST_MOD;
134  }
135 
136  break;
137  case SOUTH:
138  if (painted) {
139  data -= EAST_WEST_MOD;
140  } else {
141  data += EAST_WEST_MOD;
142  }
143 
144  break;
145  case UP:
146  if (!painted) {
147  data = 0;
148  }
149 
150  break;
151  default:
152  throw new IllegalArgumentException("Can't paint that face of a mushroom!");
153  }
154 
155  setData(data);
156  }
157 
158  /**
159  * @return A set of all faces that are currently painted (an empty set if
160  * it is a stem)
161  */
162  public Set<BlockFace> getPaintedFaces() {
163  EnumSet<BlockFace> faces = EnumSet.noneOf(BlockFace.class);
164 
165  if (isFacePainted(BlockFace.WEST)) {
166  faces.add(BlockFace.WEST);
167  }
168 
169  if (isFacePainted(BlockFace.NORTH)) {
170  faces.add(BlockFace.NORTH);
171  }
172 
173  if (isFacePainted(BlockFace.SOUTH)) {
174  faces.add(BlockFace.SOUTH);
175  }
176 
177  if (isFacePainted(BlockFace.EAST)) {
178  faces.add(BlockFace.EAST);
179  }
180 
181  if (isFacePainted(BlockFace.UP)) {
182  faces.add(BlockFace.UP);
183  }
184 
185  return faces;
186  }
187 
188  @Override
189  public String toString() {
190  return Material.getMaterial(getItemTypeId()).toString() + (isStem() ? "{STEM}" : getPaintedFaces());
191  }
192 
193  @Override
194  public Mushroom clone() {
195  return (Mushroom) super.clone();
196  }
197 }
Set< BlockFace > getPaintedFaces()
Definition: Mushroom.java:162
Mushroom(int type, byte data)
Definition: Mushroom.java:44
static Material getMaterial(final int id)
Definition: Material.java:575
boolean isFacePainted(BlockFace face)
Definition: Mushroom.java:69
void setFacePainted(BlockFace face, boolean painted)
Definition: Mushroom.java:101
Mushroom(Material shroom, byte data)
Definition: Mushroom.java:34