Proper initialization of some classes with the injector

This commit is contained in:
Jaime Martinez Rincon 2017-08-10 10:45:05 +02:00
parent 4071fc130d
commit aba3c161d7
6 changed files with 50 additions and 38 deletions

View File

@ -83,6 +83,7 @@
<version>1.11-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>configme</artifactId>
@ -96,6 +97,7 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.jalu</groupId>
<artifactId>injector</artifactId>
@ -123,15 +125,17 @@
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bungeecord</artifactId>
<version>LATEST</version>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -1,5 +1,6 @@
package com.jaimemartz.playerbalancer;
import ch.jalu.configme.SettingsManager;
import ch.jalu.injector.Injector;
import ch.jalu.injector.InjectorBuilder;
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
@ -11,10 +12,13 @@ import com.jaimemartz.playerbalancer.manager.PasteHelper;
import com.jaimemartz.playerbalancer.manager.PlayerLocker;
import com.jaimemartz.playerbalancer.ping.StatusManager;
import com.jaimemartz.playerbalancer.section.SectionManager;
import com.jaimemartz.playerbalancer.settings.Settings;
import com.jaimemartz.playerbalancer.settings.SettingsProvider;
import com.jaimemartz.playerbalancer.settings.beans.SectionHandler;
import com.jaimemartz.playerbalancer.settings.types.*;
import com.jaimemartz.playerbalancer.settings.provider.SectionHandlerProvider;
import com.jaimemartz.playerbalancer.settings.provider.SettingsProvider;
import com.jaimemartz.playerbalancer.settings.types.CheckerProperties;
import com.jaimemartz.playerbalancer.settings.types.CommandProperties;
import com.jaimemartz.playerbalancer.settings.types.GeneralProperties;
import com.jaimemartz.playerbalancer.settings.types.ReconnectorProperties;
import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Command;
@ -33,7 +37,7 @@ public class PlayerBalancer extends Plugin {
//Private instances
private Injector injector;
private Settings settings;
private SettingsManager settings;
private StatusManager statusManager;
private SectionManager sectionManager;
@ -42,19 +46,15 @@ public class PlayerBalancer extends Plugin {
@Override
public void onEnable() {
getDataFolder().mkdir();
injector = new InjectorBuilder()
.addDefaultHandlers(getClass().getPackage().getName())
.create();
injector.register(PlayerBalancer.class, this);
injector.register(ProxyServer.class, this.getProxy());
injector.register(SettingsProvider.class, new SettingsProvider(this.getDataFolder()));
injector.registerProvider(Settings.class, SettingsProvider.class);
settings = injector.getSingleton(Settings.class);
injector.register(SectionHandler.class, settings.getProperty(SectionsHolder.SECTION_HOLDER));
injector.registerProvider(SettingsManager.class, SettingsProvider.class);
injector.registerProvider(SectionHandler.class, SectionHandlerProvider.class);
settings = injector.getSingleton(SettingsManager.class);
Metrics metrics = new Metrics(this);
if (this.enable()) {
@ -93,7 +93,7 @@ public class PlayerBalancer extends Plugin {
}
if (settings.getProperty(CommandProperties.ENABLED)) {
fallbackCommand = new FallbackCommand(this);
fallbackCommand = injector.newInstance(FallbackCommand.class);
getProxy().getPluginManager().registerCommand(this, fallbackCommand);
}

View File

@ -1,9 +1,9 @@
package com.jaimemartz.playerbalancer.commands;
import ch.jalu.configme.SettingsManager;
import com.google.common.collect.Iterables;
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.settings.Settings;
import com.jaimemartz.playerbalancer.settings.beans.MapBean;
import com.jaimemartz.playerbalancer.settings.types.CommandProperties;
import com.jaimemartz.playerbalancer.settings.types.GeneralProperties;
@ -22,13 +22,13 @@ import java.util.concurrent.Callable;
public class FallbackCommand extends Command {
@Inject
private Settings settings;
private SettingsManager settings;
@Inject
private SectionsHolder sections;
@Inject
public FallbackCommand(Settings settings) {
public FallbackCommand(SettingsManager settings) {
super(
settings.getProperty(CommandProperties.COMMAND).getName(),
settings.getProperty(CommandProperties.COMMAND).getPermission(),

View File

@ -1,12 +0,0 @@
package com.jaimemartz.playerbalancer.settings;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.migration.MigrationService;
import ch.jalu.configme.resource.PropertyResource;
public class Settings extends SettingsManager {
public Settings(PropertyResource resource, MigrationService migrationService, ConfigurationData configurationData) {
super(resource, migrationService, configurationData);
}
}

View File

@ -0,0 +1,18 @@
package com.jaimemartz.playerbalancer.settings.provider;
import ch.jalu.configme.SettingsManager;
import com.jaimemartz.playerbalancer.settings.beans.SectionHandler;
import com.jaimemartz.playerbalancer.settings.types.SectionsHolder;
import javax.inject.Inject;
import javax.inject.Provider;
public class SectionHandlerProvider implements Provider<SectionHandler> {
@Inject
private SettingsManager settings;
@Override
public SectionHandler get() {
return settings.getProperty(SectionsHolder.SECTION_HOLDER);
}
}

View File

@ -1,26 +1,28 @@
package com.jaimemartz.playerbalancer.settings;
package com.jaimemartz.playerbalancer.settings.provider;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.migration.PlainMigrationService;
import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.YamlFileResource;
import com.jaimemartz.playerbalancer.settings.types.*;
import net.md_5.bungee.api.plugin.Plugin;
import javax.inject.Inject;
import javax.inject.Provider;
import java.io.File;
import java.io.IOException;
public class SettingsProvider implements Provider<Settings> {
private final File dataFolder;
public SettingsProvider(File dataFolder) {
this.dataFolder = dataFolder;
}
public class SettingsProvider implements Provider<SettingsManager> {
@Inject
private Plugin plugin;
@Override
public Settings get() {
File configFile = new File(dataFolder, "config.yml");
public SettingsManager get() {
plugin.getDataFolder().mkdir();
File configFile = new File(plugin.getDataFolder(), "config.yml");
if (!configFile.exists()) {
try {
boolean result = configFile.createNewFile();
@ -38,6 +40,6 @@ public class SettingsProvider implements Provider<Settings> {
CommandProperties.class, MessageProperties.class, SectionsHolder.class
);
return new Settings(resource, new PlainMigrationService(), configurationData);
return new SettingsManager(resource, new PlainMigrationService(), configurationData);
}
}