1
1
package cat .nyaa .nyaautils .mailbox ;
2
2
3
3
import cat .nyaa .nyaautils .NyaaUtils ;
4
+ import cat .nyaa .utils .FileConfigure ;
4
5
import com .google .common .collect .BiMap ;
5
6
import com .google .common .collect .HashBiMap ;
6
7
import org .apache .commons .lang .Validate ;
9
10
import org .bukkit .configuration .InvalidConfigurationException ;
10
11
import org .bukkit .configuration .file .YamlConfiguration ;
11
12
import org .bukkit .entity .Player ;
13
+ import org .bukkit .plugin .java .JavaPlugin ;
12
14
13
15
import java .io .File ;
14
16
import java .io .IOException ;
15
17
import java .util .HashMap ;
16
18
import java .util .Map ;
17
19
import java .util .UUID ;
18
20
19
- public class MailboxLocations {
21
+ public class MailboxLocations extends FileConfigure {
20
22
public final static String CFG_FILE_NAME = "mailbox_location.yml" ;
21
23
private final NyaaUtils plugin ;
22
24
@@ -27,51 +29,22 @@ public MailboxLocations(NyaaUtils plugin) {
27
29
this .plugin = plugin ;
28
30
}
29
31
30
- private File ensureFile () {
31
- File cfgFile = new File (plugin .getDataFolder (), CFG_FILE_NAME );
32
- if (!cfgFile .exists ()) {
33
- cfgFile .getParentFile ().mkdirs ();
34
- try {
35
- cfgFile .createNewFile ();
36
- } catch (IOException ex ) {
37
- throw new RuntimeException (ex );
38
- }
39
- }
40
- return cfgFile ;
32
+ @ Override
33
+ protected String getFileName () {
34
+ return CFG_FILE_NAME ;
41
35
}
42
36
43
- public void save () {
44
- YamlConfiguration cfg = new YamlConfiguration ();
45
- ConfigurationSection locationSection = cfg .createSection ("locations" );
46
- for (UUID uuid : locationMap .keySet ()) {
47
- locationSection .set (uuid .toString (), locationMap .get (uuid ));
48
- }
49
- ConfigurationSection nameSection = cfg .createSection ("names" );
50
- for (UUID uuid : nameMap .keySet ()) {
51
- nameSection .set (uuid .toString (), nameMap .get (uuid ));
52
- }
53
-
54
- try {
55
- cfg .save (ensureFile ());
56
- } catch (IOException ex ) {
57
- plugin .getLogger ().severe ("Cannot save Mailbox location info. Emergency dump:" );
58
- plugin .getLogger ().severe ("\n " + cfg .saveToString ());
59
- plugin .getLogger ().severe ("Cannot save Mailbox location info. Emergency dump End." );
60
- throw new RuntimeException (ex );
61
- }
37
+ @ Override
38
+ protected JavaPlugin getPlugin () {
39
+ return plugin ;
62
40
}
63
41
64
- public void load () {
65
- YamlConfiguration cfg = new YamlConfiguration ();
66
- try {
67
- cfg .load (ensureFile ());
68
- } catch (IOException | InvalidConfigurationException ex ) {
69
- throw new RuntimeException (ex );
70
- }
42
+ @ Override
43
+ public void deserialize (ConfigurationSection config ) {
71
44
locationMap .clear ();
72
45
nameMap .clear ();
73
- ConfigurationSection locations = cfg .getConfigurationSection ("locations" );
74
- ConfigurationSection names = cfg .getConfigurationSection ("names" );
46
+ ConfigurationSection locations = config .getConfigurationSection ("locations" );
47
+ ConfigurationSection names = config .getConfigurationSection ("names" );
75
48
if (locations != null ) {
76
49
for (String uuid_s : locations .getKeys (false )) {
77
50
locationMap .put (UUID .fromString (uuid_s ), (Location ) locations .get (uuid_s ));
@@ -86,8 +59,18 @@ public void load() {
86
59
for (Player p : plugin .getServer ().getOnlinePlayers ()) {
87
60
nameMap .put (p .getUniqueId (), p .getName ().toLowerCase ());
88
61
}
62
+ }
89
63
90
- save ();
64
+ @ Override
65
+ public void serialize (ConfigurationSection config ) {
66
+ ConfigurationSection locationSection = config .createSection ("locations" );
67
+ for (UUID uuid : locationMap .keySet ()) {
68
+ locationSection .set (uuid .toString (), locationMap .get (uuid ));
69
+ }
70
+ ConfigurationSection nameSection = config .createSection ("names" );
71
+ for (UUID uuid : nameMap .keySet ()) {
72
+ nameSection .set (uuid .toString (), nameMap .get (uuid ));
73
+ }
91
74
}
92
75
93
76
public void updateNameMapping (UUID uuid , String name ) {
@@ -128,5 +111,4 @@ public UUID getUUIDbyName(String name) {
128
111
if (name == null ) return null ;
129
112
return nameMap .inverse ().get (name .toLowerCase ());
130
113
}
131
-
132
114
}
0 commit comments