Code Patterns

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

← Back to Patterns
command

Reload Command

/reload 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.universe.Universe;
// import dev.myplugin.example.BetterMapConfig;  // Anonymized
// import dev.myplugin.example.MapPrivacyManager;  // Anonymized
// import dev.myplugin.example.WarpPrivacyManager;  // Anonymized
// import dev.myplugin.example.PoiPrivacyManager;  // Anonymized
// import dev.myplugin.example.WorldMapHook;  // Anonymized
import org.checkerframework.checker.nullness.compatqual.NonNullDecl;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

import java.awt.*;
import java.util.concurrent.CompletableFuture;

/**
 * Command to reload the BetterMap configuration.
 */
public class ExampleCommand extends AbstractCommand {
    /**
     * Constructs the Reload command.
     */
    protected ExampleCommand() {
        super("reload", "Reload BetterMap configuration");
        this.requirePermission("dev.ninesliced.bettermap.command.reload");
    }

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

    @Override
    protected String generatePermissionNode() {
        return "reload";
    }

    /**
     * Executes the reload command, applying new configuration and updating worlds.
     *
     * @param context The command execution context.
     * @return A future that completes when execution is finished.
     */
    @NullableDecl
    @Override
    protected CompletableFuture<Void> execute(@NonNullDecl CommandContext context) {
        BetterMapConfig.getInstance().reload();
        MapPrivacyManager.getInstance().updatePrivacyState();
        WarpPrivacyManager.getInstance().updatePrivacyState();
        PoiPrivacyManager.getInstance().updatePrivacyState();

        Universe universe = Universe.get();
        if (universe != null) {
            universe.getWorlds().values().forEach(world -> world.execute(() -> {
                WorldMapHook.updateWorldMapConfigs(world);
                WorldMapHook.broadcastMapSettings(world);
                WorldMapHook.refreshTrackers(world);
            }));
        }

        context.sendMessage(Message.raw("BetterMap configuration reloaded!").color(Color.GREEN));
        context.sendMessage(Message.raw("Exploration Radius: ").color(Color.YELLOW).insert(Message.raw(String.valueOf(BetterMapConfig.getInstance().getExplorationRadius())).color(Color.WHITE)));
        context.sendMessage(Message.raw("Min Scale: ").color(Color.YELLOW).insert(Message.raw(String.valueOf(BetterMapConfig.getInstance().getMinScale())).color(Color.WHITE)));
        context.sendMessage(Message.raw("Max Scale: ").color(Color.YELLOW).insert(Message.raw(String.valueOf(BetterMapConfig.getInstance().getMaxScale())).color(Color.WHITE)));

        BetterMapConfig config = BetterMapConfig.getInstance();
        context.sendMessage(Message.raw("Map Quality: ").color(Color.YELLOW).insert(Message.raw(config.getMapQuality().name()).color(Color.WHITE)));

        if (config.getMapQuality() != config.getActiveMapQuality()) {
            context.sendMessage(Message.raw("WARNING: Map Quality change pending restart (Active: " + config.getActiveMapQuality().name() + ")").color(Color.RED));
        }

        context.sendMessage(Message.raw("NOTE: The server must be restarted for map quality/max chunks changes to take effect."));

        return CompletableFuture.completedFuture(null);
    }
}