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

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

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

revision 1526 by torben, Sun Jun 26 14:26:58 2011 UTC revision 1527 by torben, Sun Jun 26 16:01:09 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.bukkit.hoeruputils;  package dk.thoerup.bukkit.hoeruputils;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.io.IOException;
6    import java.io.RandomAccessFile;
7    
8  import java.util.HashMap;  import java.util.HashMap;
9    import java.util.LinkedList;
10  import java.util.Map;  import java.util.Map;
11    
12  import org.bukkit.Effect;  import org.bukkit.Effect;
13  import org.bukkit.Location;  import org.bukkit.Location;
14  import org.bukkit.Material;  import org.bukkit.Material;
15    import org.bukkit.Server;
16  import org.bukkit.World;  import org.bukkit.World;
17  import org.bukkit.block.Block;  import org.bukkit.block.Block;
18  import org.bukkit.entity.Player;  import org.bukkit.entity.Player;
# Line 20  import org.bukkit.plugin.Plugin; Line 27  import org.bukkit.plugin.Plugin;
27  public class SecretDoor extends BlockListener {  public class SecretDoor extends BlockListener {
28                    
29          HashMap<Location,Door> doormap = new HashMap<Location,Door>();          HashMap<Location,Door> doormap = new HashMap<Location,Door>();
30            LinkedList<Door> doors = new LinkedList<Door>();
31    
32          private PlayerHandler handler = new PlayerHandler();          private PlayerHandler handler = new PlayerHandler();
33    
# Line 27  public class SecretDoor extends BlockLis Line 35  public class SecretDoor extends BlockLis
35    
36          public SecretDoor(Plugin plugin) {          public SecretDoor(Plugin plugin) {
37                  this.plugin = plugin;                  this.plugin = plugin;
38    
39                    loadAll();
40          }          }
41                    
42          public PlayerListener getPlayerListener() {          public PlayerListener getPlayerListener() {
# Line 39  public class SecretDoor extends BlockLis Line 49  public class SecretDoor extends BlockLis
49                  if (door != null) {                  if (door != null) {
50                          event.getPlayer().sendMessage("Secret door broken");                          event.getPlayer().sendMessage("Secret door broken");
51                          door.unregisterMap(doormap);                          door.unregisterMap(doormap);
52                            doors.remove(door);
53                  }                  }
54          }          }
55    
# Line 57  public class SecretDoor extends BlockLis Line 68  public class SecretDoor extends BlockLis
68                          Door door = new Door(sign);                          Door door = new Door(sign);
69                          door.registerMap(doormap);                          door.registerMap(doormap);
70    
71                            doors.add(door);
72    
73                          event.getPlayer().sendMessage("Secret door created");                          event.getPlayer().sendMessage("Secret door created");
74                                    
75                          sign.setTypeId(0);                          sign.setTypeId(0);
76    
77                            saveAll();
78                    }
79            }
80    
81            private void saveAll() {
82                    try {
83    
84                            File f = new File( plugin.getDataFolder(), "secretdoors.csv");
85                            RandomAccessFile out = new RandomAccessFile( f, "rw" );
86                            out.setLength(0);      
87    
88                            for (Door door : doors) {
89                                    out.writeBytes( door.toCsv() );
90                                    out.writeBytes( "\n");
91                            }
92    
93                            out.close();
94                    } catch (IOException e) {
95                            System.out.println(e.getMessage() );
96                            e.printStackTrace();
97                    }
98            }
99    
100            private void loadAll() {
101                    try {
102                            File f = new File( plugin.getDataFolder(), "secretdoors.csv");
103                            RandomAccessFile in = new RandomAccessFile( f, "r" );
104    
105                            String line;
106                            while ( (line = in.readLine() ) != null ) {
107                                    Door door = new Door(line, plugin.getServer() );                
108                                    doors.add( door );
109                                    door.registerMap( doormap );    
110                            }
111                            in.close();
112                            
113                    }
114                    catch (FileNotFoundException fnfe) {
115                            return;
116                    }
117                    catch (IOException e) {
118                            System.out.println(e.getMessage() );
119                            e.printStackTrace();
120                  }                  }
121          }          }
122    
# Line 103  public class SecretDoor extends BlockLis Line 160  public class SecretDoor extends BlockLis
160                  }                  }
161          }          }
162    
163    
164          class Door {          class Door {
165                  Location leftUpper;                  Location leftUpper;
166                  Location rightUpper;                  Location rightUpper;
# Line 147  public class SecretDoor extends BlockLis Line 205  public class SecretDoor extends BlockLis
205                          rightLower.setY( rightLower.getY() - 1);                          rightLower.setY( rightLower.getY() - 1);
206                  }                  }
207    
208                    public Door(String input, Server server) {
209                            String parts[] = input.split(":");
210    
211                            World w = server.getWorld( parts[0] );
212    
213                            leftUpper  = new Location(w, Integer.parseInt(parts[1]),  Integer.parseInt(parts[2]),  Integer.parseInt(parts[3]) );
214                            rightUpper = new Location(w, Integer.parseInt(parts[4]),  Integer.parseInt(parts[5]),  Integer.parseInt(parts[6]) );
215                            leftLower  = new Location(w, Integer.parseInt(parts[7]),  Integer.parseInt(parts[8]),  Integer.parseInt(parts[9]) );
216                            rightLower = new Location(w, Integer.parseInt(parts[10]), Integer.parseInt(parts[11]), Integer.parseInt(parts[12]) );
217                            material = Integer.parseInt( parts[13] );
218                    }
219    
220                    public String toCsv() {
221                            StringBuilder sb = new StringBuilder();
222                            sb.append( leftUpper.getWorld().getName() );
223    
224                            sb.append( ":" + leftUpper.getBlockX() );
225                            sb.append( ":" + leftUpper.getBlockY() );
226                            sb.append( ":" + leftUpper.getBlockZ() );
227    
228                            sb.append( ":" + rightUpper.getBlockX() );
229                            sb.append( ":" + rightUpper.getBlockY() );
230                            sb.append( ":" + rightUpper.getBlockZ() );
231    
232    
233                            sb.append( ":" + leftLower.getBlockX() );
234                            sb.append( ":" + leftLower.getBlockY() );
235                            sb.append( ":" + leftLower.getBlockZ() );
236    
237    
238                            sb.append( ":" + rightLower.getBlockX() );
239                            sb.append( ":" + rightLower.getBlockY() );
240                            sb.append( ":" + rightLower.getBlockZ() );
241    
242                            sb.append( ":" + material );
243    
244                            return sb.toString();
245                    }
246    
247    
248                  public void registerMap(Map<Location,Door> map) {                  public void registerMap(Map<Location,Door> map) {
249                          map.put(leftUpper, this );                          map.put(leftUpper, this );
250                          map.put(rightUpper, this );                          map.put(rightUpper, this );

Legend:
Removed from v.1526  
changed lines
  Added in v.1527

  ViewVC Help
Powered by ViewVC 1.1.20