Skip to content

Creating your first extension

MrUniverse44 edited this page Dec 4, 2023 · 4 revisions
  • You can create your custom extension using this next template.

Extension Example:

import me.blueslime.minedis.api.extension.MinedisExtension;

public class Example extends MinedisExtension {
    /**
     * Please select a different identifier per extension
     *
     * @return custom identifier
     */
    @Override
    public String getIdentifier() {
        return "ExampleIdentifier";
    }

    /**
     * This will be the displayed name in commands for this extension
     *
     * @return custom name
     */
    @Override
    public String getName() {
        return "Example Name";
    }

    @Override
    public void onEnabled() {

    }

    @Override
    public void onDisable() {

    }
}

You can register a minecraft command using methods included in the same MinedisExtension, example:

    @Override
    public void onEnabled() {
        registerMinecraftCommand(
                new MinecraftCommand("hello") {
                    @Override
                    public void execute(Sender sender, String[] arguments) {
                        sender.send("&aHello world");
                    }
                }
        );
    }

This command will be removed and will be re-added in all plugin reload command. All extensions will be loaded automatically with onEnable and onDisable methods.

onEnabled() will be used when the plugin is being loaded and after the plugin reloads. the plugin will call the onDisable() when a user or the proxy uses the /minedis reload, and after that it will use the onEnabled() again but with the modifications applied, because the plugin detects changes applied in the .jar file when is individual.

If you want to add a extension from another plugin you need to register again the extension after 1 second of using the minedis reload command. I will add a method in the future to keep loaded your extension but for now is a pre-release so some things can change.

Extra Methods

Configuration

By default the plugin will create a configuration file for all extensions for users, you can use: getConfiguration() method and save all changes using saveConfiguration(), you can modify it using a different 'template' for your configuration changing the getConfigurationInputStream() method. You can reload the configuration using reloadConfiguration()

Discord Bot

You can call the Discord bot using 'Modules method' using the Controller module, example: getModule(Controller.class).getBot()

usage example:

package me.blueslime.minedis.examples;

import me.blueslime.minedis.api.extension.MinedisExtension;
import me.blueslime.minedis.modules.discord.Controller;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.Commands;

public class Example extends MinedisExtension {
    private CommandListener listener = null;
    /**
     * Please select a different identifier per extension
     *
     * @return custom identifier
     */
    @Override
    public String getIdentifier() {
        return "ExampleIdentifier";
    }

    /**
     * This will be the displayed name in commands for this extension
     *
     * @return custom name
     */
    @Override
    public String getName() {
        return "Example Name";
    }

    @Override
    public void onEnabled() {
        String guildString = getConfiguration().getString("settings.guild", "NOT_SET");

        if (guildString.isEmpty() || guildString.equalsIgnoreCase("NOT_SET")) {
            return;
        }
        Guild guild = getModule(Controller.class).getBot().getClient()
                .getGuildById(guildString);

        if (guild == null) {
            return;
        }

        guild.updateCommands().addCommands(
            Commands.slash(
                "online",
                "Check the online users in the network"
            )
        ).queue();

        listener = new CommandListener();

        getModule(Controller.class).getBot().getClient().getRegisteredListeners().add(
                listener
        );
    }

    @Override
    public void onDisable() {
        getModule(Controller.class).getBot().getClient().getRegisteredListeners().remove(
                listener
        );
    }
}

command listener:

package me.blueslime.minedis.examples;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.md_5.bungee.api.ProxyServer;

import java.awt.*;

public class CommandListener extends ListenerAdapter {
    @Override
    public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
        if (event.getName().equals("online")) {
            if (event.getGuild() == null) {
                return;
            }
            int online = ProxyServer.getInstance().getPlayers().size();

            event.replyEmbeds(
                    new EmbedBuilder()
                            .setTitle("Example")
                            .setDescription("Currently " + online + " player(s) are online.")
                            .setColor(Color.YELLOW)
                            .setFooter("mc.spigotmc.org")
                            .build()
            ).setEphemeral(true).queue();
            return;
        }
    }
}

Cache

you can handle shared caches with other extensions or with the own plugin using getCache(Class<T> cache)

Clone this wiki locally