Skip to content

MC-Replay/Extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extensions

This extension system allows developers to create jar files that can be loaded as extensions into other projects. This is achieved using a custom loader that can dynamically load classes from the extension jar files at runtime.

Once an extension is loaded, it can be used just like any other class in the project. The main project can access the extension's methods and fields, and can even instantiate objects from the extension's classes. This extension system provides a flexible way to add functionality to a project without having to modify the project's code directly. Extensions can be developed and maintained separately, and can be added or removed from the project as needed.

Here is a code example of a discord bot using the extension system:

public abstract class BotExtension extends JavaExtension {

    private Bot discordBot;

    public void onEnable() {
    }

    public void onDisable() {
    }

    public void log(String message) {
        this.discordBot.log("[" + getConfig().getName() + "] " + message);
    }
}
public final class BotExtensionHandler extends JavaExtensionLoader {

    public BotExtensionHandler(HarmBot bot, File extensionFolder) {
        super(extensionFolder);

        try {
            this.loadExtensions();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        for (JavaExtension extension : getExtensions()) {
            if (extension instanceof BotExtension botExtension) {
                try {
                    JavaReflections.getField(BotExtension.class, Bot.class, "discordBot").set(botExtension, bot);

                    botExtension.onEnable();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }

        bot.log("Enabled " + getExtensions().size() + " extensions");
    }

    public void disable() {
        for (JavaExtension extension : getExtensions()) {
            if (extension instanceof BotExtension botExtension) {
                botExtension.onDisable();
            }
        }

        try {
            this.unloadExtensions();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
@Getter
public class DiscordSyncExtension extends BotExtension {

    @Getter
    private static DiscordSyncExtension instance;

    private UserStorage userStorage;

    @Override
    public void onEnable() {
        instance = this;
        this.userStorage = new UserStorage(this.getHarmBot().getDatabase());

        HarmBotAPI.getCommandHandler().registerSlashCommands(
                new DiscordSyncCommand(),
                new LinkCommand()
        );

        HarmBotAPI.registerEvent(
                new SyncListeners()
        );

        DiscordSyncTasks.start();
    }
}

image

image