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 |
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 |
private void loadBlocks() { |
97 |
|
98 |
for (int w=0; w<width; w++) { |
99 |
for (int h=0; h<height; h++) { |
100 |
int x = leftUpper.getBlockX(); |
101 |
int y = leftUpper.getBlockY(); |
102 |
int z = leftUpper.getBlockZ(); |
103 |
switch(direction) { |
104 |
case EAST: |
105 |
x -= w; |
106 |
break; |
107 |
case WEST: |
108 |
x += w; |
109 |
break; |
110 |
case NORTH: |
111 |
z += w; |
112 |
break; |
113 |
case SOUTH: |
114 |
z -= w; |
115 |
break; |
116 |
default: |
117 |
System.out.println("[SecretDoor] this should never happen"); |
118 |
} |
119 |
y -= h; |
120 |
|
121 |
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
122 |
|
123 |
blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
124 |
|
125 |
} |
126 |
} |
127 |
|
128 |
} |
129 |
|
130 |
public String toCsv() { |
131 |
StringBuilder sb = new StringBuilder(); |
132 |
sb.append( leftUpper.getWorld().getName() ); |
133 |
|
134 |
sb.append( ":" + leftUpper.getBlockX() ); |
135 |
sb.append( ":" + leftUpper.getBlockY() ); |
136 |
sb.append( ":" + leftUpper.getBlockZ() ); |
137 |
|
138 |
sb.append( ":" + direction ); |
139 |
|
140 |
sb.append( ":" + material ); |
141 |
|
142 |
sb.append( ":" + width ); |
143 |
sb.append( ":" + height ); |
144 |
sb.append( ":" + owner); |
145 |
|
146 |
return sb.toString(); |
147 |
} |
148 |
|
149 |
|
150 |
public void registerMap(Map<Location,Door> map) { |
151 |
for (int w=0; w<width; w++) { |
152 |
for (int h=0; h<height; h++) { |
153 |
map.put(blocks[w][h], this); |
154 |
} |
155 |
} |
156 |
} |
157 |
|
158 |
public void unregisterMap(Map<Location,Door> map) { |
159 |
for (int w=0; w<width; w++) { |
160 |
for (int h=0; h<height; h++) { |
161 |
map.remove( blocks[w][h] ); |
162 |
} |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
public void open(World world) { |
168 |
for (int w=0; w<width; w++) { |
169 |
for (int h=0; h<height; h++) { |
170 |
blocks[w][h].getBlock().setTypeId(0); |
171 |
} |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
public void close(World world) { |
177 |
for (int w=0; w<width; w++) { |
178 |
for (int h=0; h<height; h++) { |
179 |
blocks[w][h].getBlock().setTypeId( material ); |
180 |
} |
181 |
} |
182 |
} |
183 |
|
184 |
} |