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 |
torben |
1532 |
System.out.println("[SecretDoor] loaded " + doors.size() + " doors"); |
72 |
|
|
|
73 |
torben |
1530 |
} |
74 |
|
|
catch (FileNotFoundException fnfe) { |
75 |
torben |
1532 |
System.out.println("[SecretDoor] door file not found"); |
76 |
torben |
1530 |
return; |
77 |
|
|
} |
78 |
|
|
catch (IOException e) { |
79 |
|
|
System.out.println(e.getMessage() ); |
80 |
|
|
e.printStackTrace(); |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
} |