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.NotNull;
031import org.jetbrains.annotations.Nullable;
032import revxrsal.commands.Lamp;
033import revxrsal.commands.LampBuilderVisitor;
034import revxrsal.commands.command.CommandActor;
035import revxrsal.commands.command.ExecutableCommand;
036
037import java.util.HashMap;
038import java.util.Map;
039
040/**
041 * PluginEngine
042 *
043 * @author creatorfromhell
044 * @since 0.0.1.0
045 */
046public abstract class PluginEngine {
047
048  protected Map<String, Component> components = new HashMap<>();
049  protected Map<String, Query> queries = new HashMap<>();
050  protected Map<String, Transaction> transactions = new HashMap<>();
051  protected Map<String, ComponentBuilder> builders = new HashMap<>();
052
053  protected StorageManager storage;
054  protected Lamp<? extends CommandActor> command;
055
056  protected MenuHandler menuHandler;
057  protected HelperMethods helperMethods;
058
059  protected UpdateChecker updateChecker = null;
060
061  //Phase-related methods.
062  //TODO: Early onEnable, middle, end
063
064  //TODO: onDisable
065
066  public void load() {
067
068  }
069
070  public void postLoad() {
071
072  }
073
074  public abstract String versionCheckSite();
075
076  /**
077   * @return The current version for this plugin.
078   */
079  public abstract String version();
080
081  /**
082   * @return The build for this version.
083   */
084  public abstract String build();
085
086  public abstract void registerConfigs();
087
088  /**
089   * Initializes all components with the provided platform and version.
090   *
091   * @param platform the platform to initialize the components for
092   * @param version  the Minecraft version string to initialize the components with
093   */
094  public void initComponents(final Platform platform, final String version) {
095
096    for(final Component component : components.values()) {
097
098      if(component.supports(platform, version)) {
099
100        component.initialize(platform, version);
101
102        //register our builders during initialization
103        for(final ComponentBuilder builder : component.initBuilders(platform, version)) {
104
105          builders.put(builder.identifier(), builder);
106        }
107      }
108    }
109  }
110
111  /**
112   * Initializes the registries for all components with the provided platform and version.
113   *
114   * @param platform the platform to initialize the registries for
115   * @param version  the Minecraft version string to initialize the registries with
116   */
117  public void initRegistries(final Platform platform, final String version) {
118
119    for(final Component component : components.values()) {
120
121      component.initRegistries(platform, version);
122    }
123  }
124
125  public abstract void registerPluginChannels();
126
127  public abstract void registerStorage();
128
129  /**
130   * Used to register the command handlers.
131   */
132  public abstract void registerCommandHandler();
133
134  public abstract <T extends CommandActor> String commandHelpWriter(ExecutableCommand<T> command, T actor);
135
136  /**
137   * Used to register command parameter types.
138   */
139  public abstract <A extends CommandActor> @NotNull LampBuilderVisitor<A> registerParameters();
140
141  /**
142   * Used to register commands.
143   */
144  public abstract void registerCommands();
145
146  public abstract void registerMenuHandler();
147
148  /**
149   * Used to register {@link net.tnemc.plugincore.core.api.callback.Callback Callbacks} during
150   * initialization.
151   */
152  public abstract void registerCallbacks(CallbackManager callbackManager);
153
154  public void registerUpdateChecker() {
155
156    this.updateChecker = new UpdateChecker();
157
158    PluginCore.log().inform("Build Stability: " + this.updateChecker.stable());
159
160    if(this.updateChecker.needsUpdate()) {
161      PluginCore.log().inform("Update Available! Latest: " + this.updateChecker.getBuild());
162    }
163  }
164
165  public Map<String, Component> components() {
166
167    return components;
168  }
169
170  public Map<String, ComponentBuilder> builders() {
171
172    return builders;
173  }
174
175  public Map<String, Query> queries() {
176
177    return queries;
178  }
179
180  public Map<String, Transaction> transactions() {
181
182    return transactions;
183  }
184
185  /**
186   * Retrieves the ComponentBuilder associated with the provided name, and returns a new instance
187   * for building from.
188   *
189   * @param name the name of the ComponentBuilder to retrieve
190   *
191   * @return the new instance of the ComponentBuilder if present in the builders map to start
192   * building from, null otherwise
193   */
194  public @Nullable ComponentBuilder builder(final String name) {
195
196    if(builders.containsKey(name)) {
197
198      return builders.get(name).builder();
199    }
200    return null;
201  }
202
203  public StorageManager storage() {
204
205    return storage;
206  }
207
208  public Lamp<? extends CommandActor> command() {
209
210    return command;
211  }
212
213  public MenuHandler menu() {
214
215    return menuHandler;
216  }
217
218  public HelperMethods helper() {
219
220    return helperMethods;
221  }
222
223  public UpdateChecker update() {
224
225    return updateChecker;
226  }
227
228  public void postConfigs() {
229
230  }
231
232  public void postStorage() {
233
234  }
235
236  public void postCommands() {
237
238  }
239
240  public void postEnable() {
241
242  }
243
244  public void postDisable() {
245
246  }
247}