Code Patterns

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

← Back to Patterns
command

Untrack World Command

/untrack 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.CommandSender;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.entity.entities.Player;
// import dev.myplugin.example.BetterMapConfig;  // Anonymized

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

/**
 * Command to remove a world from the allowed/tracked list.
 */
public class ExampleCommand extends AbstractCommand {
    private final OptionalArg<String> worldArg = this.withOptionalArg("world", "World name to untrack", ArgTypes.STRING);

    /**
     * Constructs the ExampleCommand.
     */
    public ExampleCommand() {
        super("untrack", "Remove a world from the exploration tracking list");
        this.requirePermission(ConfigCommand.CONFIG_PERMISSION);
    }

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

    /**
     * Executes the command.
     *
     * @param context The command execution context.
     * @return A future that completes when execution is finished.
     */
    @Nullable
    @Override
    protected CompletableFuture<Void> execute(@Nonnull CommandContext context) {
        String worldName = context.get(this.worldArg);

        if (worldName == null || worldName.isEmpty()) {
            if (context.isPlayer()) {
                CommandSender sender = context.sender();
                if (sender instanceof Player) {
                    assert ((Player) sender).getWorld() != null;
                    worldName = ((Player) sender).getWorld().getName();
                } else {
                    context.sendMessage(Message.raw("This command must be run by a player or specify a world name.").color(Color.RED));
                    return CompletableFuture.completedFuture(null);
                }
            } else {
                context.sendMessage(Message.raw("You must specify a world name when running from console.").color(Color.RED));
                return CompletableFuture.completedFuture(null);
            }
        }

        boolean removed = BetterMapConfig.getInstance().removeAllowedWorld(worldName);

        if (removed) {
            context.sendMessage(Message.raw("World '" + worldName + "' removed from tracked worlds.").color(Color.GREEN));
            context.sendMessage(Message.raw("Changes saved to config.").color(Color.GRAY));
        } else {
            context.sendMessage(Message.raw("World '" + worldName + "' is not currently tracked.").color(Color.YELLOW));
        }

        return CompletableFuture.completedFuture(null);
    }
}