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