|
1 | 1 | package de.oliver.fancyholograms; |
2 | 2 |
|
| 3 | +import com.fancyinnovations.config.ConfigHelper; |
3 | 4 | import de.oliver.fancyholograms.api.FancyHologramsPlugin; |
4 | 5 | import de.oliver.fancyholograms.api.HologramConfiguration; |
5 | | -import de.oliver.fancylib.ConfigHelper; |
6 | 6 | import org.bukkit.configuration.file.FileConfiguration; |
7 | 7 | import org.jetbrains.annotations.NotNull; |
8 | 8 |
|
|
17 | 17 | */ |
18 | 18 | public final class FancyHologramsConfiguration implements HologramConfiguration { |
19 | 19 |
|
| 20 | + private static final String CONFIG_AUTOSAVE_ENABLED = "saving.autosave.enabled"; |
| 21 | + private static final String CONFIG_AUTOSAVE_INTERVAL = "saving.autosave.interval"; |
| 22 | + private static final String CONFIG_SAVE_ON_CHANGED = "saving.save_on_changed"; |
| 23 | + private static final String CONFIG_LOG_LEVEL = "logging.log_level"; |
| 24 | + private static final String CONFIG_LOG_ON_WORLD_LOAD = "logging.log_on_world_load"; |
| 25 | + private static final String CONFIG_VERSION_NOTIFICATIONS = "logging.version_notifications"; |
| 26 | + private static final String CONFIG_VISIBILITY_DISTANCE = "visibility_distance"; |
| 27 | + private static final String CONFIG_REGISTER_COMMANDS = "register_commands"; |
| 28 | + private static final String CONFIG_UPDATE_VISIBILITY_INTERVAL = "update_visibility_interval"; |
| 29 | + private static final String CONFIG_REPORT_ERRORS_TO_SENTRY = "report_errors_to_sentry"; |
| 30 | + private static final String CONFIG_VERSION = "config_version"; |
| 31 | + private static final Map<String, List<String>> CONFIG_COMMENTS = Map.of( |
| 32 | + CONFIG_VERSION, List.of("Config version, do not modify."), |
| 33 | + CONFIG_AUTOSAVE_ENABLED, List.of("Whether autosave is enabled."), |
| 34 | + CONFIG_AUTOSAVE_INTERVAL, List.of("The interval at which autosave is performed in minutes."), |
| 35 | + CONFIG_SAVE_ON_CHANGED, List.of("Whether the plugin should save holograms when they are changed."), |
| 36 | + CONFIG_LOG_LEVEL, List.of("The log level for the plugin (DEBUG, INFO, WARN, ERROR)."), |
| 37 | + CONFIG_LOG_ON_WORLD_LOAD, List.of("Whether hologram loading should be logged on world loading. Disable this if you load worlds dynamically to prevent console spam."), |
| 38 | + CONFIG_VERSION_NOTIFICATIONS, List.of("Whether the plugin should send notifications for new updates."), |
| 39 | + CONFIG_VISIBILITY_DISTANCE, List.of("The default visibility distance for holograms."), |
| 40 | + CONFIG_REGISTER_COMMANDS, List.of("Whether the plugin should register its commands."), |
| 41 | + CONFIG_UPDATE_VISIBILITY_INTERVAL, List.of("The interval at which hologram visibility is updated in ticks.") |
| 42 | + ); |
20 | 43 | /** |
21 | 44 | * Indicates whether autosave is enabled. |
22 | 45 | */ |
23 | 46 | private boolean autosaveEnabled; |
24 | | - private static final String CONFIG_AUTOSAVE_ENABLED = "saving.autosave.enabled"; |
25 | | - |
26 | 47 | /** |
27 | 48 | * The interval at which autosave is performed. |
28 | 49 | */ |
29 | 50 | private int autosaveInterval; |
30 | | - private static final String CONFIG_AUTOSAVE_INTERVAL = "saving.autosave.interval"; |
31 | | - |
32 | 51 | /** |
33 | 52 | * Indicates whether the plugin should save holograms when they are changed. |
34 | 53 | */ |
35 | 54 | private boolean saveOnChangedEnabled; |
36 | | - private static final String CONFIG_SAVE_ON_CHANGED = "saving.save_on_changed"; |
37 | | - |
38 | 55 | /** |
39 | 56 | * The log level for the plugin. |
40 | 57 | */ |
41 | 58 | private String logLevel; |
42 | | - private static final String CONFIG_LOG_LEVEL = "logging.log_level"; |
43 | | - |
44 | 59 | /** |
45 | 60 | * Indicates whether hologram loading should be logged on world loading. |
46 | 61 | */ |
47 | 62 | private boolean hologramLoadLogging; |
48 | | - private static final String CONFIG_LOG_ON_WORLD_LOAD = "logging.log_on_world_load"; |
49 | | - |
50 | 63 | /** |
51 | 64 | * Indicates whether version notifications are enabled or disabled. |
52 | 65 | */ |
53 | 66 | private boolean versionNotifs; |
54 | | - private static final String CONFIG_VERSION_NOTIFICATIONS = "logging.version_notifications"; |
55 | | - |
56 | 67 | /** |
57 | 68 | * The default visibility distance for holograms. |
58 | 69 | */ |
59 | 70 | private int defaultVisibilityDistance; |
60 | | - private static final String CONFIG_VISIBILITY_DISTANCE = "visibility_distance"; |
61 | | - |
62 | 71 | /** |
63 | 72 | * Indicates whether commands should be registered. |
64 | 73 | * <p> |
65 | 74 | * This is useful for users who want to use the plugin's API only. |
66 | 75 | */ |
67 | 76 | private boolean registerCommands; |
68 | | - private static final String CONFIG_REGISTER_COMMANDS = "register_commands"; |
69 | | - |
70 | 77 | /** |
71 | 78 | * The interval at which hologram visibility is updated. |
72 | 79 | */ |
73 | 80 | private int updateVisibilityInterval; |
74 | | - private static final String CONFIG_UPDATE_VISIBILITY_INTERVAL = "update_visibility_interval"; |
75 | | - |
76 | | - private static final String CONFIG_REPORT_ERRORS_TO_SENTRY = "report_errors_to_sentry"; |
77 | | - private static final String CONFIG_VERSION = "config_version"; |
78 | | - |
79 | | - private static final Map<String, List<String>> CONFIG_COMMENTS = Map.of( |
80 | | - CONFIG_VERSION, List.of("Config version, do not modify."), |
81 | | - CONFIG_AUTOSAVE_ENABLED, List.of("Whether autosave is enabled."), |
82 | | - CONFIG_AUTOSAVE_INTERVAL, List.of("The interval at which autosave is performed in minutes."), |
83 | | - CONFIG_SAVE_ON_CHANGED, List.of("Whether the plugin should save holograms when they are changed."), |
84 | | - CONFIG_LOG_LEVEL, List.of("The log level for the plugin (DEBUG, INFO, WARN, ERROR)."), |
85 | | - CONFIG_LOG_ON_WORLD_LOAD, List.of("Whether hologram loading should be logged on world loading. Disable this if you load worlds dynamically to prevent console spam."), |
86 | | - CONFIG_VERSION_NOTIFICATIONS, List.of("Whether the plugin should send notifications for new updates."), |
87 | | - CONFIG_VISIBILITY_DISTANCE, List.of("The default visibility distance for holograms."), |
88 | | - CONFIG_REGISTER_COMMANDS, List.of("Whether the plugin should register its commands."), |
89 | | - CONFIG_UPDATE_VISIBILITY_INTERVAL, List.of("The interval at which hologram visibility is updated in ticks.") |
90 | | - ); |
91 | 81 |
|
92 | 82 | private void updateChecker(@NotNull FancyHolograms plugin, @NotNull FileConfiguration config) { |
93 | 83 | final int latestVersion = 1; |
94 | 84 | int configVersion = (int) ConfigHelper.getOrDefault(config, CONFIG_VERSION, 0); |
95 | 85 |
|
96 | | - if (configVersion >= latestVersion ) { |
| 86 | + if (configVersion >= latestVersion) { |
97 | 87 | setOptions(config); |
98 | 88 | return; |
99 | 89 | } |
100 | | - plugin.getFancyLogger().warn("Outdated config detected! Attempting to migrate previous settings to new config..."); |
101 | | - |
102 | | - var oldConfig = plugin.getConfig(); |
103 | | - try { |
104 | | - File backupFile = new File(plugin.getDataFolder(), "config_old.yml"); |
105 | | - oldConfig.save(backupFile); |
106 | | - } catch (IOException e) { |
107 | | - plugin.getFancyLogger().warn("Unable to backup config to config_old.yml:" + e); |
108 | | - } |
| 90 | + plugin.getFancyLogger().warn("Outdated config detected! Attempting to migrate previous settings to new config..."); |
| 91 | + |
| 92 | + var oldConfig = plugin.getConfig(); |
| 93 | + try { |
| 94 | + File backupFile = new File(plugin.getDataFolder(), "config_old.yml"); |
| 95 | + oldConfig.save(backupFile); |
| 96 | + } catch (IOException e) { |
| 97 | + plugin.getFancyLogger().warn("Unable to backup config to config_old.yml:" + e); |
| 98 | + } |
109 | 99 |
|
110 | 100 |
|
111 | | - var newConfig = plugin.getConfig(); |
| 101 | + var newConfig = plugin.getConfig(); |
112 | 102 |
|
113 | | - Map<String, Object> oldConfigValues = oldConfig.getValues(true); |
114 | | - Map<String, String> keyMap = Map.of( |
115 | | - "enable_autosave", CONFIG_AUTOSAVE_ENABLED, |
116 | | - "autosave_interval", CONFIG_AUTOSAVE_INTERVAL, |
117 | | - "save_on_changed", CONFIG_SAVE_ON_CHANGED, |
118 | | - "log_level", CONFIG_LOG_LEVEL, |
119 | | - "mute_version_notifications", CONFIG_VERSION_NOTIFICATIONS |
120 | | - ); |
| 103 | + Map<String, Object> oldConfigValues = oldConfig.getValues(true); |
| 104 | + Map<String, String> keyMap = Map.of( |
| 105 | + "enable_autosave", CONFIG_AUTOSAVE_ENABLED, |
| 106 | + "autosave_interval", CONFIG_AUTOSAVE_INTERVAL, |
| 107 | + "save_on_changed", CONFIG_SAVE_ON_CHANGED, |
| 108 | + "log_level", CONFIG_LOG_LEVEL, |
| 109 | + "mute_version_notifications", CONFIG_VERSION_NOTIFICATIONS |
| 110 | + ); |
121 | 111 |
|
122 | | - oldConfigValues.forEach((key, value) -> { |
| 112 | + oldConfigValues.forEach((key, value) -> { |
123 | 113 |
|
124 | | - String newKey = keyMap.getOrDefault(key, null); |
125 | | - if (newKey != null) { |
126 | | - if (newKey.equals(CONFIG_VERSION_NOTIFICATIONS)) { |
127 | | - newConfig.set(newKey, !(Boolean) value); |
128 | | - } else { |
129 | | - newConfig.set(newKey, value); |
130 | | - } |
131 | | - plugin.getFancyLogger().info("> CONFIG: Set option '" + key + "' to '" + value + "' from old config."); |
| 114 | + String newKey = keyMap.getOrDefault(key, null); |
| 115 | + if (newKey != null) { |
| 116 | + if (newKey.equals(CONFIG_VERSION_NOTIFICATIONS)) { |
| 117 | + newConfig.set(newKey, !(Boolean) value); |
132 | 118 | } else { |
133 | | - plugin.getFancyLogger().warn("> CONFIG: Option '" + key + "' is deprecated/invalid! Please migrate this manually from config_old.yml"); |
| 119 | + newConfig.set(newKey, value); |
134 | 120 | } |
135 | | - }); |
| 121 | + plugin.getFancyLogger().info("> CONFIG: Set option '" + key + "' to '" + value + "' from old config."); |
| 122 | + } else { |
| 123 | + plugin.getFancyLogger().warn("> CONFIG: Option '" + key + "' is deprecated/invalid! Please migrate this manually from config_old.yml"); |
| 124 | + } |
| 125 | + }); |
136 | 126 |
|
137 | | - newConfig.set(CONFIG_VERSION, latestVersion); |
138 | | - setOptions(newConfig); |
139 | | - CONFIG_COMMENTS.forEach(config::setInlineComments); |
| 127 | + newConfig.set(CONFIG_VERSION, latestVersion); |
| 128 | + setOptions(newConfig); |
| 129 | + CONFIG_COMMENTS.forEach(config::setInlineComments); |
140 | 130 |
|
141 | | - plugin.getFancyLogger().info("Configuration has finished migrating. Please double check your settings in config.yml."); |
| 131 | + plugin.getFancyLogger().info("Configuration has finished migrating. Please double check your settings in config.yml."); |
142 | 132 | } |
143 | 133 |
|
144 | 134 | private void setOptions(@NotNull FileConfiguration config) { |
|
0 commit comments