/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/secretdoor/Door.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/secretdoor/Door.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20