3 |
import java.util.Map; |
import java.util.Map; |
4 |
|
|
5 |
import org.bukkit.Location; |
import org.bukkit.Location; |
6 |
import org.bukkit.Server; |
import org.bukkit.Material; |
7 |
import org.bukkit.World; |
import org.bukkit.World; |
8 |
import org.bukkit.block.Block; |
import org.bukkit.block.Block; |
9 |
|
import org.bukkit.block.BlockFace; |
10 |
|
import org.bukkit.block.BlockState; |
11 |
|
|
12 |
|
|
13 |
class Door { |
class Door { |
|
Location leftUpper; |
|
|
Location rightUpper; |
|
|
Location leftLower; |
|
|
Location rightLower; |
|
14 |
|
|
15 |
int material; |
private Location leftUpper; |
16 |
|
|
17 |
public Door(Block sign) { |
|
18 |
leftUpper = sign.getLocation().clone(); |
private Location blockLocations[][]; // [width][height] - [0][0] == leftUpper |
19 |
rightUpper = sign.getLocation().clone(); |
private BlockState blockStates[][]; |
20 |
|
|
21 |
|
private BlockFace direction; |
22 |
|
|
23 |
|
private int width; |
24 |
|
private int height; |
25 |
|
|
26 |
|
private String owner; |
27 |
|
|
28 |
|
private boolean isPrivate; |
29 |
|
|
30 |
|
private String password; |
31 |
|
|
32 |
|
|
33 |
switch ( sign.getData() ) { |
public Door(Block sign, int width, int height, DoorStorage store, String owner, boolean isPrivate, String password) throws ConflictingDoorException{ |
34 |
case 2: //facing east |
leftUpper = sign.getLocation().clone(); |
35 |
|
|
36 |
|
this.width = width; |
37 |
|
this.height = height; |
38 |
|
|
39 |
|
this.owner = owner; |
40 |
|
|
41 |
|
this.isPrivate = isPrivate; |
42 |
|
this.password = password; |
43 |
|
|
44 |
|
BlockState signState = sign.getState(); |
45 |
|
org.bukkit.material.Sign signData = (org.bukkit.material.Sign) signState.getData(); |
46 |
|
|
47 |
|
direction = signData.getFacing(); |
48 |
|
|
49 |
|
switch ( direction ) { |
50 |
|
case EAST: //facing east |
51 |
leftUpper.setZ( leftUpper.getZ() + 1 ); |
leftUpper.setZ( leftUpper.getZ() + 1 ); |
|
rightUpper.setZ( rightUpper.getZ() + 1 ); |
|
|
rightUpper.setX( rightUpper.getX() - 1 ); |
|
52 |
break; |
break; |
53 |
case 3: //facing west |
case WEST: //facing west |
54 |
leftUpper.setZ( leftUpper.getZ() - 1 ); |
leftUpper.setZ( leftUpper.getZ() - 1 ); |
|
rightUpper.setZ( rightUpper.getZ() - 1 ); |
|
|
rightUpper.setX( rightUpper.getX() + 1 ); |
|
55 |
break; |
break; |
56 |
case 4: //facing north |
case NORTH: //facing north |
57 |
leftUpper.setX( leftUpper.getX() + 1); |
leftUpper.setX( leftUpper.getX() + 1); |
|
rightUpper.setX( rightUpper.getX() + 1); |
|
|
rightUpper.setZ( rightUpper.getZ() + 1); |
|
58 |
break; |
break; |
59 |
case 5: //facing south |
case SOUTH: //facing south |
60 |
leftUpper.setX( leftUpper.getX() - 1); |
leftUpper.setX( leftUpper.getX() - 1); |
61 |
rightUpper.setX( rightUpper.getX() - 1); |
break; |
62 |
rightUpper.setZ( rightUpper.getZ() - 1); |
default: |
63 |
|
//DoNothing |
64 |
break; |
break; |
65 |
} |
} |
66 |
|
|
67 |
material = sign.getWorld().getBlockAt( leftUpper ).getTypeId(); |
|
68 |
|
|
69 |
leftLower = leftUpper.clone(); |
//material = leftUpper.getBlock().getTypeId(); |
70 |
rightLower = rightUpper.clone(); |
|
71 |
|
loadBlocks(); |
72 |
|
|
73 |
|
|
74 |
|
for (int w=0; w<width; w++) { |
75 |
|
for (int h=0; h<height; h++) { |
76 |
|
Door d = store.findDoor( blockLocations[w][h] ); |
77 |
|
if (d != null) { |
78 |
|
throw new ConflictingDoorException(); |
79 |
|
} |
80 |
|
} |
81 |
|
} |
82 |
|
|
|
leftLower.setY( leftLower.getY() - 1); |
|
|
rightLower.setY( rightLower.getY() - 1); |
|
83 |
} |
} |
84 |
|
|
85 |
|
/* |
86 |
|
@Deprecated |
87 |
public Door(String input, Server server) { |
public Door(String input, Server server) { |
88 |
String parts[] = input.split(":"); |
String parts[] = input.split(":"); |
89 |
|
|
90 |
World w = server.getWorld( parts[0] ); |
World w = server.getWorld( parts[0] ); |
91 |
|
|
92 |
leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) ); |
leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) ); |
93 |
rightUpper = new Location(w, Integer.parseInt(parts[4]), Integer.parseInt(parts[5]), Integer.parseInt(parts[6]) ); |
direction = Integer.parseInt( parts[4] ); |
94 |
leftLower = new Location(w, Integer.parseInt(parts[7]), Integer.parseInt(parts[8]), Integer.parseInt(parts[9]) ); |
//material = Integer.parseInt( parts[5] ); |
95 |
rightLower = new Location(w, Integer.parseInt(parts[10]), Integer.parseInt(parts[11]), Integer.parseInt(parts[12]) ); |
|
96 |
material = Integer.parseInt( parts[13] ); |
width = Integer.parseInt( parts[6] ); |
97 |
|
height = Integer.parseInt( parts[7] ); |
98 |
|
owner = parts[8]; |
99 |
|
|
100 |
|
|
101 |
|
loadBlocks(); |
102 |
|
}*/ |
103 |
|
|
104 |
|
public Door(Location loc, BlockFace direction, int width, int height, String owner, boolean isPrivate, String password) { |
105 |
|
leftUpper = loc; |
106 |
|
this.direction = direction; |
107 |
|
this.width = width; |
108 |
|
this.height = height; |
109 |
|
this.owner = owner; |
110 |
|
this.isPrivate = isPrivate; |
111 |
|
this.password = password; |
112 |
|
|
113 |
|
loadBlocks(); |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
public void powerChange(World world, int oldCurrent, int newCurrent ) { |
119 |
|
if (isPowered() == true) { |
120 |
|
open(); |
121 |
|
} else { |
122 |
|
close(); |
123 |
|
} |
124 |
} |
} |
125 |
|
|
126 |
public String toCsv() { |
public boolean isPowered() { |
127 |
|
for (int w=0; w<width; w++) { |
128 |
|
for (int h=0; h<height; h++) { |
129 |
|
Block b = blockLocations[w][h].getBlock(); |
130 |
|
|
131 |
|
if ( b.isBlockPowered() || b.isBlockIndirectlyPowered() ) |
132 |
|
return true; |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
return false; |
137 |
|
} |
138 |
|
|
139 |
|
private void loadBlocks() { |
140 |
|
blockLocations = new Location[width][height]; |
141 |
|
blockStates = new BlockState[width][height]; |
142 |
|
|
143 |
|
for (int w=0; w<width; w++) { |
144 |
|
for (int h=0; h<height; h++) { |
145 |
|
int x = leftUpper.getBlockX(); |
146 |
|
int y = leftUpper.getBlockY(); |
147 |
|
int z = leftUpper.getBlockZ(); |
148 |
|
switch(direction) { |
149 |
|
case EAST: |
150 |
|
x -= w; |
151 |
|
break; |
152 |
|
case WEST: |
153 |
|
x += w; |
154 |
|
break; |
155 |
|
case NORTH: |
156 |
|
z += w; |
157 |
|
break; |
158 |
|
case SOUTH: |
159 |
|
z -= w; |
160 |
|
break; |
161 |
|
default: |
162 |
|
System.out.println("[SecretDoor] this should never happen"); |
163 |
|
} |
164 |
|
y -= h; |
165 |
|
|
166 |
|
//System.out.println("Add block: " + x + ":" + y + ":" + z); |
167 |
|
|
168 |
|
blockLocations[w][h] = new Location( leftUpper.getWorld(), x, y, z); |
169 |
|
|
170 |
|
blockStates[w][h] = blockLocations[w][h].getBlock().getState(); |
171 |
|
|
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
} |
176 |
|
|
177 |
|
/* public String toCsv() { |
178 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
179 |
sb.append( leftUpper.getWorld().getName() ); |
sb.append( leftUpper.getWorld().getName() ); |
180 |
|
|
181 |
sb.append( ":" + leftUpper.getBlockX() ); |
sb.append( ":" + leftUpper.getBlockX() ); |
182 |
sb.append( ":" + leftUpper.getBlockY() ); |
sb.append( ":" + leftUpper.getBlockY() ); |
183 |
sb.append( ":" + leftUpper.getBlockZ() ); |
sb.append( ":" + leftUpper.getBlockZ() ); |
184 |
|
|
185 |
|
sb.append( ":" + direction ); |
186 |
|
|
187 |
sb.append( ":" + rightUpper.getBlockX() ); |
sb.append( ":" + material[0][0] ); |
188 |
sb.append( ":" + rightUpper.getBlockY() ); |
|
189 |
sb.append( ":" + rightUpper.getBlockZ() ); |
sb.append( ":" + width ); |
190 |
|
sb.append( ":" + height ); |
191 |
|
sb.append( ":" + owner); |
192 |
|
|
193 |
|
return sb.toString(); |
194 |
|
}*/ |
195 |
|
|
|
sb.append( ":" + leftLower.getBlockX() ); |
|
|
sb.append( ":" + leftLower.getBlockY() ); |
|
|
sb.append( ":" + leftLower.getBlockZ() ); |
|
196 |
|
|
197 |
|
public void registerMap(Map<Location,Door> map) { |
198 |
|
for (int w=0; w<width; w++) { |
199 |
|
for (int h=0; h<height; h++) { |
200 |
|
map.put(blockLocations[w][h], this); |
201 |
|
} |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
sb.append( ":" + rightLower.getBlockX() ); |
public void unregisterMap(Map<Location,Door> map) { |
206 |
sb.append( ":" + rightLower.getBlockY() ); |
for (int w=0; w<width; w++) { |
207 |
sb.append( ":" + rightLower.getBlockZ() ); |
for (int h=0; h<height; h++) { |
208 |
|
map.remove( blockLocations[w][h] ); |
209 |
|
} |
210 |
|
} |
211 |
|
|
212 |
sb.append( ":" + material ); |
} |
213 |
|
|
214 |
|
public void open() { |
215 |
|
for (int w=0; w<width; w++) { |
216 |
|
for (int h=0; h<height; h++) { |
217 |
|
//blocks[w][h].getBlock().setTypeId( 0, false); |
218 |
|
blockLocations[w][h].getBlock().setType( Material.AIR ); |
219 |
|
} |
220 |
|
} |
221 |
|
|
|
return sb.toString(); |
|
222 |
} |
} |
223 |
|
|
224 |
|
public void close() { |
225 |
|
for (int w=0; w<width; w++) { |
226 |
|
for (int h=0; h<height; h++) { |
227 |
|
blockStates[w][h].update(true, true); |
228 |
|
} |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
public void registerMap(Map<Location,Door> map) { |
|
233 |
map.put(leftUpper, this ); |
/////// setters and getters |
234 |
map.put(rightUpper, this ); |
public String getOwner() { |
235 |
map.put(leftLower, this ); |
return owner; |
|
map.put(rightLower, this ); |
|
236 |
} |
} |
237 |
|
|
238 |
public void unregisterMap(Map<Location,Door> map) { |
public Location getLeftUpper() { |
239 |
map.remove(leftUpper); |
return leftUpper; |
240 |
map.remove(rightUpper); |
} |
241 |
map.remove(leftLower); |
|
242 |
map.remove(rightLower); |
public int getWidth() { |
243 |
|
return width; |
244 |
|
} |
245 |
|
|
246 |
|
public int getHeight() { |
247 |
|
return height; |
248 |
} |
} |
249 |
|
|
250 |
public void open(World world) { |
public BlockFace getDirection() { |
251 |
world.getBlockAt( leftUpper ).setTypeId( 0 ); |
return direction; |
|
world.getBlockAt( rightUpper ).setTypeId( 0 ); |
|
|
world.getBlockAt( leftLower ).setTypeId( 0 ); |
|
|
world.getBlockAt( rightLower ).setTypeId( 0 ); |
|
252 |
} |
} |
253 |
|
|
254 |
public void close(World world) { |
public boolean isPrivate() { |
255 |
world.getBlockAt( leftUpper ).setTypeId( material ); |
return isPrivate; |
256 |
world.getBlockAt( rightUpper ).setTypeId( material ); |
} |
257 |
world.getBlockAt( leftLower ).setTypeId( material ); |
|
258 |
world.getBlockAt( rightLower ).setTypeId( material ); |
public String getPassword() { |
259 |
|
return password; |
260 |
} |
} |
261 |
|
|
|
} |
|
262 |
|
} |