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 |
1532 |
|
22 |
torben |
1538 |
private int material; |
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 |
|
|
blocks = new Location[width][height]; |
41 |
torben |
1530 |
|
42 |
torben |
1532 |
direction = sign.getData() - 2; |
43 |
|
|
|
44 |
torben |
1530 |
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 |
torben |
1532 |
|
60 |
|
|
|
61 |
|
|
material = leftUpper.getBlock().getTypeId(); |
62 |
|
|
|
63 |
|
|
loadBlocks(); |
64 |
torben |
1534 |
|
65 |
torben |
1578 |
|
66 |
torben |
1534 |
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 |
torben |
1530 |
|
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 |
torben |
1532 |
direction = Integer.parseInt( parts[4] ); |
84 |
|
|
material = Integer.parseInt( parts[5] ); |
85 |
torben |
1533 |
|
86 |
|
|
width = Integer.parseInt( parts[6] ); |
87 |
|
|
height = Integer.parseInt( parts[7] ); |
88 |
torben |
1536 |
owner = parts[8]; |
89 |
torben |
1535 |
|
90 |
|
|
|
91 |
|
|
blocks = new Location[width][height]; |
92 |
torben |
1533 |
|
93 |
torben |
1532 |
loadBlocks(); |
94 |
torben |
1530 |
} |
95 |
torben |
1578 |
|
96 |
|
|
public void powerChange(World world, int oldCurrent, int newCurrent ) { |
97 |
|
|
if (newCurrent != 0) { |
98 |
|
|
open(world); |
99 |
|
|
} else { |
100 |
|
|
close(world); |
101 |
|
|
} |
102 |
|
|
} |
103 |
torben |
1532 |
|
104 |
|
|
private void loadBlocks() { |
105 |
|
|
|
106 |
|
|
for (int w=0; w<width; w++) { |
107 |
|
|
for (int h=0; h<height; h++) { |
108 |
|
|
int x = leftUpper.getBlockX(); |
109 |
|
|
int y = leftUpper.getBlockY(); |
110 |
|
|
int z = leftUpper.getBlockZ(); |
111 |
|
|
switch(direction) { |
112 |
|
|
case EAST: |
113 |
|
|
x -= w; |
114 |
|
|
break; |
115 |
|
|
case WEST: |
116 |
|
|
x += w; |
117 |
|
|
break; |
118 |
|
|
case NORTH: |
119 |
|
|
z += w; |
120 |
|
|
break; |
121 |
|
|
case SOUTH: |
122 |
|
|
z -= w; |
123 |
|
|
break; |
124 |
|
|
default: |
125 |
|
|
System.out.println("[SecretDoor] this should never happen"); |
126 |
|
|
} |
127 |
|
|
y -= h; |
128 |
|
|
|
129 |
|
|
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
130 |
|
|
|
131 |
|
|
blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
132 |
|
|
|
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
} |
137 |
torben |
1530 |
|
138 |
|
|
public String toCsv() { |
139 |
|
|
StringBuilder sb = new StringBuilder(); |
140 |
|
|
sb.append( leftUpper.getWorld().getName() ); |
141 |
|
|
|
142 |
|
|
sb.append( ":" + leftUpper.getBlockX() ); |
143 |
|
|
sb.append( ":" + leftUpper.getBlockY() ); |
144 |
|
|
sb.append( ":" + leftUpper.getBlockZ() ); |
145 |
torben |
1532 |
|
146 |
|
|
sb.append( ":" + direction ); |
147 |
torben |
1530 |
|
148 |
|
|
sb.append( ":" + material ); |
149 |
torben |
1533 |
|
150 |
|
|
sb.append( ":" + width ); |
151 |
|
|
sb.append( ":" + height ); |
152 |
torben |
1536 |
sb.append( ":" + owner); |
153 |
torben |
1530 |
|
154 |
|
|
return sb.toString(); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
public void registerMap(Map<Location,Door> map) { |
159 |
torben |
1532 |
for (int w=0; w<width; w++) { |
160 |
|
|
for (int h=0; h<height; h++) { |
161 |
|
|
map.put(blocks[w][h], this); |
162 |
|
|
} |
163 |
|
|
} |
164 |
torben |
1530 |
} |
165 |
|
|
|
166 |
|
|
public void unregisterMap(Map<Location,Door> map) { |
167 |
torben |
1532 |
for (int w=0; w<width; w++) { |
168 |
|
|
for (int h=0; h<height; h++) { |
169 |
|
|
map.remove( blocks[w][h] ); |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
|
173 |
torben |
1530 |
} |
174 |
|
|
|
175 |
|
|
public void open(World world) { |
176 |
torben |
1532 |
for (int w=0; w<width; w++) { |
177 |
|
|
for (int h=0; h<height; h++) { |
178 |
torben |
1540 |
blocks[w][h].getBlock().setTypeId(0, false); |
179 |
torben |
1532 |
} |
180 |
|
|
} |
181 |
|
|
|
182 |
torben |
1530 |
} |
183 |
|
|
|
184 |
|
|
public void close(World world) { |
185 |
torben |
1532 |
for (int w=0; w<width; w++) { |
186 |
|
|
for (int h=0; h<height; h++) { |
187 |
|
|
blocks[w][h].getBlock().setTypeId( material ); |
188 |
|
|
} |
189 |
|
|
} |
190 |
torben |
1530 |
} |
191 |
|
|
|
192 |
torben |
1538 |
|
193 |
|
|
/////// setters and getters |
194 |
|
|
public String getOwner() { |
195 |
|
|
return owner; |
196 |
|
|
} |
197 |
|
|
|
198 |
torben |
1535 |
} |