Ensure configurable object is converted from map to object on /papi reload

This commit is contained in:
PiggyPiglet
2026-02-19 19:11:37 +08:00
parent 20aa9042f3
commit 0067540ec1
2 changed files with 35 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ package at.helpch.placeholderapi;
import at.helpch.placeholderapi.commands.PlaceholderCommandRouter;
import at.helpch.placeholderapi.configuration.ConfigManager;
import at.helpch.placeholderapi.expansion.PlaceholderExpansion;
import at.helpch.placeholderapi.expansion.manager.CloudExpansionManager;
import at.helpch.placeholderapi.expansion.manager.LocalExpansionManager;
import at.helpch.placeholderapi.metrics.MetricsManager;
@@ -78,6 +79,12 @@ public class PlaceholderAPIPlugin extends JavaPlugin {
configManager.setup();
localExpansionManager.load(sender);
for (final PlaceholderExpansion expansion : localExpansionManager.getExpansions()) {
if (expansion.getExpansionType() == PlaceholderExpansion.Type.INTERNAL) {
// when the config gets reloaded, getConfig for internal expansions will return a map instead of the correct object
localExpansionManager.createConfig(expansion);
}
}
if (configManager.config().cloudEnabled()) {
cloudExpansionManager.load();

View File

@@ -220,21 +220,7 @@ public final class LocalExpansionManager /*implements Listener*/ {
public boolean register(@NotNull final PlaceholderExpansion expansion) {
final String identifier = expansion.getIdentifier().toLowerCase(Locale.ROOT);
if (expansion instanceof Configurable<?> configurable) {
final PlaceholderAPIConfig config = configManager.config();
if (config.expansions() == null) {
config.expansions(new ConcurrentHashMap<>());
}
if (!config.expansions().containsKey(expansion.getIdentifier())) {
config.expansions().put(expansion.getIdentifier(), configurable.provideDefault());
configManager.save();
} else {
final Object expansionConfig = configManager.convertExpansion((Map<String, Object>) config.expansions().get(expansion.getIdentifier()), configurable.provideConfigType());
config.expansions().put(expansion.getIdentifier(), expansionConfig);
}
}
createConfig(expansion);
if (!expansion.canRegister()) {
return false;
@@ -307,6 +293,33 @@ public final class LocalExpansionManager /*implements Listener*/ {
return true;
}
/**
* Creates and initializes the configuration for the provided {@link PlaceholderExpansion}.
* If the expansion implements the {@link Configurable} interface, this method ensures that
* the expansion's default configuration is registered and saved if it is not already present.
* If a configuration already exists, it converts and updates it using the provided configuration type.
*
* @param expansion the {@link PlaceholderExpansion} for which the configuration is being created
*/
@ApiStatus.Internal
public void createConfig(PlaceholderExpansion expansion) {
if (expansion instanceof Configurable<?> configurable) {
final PlaceholderAPIConfig config = configManager.config();
if (config.expansions() == null) {
config.expansions(new ConcurrentHashMap<>());
}
if (!config.expansions().containsKey(expansion.getIdentifier())) {
config.expansions().put(expansion.getIdentifier(), configurable.provideDefault());
configManager.save();
} else {
final Object expansionConfig = configManager.convertExpansion((Map<String, Object>) config.expansions().get(expansion.getIdentifier()), configurable.provideConfigType());
config.expansions().put(expansion.getIdentifier(), expansionConfig);
}
}
}
@ApiStatus.Internal
public boolean unregister(@NotNull final PlaceholderExpansion expansion) {
if (expansions.remove(expansion.getIdentifier().toLowerCase(Locale.ROOT)) == null) {