forked from FancyInnovations/FancyPlugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFancyHologramsConfiguration.java
More file actions
224 lines (194 loc) · 9.27 KB
/
Copy pathFancyHologramsConfiguration.java
File metadata and controls
224 lines (194 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package de.oliver.fancyholograms;
import com.fancyinnovations.config.ConfigHelper;
import de.oliver.fancyholograms.api.FancyHologramsPlugin;
import de.oliver.fancyholograms.api.HologramConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* The FancyHologramsConfig class is responsible for managing the configuration of the FancyHolograms plugin.
* It handles loading and saving hologram data, as well as providing access to various configuration settings.
*/
public final class FancyHologramsConfiguration implements HologramConfiguration {
private static final String CONFIG_AUTOSAVE_ENABLED = "saving.autosave.enabled";
private static final String CONFIG_AUTOSAVE_INTERVAL = "saving.autosave.interval";
private static final String CONFIG_SAVE_ON_CHANGED = "saving.save_on_changed";
private static final String CONFIG_LOG_LEVEL = "logging.log_level";
private static final String CONFIG_LOG_ON_WORLD_LOAD = "logging.log_on_world_load";
private static final String CONFIG_VERSION_NOTIFICATIONS = "logging.version_notifications";
private static final String CONFIG_VISIBILITY_DISTANCE = "visibility_distance";
private static final String CONFIG_REGISTER_COMMANDS = "register_commands";
private static final String CONFIG_UPDATE_VISIBILITY_INTERVAL = "update_visibility_interval";
private static final String CONFIG_HOLOGRAM_UPDATE_INTERVAL = "performance.hologram_update_interval_ms";
private static final String CONFIG_REPORT_ERRORS_TO_SENTRY = "report_errors_to_sentry";
private static final String CONFIG_VERSION = "config_version";
private static final Map<String, List<String>> CONFIG_COMMENTS = Map.ofEntries(
Map.entry(CONFIG_VERSION, List.of("Config version, do not modify.")),
Map.entry(CONFIG_AUTOSAVE_ENABLED, List.of("Whether autosave is enabled.")),
Map.entry(CONFIG_AUTOSAVE_INTERVAL, List.of("The interval at which autosave is performed in minutes.")),
Map.entry(CONFIG_SAVE_ON_CHANGED, List.of("Whether the plugin should save holograms when they are changed.")),
Map.entry(CONFIG_LOG_LEVEL, List.of("The log level for the plugin (DEBUG, INFO, WARN, ERROR).")),
Map.entry(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.")),
Map.entry(CONFIG_VERSION_NOTIFICATIONS, List.of("Whether the plugin should send notifications for new updates.")),
Map.entry(CONFIG_VISIBILITY_DISTANCE, List.of("The default visibility distance for holograms.")),
Map.entry(CONFIG_REGISTER_COMMANDS, List.of("Whether the plugin should register its commands.")),
Map.entry(CONFIG_UPDATE_VISIBILITY_INTERVAL, List.of("The interval at which hologram visibility is updated in ticks.")),
Map.entry(CONFIG_HOLOGRAM_UPDATE_INTERVAL, List.of("The interval at which text holograms refresh placeholder updates in milliseconds. Lower values are more responsive but use more CPU."))
);
/**
* Indicates whether autosave is enabled.
*/
private boolean autosaveEnabled;
/**
* The interval at which autosave is performed.
*/
private int autosaveInterval;
/**
* Indicates whether the plugin should save holograms when they are changed.
*/
private boolean saveOnChangedEnabled;
/**
* The log level for the plugin.
*/
private String logLevel;
/**
* Indicates whether hologram loading should be logged on world loading.
*/
private boolean hologramLoadLogging;
/**
* Indicates whether version notifications are enabled or disabled.
*/
private boolean versionNotifs;
/**
* The default visibility distance for holograms.
*/
private int defaultVisibilityDistance;
/**
* Indicates whether commands should be registered.
* <p>
* This is useful for users who want to use the plugin's API only.
*/
private boolean registerCommands;
/**
* The interval at which hologram visibility is updated.
*/
private int updateVisibilityInterval;
/**
* The interval at which text holograms refresh dynamic text updates.
*/
private int hologramUpdateInterval;
private void updateChecker(@NotNull FancyHolograms plugin, @NotNull FileConfiguration config) {
final int latestVersion = 1;
int configVersion = (int) ConfigHelper.getOrDefault(config, CONFIG_VERSION, 0);
if (configVersion >= latestVersion) {
setOptions(config);
return;
}
plugin.getFancyLogger().warn("Outdated config detected! Attempting to migrate previous settings to new config...");
var oldConfig = plugin.getConfig();
try {
File backupFile = new File(plugin.getDataFolder(), "config_old.yml");
oldConfig.save(backupFile);
} catch (IOException e) {
plugin.getFancyLogger().warn("Unable to backup config to config_old.yml:" + e);
}
var newConfig = plugin.getConfig();
Map<String, Object> oldConfigValues = oldConfig.getValues(true);
Map<String, String> keyMap = Map.of(
"enable_autosave", CONFIG_AUTOSAVE_ENABLED,
"autosave_interval", CONFIG_AUTOSAVE_INTERVAL,
"save_on_changed", CONFIG_SAVE_ON_CHANGED,
"log_level", CONFIG_LOG_LEVEL,
"mute_version_notifications", CONFIG_VERSION_NOTIFICATIONS
);
oldConfigValues.forEach((key, value) -> {
String newKey = keyMap.getOrDefault(key, null);
if (newKey != null) {
if (newKey.equals(CONFIG_VERSION_NOTIFICATIONS)) {
newConfig.set(newKey, !(Boolean) value);
} else {
newConfig.set(newKey, value);
}
plugin.getFancyLogger().info("> CONFIG: Set option '" + key + "' to '" + value + "' from old config.");
} else {
plugin.getFancyLogger().warn("> CONFIG: Option '" + key + "' is deprecated/invalid! Please migrate this manually from config_old.yml");
}
});
newConfig.set(CONFIG_VERSION, latestVersion);
setOptions(newConfig);
CONFIG_COMMENTS.forEach(config::setInlineComments);
plugin.getFancyLogger().info("Configuration has finished migrating. Please double check your settings in config.yml.");
}
private void setOptions(@NotNull FileConfiguration config) {
// saving
autosaveEnabled = (boolean) ConfigHelper.getOrDefault(config, CONFIG_AUTOSAVE_ENABLED, true);
autosaveInterval = (int) ConfigHelper.getOrDefault(config, CONFIG_AUTOSAVE_INTERVAL, 15);
saveOnChangedEnabled = (boolean) ConfigHelper.getOrDefault(config, CONFIG_SAVE_ON_CHANGED, true);
// logging
logLevel = (String) ConfigHelper.getOrDefault(config, CONFIG_LOG_LEVEL, "INFO");
hologramLoadLogging = (boolean) ConfigHelper.getOrDefault(config, CONFIG_LOG_ON_WORLD_LOAD, true);
versionNotifs = (boolean) ConfigHelper.getOrDefault(config, CONFIG_VERSION_NOTIFICATIONS, true);
// options
defaultVisibilityDistance = (int) ConfigHelper.getOrDefault(config, CONFIG_VISIBILITY_DISTANCE, 20);
registerCommands = (boolean) ConfigHelper.getOrDefault(config, CONFIG_REGISTER_COMMANDS, true);
updateVisibilityInterval = (int) ConfigHelper.getOrDefault(config, CONFIG_UPDATE_VISIBILITY_INTERVAL, 20);
hologramUpdateInterval = Math.max(10, (int) ConfigHelper.getOrDefault(config, CONFIG_HOLOGRAM_UPDATE_INTERVAL, 200));
config.set(CONFIG_REPORT_ERRORS_TO_SENTRY, null);
}
@Override
public synchronized void reload(@NotNull FancyHologramsPlugin plugin) {
FancyHolograms pluginImpl = (FancyHolograms) plugin;
pluginImpl.reloadConfig();
var config = pluginImpl.getConfig();
updateChecker(pluginImpl, config);
if (pluginImpl.isEnabled() && !plugin.getHologramThread().isShutdown()) {
plugin.getHologramThread().submit(pluginImpl::saveConfig);
} else {
// Can't dispatch task if plugin is disabled
pluginImpl.saveConfig();
}
}
@Override
public boolean isAutosaveEnabled() {
return autosaveEnabled;
}
@Override
public int getAutosaveInterval() {
return autosaveInterval;
}
@Override
public boolean isSaveOnChangedEnabled() {
return saveOnChangedEnabled;
}
@Override
public String getLogLevel() {
return logLevel;
}
@Override
public boolean isHologramLoadLogging() {
return hologramLoadLogging;
}
@Override
public boolean areVersionNotificationsEnabled() {
return versionNotifs;
}
@Override
public int getDefaultVisibilityDistance() {
return defaultVisibilityDistance;
}
@Override
public boolean isRegisterCommands() {
return registerCommands;
}
@Override
public int getUpdateVisibilityInterval() {
return updateVisibilityInterval;
}
@Override
public int getHologramUpdateInterval() {
return hologramUpdateInterval;
}
}