1 |
package dk.thoerup.bukkit.hoeruputils.secretdoor; |
2 |
|
3 |
import java.util.Map; |
4 |
|
5 |
import org.bukkit.Location; |
6 |
import org.bukkit.Server; |
7 |
import org.bukkit.World; |
8 |
import org.bukkit.block.Block; |
9 |
|
10 |
|
11 |
class Door { |
12 |
final static int EAST = 0; |
13 |
final static int WEST = 1; |
14 |
final static int NORTH = 2; |
15 |
final static int SOUTH = 3; |
16 |
|
17 |
private Location leftUpper; |
18 |
|
19 |
|
20 |
private Location blocks[][]; // [width][height] - [0][0] == leftUpper |
21 |
private int material[][]; |
22 |
private byte data[][]; |
23 |
|
24 |
private int direction; |
25 |
|
26 |
private int width; |
27 |
private int height; |
28 |
|
29 |
private String owner; |
30 |
|
31 |
|
32 |
public Door(Block sign, int width, int height, DoorStorage store, String owner) throws ConflictingDoorException{ |
33 |
leftUpper = sign.getLocation().clone(); |
34 |
|
35 |
this.width = width; |
36 |
this.height = height; |
37 |
|
38 |
this.owner = owner; |
39 |
|
40 |
|
41 |
direction = sign.getData() - 2; |
42 |
|
43 |
switch ( sign.getData() ) { |
44 |
case 2: //facing east |
45 |
leftUpper.setZ( leftUpper.getZ() + 1 ); |
46 |
break; |
47 |
case 3: //facing west |
48 |
leftUpper.setZ( leftUpper.getZ() - 1 ); |
49 |
break; |
50 |
case 4: //facing north |
51 |
leftUpper.setX( leftUpper.getX() + 1); |
52 |
break; |
53 |
case 5: //facing south |
54 |
leftUpper.setX( leftUpper.getX() - 1); |
55 |
break; |
56 |
} |
57 |
|
58 |
|
59 |
|
60 |
//material = leftUpper.getBlock().getTypeId(); |
61 |
|
62 |
loadBlocks(); |
63 |
|
64 |
|
65 |
for (int w=0; w<width; w++) { |
66 |
for (int h=0; h<height; h++) { |
67 |
Door d = store.findDoor( blocks[w][h] ); |
68 |
if (d != null) { |
69 |
throw new ConflictingDoorException(); |
70 |
} |
71 |
} |
72 |
} |
73 |
|
74 |
} |
75 |
|
76 |
/* |
77 |
@Deprecated |
78 |
public Door(String input, Server server) { |
79 |
String parts[] = input.split(":"); |
80 |
|
81 |
World w = server.getWorld( parts[0] ); |
82 |
|
83 |
leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) ); |
84 |
direction = Integer.parseInt( parts[4] ); |
85 |
//material = Integer.parseInt( parts[5] ); |
86 |
|
87 |
width = Integer.parseInt( parts[6] ); |
88 |
height = Integer.parseInt( parts[7] ); |
89 |
owner = parts[8]; |
90 |
|
91 |
|
92 |
loadBlocks(); |
93 |
}*/ |
94 |
|
95 |
public Door(Location loc, int direction, int width, int height, String owner) { |
96 |
leftUpper = loc; |
97 |
this.direction = direction; |
98 |
this.width = width; |
99 |
this.height = height; |
100 |
this.owner = owner; |
101 |
|
102 |
loadBlocks(); |
103 |
} |
104 |
|
105 |
|
106 |
|
107 |
public void powerChange(World world, int oldCurrent, int newCurrent ) { |
108 |
if (isPowered() == true) { |
109 |
open(); |
110 |
} else { |
111 |
close(); |
112 |
} |
113 |
} |
114 |
|
115 |
public boolean isPowered() { |
116 |
for (int w=0; w<width; w++) { |
117 |
for (int h=0; h<height; h++) { |
118 |
Block b = blocks[w][h].getBlock(); |
119 |
|
120 |
if ( b.isBlockPowered() || b.isBlockIndirectlyPowered() ) |
121 |
return true; |
122 |
} |
123 |
} |
124 |
|
125 |
return false; |
126 |
} |
127 |
|
128 |
private void loadBlocks() { |
129 |
blocks = new Location[width][height]; |
130 |
material = new int[width][height]; |
131 |
data = new byte[width][height]; |
132 |
|
133 |
for (int w=0; w<width; w++) { |
134 |
for (int h=0; h<height; h++) { |
135 |
int x = leftUpper.getBlockX(); |
136 |
int y = leftUpper.getBlockY(); |
137 |
int z = leftUpper.getBlockZ(); |
138 |
switch(direction) { |
139 |
case EAST: |
140 |
x -= w; |
141 |
break; |
142 |
case WEST: |
143 |
x += w; |
144 |
break; |
145 |
case NORTH: |
146 |
z += w; |
147 |
break; |
148 |
case SOUTH: |
149 |
z -= w; |
150 |
break; |
151 |
default: |
152 |
System.out.println("[SecretDoor] this should never happen"); |
153 |
} |
154 |
y -= h; |
155 |
|
156 |
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
157 |
|
158 |
blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
159 |
|
160 |
material[w][h] = blocks[w][h].getBlock().getTypeId(); |
161 |
data[w][h] = blocks[w][h].getBlock().getData(); |
162 |
|
163 |
} |
164 |
} |
165 |
|
166 |
} |
167 |
|
168 |
/* public String toCsv() { |
169 |
StringBuilder sb = new StringBuilder(); |
170 |
sb.append( leftUpper.getWorld().getName() ); |
171 |
|
172 |
sb.append( ":" + leftUpper.getBlockX() ); |
173 |
sb.append( ":" + leftUpper.getBlockY() ); |
174 |
sb.append( ":" + leftUpper.getBlockZ() ); |
175 |
|
176 |
sb.append( ":" + direction ); |
177 |
|
178 |
sb.append( ":" + material[0][0] ); |
179 |
|
180 |
sb.append( ":" + width ); |
181 |
sb.append( ":" + height ); |
182 |
sb.append( ":" + owner); |
183 |
|
184 |
return sb.toString(); |
185 |
}*/ |
186 |
|
187 |
|
188 |
public void registerMap(Map<Location,Door> map) { |
189 |
for (int w=0; w<width; w++) { |
190 |
for (int h=0; h<height; h++) { |
191 |
map.put(blocks[w][h], this); |
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
public void unregisterMap(Map<Location,Door> map) { |
197 |
for (int w=0; w<width; w++) { |
198 |
for (int h=0; h<height; h++) { |
199 |
map.remove( blocks[w][h] ); |
200 |
} |
201 |
} |
202 |
|
203 |
} |
204 |
|
205 |
public void open() { |
206 |
for (int w=0; w<width; w++) { |
207 |
for (int h=0; h<height; h++) { |
208 |
blocks[w][h].getBlock().setTypeId(0, false); |
209 |
} |
210 |
} |
211 |
|
212 |
} |
213 |
|
214 |
public void close() { |
215 |
for (int w=0; w<width; w++) { |
216 |
for (int h=0; h<height; h++) { |
217 |
blocks[w][h].getBlock().setTypeIdAndData( material[w][h], data[w][h], true ); |
218 |
} |
219 |
} |
220 |
} |
221 |
|
222 |
|
223 |
/////// setters and getters |
224 |
public String getOwner() { |
225 |
return owner; |
226 |
} |
227 |
|
228 |
public Location getLeftUpper() { |
229 |
return leftUpper; |
230 |
} |
231 |
|
232 |
public int getWidth() { |
233 |
return width; |
234 |
} |
235 |
|
236 |
public int getHeight() { |
237 |
return height; |
238 |
} |
239 |
|
240 |
public int getDirection() { |
241 |
return direction; |
242 |
} |
243 |
|
244 |
} |