001package net.tnemc.plugincore.core;
002/*
003 * The New Plugin Core
004 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 *
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020import net.tnemc.item.providers.HelperMethods;
021import net.tnemc.menu.core.MenuHandler;
022import net.tnemc.plugincore.PluginCore;
023import net.tnemc.plugincore.core.api.CallbackManager;
024import net.tnemc.plugincore.core.component.Component;
025import net.tnemc.plugincore.core.component.ComponentBuilder;
026import net.tnemc.plugincore.core.component.query.Query;
027import net.tnemc.plugincore.core.component.transaction.Transaction;
028import net.tnemc.plugincore.core.io.storage.StorageManager;
029import net.tnemc.plugincore.core.utils.UpdateChecker;
030import org.jetbrains.annotations.Nullable;
031import revxrsal.commands.Lamp;
032import revxrsal.commands.command.CommandActor;
033import revxrsal.commands.command.ExecutableCommand;
034
035import java.util.HashMap;
036import java.util.Map;
037
038/**
039 * PluginEngine
040 *
041 * @author creatorfromhell
042 * @since 0.0.1.0
043 */
044public abstract class PluginEngine {
045
046  protected Map<String, Component> components = new HashMap<>();
047  protected Map<String, Query> queries = new HashMap<>();
048  protected Map<String, Transaction> transactions = new HashMap<>();
049  protected Map<String, ComponentBuilder> builders = new HashMap<>();
050
051  protected StorageManager storage;
052  protected Lamp.Builder<? extends CommandActor> command;
053
054  protected MenuHandler menuHandler;
055  protected HelperMethods helperMethods;
056
057  protected UpdateChecker updateChecker = null;
058
059  //Phase-related methods.
060  //TODO: Early onEnable, middle, end
061
062  //TODO: onDisable
063
064  public abstract String versionCheckSite();
065
066  /**
067   * @return The current version for this plugin.
068   */
069  public abstract String version();
070
071  /**
072   * @return The build for this version.
073   */
074  public abstract String build();
075
076  public abstract void registerConfigs();
077
078  /**
079   * Initializes all components with the provided platform and version.
080   *
081   * @param platform the platform to initialize the components for
082   * @param version the Minecraft version string to initialize the components with
083   */
084  public void initComponents(final Platform platform, final String version) {
085
086    for(final Component component : components.values()) {
087
088      if(component.supports(platform, version)) {
089
090        component.initialize(platform, version);
091
092        //register our builders during initialization
093        for(final ComponentBuilder builder : component.initBuilders(platform, version)) {
094
095          builders.put(builder.identifier(), builder);
096        }
097      }
098    }
099  }
100
101  /**
102   * Initializes the registries for all components with the provided platform and version.
103   *
104   * @param platform the platform to initialize the registries for
105   * @param version the Minecraft version string to initialize the registries with
106   */
107  public void initRegistries(final Platform platform, final String version) {
108
109    for(final Component component : components.values()) {
110
111      component.initRegistries(platform, version);
112    }
113  }
114
115  public abstract void registerPluginChannels();
116
117  public abstract void registerStorage();
118
119  /**
120   * Used to register the command handlers.
121   */
122  public abstract void registerCommandHandler();
123
124  public abstract <T extends CommandActor> String commandHelpWriter(ExecutableCommand<T> command, T actor);
125
126  /**
127   * Used to register commands.
128   */
129  public abstract void registerCommands();
130
131  public abstract void registerMenuHandler();
132
133  /**
134   * Used to register {@link net.tnemc.plugincore.core.api.callback.Callback Callbacks} during initialization.
135   */
136  public abstract void registerCallbacks(CallbackManager callbackManager);
137
138  public void registerUpdateChecker() {
139    this.updateChecker = new UpdateChecker();
140
141    PluginCore.log().inform("Build Stability: " + this.updateChecker.stable());
142
143    if(this.updateChecker.needsUpdate()) {
144      PluginCore.log().inform("Update Available! Latest: " + this.updateChecker.getBuild());
145    }
146  }
147
148  public Map<String, Component> components() {
149    return components;
150  }
151
152  public Map<String, ComponentBuilder> builders() {
153    return builders;
154  }
155
156  public Map<String, Query> queries() {
157    return queries;
158  }
159
160  public Map<String, Transaction> transactions() {
161    return transactions;
162  }
163
164  /**
165   * Retrieves the ComponentBuilder associated with the provided name, and returns a new instance for
166   * building from.
167   *
168   * @param name the name of the ComponentBuilder to retrieve
169   * @return the new instance of the ComponentBuilder if present in the builders map to start building
170   * from, null otherwise
171   */
172  public @Nullable ComponentBuilder builder(final String name) {
173    if(builders.containsKey(name)) {
174
175      return builders.get(name).builder();
176    }
177    return null;
178  }
179
180  public StorageManager storage() {
181    return storage;
182  }
183
184  public Lamp.Builder<? extends CommandActor> command() {
185    return command;
186  }
187
188  public MenuHandler menu() {
189    return menuHandler;
190  }
191
192  public HelperMethods helper() {
193    return helperMethods;
194  }
195
196  public UpdateChecker update() {
197    return updateChecker;
198  }
199
200  public void postConfigs() {
201  }
202
203  public void postStorage() {
204  }
205
206  public void postCommands() {
207  }
208
209  public void postEnable() {
210  }
211
212  public void postDisable() {
213  }
214}