Code Patterns

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

← Back to Patterns
command

Share All Exploration Command

/shareallexploration 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.WorldMapHook;  // Anonymized

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

/**
 * Command to toggle sharing of all exploration data among players.
 */
public class ExampleCommand extends AbstractCommand {

    public ExampleCommand() {
        super("shareallexploration", "Toggle sharing of all exploration data");
        this.requirePermission(ConfigCommand.CONFIG_PERMISSION);
        this.addAliases("shareall");
    }

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

    @Override
    public CompletableFuture<Void> execute(@Nonnull CommandContext context) {
        BetterMapConfig config = BetterMapConfig.getInstance();
        BetterMapConfig.getInstance().setShareAllExploration(!config.isShareAllExploration());
        context.sendMessage(Message.raw("ShareAllExploration set to: " + config.isShareAllExploration()).color(Color.GREEN));

        Universe universe = Universe.get();
        if (universe != null) {
            universe.getWorlds().values().forEach(WorldMapHook::refreshTrackers);
        }

        return CompletableFuture.completedFuture(null);
    }
}