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