Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
command

Auto Save Interval Command

/autosave command using AbstractCommand

Example Code

java
package dev.myplugin.example;

import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
// import dev.myplugin.example.BetterMapConfig;  // Anonymized
// import dev.myplugin.example.ExplorationManager;  // Anonymized

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.util.concurrent.CompletableFuture;

/**
 * Command to get or set the auto-save interval.
 */
public class ExampleCommand extends AbstractCommand {
    private final OptionalArg<Integer> intervalArg = this.withOptionalArg("interval", "Auto-save interval in minutes", ArgTypes.INTEGER);

    /**
     * Constructs the ExampleCommand.
     */
    public ExampleCommand() {
        super("autosave", "Get or set the exploration auto-save interval");
        this.requirePermission(ConfigCommand.CONFIG_PERMISSION);
    }

    @Override
    protected boolean canGeneratePermission() {
        return false;
    }

    @Nullable
    @Override
    protected CompletableFuture<Void> execute(@Nonnull CommandContext context) {
        Integer interval = context.get(this.intervalArg);

        if (interval == null) {
            int current = BetterMapConfig.getInstance().getAutoSaveInterval();
            context.sendMessage(Message.raw("Current auto-save interval: " + current + " minutes.").color(Color.YELLOW));
        } else {
            if (interval < 0) {
                context.sendMessage(Message.raw("Interval cannot be negative.").color(Color.RED));
                return CompletableFuture.completedFuture(null);
            }

            BetterMapConfig.getInstance().setAutoSaveInterval(interval);
            ExplorationManager.getInstance().startAutoSave();

            context.sendMessage(Message.raw("Auto-save interval set to " + interval + " minutes.").color(Color.GREEN));
            if (interval == 0) {
                context.sendMessage(Message.raw("Auto-save is now DISABLED.").color(Color.YELLOW));
            }
        }

        return CompletableFuture.completedFuture(null);
    }
}