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 |
Location leftUpper; |
18 |
|
19 |
|
20 |
Location blocks[][]; // [width][height] - [0][0] == leftUpper |
21 |
|
22 |
int material; |
23 |
|
24 |
int direction; |
25 |
|
26 |
int width; |
27 |
int height; |
28 |
|
29 |
|
30 |
public Door(Block sign, int width, int height, DoorStorage store) throws ConflictingDoorException{ |
31 |
leftUpper = sign.getLocation().clone(); |
32 |
|
33 |
this.width = width; |
34 |
this.height = height; |
35 |
|
36 |
blocks = new Location[width][height]; |
37 |
|
38 |
direction = sign.getData() - 2; |
39 |
|
40 |
switch ( sign.getData() ) { |
41 |
case 2: //facing east |
42 |
leftUpper.setZ( leftUpper.getZ() + 1 ); |
43 |
break; |
44 |
case 3: //facing west |
45 |
leftUpper.setZ( leftUpper.getZ() - 1 ); |
46 |
break; |
47 |
case 4: //facing north |
48 |
leftUpper.setX( leftUpper.getX() + 1); |
49 |
break; |
50 |
case 5: //facing south |
51 |
leftUpper.setX( leftUpper.getX() - 1); |
52 |
break; |
53 |
} |
54 |
|
55 |
|
56 |
|
57 |
material = leftUpper.getBlock().getTypeId(); |
58 |
|
59 |
loadBlocks(); |
60 |
|
61 |
|
62 |
for (int w=0; w<width; w++) { |
63 |
for (int h=0; h<height; h++) { |
64 |
Door d = store.findDoor( blocks[w][h] ); |
65 |
if (d != null) { |
66 |
throw new ConflictingDoorException(); |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
} |
72 |
|
73 |
public Door(String input, Server server) { |
74 |
String parts[] = input.split(":"); |
75 |
|
76 |
World w = server.getWorld( parts[0] ); |
77 |
|
78 |
leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) ); |
79 |
direction = Integer.parseInt( parts[4] ); |
80 |
material = Integer.parseInt( parts[5] ); |
81 |
|
82 |
width = Integer.parseInt( parts[6] ); |
83 |
height = Integer.parseInt( parts[7] ); |
84 |
|
85 |
|
86 |
blocks = new Location[width][height]; |
87 |
|
88 |
loadBlocks(); |
89 |
} |
90 |
|
91 |
private void loadBlocks() { |
92 |
|
93 |
for (int w=0; w<width; w++) { |
94 |
for (int h=0; h<height; h++) { |
95 |
int x = leftUpper.getBlockX(); |
96 |
int y = leftUpper.getBlockY(); |
97 |
int z = leftUpper.getBlockZ(); |
98 |
switch(direction) { |
99 |
case EAST: |
100 |
x -= w; |
101 |
break; |
102 |
case WEST: |
103 |
x += w; |
104 |
break; |
105 |
case NORTH: |
106 |
z += w; |
107 |
break; |
108 |
case SOUTH: |
109 |
z -= w; |
110 |
break; |
111 |
default: |
112 |
System.out.println("[SecretDoor] this should never happen"); |
113 |
} |
114 |
y -= h; |
115 |
|
116 |
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
117 |
|
118 |
blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
119 |
|
120 |
} |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
public String toCsv() { |
126 |
StringBuilder sb = new StringBuilder(); |
127 |
sb.append( leftUpper.getWorld().getName() ); |
128 |
|
129 |
sb.append( ":" + leftUpper.getBlockX() ); |
130 |
sb.append( ":" + leftUpper.getBlockY() ); |
131 |
sb.append( ":" + leftUpper.getBlockZ() ); |
132 |
|
133 |
sb.append( ":" + direction ); |
134 |
|
135 |
sb.append( ":" + material ); |
136 |
|
137 |
sb.append( ":" + width ); |
138 |
sb.append( ":" + height ); |
139 |
|
140 |
return sb.toString(); |
141 |
} |
142 |
|
143 |
|
144 |
public void registerMap(Map<Location,Door> map) { |
145 |
for (int w=0; w<width; w++) { |
146 |
for (int h=0; h<height; h++) { |
147 |
map.put(blocks[w][h], this); |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
public void unregisterMap(Map<Location,Door> map) { |
153 |
for (int w=0; w<width; w++) { |
154 |
for (int h=0; h<height; h++) { |
155 |
map.remove( blocks[w][h] ); |
156 |
} |
157 |
} |
158 |
|
159 |
} |
160 |
|
161 |
public void open(World world) { |
162 |
for (int w=0; w<width; w++) { |
163 |
for (int h=0; h<height; h++) { |
164 |
blocks[w][h].getBlock().setTypeId(0); |
165 |
} |
166 |
} |
167 |
|
168 |
} |
169 |
|
170 |
public void close(World world) { |
171 |
for (int w=0; w<width; w++) { |
172 |
for (int h=0; h<height; h++) { |
173 |
blocks[w][h].getBlock().setTypeId( material ); |
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
} |