mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancyholograms v3: Use new config system
This commit is contained in:
@@ -1,16 +1,7 @@
|
||||
package de.oliver.fancyholograms.api;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface HologramConfiguration {
|
||||
|
||||
/**
|
||||
* Reloads the configuration.
|
||||
*
|
||||
* @param plugin The plugin instance.
|
||||
*/
|
||||
void reload(@NotNull FancyHolograms plugin);
|
||||
|
||||
/**
|
||||
* Returns whether version notifications are muted.
|
||||
*
|
||||
|
||||
@@ -43,7 +43,7 @@ public final class FancyHologramsCMD extends Command {
|
||||
MessageHelper.success(sender, "Saved all holograms");
|
||||
}
|
||||
case "reload" -> {
|
||||
this.plugin.getHologramConfiguration().reload(plugin);
|
||||
this.plugin.getFHConfiguration().reload();
|
||||
|
||||
this.plugin.getRegistry().clear();
|
||||
Collection<HologramData> hologramData = this.plugin.getStorage().loadAll();
|
||||
|
||||
@@ -1,126 +1,115 @@
|
||||
package de.oliver.fancyholograms.config;
|
||||
|
||||
import de.oliver.fancyholograms.api.FancyHolograms;
|
||||
import com.fancyinnovations.config.Config;
|
||||
import com.fancyinnovations.config.ConfigField;
|
||||
import de.oliver.fancyholograms.api.HologramConfiguration;
|
||||
import de.oliver.fancyholograms.main.FancyHologramsPlugin;
|
||||
import de.oliver.fancylib.ConfigHelper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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 FHConfiguration implements HologramConfiguration {
|
||||
|
||||
/**
|
||||
* Indicates whether version notifications are muted.
|
||||
*/
|
||||
private boolean versionNotifsMuted;
|
||||
private static final String CONFIG_FILE_PATH = "plugins/FancyHolograms/config.yml";
|
||||
private static final String MUTE_VERSION_NOTIFICATION_PATH = "mute_version_notification";
|
||||
private static final String ENABLE_AUTOSAVE_PATH = "enable_autosave";
|
||||
private static final String AUTOSAVE_INTERVAL_PATH = "autosave_interval";
|
||||
private static final String SAVE_ON_CHANGED_PATH = "save_on_changed";
|
||||
private static final String VISIBILITY_DISTANCE_PATH = "visibility_distance";
|
||||
private static final String REGISTER_COMMANDS_PATH = "register_commands";
|
||||
private static final String LOG_LEVEL_PATH = "log_level";
|
||||
|
||||
/**
|
||||
* Indicates whether autosave is enabled.
|
||||
*/
|
||||
private boolean autosaveEnabled;
|
||||
private Config config;
|
||||
|
||||
/**
|
||||
* The interval at which autosave is performed.
|
||||
*/
|
||||
private int autosaveInterval;
|
||||
public void init() {
|
||||
config = new Config(FancyHologramsPlugin.get().getFancyLogger(), CONFIG_FILE_PATH);
|
||||
|
||||
/**
|
||||
* Indicates whether the plugin should save holograms when they are changed.
|
||||
*/
|
||||
private boolean saveOnChangedEnabled;
|
||||
|
||||
/**
|
||||
* The default visibility distance for holograms.
|
||||
*/
|
||||
private int defaultVisibilityDistance;
|
||||
config.addField(new ConfigField<>(
|
||||
MUTE_VERSION_NOTIFICATION_PATH,
|
||||
"Whether version notifications are muted.",
|
||||
false,
|
||||
Boolean.class
|
||||
));
|
||||
|
||||
/**
|
||||
* Indicates whether commands should be registered.
|
||||
* <p>
|
||||
* This is useful for users who want to use the plugin's API only.
|
||||
*/
|
||||
private boolean registerCommands;
|
||||
config.addField(new ConfigField<>(
|
||||
ENABLE_AUTOSAVE_PATH,
|
||||
"Whether autosave is enabled.",
|
||||
true,
|
||||
Boolean.class
|
||||
));
|
||||
|
||||
/**
|
||||
* The log level for the plugin.
|
||||
*/
|
||||
private String logLevel;
|
||||
config.addField(new ConfigField<>(
|
||||
AUTOSAVE_INTERVAL_PATH,
|
||||
"The interval at which autosave is performed in minutes.",
|
||||
15,
|
||||
Integer.class
|
||||
));
|
||||
|
||||
@Override
|
||||
public void reload(@NotNull FancyHolograms plugin) {
|
||||
FancyHologramsPlugin pluginImpl = (FancyHologramsPlugin) plugin;
|
||||
pluginImpl.reloadConfig();
|
||||
config.addField(new ConfigField<>(
|
||||
SAVE_ON_CHANGED_PATH,
|
||||
"Whether the plugin should save holograms when they are changed.",
|
||||
true,
|
||||
Boolean.class
|
||||
));
|
||||
|
||||
final var config = pluginImpl.getConfig();
|
||||
config.addField(new ConfigField<>(
|
||||
VISIBILITY_DISTANCE_PATH,
|
||||
"The default visibility distance for holograms.",
|
||||
20,
|
||||
Integer.class
|
||||
));
|
||||
|
||||
versionNotifsMuted = (boolean) ConfigHelper.getOrDefault(config, "mute_version_notification", false);
|
||||
config.setInlineComments("mute_version_notification", List.of("Whether version notifications are muted."));
|
||||
config.addField(new ConfigField<>(
|
||||
REGISTER_COMMANDS_PATH,
|
||||
"Whether the plugin should register its commands.",
|
||||
true,
|
||||
Boolean.class
|
||||
));
|
||||
|
||||
autosaveEnabled = (boolean) ConfigHelper.getOrDefault(config, "enable_autosave", true);
|
||||
config.setInlineComments("enable_autosave", List.of("Whether autosave is enabled."));
|
||||
config.addField(new ConfigField<>(
|
||||
LOG_LEVEL_PATH,
|
||||
"The log level for the plugin (DEBUG, INFO, WARN, ERROR).",
|
||||
"INFO",
|
||||
String.class
|
||||
));
|
||||
|
||||
autosaveInterval = (int) ConfigHelper.getOrDefault(config, "autosave_interval", 15);
|
||||
config.setInlineComments("autosave_interval", List.of("The interval at which autosave is performed in minutes."));
|
||||
config.reload();
|
||||
}
|
||||
|
||||
saveOnChangedEnabled = (boolean) ConfigHelper.getOrDefault(config, "save_on_changed", true);
|
||||
config.setInlineComments("save_on_changed", List.of("Whether the plugin should save holograms when they are changed."));
|
||||
|
||||
defaultVisibilityDistance = (int) ConfigHelper.getOrDefault(config, "visibility_distance", 20);
|
||||
config.setInlineComments("visibility_distance", List.of("The default visibility distance for holograms."));
|
||||
|
||||
registerCommands = (boolean) ConfigHelper.getOrDefault(config, "register_commands", true);
|
||||
config.setInlineComments("register_commands", List.of("Whether the plugin should register its commands."));
|
||||
|
||||
config.set("report_errors_to_sentry", null);
|
||||
config.setInlineComments("report_errors_to_sentry", null);
|
||||
|
||||
config.setInlineComments("log_level", List.of("The log level for the plugin (DEBUG, INFO, WARN, ERROR)."));
|
||||
logLevel = (String) ConfigHelper.getOrDefault(config, "log_level", "INFO");
|
||||
|
||||
if (pluginImpl.isEnabled()) {
|
||||
plugin.getHologramThread().submit(pluginImpl::saveConfig);
|
||||
} else {
|
||||
// Can't dispatch task if plugin is disabled
|
||||
pluginImpl.saveConfig();
|
||||
}
|
||||
public void reload() {
|
||||
config.reload();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areVersionNotificationsMuted() {
|
||||
return versionNotifsMuted;
|
||||
return config.get(MUTE_VERSION_NOTIFICATION_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutosaveEnabled() {
|
||||
return autosaveEnabled;
|
||||
return config.get(ENABLE_AUTOSAVE_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAutosaveInterval() {
|
||||
return autosaveInterval;
|
||||
return config.get(AUTOSAVE_INTERVAL_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSaveOnChangedEnabled() {
|
||||
return saveOnChangedEnabled;
|
||||
return config.get(SAVE_ON_CHANGED_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDefaultVisibilityDistance() {
|
||||
return defaultVisibilityDistance;
|
||||
return config.get(VISIBILITY_DISTANCE_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRegisterCommands() {
|
||||
return registerCommands;
|
||||
return config.get(REGISTER_COMMANDS_PATH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLogLevel() {
|
||||
return logLevel;
|
||||
return config.get(LOG_LEVEL_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public final class FancyHologramsPlugin extends JavaPlugin implements FancyHolog
|
||||
private final ScheduledExecutorService hologramThread;
|
||||
private final ExecutorService storageThread;
|
||||
|
||||
private final HologramConfiguration configuration;
|
||||
private final FHConfiguration configuration;
|
||||
|
||||
private Function<HologramData, Hologram> hologramFactory;
|
||||
|
||||
@@ -132,7 +132,7 @@ public final class FancyHologramsPlugin extends JavaPlugin implements FancyHolog
|
||||
@Override
|
||||
public void onLoad() {
|
||||
FHFeatureFlags.load();
|
||||
configuration.reload(this);
|
||||
configuration.init();
|
||||
|
||||
LogLevel logLevel;
|
||||
try {
|
||||
@@ -373,7 +373,7 @@ public final class FancyHologramsPlugin extends JavaPlugin implements FancyHolog
|
||||
return this.storageThread;
|
||||
}
|
||||
|
||||
public HologramConfiguration getFHConfiguration() {
|
||||
public FHConfiguration getFHConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user