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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2431 - (hide annotations) (download)
Mon Mar 9 13:45:03 2015 UTC (9 years, 2 months ago) by torben
File size: 3285 byte(s)
Correct door persistance
1 torben 1530 package dk.thoerup.bukkit.hoeruputils.secretdoor;
2    
3     import java.io.File;
4     import java.util.HashMap;
5     import java.util.LinkedList;
6 torben 1614 import java.util.Set;
7 torben 1530
8     import org.bukkit.Location;
9 torben 1604 import org.bukkit.World;
10 torben 2431 import org.bukkit.block.BlockFace;
11     import org.bukkit.configuration.file.YamlConfiguration;
12 torben 1530 import org.bukkit.plugin.Plugin;
13    
14     public class DoorStorage {
15     private HashMap<Location,Door> doormap = new HashMap<Location,Door>();
16     private LinkedList<Door> doors = new LinkedList<Door>();
17    
18 torben 1538 private Plugin plugin;
19 torben 1530
20     public DoorStorage(Plugin plugin) {
21     this.plugin = plugin;
22     }
23    
24     public Door findDoor(Location location) {
25     return doormap.get( location );
26     }
27    
28     public void addDoor(Door door) {
29     door.registerMap(doormap);
30     doors.add(door);
31     saveAll();
32     }
33    
34     public void removeDoor(Door door) {
35     door.unregisterMap(doormap);
36     doors.remove(door);
37     saveAll();
38     }
39    
40     public void saveAll() {
41    
42 torben 1604 File f2 = new File( plugin.getDataFolder(), "secretdoors.yml");
43 torben 1614 YamlConfiguration config = new YamlConfiguration();
44 torben 1530
45 torben 1912
46 torben 1604 for (int i=0; i<doors.size(); i++) {
47     Door door = doors.get(i);
48 torben 1530
49 torben 1614 config.set( i + ".world", door.getLeftUpper().getWorld().getName() );
50     config.set( i + ".x", door.getLeftUpper().getBlockX() );
51     config.set( i + ".y", door.getLeftUpper().getBlockY() );
52     config.set( i + ".z", door.getLeftUpper().getBlockZ() );
53 torben 2431 config.set( i + ".direction", door.getDirection().toString() );
54 torben 1614 config.set( i + ".width", door.getWidth() );
55     config.set( i + ".height", door.getHeight() );
56     config.set( i + ".owner", door.getOwner() );
57 torben 1912 config.set( i + ".private", door.isPrivate() );
58     config.set( i + ".password", door.getPassword() );
59 torben 1530 }
60 torben 1604
61 torben 1614 try {
62     config.save(f2);
63     } catch (java.io.IOException e) {
64     System.out.println("[SecretDoor] DoorStorage : exception saving doors : " + e.getMessage() );
65     }
66 torben 1604
67 torben 1530 }
68    
69     public void loadAll() {
70 torben 1604 File f = new File( plugin.getDataFolder(), "secretdoors.yml");
71 torben 1530
72 torben 1604 if ( !f.exists() ) {
73     System.out.println("[SecretDoor] coult not find secretdoors.yml");
74     return;
75     }
76 torben 1614 YamlConfiguration config = new YamlConfiguration();
77     try {
78     config.load(f);
79     } catch (Exception e) {
80     System.out.println("[SecretDoor] DoorStorage: exception loading doors : " + e.getMessage() );
81     return;
82     }
83 torben 1604
84 torben 1614 Set<String> keys = config.getKeys(false); //get all root keys
85 torben 1604
86 torben 1530
87 torben 1604
88     for (String key : keys) {
89 torben 1532
90 torben 1604 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 torben 2431 String directionStr = config.getString( key + ".direction", "");
97     BlockFace direction = BlockFace.valueOf(directionStr);
98    
99 torben 1604 int width = config.getInt( key + ".width", 0);
100     int height = config.getInt( key + ".height", 0);
101    
102     String owner = config.getString( key + ".owner");
103 torben 1605
104     boolean isPrivate = config.getBoolean( key + ".private", false);
105 torben 1912 String password = config.getString( key + ".password", "");
106 torben 2431
107    
108 torben 1604
109 torben 1912 Door door = new Door(loc, direction, width, height, owner, isPrivate, password);
110 torben 1604 doors.add( door );
111     door.registerMap(doormap);
112     }
113    
114     System.out.println("[SecretDoor] loaded " + doors.size() + " doors");
115    
116 torben 1530 }
117    
118    
119    
120     }

  ViewVC Help
Powered by ViewVC 1.1.20