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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1530  
changed lines
  Added in v.1604

  ViewVC Help
Powered by ViewVC 1.1.20