6 |
import java.io.RandomAccessFile; |
import java.io.RandomAccessFile; |
7 |
import java.util.HashMap; |
import java.util.HashMap; |
8 |
import java.util.LinkedList; |
import java.util.LinkedList; |
9 |
|
import java.util.Set; |
10 |
|
|
11 |
|
import org.bukkit.configuration.file.YamlConfiguration; |
12 |
import org.bukkit.Location; |
import org.bukkit.Location; |
13 |
|
import org.bukkit.World; |
14 |
import org.bukkit.plugin.Plugin; |
import org.bukkit.plugin.Plugin; |
15 |
|
|
16 |
public class DoorStorage { |
public class DoorStorage { |
17 |
private HashMap<Location,Door> doormap = new HashMap<Location,Door>(); |
private HashMap<Location,Door> doormap = new HashMap<Location,Door>(); |
18 |
private LinkedList<Door> doors = new LinkedList<Door>(); |
private LinkedList<Door> doors = new LinkedList<Door>(); |
19 |
|
|
20 |
Plugin plugin; |
private Plugin plugin; |
21 |
|
|
22 |
public DoorStorage(Plugin plugin) { |
public DoorStorage(Plugin plugin) { |
23 |
this.plugin = plugin; |
this.plugin = plugin; |
40 |
} |
} |
41 |
|
|
42 |
public void saveAll() { |
public void saveAll() { |
|
try { |
|
43 |
|
|
44 |
File f = new File( plugin.getDataFolder(), "secretdoors.csv"); |
File f2 = new File( plugin.getDataFolder(), "secretdoors.yml"); |
45 |
RandomAccessFile out = new RandomAccessFile( f, "rw" ); |
YamlConfiguration config = new YamlConfiguration(); |
46 |
out.setLength(0); |
|
47 |
|
|
48 |
for (Door door : doors) { |
for (int i=0; i<doors.size(); i++) { |
49 |
out.writeBytes( door.toCsv() ); |
Door door = doors.get(i); |
50 |
out.writeBytes( "\n"); |
|
51 |
} |
config.set( i + ".world", door.getLeftUpper().getWorld().getName() ); |
52 |
|
config.set( i + ".x", door.getLeftUpper().getBlockX() ); |
53 |
out.close(); |
config.set( i + ".y", door.getLeftUpper().getBlockY() ); |
54 |
} catch (IOException e) { |
config.set( i + ".z", door.getLeftUpper().getBlockZ() ); |
55 |
System.out.println(e.getMessage() ); |
config.set( i + ".direction", door.getDirection() ); |
56 |
e.printStackTrace(); |
config.set( i + ".width", door.getWidth() ); |
57 |
|
config.set( i + ".height", door.getHeight() ); |
58 |
|
config.set( i + ".owner", door.getOwner() ); |
59 |
|
config.set( i + ".private", door.isPrivate() ); |
60 |
|
config.set( i + ".password", door.getPassword() ); |
61 |
|
} |
62 |
|
|
63 |
|
try { |
64 |
|
config.save(f2); |
65 |
|
} catch (java.io.IOException e) { |
66 |
|
System.out.println("[SecretDoor] DoorStorage : exception saving doors : " + e.getMessage() ); |
67 |
} |
} |
68 |
|
|
69 |
} |
} |
70 |
|
|
71 |
public void loadAll() { |
public void loadAll() { |
72 |
try { |
File f = new File( plugin.getDataFolder(), "secretdoors.yml"); |
|
File f = new File( plugin.getDataFolder(), "secretdoors.csv"); |
|
|
RandomAccessFile in = new RandomAccessFile( f, "r" ); |
|
73 |
|
|
74 |
String line; |
if ( !f.exists() ) { |
75 |
while ( (line = in.readLine() ) != null ) { |
System.out.println("[SecretDoor] coult not find secretdoors.yml"); |
|
Door door = new Door(line, plugin.getServer() ); |
|
|
doors.add( door ); |
|
|
door.registerMap( doormap ); |
|
|
} |
|
|
in.close(); |
|
|
|
|
|
System.out.println("[SecretDoor] loaded " + doors.size() + " doors"); |
|
|
|
|
|
} |
|
|
catch (FileNotFoundException fnfe) { |
|
|
System.out.println("[SecretDoor] door file not found"); |
|
76 |
return; |
return; |
77 |
} |
} |
78 |
catch (IOException e) { |
YamlConfiguration config = new YamlConfiguration(); |
79 |
System.out.println(e.getMessage() ); |
try { |
80 |
e.printStackTrace(); |
config.load(f); |
81 |
|
} catch (Exception e) { |
82 |
|
System.out.println("[SecretDoor] DoorStorage: exception loading doors : " + e.getMessage() ); |
83 |
|
return; |
84 |
} |
} |
85 |
|
|
86 |
|
Set<String> keys = config.getKeys(false); //get all root keys |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
for (String key : keys) { |
91 |
|
|
92 |
|
World world = plugin.getServer().getWorld( config.getString(key + ".world") ); |
93 |
|
int x = config.getInt( key + ".x", 0); |
94 |
|
int y = config.getInt( key + ".y", 0); |
95 |
|
int z = config.getInt( key + ".z", 0); |
96 |
|
Location loc = new Location(world, x, y, z); |
97 |
|
|
98 |
|
int direction = config.getInt( key + ".direction", 0); |
99 |
|
int width = config.getInt( key + ".width", 0); |
100 |
|
int height = config.getInt( key + ".height", 0); |
101 |
|
|
102 |
|
String owner = config.getString( key + ".owner"); |
103 |
|
|
104 |
|
boolean isPrivate = config.getBoolean( key + ".private", false); |
105 |
|
String password = config.getString( key + ".password", ""); |
106 |
|
|
107 |
|
Door door = new Door(loc, direction, width, height, owner, isPrivate, password); |
108 |
|
doors.add( door ); |
109 |
|
door.registerMap(doormap); |
110 |
|
} |
111 |
|
|
112 |
|
System.out.println("[SecretDoor] loaded " + doors.size() + " doors"); |
113 |
|
|
114 |
} |
} |
115 |
|
|
116 |
|
|