Skip to content

Commit

Permalink
[Configuration] Rework config loading (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Aug 13, 2024
1 parent 385516f commit c15934f
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,6 @@ public void loadFeaturesFromConfig() {
Config config = TAB.getInstance().getConfiguration().getConfig();
FeatureManager featureManager = TAB.getInstance().getFeatureManager();

if (config.getLayout() != null) {
if (config.getPerWorldPlayerList() != null) TAB.getInstance().getConfigHelper().startup().bothPerWorldPlayerListAndLayoutEnabled();
if (config.getPlayerlistObjective() != null) TAB.getInstance().getConfigHelper().startup().layoutBreaksYellowNumber();
if (config.isPreventSpectatorEffect()) TAB.getInstance().getConfigHelper().hint().layoutIncludesPreventSpectatorEffect();
if (config.getGlobalPlayerList() != null) TAB.getInstance().getConfigHelper().startup().bothGlobalPlayerListAndLayoutEnabled();
}

// Load the feature first, because it will be processed in main thread (to make it run before feature threads)
if (config.isEnableRedisHook()) {
RedisSupport redis = TAB.getInstance().getPlatform().getRedisSupport();
Expand Down
1 change: 0 additions & 1 deletion shared/src/main/java/me/neznamy/tab/shared/TAB.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public String load() {
if (eventBus != null) eventBus.fire(TabLoadEventImpl.getInstance());
pluginDisabled = false;
cpu.enable();
configHelper.startup().checkErrorLog();
configHelper.startup().printWarnCount();
platform.logInfo(TabComponent.fromColoredText(EnumChatFormat.GREEN + "Enabled in " + (System.currentTimeMillis()-time) + "ms"));
return configuration.getMessages().getReloadSuccess();
Expand Down
17 changes: 10 additions & 7 deletions shared/src/main/java/me/neznamy/tab/shared/config/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import me.neznamy.tab.shared.config.file.ConfigurationFile;
import me.neznamy.tab.shared.config.file.YamlConfigurationFile;
import me.neznamy.tab.shared.config.file.YamlPropertyConfigurationFile;
import me.neznamy.tab.shared.config.files.animations.Animations;
import me.neznamy.tab.shared.config.files.config.Config;
import me.neznamy.tab.shared.config.mysql.MySQL;
import me.neznamy.tab.shared.config.mysql.MySQLGroupConfiguration;
import me.neznamy.tab.shared.config.mysql.MySQLUserConfiguration;
import me.neznamy.tab.shared.config.section.AnimationConfiguration;
import me.neznamy.tab.shared.features.globalplayerlist.GlobalPlayerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -31,9 +31,8 @@ public class Configs {
/** config.yml file */
private final Config config = new Config();

//animations.yml file
private final ConfigurationFile animationFile = new YamlConfigurationFile(getClass().getClassLoader().getResourceAsStream("config/animations.yml"),
new File(TAB.getInstance().getDataFolder(), "animations.yml"));
/** animations.yml file */
private final Animations animations = new Animations();

//messages.yml file
private final MessageFile messages = new MessageFile();
Expand All @@ -47,8 +46,6 @@ public class Configs {

private MySQL mysql;

@NotNull private final AnimationConfiguration animations = new AnimationConfiguration(animationFile);

/**
* Constructs new instance and loads configuration files.
* If needed, converts old configuration files as well.
Expand All @@ -59,7 +56,13 @@ public class Configs {
* if files contain syntax errors
*/
public Configs() throws IOException {
new Converter().convert2810to290(animationFile);
File errorLog = TAB.getInstance().getErrorManager().getErrorLog();
if (errorLog.length() > TabConstants.MAX_LOG_SIZE) {
TAB.getInstance().getConfigHelper().startup().startupWarn(errorLog, "The file has reached its size limit (1MB). No new errors will be logged. " +
"Take a look at the existing logged errors, as they may have caused the plugin to not work properly " +
"in the past and if not fixed, will most likely cause problems in the future as well. If you are using latest version " +
"of the plugin, consider reporting them.");
}
if (config.getMysql() != null) {
try {
mysql = new MySQL(config.getMysql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import lombok.RequiredArgsConstructor;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.chat.EnumChatFormat;
import me.neznamy.tab.shared.chat.TabComponent;
import me.neznamy.tab.shared.config.file.ConfigurationFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -39,11 +37,11 @@ protected void checkForUnknownKey(@NotNull String[] section, @NotNull List<Strin
}

protected void startupWarn(@NotNull String message) {
TAB.getInstance().getConfigHelper().startup().startupWarn("[" + file.getFile().getName() + "] " + message);
TAB.getInstance().getConfigHelper().startup().startupWarn(file.getFile(), message);
}

protected void hint(@NotNull String message) {
TAB.getInstance().getPlatform().logInfo(TabComponent.fromColoredText(EnumChatFormat.GOLD + "[Hint] " + message));
TAB.getInstance().getConfigHelper().hint(file.getFile(), message);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.neznamy.tab.shared.config.section;
package me.neznamy.tab.shared.config.files.animations;

import lombok.RequiredArgsConstructor;
import me.neznamy.tab.shared.TabConstants;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.neznamy.tab.shared.config.files.animations;

import lombok.Getter;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.config.Converter;
import me.neznamy.tab.shared.config.file.ConfigurationFile;
import me.neznamy.tab.shared.config.file.YamlConfigurationFile;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

@Getter
public class Animations {

private final ConfigurationFile animationFile = new YamlConfigurationFile(getClass().getClassLoader().getResourceAsStream("config/animations.yml"),
new File(TAB.getInstance().getDataFolder(), "animations.yml"));

@NotNull private final AnimationConfiguration animations;

public Animations() throws IOException {
new Converter().convert2810to290(animationFile);
animations = new AnimationConfiguration(animationFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ public Config() throws IOException {
if (config.getBoolean("scoreboard-teams.enabled", true) || config.getBoolean("layout.enabled", false)) sorting = new SortingConfiguration(config);
if (config.getBoolean("tablist-name-formatting.enabled", false)) tablistFormatting = new TablistFormattingConfiguration(config);
if (config.getBoolean("scoreboard-teams.enabled", false)) teams = new TeamConfiguration(config);

if (layout != null) {
if (perWorldPlayerList != null) {
TAB.getInstance().getConfigHelper().startup().startupWarn(config.getFile(), "Both per world playerlist and layout features are enabled, but layout makes per world playerlist redundant. " +
"Layout automatically works with all connected players and replaces real player entries with" +
" fake players, making per world playerlist completely useless as real players are pushed out of the playerlist. " +
"Disable per world playerlist for the same result, but with better performance.");
}
if (playerlistObjective != null) {
TAB.getInstance().getConfigHelper().startup().startupWarn(config.getFile(), "Layout feature breaks playerlist-objective feature, because it replaces real player with fake slots " +
"with different usernames for more reliable functionality. Disable playerlist-objective feature, as it will only look bad " +
"and consume resources.");
}
if (preventSpectatorEffect) {
TAB.getInstance().getConfigHelper().hint(config.getFile(), "Layout feature automatically includes prevent-spectator-effect, therefore the feature can be disabled " +
"for better performance, as it is not needed at all (assuming it is configured to always display some layout).");
}
if (globalPlayerList != null) {
TAB.getInstance().getConfigHelper().startup().startupWarn(config.getFile(), "Both global playerlist and layout features are enabled, but layout makes global playerlist redundant. " +
"Layout automatically works with all connected players on the proxy and replaces real player entries with" +
" fake players, making global playerlist completely useless. " +
"Disable global playerlist for the same result, but with better performance.");
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package me.neznamy.tab.shared.config.helper;

import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.chat.EnumChatFormat;
import me.neznamy.tab.shared.chat.TabComponent;
import org.jetbrains.annotations.NotNull;

import java.io.File;

/**
* Class for detecting misconfiguration in config files and fix mistakes
* to avoid headaches when making a configuration mistake.
Expand All @@ -9,9 +16,6 @@ public class ConfigHelper {
/** Printer for startup warns */
private final StartupWarnPrinter startupWarnPrinter = new StartupWarnPrinter();

/** Hint detector */
private final HintPrinter hintPrinter = new HintPrinter();

/** Printer for runtime errors */
private final RuntimeErrorPrinter runtimeErrorPrinter = new RuntimeErrorPrinter();

Expand All @@ -25,20 +29,23 @@ public StartupWarnPrinter startup() {
}

/**
* Returns hint printer.
* Returns runtime error printer.
*
* @return hint printer
* @return runtime error printer
*/
public HintPrinter hint() {
return hintPrinter;
public RuntimeErrorPrinter runtime() {
return runtimeErrorPrinter;
}

/**
* Returns runtime error printer.
* Prints a configuration hint into console, typically when a redundancy is found.
*
* @return runtime error printer
* @param file
* File where the redundancy was found
* @param message
* Hint message to print
*/
public RuntimeErrorPrinter runtime() {
return runtimeErrorPrinter;
public void hint(@NotNull File file, @NotNull String message) {
TAB.getInstance().getPlatform().logInfo(TabComponent.fromColoredText(EnumChatFormat.GOLD + "[" + file.getName() + "] [Hint] " + message));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package me.neznamy.tab.shared.config.helper;

import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.TabComponent;
import me.neznamy.tab.shared.features.sorting.types.SortingType;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.*;
import java.util.Set;

/**
* Class for printing startup warns when features are
Expand Down Expand Up @@ -44,35 +43,6 @@ public void invalidConditionPattern(@NotNull String conditionName, @NotNull Stri
startupWarn("Line \"" + line + "\" in condition " + conditionName + " is not a valid condition pattern.");
}

public void bothGlobalPlayerListAndLayoutEnabled() {
startupWarn("Both global playerlist and layout features are enabled, but layout makes global playerlist redundant.",
"Layout automatically works with all connected players on the proxy and replaces real player entries with" +
" fake players, making global playerlist completely useless.",
"Disable global playerlist for the same result, but with better performance.");
}

public void bothPerWorldPlayerListAndLayoutEnabled() {
startupWarn("Both per world playerlist and layout features are enabled, but layout makes per world playerlist redundant.",
"Layout automatically works with all connected players and replaces real player entries with" +
" fake players, making per world playerlist completely useless as real players are pushed out of the playerlist.",
"Disable per world playerlist for the same result, but with better performance.");
}

public void layoutBreaksYellowNumber() {
startupWarn("Layout feature breaks playerlist-objective feature, because it replaces real player with fake slots " +
"with different usernames for more reliable functionality. Disable playerlist-objective feature, as it will only look bad " +
"and consume resources.");
}

public void checkErrorLog() {
File errorLog = TAB.getInstance().getErrorManager().getErrorLog();
if (errorLog.length() > TabConstants.MAX_LOG_SIZE) {
startupWarn("File " + errorLog.getPath() + " has reached its size limit (1MB). No new errors will be logged. " +
"Take a look at the existing reported errors, as they may have caused the plugin to not work properly " +
"in the past and if not fixed, will most likely cause problems in the future as well.");
}
}

public void incompleteSortingLine(@NotNull String configuredLine) {
startupWarn("Sorting line \"" + configuredLine + "\" is incomplete.");
}
Expand All @@ -90,6 +60,11 @@ public void startupWarn(@NotNull String... messages) {
}
}

public void startupWarn(@NotNull File file, @NotNull String message) {
warnCount++;
TAB.getInstance().getPlatform().logWarn(TabComponent.fromColoredText("[" + file.getName() + "] " + message));
}

/**
* Prints amount of warns logged into console previously, if more than 0.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import me.neznamy.tab.shared.chat.rgb.RGBUtils;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.config.section.AnimationConfiguration.AnimationDefinition;
import me.neznamy.tab.shared.config.files.animations.AnimationConfiguration;
import me.neznamy.tab.shared.config.files.animations.AnimationConfiguration.AnimationDefinition;
import me.neznamy.tab.shared.features.PlaceholderManagerImpl;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -64,10 +65,8 @@ public Animation(@NotNull PlaceholderManagerImpl placeholderManager, @NonNull St
for (String placeholder : nestedPlaceholders) {
int localRefresh;
if (placeholder.startsWith("%animation:")) {
//nested animations may not be loaded into the system yet due to load order, manually getting the refresh interval
String nestedAnimation = placeholder.substring("%animation:".length(), placeholder.length()-1);
localRefresh = TAB.getInstance().getConfiguration().getAnimationFile().hasConfigOption(nestedAnimation + ".change-interval") ?
TAB.getInstance().getConfiguration().getAnimationFile().getInt(nestedAnimation + ".change-interval") : interval;
AnimationConfiguration cfg = TAB.getInstance().getConfiguration().getAnimations().getAnimations();
localRefresh = cfg.animations.containsKey(placeholder) ? cfg.animations.get(placeholder).changeInterval : interval;
} else {
localRefresh = placeholderManager.getPlaceholder(placeholder).getRefresh();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.config.files.config.ConditionsSection.ConditionDefinition;
import me.neznamy.tab.shared.config.files.config.PlaceholdersConfiguration;
import me.neznamy.tab.shared.config.section.AnimationConfiguration.AnimationDefinition;
import me.neznamy.tab.shared.config.files.animations.AnimationConfiguration.AnimationDefinition;
import me.neznamy.tab.shared.features.PlaceholderManagerImpl;
import me.neznamy.tab.shared.hook.LuckPermsHook;
import me.neznamy.tab.shared.placeholders.conditions.Condition;
Expand Down Expand Up @@ -125,7 +125,7 @@ private void registerPlayerPlaceholders(@NotNull PlaceholderManager manager) {
manager.registerPlayerPlaceholder(TabConstants.Placeholder.LUCKPERMS_SUFFIX, refresh,
p -> LuckPermsHook.getInstance().getSuffix((TabPlayer) p));
}
for (Entry<String, AnimationDefinition> entry : TAB.getInstance().getConfiguration().getAnimations().animations.entrySet()) {
for (Entry<String, AnimationDefinition> entry : TAB.getInstance().getConfiguration().getAnimations().getAnimations().animations.entrySet()) {
Animation a = new Animation((PlaceholderManagerImpl) manager, entry.getKey(), entry.getValue());
manager.registerPlayerPlaceholder(TabConstants.Placeholder.animation(a.getName()), a.getRefresh(), p -> a.getMessage());
}
Expand Down

0 comments on commit c15934f

Please sign in to comment.