Code Patterns

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

← Back to Patterns
command

Finish Current Map Command

/finish command using AbstractAsyncCommand

Example Code

java
package dev.myplugin.example;

// import dev.myplugin.example.FinishCurrentMapEvent;  // Anonymized
import com.hypixel.hytale.server.core.HytaleServer;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractAsyncCommand;
import com.hypixel.hytale.server.core.universe.world.World;
import org.checkerframework.checker.nullness.compatqual.NonNullDecl;

import java.util.concurrent.CompletableFuture;

import static ar.ncode.plugin.commands.spawn.ShowSpawnPoints.getWorldFromCommandContext;

public class ExampleCommand extends AbstractAsyncCommand {

	public ExampleCommand() {
		super("finish", "description");
		addAliases("end");
	}

	@NonNullDecl
	@Override
	protected CompletableFuture<Void> executeAsync(@NonNullDecl CommandContext commandContext) {
		World world = getWorldFromCommandContext(commandContext);

		if (world == null) {
			return CompletableFuture.runAsync(() -> {
				commandContext.sendMessage(Message.raw("Could not get world."));
			});
		}

		return HytaleServer.get().getEventBus()
				.dispatchForAsync(FinishCurrentMapEvent.class)
				.dispatch(new FinishCurrentMapEvent(world.getWorldConfig().getUuid()))
				.thenAccept(_ -> commandContext.sendMessage(Message.raw("Finished current map.")));
	}
}