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