Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Improved /servers command
Browse files Browse the repository at this point in the history
  • Loading branch information
Raft08 committed Jan 17, 2024
1 parent 1ccdf93 commit 6674cce
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package fr.atlasworld.network.core.console.commands;

import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import fr.atlasworld.network.api.command.Command;
import fr.atlasworld.network.api.command.CommandSource;
import fr.atlasworld.network.api.command.arguments.UuidArgumentType;
import fr.atlasworld.network.api.concurrent.action.FutureAction;
import fr.atlasworld.network.api.server.NetworkServer;
import fr.atlasworld.network.api.server.ServerManager;
import fr.atlasworld.network.api.server.entities.ServerBlueprint;
import fr.atlasworld.network.api.server.entities.ServerCreationRequest;

import java.util.Arrays;
import java.util.NoSuchElementException;
Expand All @@ -26,14 +29,37 @@ public static Command<CommandSource> create() {
.executes(ctx -> listServers(ctx.getSource(), ctx.getArgument("size", Integer.class)))
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("delete")
.then(RequiredArgumentBuilder.<CommandSource, UUID>argument("server_id", new UuidArgumentType())
.executes(ctx -> deleteServer(ctx.getSource(), ctx.getArgument("server_id", UUID.class)))
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("create")
.then(RequiredArgumentBuilder.<CommandSource, String>argument("blueprint", StringArgumentType.word())
.executes(ctx -> createServer(ctx.getSource(), ctx.getArgument("blueprint", String.class)))
.executes(ServersCommand::createServer)
.then(RequiredArgumentBuilder.<CommandSource, String>argument("name", StringArgumentType.word())
.executes(ServersCommand::createServer)
.then(RequiredArgumentBuilder.<CommandSource, Boolean>argument("dynamic", BoolArgumentType.bool())
.executes(ServersCommand::createServer)
)
)
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("purge")
.executes(ctx -> purgeServers(ctx.getSource(), false))
.then(RequiredArgumentBuilder.<CommandSource, String>argument("args", StringArgumentType.greedyString())
.executes(ctx -> purgeServers(ctx.getSource(), ctx.getArgument("args", String.class).contains("-y")))
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("manage")
.then(RequiredArgumentBuilder.<CommandSource, UUID>argument("server", new UuidArgumentType())
.then(LiteralArgumentBuilder.<CommandSource>literal("delete")
.executes(ctx -> deleteServer(ctx.getSource(), ctx.getArgument("server", UUID.class)))
)
.then(LiteralArgumentBuilder.<CommandSource>literal("start")
.executes(ctx -> manageServer(ctx.getSource(), ctx.getArgument("server", UUID.class), 1))
)
.then(LiteralArgumentBuilder.<CommandSource>literal("stop")
.executes(ctx -> manageServer(ctx.getSource(), ctx.getArgument("server", UUID.class), 2))
)
.then(LiteralArgumentBuilder.<CommandSource>literal("status")
.executes(ctx -> manageServer(ctx.getSource(), ctx.getArgument("server", UUID.class), 3))
)
)
)
);
Expand Down Expand Up @@ -63,10 +89,62 @@ private static int listServers(CommandSource source, int size) {
return Command.SINGLE_SUCCESS;
}

private static int createServer(CommandContext<CommandSource> ctx) {
final CommandSource source = ctx.getSource();
final String blueprintId = ctx.getArgument("blueprint", String.class);

source.sendMessage("Creating server..");

ServerManager serverManager = source.getServer().getServerManager();
ServerBlueprint blueprint = serverManager.getBlueprint(blueprintId);

if (blueprint == null) {
source.sendError("Could not find any blueprints with id '" + blueprintId + "'!");
return Command.SINGLE_SUCCESS;
}

ServerCreationRequest request = serverManager.createRequest(blueprint);

try {
request.name(ctx.getArgument("name", String.class));
request.dynamic(ctx.getArgument("dynamic", Boolean.class));
} catch (IllegalArgumentException e) {
request.dynamic(false);
}

FutureAction<NetworkServer> future = request.execute();
NetworkServer server = future.syncUninterruptibly();

if (future.success()) {
source.sendMessage("Successfully created new server.");
printServer(source, server);
return Command.SINGLE_SUCCESS;
}

source.sendError("Failed to create server: " + future.cause());
return -1;
}

private static int purgeServers(CommandSource source, boolean confirmed) {
if (!confirmed) {
source.sendMessage("WARNING: This will delete all servers created by AtlasNetwork!");
source.sendMessage("please add '-y' at the command to confirm execution.");
return Command.SINGLE_SUCCESS;
}

ServerManager serverManager = source.getServer().getServerManager();

for (NetworkServer server : serverManager.getServers()) {
deleteServer(source, server.identifier());
}

return Command.SINGLE_SUCCESS;
}

private static int deleteServer(CommandSource source, UUID serverId) {
ServerManager serverManager = source.getServer().getServerManager();

source.sendMessage("Deleting server '" + serverId + "'...");
source.sendMessage("Deleting server '" + serverId + "'..");

FutureAction<Void> future = serverManager.deleteServer(serverId.toString(), true);
future.syncUninterruptibly();
Expand All @@ -87,26 +165,31 @@ private static int deleteServer(CommandSource source, UUID serverId) {
return -1;
}

private static int createServer(CommandSource source, String blueprintId) {
private static int manageServer(CommandSource source, UUID serverId, int option) {
ServerManager serverManager = source.getServer().getServerManager();
ServerBlueprint blueprint = serverManager.getBlueprint(blueprintId);
NetworkServer server = serverManager.getServer(serverId.toString());

if (server != null) {
switch (option) {
case 1 -> {
source.sendMessage("Starting server '" + serverId + "'..");
server.start().syncUninterruptibly();
source.sendMessage("Server started!");
}
case 2 -> {
source.sendMessage("Stopping server '" + serverId + "'..");
server.stop().syncUninterruptibly();
source.sendMessage("Server stopped!");
}
case 3 -> source.sendMessage("Server status: " + server.getStatus());
default -> throw new IllegalArgumentException("Unknown option!");
}

if (blueprint == null) {
source.sendError("Could not find any blueprints with id '" + blueprintId + "'!");
return Command.SINGLE_SUCCESS;
}

FutureAction<NetworkServer> future = serverManager.createRequest(blueprint).execute();
NetworkServer server = future.syncUninterruptibly();

if (future.success()) {
source.sendMessage("Successfully created new server.");
printServer(source, server);
return Command.SINGLE_SUCCESS;
}

source.sendError("Failed to create server: " + future.cause());
return -1;
source.sendError("Could not find any server that matches '" + serverId + "'!");
return Command.SINGLE_SUCCESS;
}

private static void printServer(CommandSource source, NetworkServer server) {
Expand All @@ -115,6 +198,6 @@ private static void printServer(CommandSource source, NetworkServer server) {
source.sendMessage("Address: " + server.remoteAddress());
source.sendMessage("Tags: " + Arrays.toString(server.tags()));
source.sendMessage("Listed: " + server.shouldBeListed());
source.sendMessage("Manual: " + !server.isDynamicallyCreated() + "\n");
source.sendMessage("Manual: " + !server.isDynamicallyCreated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import fr.atlasworld.network.api.database.DatabaseEntityFactory;
import fr.atlasworld.network.api.database.exception.DatabaseDataTransformationException;
import fr.atlasworld.network.api.server.NetworkServer;
import fr.atlasworld.network.api.server.ServerManager;
import fr.atlasworld.network.api.server.entities.ServerBlueprint;
import fr.atlasworld.network.api.server.lifecycle.ServerStatus;
import fr.atlasworld.network.api.util.exception.NotImplementedException;
import fr.atlasworld.network.core.AtlasNetwork;
import fr.atlasworld.network.core.concurrent.action.PteroFutureAction;
import fr.atlasworld.network.core.logging.LogUtils;
Expand Down Expand Up @@ -252,6 +250,8 @@ public void updateStatus(ServerStatus status) {
ServerStatus oldStatus = this.status;
this.status = status;

LOGGER.trace("{}({}) changed status from {} to {}.", this.name, this.identifier, oldStatus, status);

AtlasNetwork.getInstance().getModuleManager().callEvent(new ServerStateChangedEventImpl(this, oldStatus, status));
}

Expand Down

0 comments on commit 6674cce

Please sign in to comment.