Skip to content

Commit

Permalink
fix: create ConfigurationModule and ConfigurationProvider to manage C…
Browse files Browse the repository at this point in the history
…onfiguration injections
  • Loading branch information
MemencioPerez committed Nov 7, 2024
1 parent 056f008 commit 7136f7d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.velocitypowered.proxy.config.VelocityConfiguration;
import io.github._4drian3d.unsignedvelocity.commands.UnSignedVelocityCommand;
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.configuration.ConfigurationModule;
import io.github._4drian3d.unsignedvelocity.configuration.ConfigurationProvider;
import io.github._4drian3d.unsignedvelocity.listener.packet.ConfigurablePacketListener;
import io.github._4drian3d.unsignedvelocity.listener.packet.chat.ChatHeaderListener;
import io.github._4drian3d.unsignedvelocity.listener.packet.chat.ChatSessionListener;
Expand Down Expand Up @@ -51,7 +53,7 @@ public class UnSignedVelocity {
private final Path dataDirectory;
private final Metrics.Factory factory;
private final ComponentLogger logger;
private Configuration configuration;
private ConfigurationModule configurationModule;
private List<? extends ConfigurablePacketListener> packetListeners;

@Inject
Expand All @@ -68,15 +70,13 @@ public void onProxyInitialize(ProxyInitializeEvent event) {
factory.make(this, 17514);

try {
setupConfiguration();
setupConfigurationModule();
} catch (IOException e) {
logger.error("Cannot load configuration", e);
return;
}

injector = injector.createChildInjector(
binder -> binder.bind(Configuration.class).toInstance(configuration)
);
Configuration configuration = configurationModule.getConfigurationProvider().get();

if (configuration.removeSignedKeyOnJoin()) {
try {
Expand Down Expand Up @@ -104,8 +104,15 @@ public void onProxyInitialize(ProxyInitializeEvent event) {
updateChecker.checkForUpdates();
}

public void setupConfiguration() throws IOException {
configuration = Configuration.loadConfig(dataDirectory);
public void setupConfigurationModule() throws IOException {
Configuration configuration = Configuration.loadConfig(dataDirectory);
if (configurationModule == null) {
configurationModule = new ConfigurationModule(configuration);
injector = injector.createChildInjector(configurationModule);
} else {
ConfigurationProvider configurationProvider = configurationModule.getConfigurationProvider();
configurationProvider.updateConfiguration(configuration);
}
}

private void forciblyDisableForceKeyAuthentication() throws NoSuchFieldException, IllegalAccessException {
Expand Down Expand Up @@ -147,6 +154,7 @@ public void setupConfigurablePacketListeners() {
}

public List<Component> getPluginStatusMessages() {
Configuration configuration = configurationModule.getConfigurationProvider().get();
return List.of(
miniMessage().deserialize(
"<#6892bd>Remove Signed Key: <aqua>" + configuration.removeSignedKeyOnJoin()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static BrigadierCommand createBrigadierCommand(final UnSignedVelocity plu
case "reload" -> {
Component message;
try {
plugin.setupConfiguration();
plugin.setupConfigurationModule();
plugin.setupConfigurablePacketListeners();
message = miniMessage().deserialize("<gradient:#166D3B:#7F8C8D:#A29BFE>UnSignedVelocity</gradient> <#6892bd>has been successfully reloaded");
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github._4drian3d.unsignedvelocity.configuration;

import com.google.inject.AbstractModule;

public class ConfigurationModule extends AbstractModule {
private final ConfigurationProvider configurationProvider;

public ConfigurationModule(Configuration initialConfiguration) {
this.configurationProvider = new ConfigurationProvider(initialConfiguration);
}

@Override
protected void configure() {
bind(Configuration.class).toProvider(configurationProvider);
}

public ConfigurationProvider getConfigurationProvider() {
return configurationProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github._4drian3d.unsignedvelocity.configuration;

import com.google.inject.Provider;

public class ConfigurationProvider implements Provider<Configuration> {
private Configuration configuration;

public ConfigurationProvider(Configuration initialConfiguration) {
this.configuration = initialConfiguration;
}

public void updateConfiguration(Configuration newConfiguration) {
this.configuration = newConfiguration;
}

@Override
public Configuration get() {
return configuration;
}
}

0 comments on commit 7136f7d

Please sign in to comment.