From 2a4a9035b6dc6ceff6a6f42e194b4975dc80a1f8 Mon Sep 17 00:00:00 2001 From: Qekly Date: Fri, 26 Jan 2024 14:02:06 -0300 Subject: [PATCH] feat(services): perform ServiceModel implementations for plugin running --- .../homes/service/type/ConfigServiceType.java | 58 +++++++++++++ .../homes/service/type/DataServiceType.java | 83 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 plugin/src/main/java/com/aivruu/homes/service/type/ConfigServiceType.java create mode 100644 plugin/src/main/java/com/aivruu/homes/service/type/DataServiceType.java diff --git a/plugin/src/main/java/com/aivruu/homes/service/type/ConfigServiceType.java b/plugin/src/main/java/com/aivruu/homes/service/type/ConfigServiceType.java new file mode 100644 index 0000000..c2d0f52 --- /dev/null +++ b/plugin/src/main/java/com/aivruu/homes/service/type/ConfigServiceType.java @@ -0,0 +1,58 @@ +package com.aivruu.homes.service.type; + +import com.aivruu.homes.config.ValueObjectConfigManager; +import com.aivruu.homes.config.model.BaseConfigModel; +import com.aivruu.homes.config.model.ConfigModel; +import com.aivruu.homes.config.model.MessageConfigModel; +import com.aivruu.homes.result.ValueObjectConfigResult; +import com.aivruu.homes.service.ConfigServiceModelImpl; +import org.jetbrains.annotations.NotNull; + +import java.nio.file.Path; +import java.util.List; + +public class ConfigServiceType implements ConfigServiceModelImpl { + private final Path pluginFolder; + private ValueObjectConfigManager configManager; + private ConfigModel configModel; + private MessageConfigModel messageConfigModel; + private boolean isOk; + + public ConfigServiceType(final @NotNull Path pluginFolder) { + this.pluginFolder = pluginFolder; + } + + @Override + public boolean isOk() { + return this.isOk; + } + + @Override + public void setOk(final boolean ok) { + this.isOk = ok; + } + + @Override + public @NotNull String id() { + return "config-service"; + } + + @Override + public boolean start() { + this.configManager = ValueObjectConfigManager.INSTANCE; + final ValueObjectConfigResult configResult = this.configManager.loadConfig(this.pluginFolder); + if (configResult.load()) { + this.configModel = configResult.result(); + } + final ValueObjectConfigResult messageResult = this.configManager.loadMessages(this.pluginFolder); + if (messageResult.load()) { + this.messageConfigModel = messageResult.result(); + } + return (this.configModel != null) && (this.messageConfigModel != null); + } + + @Override + public @NotNull List getConfigurationModels() { + return List.of(this.configModel, this.messageConfigModel); + } +} diff --git a/plugin/src/main/java/com/aivruu/homes/service/type/DataServiceType.java b/plugin/src/main/java/com/aivruu/homes/service/type/DataServiceType.java new file mode 100644 index 0000000..759a4d2 --- /dev/null +++ b/plugin/src/main/java/com/aivruu/homes/service/type/DataServiceType.java @@ -0,0 +1,83 @@ +package com.aivruu.homes.service.type; + +import com.aivruu.homes.HomesPlugin; +import com.aivruu.homes.config.model.ConfigModel; +import com.aivruu.homes.repository.PlayerModelRepository; +import com.aivruu.homes.service.ServiceModel; +import com.aivruu.homes.shared.DataModel; +import com.aivruu.homes.shared.cloud.MongoDBModelData; +import com.aivruu.homes.shared.disk.JsonModelData; +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.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class DataServiceType implements ServiceModel { + private final ComponentLogger logger; + private final ConfigModel config; + private DataModel dataModel; + private boolean isOk; + + public DataServiceType(final @NotNull ComponentLogger logger, final @NotNull ConfigModel config) { + this.logger = logger; + this.config = config; + } + + @Override + public boolean isOk() { + return this.isOk; + } + + @Override + public void setOk(final boolean ok) { + this.isOk = ok; + } + + @Override + public @NotNull String id() { + return "data-service"; + } + + @Override + public boolean start() { + if (this.config.dataType.equals("JSON")) { + this.dataModel = new JsonModelData(HomesPlugin.getPlugin(HomesPlugin.class).getDataFolder()); + } else if (this.config.dataType.equals("MONGODB")) { + this.dataModel = new MongoDBModelData(); + } else { + this.logger.info(ComponentUtils.parse("Unknown storage type detected in configuration. Illegal type ", Placeholder.parsed("data-type", this.config.dataType))); + return false; + } + final boolean couldStartCorrectly = this.dataModel.performLoad().join(); + if (!couldStartCorrectly) { + this.logger.error(ComponentUtils.parse("Storage could not be loaded correctly.")); + return false; + } + this.logger.info(ComponentUtils.parse("Storage loaded correctly. Using type ", Placeholder.parsed("data-type", this.config.dataType))); + return true; + } + + @Override + public void stop() { + if (this.dataModel == null) { + return; + } + this.logger.info(ComponentUtils.parse("Performing repository data writing backup.")); + final PlayerModelRepository repository = PlayerModelRepository.get(); + repository.playerModelsCollection().forEach(this.dataModel::performAsyncWrite); + repository.clean(); + this.dataModel.performUnload().thenAccept(couldStoppedCorrectly -> { + if (!couldStoppedCorrectly) { + this.logger.warn(ComponentUtils.parse("The connection with the storage could not be closed correctly.")); + return; + } + this.logger.info(ComponentUtils.parse("The storage has been closed correctly and the data has been saved.")); + }); + } + + @Override + public @Nullable DataModel getGenericType() { + return this.dataModel; + } +}