Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit 955d527

Browse files
committed
Added FileConfigure
1 parent ef2ed0a commit 955d527

File tree

2 files changed

+74
-42
lines changed

2 files changed

+74
-42
lines changed

src/main/java/cat/nyaa/nyaautils/mailbox/MailboxLocations.java

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cat.nyaa.nyaautils.mailbox;
22

33
import cat.nyaa.nyaautils.NyaaUtils;
4+
import cat.nyaa.utils.FileConfigure;
45
import com.google.common.collect.BiMap;
56
import com.google.common.collect.HashBiMap;
67
import org.apache.commons.lang.Validate;
@@ -9,14 +10,15 @@
910
import org.bukkit.configuration.InvalidConfigurationException;
1011
import org.bukkit.configuration.file.YamlConfiguration;
1112
import org.bukkit.entity.Player;
13+
import org.bukkit.plugin.java.JavaPlugin;
1214

1315
import java.io.File;
1416
import java.io.IOException;
1517
import java.util.HashMap;
1618
import java.util.Map;
1719
import java.util.UUID;
1820

19-
public class MailboxLocations {
21+
public class MailboxLocations extends FileConfigure {
2022
public final static String CFG_FILE_NAME = "mailbox_location.yml";
2123
private final NyaaUtils plugin;
2224

@@ -27,51 +29,22 @@ public MailboxLocations(NyaaUtils plugin) {
2729
this.plugin = plugin;
2830
}
2931

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;
4135
}
4236

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;
6240
}
6341

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) {
7144
locationMap.clear();
7245
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");
7548
if (locations != null) {
7649
for (String uuid_s : locations.getKeys(false)) {
7750
locationMap.put(UUID.fromString(uuid_s), (Location) locations.get(uuid_s));
@@ -86,8 +59,18 @@ public void load() {
8659
for (Player p : plugin.getServer().getOnlinePlayers()) {
8760
nameMap.put(p.getUniqueId(), p.getName().toLowerCase());
8861
}
62+
}
8963

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+
}
9174
}
9275

9376
public void updateNameMapping(UUID uuid, String name) {
@@ -128,5 +111,4 @@ public UUID getUUIDbyName(String name) {
128111
if (name == null) return null;
129112
return nameMap.inverse().get(name.toLowerCase());
130113
}
131-
132114
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cat.nyaa.utils;
2+
3+
import org.bukkit.configuration.InvalidConfigurationException;
4+
import org.bukkit.configuration.file.YamlConfiguration;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
public abstract class FileConfigure implements ISerializable {
11+
protected abstract String getFileName();
12+
protected abstract JavaPlugin getPlugin();
13+
14+
private File ensureFile() {
15+
File cfgFile = new File(getPlugin().getDataFolder(), getFileName());
16+
if (!cfgFile.exists()) {
17+
cfgFile.getParentFile().mkdirs();
18+
try {
19+
cfgFile.createNewFile();
20+
} catch (IOException ex) {
21+
throw new RuntimeException(ex);
22+
}
23+
}
24+
return cfgFile;
25+
}
26+
27+
public final void save() {
28+
YamlConfiguration cfg = new YamlConfiguration();
29+
serialize(cfg);
30+
try {
31+
cfg.save(ensureFile());
32+
} catch (IOException ex) {
33+
getPlugin().getLogger().severe("Cannot save " + getFileName() +". Emergency dump:");
34+
getPlugin().getLogger().severe("\n" + cfg.saveToString());
35+
getPlugin().getLogger().severe("Cannot save " + getFileName() +". Emergency dump End.");
36+
throw new RuntimeException(ex);
37+
}
38+
}
39+
40+
public final void load() {
41+
YamlConfiguration cfg = new YamlConfiguration();
42+
try {
43+
cfg.load(ensureFile());
44+
} catch (IOException | InvalidConfigurationException ex) {
45+
throw new RuntimeException(ex);
46+
}
47+
deserialize(cfg);
48+
save();
49+
}
50+
}

0 commit comments

Comments
 (0)