BlueGit

timfa copper-key

Files

copper-key / src / main / java / nl / timfalken / copperkeys / LockStore.java

LockStore.java

LockStore.java 3.9 KiB Download Raw
1package nl.timfalken.copperkeys;
2
3import org.bukkit.configuration.ConfigurationSection;
4import org.bukkit.configuration.file.YamlConfiguration;
5import org.bukkit.plugin.java.JavaPlugin;
6
7import java.io.File;
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13import java.util.UUID;
14import java.util.logging.Level;
15
16final class LockStore {
17 private final JavaPlugin plugin;
18 private final File file;
19 private final Map<BlockKey, LockRecord> byBlock = new HashMap<>();
20 private final Map<UUID, LockRecord> byBinding = new HashMap<>();
21
22 LockStore(JavaPlugin plugin) {
23 this.plugin = plugin;
24 this.file = new File(plugin.getDataFolder(), "locks.yml");
25 }
26
27 void load() {
28 byBlock.clear();
29 byBinding.clear();
30 if (!file.isFile()) {
31 return;
32 }
33
34 YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file);
35 ConfigurationSection root = yaml.getConfigurationSection("locks");
36 if (root == null) {
37 return;
38 }
39
40 for (String idText : root.getKeys(false)) {
41 try {
42 UUID id = UUID.fromString(idText);
43 ConfigurationSection section = root.getConfigurationSection(idText);
44 if (section == null) {
45 continue;
46 }
47 String displayName = section.getString("display-name", "Block");
48 List<BlockKey> members = new ArrayList<>();
49 for (String encoded : section.getStringList("members")) {
50 members.add(BlockKey.decode(encoded));
51 }
52 if (members.isEmpty()) {
53 continue;
54 }
55 LockRecord record = new LockRecord(id, displayName, members);
56 byBinding.put(id, record);
57 for (BlockKey member : members) {
58 byBlock.put(member, record);
59 }
60 } catch (RuntimeException ex) {
61 plugin.getLogger().log(Level.WARNING, "Ignoring invalid lock entry " + idText, ex);
62 }
63 }
64 }
65
66 LockRecord get(BlockKey block) {
67 return byBlock.get(block);
68 }
69
70 LockRecord get(UUID bindingId) {
71 return byBinding.get(bindingId);
72 }
73
74 boolean exists(UUID bindingId) {
75 return byBinding.containsKey(bindingId);
76 }
77
78 void add(LockRecord record) {
79 byBinding.put(record.bindingId(), record);
80 for (BlockKey member : record.members()) {
81 byBlock.put(member, record);
82 }
83 save();
84 }
85
86 LockRecord removeByBlock(BlockKey block) {
87 LockRecord record = byBlock.get(block);
88 if (record != null) {
89 remove(record.bindingId());
90 }
91 return record;
92 }
93
94 LockRecord remove(UUID bindingId) {
95 LockRecord record = byBinding.remove(bindingId);
96 if (record == null) {
97 return null;
98 }
99 for (BlockKey member : record.members()) {
100 byBlock.remove(member, record);
101 }
102 save();
103 return record;
104 }
105
106 private void save() {
107 if (!plugin.getDataFolder().isDirectory() && !plugin.getDataFolder().mkdirs()) {
108 plugin.getLogger().warning("Could not create plugin data directory; locks are only in memory.");
109 return;
110 }
111
112 YamlConfiguration yaml = new YamlConfiguration();
113 for (LockRecord record : byBinding.values()) {
114 String base = "locks." + record.bindingId();
115 yaml.set(base + ".display-name", record.displayName());
116 yaml.set(base + ".members", record.members().stream().map(BlockKey::encode).toList());
117 }
118 try {
119 yaml.save(file);
120 } catch (IOException ex) {
121 plugin.getLogger().log(Level.SEVERE, "Could not save locks.yml", ex);
122 }
123 }
124}