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 |
1532 |
|
31 |
torben |
1530 |
|
32 |
torben |
1536 |
public Door(Block sign, int width, int height, DoorStorage store, String owner) throws ConflictingDoorException{ |
33 |
torben |
1530 |
leftUpper = sign.getLocation().clone(); |
34 |
torben |
1532 |
|
35 |
torben |
1533 |
this.width = width; |
36 |
|
|
this.height = height; |
37 |
torben |
1536 |
|
38 |
|
|
this.owner = owner; |
39 |
torben |
1533 |
|
40 |
torben |
1530 |
|
41 |
torben |
1532 |
direction = sign.getData() - 2; |
42 |
|
|
|
43 |
torben |
1530 |
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 |
torben |
1532 |
|
59 |
|
|
|
60 |
torben |
1581 |
//material = leftUpper.getBlock().getTypeId(); |
61 |
torben |
1532 |
|
62 |
|
|
loadBlocks(); |
63 |
torben |
1534 |
|
64 |
torben |
1578 |
|
65 |
torben |
1534 |
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 |
torben |
1530 |
|
74 |
|
|
} |
75 |
|
|
|
76 |
torben |
1604 |
/* |
77 |
|
|
@Deprecated |
78 |
torben |
1530 |
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 |
torben |
1532 |
direction = Integer.parseInt( parts[4] ); |
85 |
torben |
1581 |
//material = Integer.parseInt( parts[5] ); |
86 |
torben |
1533 |
|
87 |
|
|
width = Integer.parseInt( parts[6] ); |
88 |
|
|
height = Integer.parseInt( parts[7] ); |
89 |
torben |
1536 |
owner = parts[8]; |
90 |
torben |
1535 |
|
91 |
torben |
1533 |
|
92 |
torben |
1532 |
loadBlocks(); |
93 |
torben |
1604 |
}*/ |
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 |
torben |
1530 |
} |
104 |
torben |
1578 |
|
105 |
torben |
1604 |
|
106 |
|
|
|
107 |
torben |
1578 |
public void powerChange(World world, int oldCurrent, int newCurrent ) { |
108 |
torben |
1580 |
if (isPowered() == true) { |
109 |
torben |
1579 |
open(); |
110 |
torben |
1578 |
} else { |
111 |
torben |
1579 |
close(); |
112 |
torben |
1578 |
} |
113 |
|
|
} |
114 |
torben |
1579 |
|
115 |
|
|
public boolean isPowered() { |
116 |
|
|
for (int w=0; w<width; w++) { |
117 |
|
|
for (int h=0; h<height; h++) { |
118 |
torben |
1580 |
Block b = blocks[w][h].getBlock(); |
119 |
|
|
|
120 |
|
|
if ( b.isBlockPowered() || b.isBlockIndirectlyPowered() ) |
121 |
torben |
1579 |
return true; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
return false; |
126 |
|
|
} |
127 |
torben |
1532 |
|
128 |
|
|
private void loadBlocks() { |
129 |
torben |
1581 |
blocks = new Location[width][height]; |
130 |
|
|
material = new int[width][height]; |
131 |
|
|
data = new byte[width][height]; |
132 |
torben |
1532 |
|
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 |
torben |
1581 |
|
160 |
|
|
material[w][h] = blocks[w][h].getBlock().getTypeId(); |
161 |
|
|
data[w][h] = blocks[w][h].getBlock().getData(); |
162 |
torben |
1532 |
|
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
} |
167 |
torben |
1530 |
|
168 |
torben |
1604 |
/* public String toCsv() { |
169 |
torben |
1530 |
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 |
torben |
1532 |
|
176 |
|
|
sb.append( ":" + direction ); |
177 |
torben |
1530 |
|
178 |
torben |
1581 |
sb.append( ":" + material[0][0] ); |
179 |
torben |
1533 |
|
180 |
|
|
sb.append( ":" + width ); |
181 |
|
|
sb.append( ":" + height ); |
182 |
torben |
1536 |
sb.append( ":" + owner); |
183 |
torben |
1530 |
|
184 |
|
|
return sb.toString(); |
185 |
torben |
1604 |
}*/ |
186 |
torben |
1530 |
|
187 |
|
|
|
188 |
|
|
public void registerMap(Map<Location,Door> map) { |
189 |
torben |
1532 |
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 |
torben |
1530 |
} |
195 |
|
|
|
196 |
|
|
public void unregisterMap(Map<Location,Door> map) { |
197 |
torben |
1532 |
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 |
torben |
1530 |
} |
204 |
|
|
|
205 |
torben |
1579 |
public void open() { |
206 |
torben |
1532 |
for (int w=0; w<width; w++) { |
207 |
|
|
for (int h=0; h<height; h++) { |
208 |
torben |
1540 |
blocks[w][h].getBlock().setTypeId(0, false); |
209 |
torben |
1532 |
} |
210 |
|
|
} |
211 |
|
|
|
212 |
torben |
1530 |
} |
213 |
|
|
|
214 |
torben |
1579 |
public void close() { |
215 |
torben |
1532 |
for (int w=0; w<width; w++) { |
216 |
|
|
for (int h=0; h<height; h++) { |
217 |
torben |
1581 |
blocks[w][h].getBlock().setTypeIdAndData( material[w][h], data[w][h], true ); |
218 |
torben |
1532 |
} |
219 |
|
|
} |
220 |
torben |
1530 |
} |
221 |
|
|
|
222 |
torben |
1538 |
|
223 |
|
|
/////// setters and getters |
224 |
|
|
public String getOwner() { |
225 |
|
|
return owner; |
226 |
|
|
} |
227 |
|
|
|
228 |
torben |
1604 |
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 |
torben |
1535 |
} |