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 |
|
22 |
private int material; |
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 |
blocks = new Location[width][height]; |
41 |
|
42 |
direction = sign.getData() - 2; |
43 |
|
44 |
switch ( sign.getData() ) { |
45 |
case 2: //facing east |
46 |
leftUpper.setZ( leftUpper.getZ() + 1 ); |
47 |
break; |
48 |
case 3: //facing west |
49 |
leftUpper.setZ( leftUpper.getZ() - 1 ); |
50 |
break; |
51 |
case 4: //facing north |
52 |
leftUpper.setX( leftUpper.getX() + 1); |
53 |
break; |
54 |
case 5: //facing south |
55 |
leftUpper.setX( leftUpper.getX() - 1); |
56 |
break; |
57 |
} |
58 |
|
59 |
|
60 |
|
61 |
material = leftUpper.getBlock().getTypeId(); |
62 |
|
63 |
loadBlocks(); |
64 |
|
65 |
|
66 |
for (int w=0; w<width; w++) { |
67 |
for (int h=0; h<height; h++) { |
68 |
Door d = store.findDoor( blocks[w][h] ); |
69 |
if (d != null) { |
70 |
throw new ConflictingDoorException(); |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
} |
76 |
|
77 |
public Door(String input, Server server) { |
78 |
String parts[] = input.split(":"); |
79 |
|
80 |
World w = server.getWorld( parts[0] ); |
81 |
|
82 |
leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) ); |
83 |
direction = Integer.parseInt( parts[4] ); |
84 |
material = Integer.parseInt( parts[5] ); |
85 |
|
86 |
width = Integer.parseInt( parts[6] ); |
87 |
height = Integer.parseInt( parts[7] ); |
88 |
owner = parts[8]; |
89 |
|
90 |
|
91 |
blocks = new Location[width][height]; |
92 |
|
93 |
loadBlocks(); |
94 |
} |
95 |
|
96 |
public void powerChange(World world, int oldCurrent, int newCurrent ) { |
97 |
if (newCurrent != 0 && isPowered() == true) { |
98 |
open(); |
99 |
} else { |
100 |
close(); |
101 |
} |
102 |
} |
103 |
|
104 |
public boolean isPowered() { |
105 |
for (int w=0; w<width; w++) { |
106 |
for (int h=0; h<height; h++) { |
107 |
if (blocks[w][h].getBlock().isBlockIndirectlyPowered() == true) |
108 |
return true; |
109 |
} |
110 |
} |
111 |
|
112 |
return false; |
113 |
} |
114 |
|
115 |
private void loadBlocks() { |
116 |
|
117 |
for (int w=0; w<width; w++) { |
118 |
for (int h=0; h<height; h++) { |
119 |
int x = leftUpper.getBlockX(); |
120 |
int y = leftUpper.getBlockY(); |
121 |
int z = leftUpper.getBlockZ(); |
122 |
switch(direction) { |
123 |
case EAST: |
124 |
x -= w; |
125 |
break; |
126 |
case WEST: |
127 |
x += w; |
128 |
break; |
129 |
case NORTH: |
130 |
z += w; |
131 |
break; |
132 |
case SOUTH: |
133 |
z -= w; |
134 |
break; |
135 |
default: |
136 |
System.out.println("[SecretDoor] this should never happen"); |
137 |
} |
138 |
y -= h; |
139 |
|
140 |
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
141 |
|
142 |
blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
143 |
|
144 |
} |
145 |
} |
146 |
|
147 |
} |
148 |
|
149 |
public String toCsv() { |
150 |
StringBuilder sb = new StringBuilder(); |
151 |
sb.append( leftUpper.getWorld().getName() ); |
152 |
|
153 |
sb.append( ":" + leftUpper.getBlockX() ); |
154 |
sb.append( ":" + leftUpper.getBlockY() ); |
155 |
sb.append( ":" + leftUpper.getBlockZ() ); |
156 |
|
157 |
sb.append( ":" + direction ); |
158 |
|
159 |
sb.append( ":" + material ); |
160 |
|
161 |
sb.append( ":" + width ); |
162 |
sb.append( ":" + height ); |
163 |
sb.append( ":" + owner); |
164 |
|
165 |
return sb.toString(); |
166 |
} |
167 |
|
168 |
|
169 |
public void registerMap(Map<Location,Door> map) { |
170 |
for (int w=0; w<width; w++) { |
171 |
for (int h=0; h<height; h++) { |
172 |
map.put(blocks[w][h], this); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
public void unregisterMap(Map<Location,Door> map) { |
178 |
for (int w=0; w<width; w++) { |
179 |
for (int h=0; h<height; h++) { |
180 |
map.remove( blocks[w][h] ); |
181 |
} |
182 |
} |
183 |
|
184 |
} |
185 |
|
186 |
public void open() { |
187 |
for (int w=0; w<width; w++) { |
188 |
for (int h=0; h<height; h++) { |
189 |
blocks[w][h].getBlock().setTypeId(0, false); |
190 |
} |
191 |
} |
192 |
|
193 |
} |
194 |
|
195 |
public void close() { |
196 |
for (int w=0; w<width; w++) { |
197 |
for (int h=0; h<height; h++) { |
198 |
blocks[w][h].getBlock().setTypeId( material ); |
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
|
204 |
/////// setters and getters |
205 |
public String getOwner() { |
206 |
return owner; |
207 |
} |
208 |
|
209 |
} |