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 |
public Door(String input, Server server) { |
77 |
String parts[] = input.split(":"); |
78 |
|
79 |
World w = server.getWorld( parts[0] ); |
80 |
|
81 |
leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) ); |
82 |
direction = Integer.parseInt( parts[4] ); |
83 |
//material = Integer.parseInt( parts[5] ); |
84 |
|
85 |
width = Integer.parseInt( parts[6] ); |
86 |
height = Integer.parseInt( parts[7] ); |
87 |
owner = parts[8]; |
88 |
|
89 |
|
90 |
loadBlocks(); |
91 |
} |
92 |
|
93 |
public void powerChange(World world, int oldCurrent, int newCurrent ) { |
94 |
if (isPowered() == true) { |
95 |
open(); |
96 |
} else { |
97 |
close(); |
98 |
} |
99 |
} |
100 |
|
101 |
public boolean isPowered() { |
102 |
for (int w=0; w<width; w++) { |
103 |
for (int h=0; h<height; h++) { |
104 |
Block b = blocks[w][h].getBlock(); |
105 |
|
106 |
if ( b.isBlockPowered() || b.isBlockIndirectlyPowered() ) |
107 |
return true; |
108 |
} |
109 |
} |
110 |
|
111 |
return false; |
112 |
} |
113 |
|
114 |
private void loadBlocks() { |
115 |
blocks = new Location[width][height]; |
116 |
material = new int[width][height]; |
117 |
data = new byte[width][height]; |
118 |
|
119 |
for (int w=0; w<width; w++) { |
120 |
for (int h=0; h<height; h++) { |
121 |
int x = leftUpper.getBlockX(); |
122 |
int y = leftUpper.getBlockY(); |
123 |
int z = leftUpper.getBlockZ(); |
124 |
switch(direction) { |
125 |
case EAST: |
126 |
x -= w; |
127 |
break; |
128 |
case WEST: |
129 |
x += w; |
130 |
break; |
131 |
case NORTH: |
132 |
z += w; |
133 |
break; |
134 |
case SOUTH: |
135 |
z -= w; |
136 |
break; |
137 |
default: |
138 |
System.out.println("[SecretDoor] this should never happen"); |
139 |
} |
140 |
y -= h; |
141 |
|
142 |
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
143 |
|
144 |
blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
145 |
|
146 |
material[w][h] = blocks[w][h].getBlock().getTypeId(); |
147 |
data[w][h] = blocks[w][h].getBlock().getData(); |
148 |
|
149 |
} |
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
public String toCsv() { |
155 |
StringBuilder sb = new StringBuilder(); |
156 |
sb.append( leftUpper.getWorld().getName() ); |
157 |
|
158 |
sb.append( ":" + leftUpper.getBlockX() ); |
159 |
sb.append( ":" + leftUpper.getBlockY() ); |
160 |
sb.append( ":" + leftUpper.getBlockZ() ); |
161 |
|
162 |
sb.append( ":" + direction ); |
163 |
|
164 |
sb.append( ":" + material[0][0] ); |
165 |
|
166 |
sb.append( ":" + width ); |
167 |
sb.append( ":" + height ); |
168 |
sb.append( ":" + owner); |
169 |
|
170 |
return sb.toString(); |
171 |
} |
172 |
|
173 |
|
174 |
public void registerMap(Map<Location,Door> map) { |
175 |
for (int w=0; w<width; w++) { |
176 |
for (int h=0; h<height; h++) { |
177 |
map.put(blocks[w][h], this); |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
public void unregisterMap(Map<Location,Door> map) { |
183 |
for (int w=0; w<width; w++) { |
184 |
for (int h=0; h<height; h++) { |
185 |
map.remove( blocks[w][h] ); |
186 |
} |
187 |
} |
188 |
|
189 |
} |
190 |
|
191 |
public void open() { |
192 |
for (int w=0; w<width; w++) { |
193 |
for (int h=0; h<height; h++) { |
194 |
blocks[w][h].getBlock().setTypeId(0, false); |
195 |
} |
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
public void close() { |
201 |
for (int w=0; w<width; w++) { |
202 |
for (int h=0; h<height; h++) { |
203 |
blocks[w][h].getBlock().setTypeIdAndData( material[w][h], data[w][h], true ); |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
|
209 |
/////// setters and getters |
210 |
public String getOwner() { |
211 |
return owner; |
212 |
} |
213 |
|
214 |
} |