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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20