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

Annotation of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/secretdoor/DoorStorage.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1530 - (hide annotations) (download)
Mon Jun 27 16:14:34 2011 UTC (12 years, 11 months ago) by torben
File size: 1742 byte(s)
Refactor secret door code to seperate classes
1 torben 1530 package dk.thoerup.bukkit.hoeruputils.secretdoor;
2    
3     import java.io.File;
4     import java.io.FileNotFoundException;
5     import java.io.IOException;
6     import java.io.RandomAccessFile;
7     import java.util.HashMap;
8     import java.util.LinkedList;
9    
10     import org.bukkit.Location;
11     import org.bukkit.plugin.Plugin;
12    
13     public class DoorStorage {
14     private HashMap<Location,Door> doormap = new HashMap<Location,Door>();
15     private LinkedList<Door> doors = new LinkedList<Door>();
16    
17     Plugin plugin;
18    
19     public DoorStorage(Plugin plugin) {
20     this.plugin = plugin;
21     }
22    
23     public Door findDoor(Location location) {
24     return doormap.get( location );
25     }
26    
27     public void addDoor(Door door) {
28     door.registerMap(doormap);
29     doors.add(door);
30     saveAll();
31     }
32    
33     public void removeDoor(Door door) {
34     door.unregisterMap(doormap);
35     doors.remove(door);
36     saveAll();
37     }
38    
39     public void saveAll() {
40     try {
41    
42     File f = new File( plugin.getDataFolder(), "secretdoors.csv");
43     RandomAccessFile out = new RandomAccessFile( f, "rw" );
44     out.setLength(0);
45    
46     for (Door door : doors) {
47     out.writeBytes( door.toCsv() );
48     out.writeBytes( "\n");
49     }
50    
51     out.close();
52     } catch (IOException e) {
53     System.out.println(e.getMessage() );
54     e.printStackTrace();
55     }
56     }
57    
58     public void loadAll() {
59     try {
60     File f = new File( plugin.getDataFolder(), "secretdoors.csv");
61     RandomAccessFile in = new RandomAccessFile( f, "r" );
62    
63     String line;
64     while ( (line = in.readLine() ) != null ) {
65     Door door = new Door(line, plugin.getServer() );
66     doors.add( door );
67     door.registerMap( doormap );
68     }
69     in.close();
70    
71     }
72     catch (FileNotFoundException fnfe) {
73     return;
74     }
75     catch (IOException e) {
76     System.out.println(e.getMessage() );
77     e.printStackTrace();
78     }
79     }
80    
81    
82    
83     }

  ViewVC Help
Powered by ViewVC 1.1.20