| 1 | package nl.timfalken.copperkeys; |
| 2 | |
| 3 | import org.bukkit.configuration.ConfigurationSection; |
| 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 | import java.util.ArrayList; |
| 10 | import java.util.HashMap; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | import java.util.UUID; |
| 14 | import java.util.logging.Level; |
| 15 | |
| 16 | final 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 | String keyName = section.getString("key-name", "Key"); |
| 49 | List<BlockKey> members = new ArrayList<>(); |
| 50 | for (String encoded : section.getStringList("members")) { |
| 51 | members.add(BlockKey.decode(encoded)); |
| 52 | } |
| 53 | if (members.isEmpty()) { |
| 54 | continue; |
| 55 | } |
| 56 | LockRecord record = new LockRecord(id, displayName, keyName, members); |
| 57 | byBinding.put(id, record); |
| 58 | for (BlockKey member : members) { |
| 59 | byBlock.put(member, record); |
| 60 | } |
| 61 | } catch (RuntimeException ex) { |
| 62 | plugin.getLogger().log(Level.WARNING, "Ignoring invalid lock entry " + idText, ex); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | LockRecord get(BlockKey block) { |
| 68 | return byBlock.get(block); |
| 69 | } |
| 70 | |
| 71 | LockRecord get(UUID bindingId) { |
| 72 | return byBinding.get(bindingId); |
| 73 | } |
| 74 | |
| 75 | boolean exists(UUID bindingId) { |
| 76 | return byBinding.containsKey(bindingId); |
| 77 | } |
| 78 | |
| 79 | void add(LockRecord record) { |
| 80 | byBinding.put(record.bindingId(), record); |
| 81 | for (BlockKey member : record.members()) { |
| 82 | byBlock.put(member, record); |
| 83 | } |
| 84 | save(); |
| 85 | } |
| 86 | |
| 87 | LockRecord removeByBlock(BlockKey block) { |
| 88 | LockRecord record = byBlock.get(block); |
| 89 | if (record != null) { |
| 90 | remove(record.bindingId()); |
| 91 | } |
| 92 | return record; |
| 93 | } |
| 94 | |
| 95 | LockRecord remove(UUID bindingId) { |
| 96 | LockRecord record = byBinding.remove(bindingId); |
| 97 | if (record == null) { |
| 98 | return null; |
| 99 | } |
| 100 | for (BlockKey member : record.members()) { |
| 101 | byBlock.remove(member, record); |
| 102 | } |
| 103 | save(); |
| 104 | return record; |
| 105 | } |
| 106 | |
| 107 | LockRecord setKeyName(UUID bindingId, String keyName) { |
| 108 | LockRecord current = byBinding.get(bindingId); |
| 109 | if (current == null) { |
| 110 | return null; |
| 111 | } |
| 112 | LockRecord updated = current.withKeyName(keyName); |
| 113 | byBinding.put(bindingId, updated); |
| 114 | for (BlockKey member : updated.members()) { |
| 115 | byBlock.put(member, updated); |
| 116 | } |
| 117 | save(); |
| 118 | return updated; |
| 119 | } |
| 120 | |
| 121 | private void save() { |
| 122 | if (!plugin.getDataFolder().isDirectory() && !plugin.getDataFolder().mkdirs()) { |
| 123 | plugin.getLogger().warning("Could not create plugin data directory; locks are only in memory."); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | YamlConfiguration yaml = new YamlConfiguration(); |
| 128 | for (LockRecord record : byBinding.values()) { |
| 129 | String base = "locks." + record.bindingId(); |
| 130 | yaml.set(base + ".display-name", record.displayName()); |
| 131 | yaml.set(base + ".key-name", record.keyName()); |
| 132 | yaml.set(base + ".members", record.members().stream().map(BlockKey::encode).toList()); |
| 133 | } |
| 134 | try { |
| 135 | yaml.save(file); |
| 136 | } catch (IOException ex) { |
| 137 | plugin.getLogger().log(Level.SEVERE, "Could not save locks.yml", ex); |
| 138 | } |
| 139 | } |
| 140 | } |