Skip to content

Commit

Permalink
chore: move services logic into main class
Browse files Browse the repository at this point in the history
  • Loading branch information
aivruu committed Jan 29, 2024
1 parent 455c6d3 commit 3f8d313
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions plugin/src/main/java/com/aivruu/homes/HomesPlugin.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.aivruu.homes;

import com.aivruu.homes.command.CommandManager;
import com.aivruu.homes.config.ValueObjectConfigManager;
import com.aivruu.homes.config.model.ConfigModel;
import com.aivruu.homes.config.model.MessageConfigModel;
import com.aivruu.homes.home.HomeAggregate;
import com.aivruu.homes.listener.PlayerDataListener;
import com.aivruu.homes.repository.PlayerModelRepository;
import com.aivruu.homes.service.ConfigServiceModelImpl;
import com.aivruu.homes.service.ServiceManager;
import com.aivruu.homes.service.ServiceModel;
import com.aivruu.homes.service.type.ConfigServiceType;
import com.aivruu.homes.service.type.DataServiceType;
import com.aivruu.homes.result.ValueObjectConfigResult;
import com.aivruu.homes.shared.DataModel;
import com.aivruu.homes.shared.cloud.MongoDBModelData;
import com.aivruu.homes.shared.disk.JsonModelData;
import com.aivruu.homes.teleport.HomeTeleportManager;
import com.aivruu.homes.utils.ComponentUtils;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;

public final class HomesPlugin extends JavaPlugin implements Homes {
private ComponentLogger logger;
private ServiceManager serviceManager;
private ConfigModel config;
private MessageConfigModel message;
private DataModel data;
private PlayerModelRepository repository;
private HomeAggregate homeAggregate;
private HomeTeleportManager homeTeleportManager;
Expand All @@ -29,39 +36,62 @@ public void onLoad() {
this.logger = getComponentLogger();
this.logger.info(ComponentUtils.parse("<green>Prepare plugin internal components, loading service models."));
this.logger.info(ComponentUtils.parse("<yellow>Loading service manager."));
final ConfigServiceModelImpl configService = new ConfigServiceType(getDataFolder().toPath());
if (!configService.start()) {
this.logger.error(ComponentUtils.parse("<red>The configuration service could not be started correctly."));
final Path pluginFolder = this.getDataFolder().toPath();
final ValueObjectConfigResult<ConfigModel> configStatus = ValueObjectConfigManager.INSTANCE.loadConfig(pluginFolder);
final ValueObjectConfigResult<MessageConfigModel> messageStatus = ValueObjectConfigManager.INSTANCE.loadMessages(pluginFolder);
if (!configStatus.load() || !messageStatus.load()) {
this.logger.error(ComponentUtils.parse("<red>One or several of the configuration models could not be loaded correctly."));
this.setEnabled(false);
return;
}
final ServiceModel<?>[] servicesArray = new ServiceModel[] {
configService,
new DataServiceType(this.logger, (ConfigModel) configService.getConfigurationModels().get(0))
};
this.serviceManager = new ServiceManager(getSLF4JLogger(), servicesArray);
this.config = configStatus.result();
this.message = messageStatus.result();
this.repository = new PlayerModelRepository();
this.homeAggregate = new HomeAggregate(this.repository);
this.homeTeleportManager = new HomeTeleportManager(this.homeAggregate);
if (!this.config.dataFormat.equals("JSON")) {
this.data = new JsonModelData(this.getDataFolder(), this.config);
} else if (this.config.dataFormat.equals("MONGODB")) {
this.data = new MongoDBModelData();
} else {
this.logger.error(ComponentUtils.parse("<red>Unknown storage type detected in configuration. Illegal type <data-type>", Placeholder.parsed("data-type", this.config.dataFormat)));
this.setEnabled(false);
}
}

@SuppressWarnings("UnstableApiUsage")
@Override
public void onEnable() {
this.serviceManager.start().thenAccept(couldStartAll -> {
if (!couldStartAll) {
this.logger.error(ComponentUtils.parse("<red>One or several services could not start correctly."));
this.data.performLoad().thenAccept(loadStatus -> {
if (!loadStatus) {
this.logger.error(ComponentUtils.parse("<red>Storage could not be loaded correctly."));
this.setEnabled(false);
return;
}
this.logger.info(ComponentUtils.parse("<green>Plugin services enabled successful."));
this.logger.info(ComponentUtils.parse("<yellow>Has been enabled correctly."));
this.logger.info(ComponentUtils.parse("<green>Running on version: <version>", Placeholder.parsed("version", Constants.VERSION)));
this.logger.info(ComponentUtils.parse("<green>Storage loaded correctly. Using type <data-type>", Placeholder.parsed("data-type", this.config.dataFormat)));
});
final CommandManager commandManager = new CommandManager(this, this.config, this.message);
commandManager.prepareRequirements();
commandManager.prepareMessages();
commandManager.load(this.homeAggregate, this.homeTeleportManager);
this.getServer().getPluginManager().registerEvents(new PlayerDataListener(this.data, this.repository, this.logger), this);
}

@Override
public void onDisable() {
Provider.unload();
if ((this.data == null) || (this.repository == null)) {
return;
}
this.logger.info(ComponentUtils.parse("<green>Preparing plugin services to perform stop execution."));
this.serviceManager.stop();
this.repository.clean();
this.data.performUnload().thenAccept(unloadStatus -> {
if (!unloadStatus) {
this.logger.warn(ComponentUtils.parse("<red>The connection with the storage could not be closed correctly."));
return;
}
this.logger.info(ComponentUtils.parse("<green>The storage has been closed correctly and the data has been saved."));
});
}

@Override
Expand Down

0 comments on commit 3f8d313

Please sign in to comment.