Skip to content

New Show Action

Bram edited this page Feb 4, 2022 · 2 revisions

This is currently the only API this plugin supports. It exists to add new action types for shows.

Setup

To extends upon Teacups, you'll have to add this as a dependency. Unfortunately, I do not have a repository, so you'll have to add the jar file to your package manager manually.

Maven

If you use maven you can add a Teacups.jar file to your maven repository by executing the following command:

mvn install:install-file -Dfile=<path-to-teacups.jar> -DgroupId=io.github.berehum -DartifactId=teacupspro -Dversion=<teacups.jar-version> -Dpackaging=jar -DgeneratePom=true

When you have executed it, you should be able to add the following dependency to your pom.xml:

<dependencies>
    <dependency>
        <groupId>io.github.berehum</groupId>
        <artifactId>teacupspro</artifactId>
        <version>your-version</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

What does an action type look like in code?

ShowAction class which extends IShowAction

This class contains the code for loading, executing and providing the ShowActionType of the ShowAction

Creating a ShowAction class

public class PlayerShowAction implements IShowAction {

    private static final ShowActionType type = TeacupsMain.getInstance().getShowActionTypes().get("player");

    private boolean loaded = false;

    private String command;

    // Code to load the action from the config.
    // Args will be the strings after the command to execute.
    @Override
    public boolean load(String filename, int line, String[] args) {
        StringBuilder builder = new StringBuilder();
        for (String arg : args) {
            builder.append(arg).append(" ");
        }
        command = builder.substring(0, builder.length() - 1);

        if (command.isEmpty()) {
            // Create a ConfigProblem whenever the action is invalid 
            ShowFileReader.addConfigProblem(filename, new ConfigProblem(ConfigProblem.ConfigProblemType.ERROR,
                    ConfigProblemDescriptions.INVALID_ARGUMENT.getDescription("No command given."), String.valueOf(line)));
            return loaded;
        }

        loaded = true;
        return loaded;
    }

    // Code to execute this showaction for a specific teacup.
    @Override
    public void execute(Teacup teacup) {
        if (!loaded) return;
        teacup.getPlayers().forEach(player -> Bukkit.dispatchCommand(player, command));
    }

    @Override
    public @NotNull ShowActionType getType() {
        return type;
    }
}

ShowActionType

An action type consists of a ShowActionType class which contains the name, aliases and constructor of your ShowAction. This class only functions for registering purposes, so that some in the show file can be linked to an actual class.

Registering a ShowActionType by listening to RegisterShowActionTypesEvent

You can simply do the following to register the ShowActionType (and ShowAction).

public class TeacupsListener implements Listener {

    private final Logger logger;

    public TeacupsListener(Logger logger) {
        this.logger = logger;
    }

    //Listen for the RegisterShowActionTypesEvent
    @EventHandler
    public void onActionTypesRegister(RegisterShowActionTypesEvent event) {
        ShowActionTypeRegistry registry = event.getShowActionTypeRegistry();
        try {
            // The first parameter: the ShowActionType's name
            // The second parameter: a String array of the ShowActionType's aliases
            // The third parameter: a Supplier<IShowAction> for the linked ShowAction
            registry.register(new ShowActionType("rpm", new String[]{"setrpm"}, RpmShowAction::new));
            registry.register(new ShowActionType("kick", new String[]{"kickall"}, KickShowAction::new));
            registry.register(new ShowActionType("lock", new String[0], LockShowAction::new));
            registry.register(new ShowActionType("console", new String[]{"cmd", "command"}, ConsoleShowAction::new));
            registry.register(new ShowActionType("player", new String[]{"playercmd", "playercommand"}, PlayerShowAction::new));
            registry.register(new ShowActionType("actionbar", new String[]{"bar"}, ActionBarShowAction::new));
            registry.register(new ShowActionType("chat", new String[]{"message, msg"}, ChatShowAction::new));
            registry.register(new ShowActionType("stop", new String[]{"end", "finish"}, StopShowAction::new));
        } catch (ClashingActionTypesException e) {
            // the register method can throw a ClashingActionTypesException whenever
            // the name and/or aliases already exist in another ShowActionType
            logger.warning("An error occurred whilst registering show action types.");
        }
    }

} 

Clone this wiki locally