Overhaul commands (not the system)

Added admin commands, refactored help command, and created command descriptions + more
This commit is contained in:
Magix 2022-04-19 14:08:11 -04:00 committed by GitHub
commit 59d01209f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 218 additions and 56 deletions

View File

@ -7,9 +7,11 @@ import java.lang.annotation.RetentionPolicy;
public @interface Command {
String label() default "";
String usage() default "";
String usage() default "No usage specified";
String[] aliases() default {""};
String description() default "No description specified";
String[] aliases() default {};
Execution execution() default Execution.ALL;

View File

@ -68,10 +68,12 @@ public final class CommandMap {
* Returns a list of all registered commands.
* @return All command handlers as a list.
*/
public List<CommandHandler> getHandlers() {
public List<CommandHandler> getHandlersAsList() {
return new LinkedList<>(this.commands.values());
}
public HashMap<String, CommandHandler> getHandlers() { return new LinkedHashMap<>(this.commands); }
/**
* Returns a handler by label/alias.
* @param label The command label.
@ -111,8 +113,7 @@ public final class CommandMap {
if(player != null) {
String permissionNode = this.annotations.get(label).permission();
Account account = player.getAccount();
List<String> permissions = account.getPermissions();
if(!permissions.contains("*") && !permissions.contains(permissionNode)) {
if(permissionNode != "" && !account.hasPermission(permissionNode)) {
CommandHandler.sendMessage(player, "You do not have permission to run this command."); return;
}
}

View File

@ -31,7 +31,7 @@ import java.util.List;
*/
public final class PlayerCommands {
@Command(label = "give", aliases = {"g", "item", "giveitem"},
usage = "Usage: give [player] <itemId|itemName> [amount]")
usage = "give [player] <itemId|itemName> [amount]", description = "Gives an item to you or the specified player", permission = "player.give")
public static class GiveCommand implements CommandHandler {
@Override
@ -148,8 +148,8 @@ public final class PlayerCommands {
}
@Command(label = "drop", aliases = {"d", "dropitem"},
usage = "Usage: drop <itemId|itemName> [amount]",
execution = Command.Execution.PLAYER)
usage = "drop <itemId|itemName> [amount]",
execution = Command.Execution.PLAYER, description = "Drops an item near you", permission = "server.drop")
public static class DropCommand implements CommandHandler {
@Override
@ -185,8 +185,8 @@ public final class PlayerCommands {
}
}
@Command(label = "givechar", aliases = {"givec"},
usage = "Usage: givechar <player|avatarId> [level|avatarId] [level]")
@Command(label = "givechar", aliases = { "givec" }, usage = "givechar <playerId> <avatarId> [level]",
description = "Gives the player a specified character", permission = "player.givechar")
public static class GiveCharCommand implements CommandHandler {
@Override public void execute(GenshinPlayer player, List<String> args) {
int target, avatarId, level = 1, ascension = 1;
@ -305,7 +305,7 @@ public final class PlayerCommands {
}
@Command(label = "spawn", execution = Command.Execution.PLAYER,
usage = "Usage: spawn <entityId|entityName> [level] [amount]")
usage = "spawn <entityId|entityName> [level] [amount]", description = "Spawns an entity near you", permission = "server.spawn")
public static class SpawnCommand implements CommandHandler {
@Override
@ -338,7 +338,7 @@ public final class PlayerCommands {
}
@Command(label = "killall",
usage = "Usage: killall [playerUid] [sceneId]")
usage = "killall [playerUid] [sceneId]", description = "Kill all entities", permission = "server.killall")
public static class KillAllCommand implements CommandHandler {
@Override
@ -383,7 +383,8 @@ public final class PlayerCommands {
}
@Command(label = "resetconst", aliases = {"resetconstellation"},
usage = "Usage: resetconst [all]", execution = Command.Execution.PLAYER)
usage = "resetconst [all]", execution = Command.Execution.PLAYER, permission = "player.resetconstellation",
description = "Resets the constellation level on your current active character, will need to relog after using the command to see any changes.")
public static class ResetConstellationCommand implements CommandHandler {
@Override
@ -412,7 +413,7 @@ public final class PlayerCommands {
}
@Command(label = "godmode",
usage = "Usage: godmode", execution = Command.Execution.PLAYER)
usage = "godmode", execution = Command.Execution.PLAYER, description = "Prevents you from taking damage", permission = "player.godmode")
public static class GodModeCommand implements CommandHandler {
@Override
@ -423,7 +424,8 @@ public final class PlayerCommands {
}
@Command(label = "sethealth", aliases = {"sethp"},
usage = "Usage: sethealth <hp>", execution = Command.Execution.PLAYER)
usage = "sethealth <hp>", execution = Command.Execution.PLAYER, description = "Sets your health to the specified value",
permission = "player.sethealth")
public static class SetHealthCommand implements CommandHandler {
@Override
@ -447,8 +449,9 @@ public final class PlayerCommands {
}
}
@Command(label = "setworldlevel", aliases = {"setworldlvl"},
usage = "Usage: setworldlevel <level>", execution = Command.Execution.PLAYER)
@Command(label = "setworldlevel", aliases = {"setworldlvl"}, usage = "setworldlevel <level>",
description = "Sets your world level (Relog to see proper effects)", permission = "player.setworldlevel",
execution = Command.Execution.PLAYER)
public static class SetWorldLevelCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
@ -471,7 +474,8 @@ public final class PlayerCommands {
}
@Command(label = "clearartifacts", aliases = {"clearart"},
usage = "Usage: clearartifacts", execution = Command.Execution.PLAYER)
usage = "clearartifacts", execution = Command.Execution.PLAYER, permission = "player.clearartifacts",
description = "Deletes all unequipped and unlocked level 0 artifacts, including yellow rarity ones from your inventory")
public static class ClearArtifactsCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
@ -485,7 +489,7 @@ public final class PlayerCommands {
}
@Command(label = "changescene", aliases = {"scene"},
usage = "Usage: changescene <scene id>", execution = Command.Execution.PLAYER)
usage = "changescene <scene id>", description = "Changes your scene", permission = "player.changescene", execution = Command.Execution.PLAYER)
public static class ChangeSceneCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
@ -505,4 +509,31 @@ public final class PlayerCommands {
}
}
}
@Command(label = "sendservermessage", aliases = {"sendservmsg"},
usage = "sendservermessage <player> <message>", description = "Sends a message to a player as the server",
execution = Command.Execution.PLAYER, permission = "server.sendmessage")
public static class SendServerMessageCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: sendmessage <player> <message>"); return;
}
try {
int target = Integer.parseInt(args.get(0));
String message = String.join(" ", args.subList(1, args.size()));
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(null, "Player not found."); return;
}
targetPlayer.dropMessage(message);
CommandHandler.sendMessage(null, "Message sent.");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid player ID.");
}
}
}
}

View File

@ -1,10 +1,15 @@
package emu.grasscutter.commands;
import com.mongodb.internal.connection.CommandHelper;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.server.packet.send.PacketSceneKickPlayerRsp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@ -12,7 +17,7 @@ import java.util.stream.Collectors;
* A container for server-related commands.
*/
public final class ServerCommands {
@Command(label = "reload", usage = "Usage: reload")
@Command(label = "reload", usage = "reload", description = "Reload server config", permission = "server.reload")
public static class ReloadCommand implements CommandHandler {
@Override
@ -25,12 +30,88 @@ public final class ServerCommands {
@Override
public void execute(GenshinPlayer player, List<String> args) {
CommandHandler.sendMessage(player, "Reloading config.");
this.execute(args);
CommandHandler.sendMessage(player, "Reload complete.");
}
}
@Command(label = "kick", usage = "kick <player>", description = "Kicks the specified player from the server (WIP)", permission = "server.kick")
public static class KickCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
this.execute(null, args);
}
@Override
public void execute(GenshinPlayer player, List<String> args) {
int target = Integer.parseInt(args.get(0));
String message = String.join(" ", args.subList(1, args.size()));
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(player, "Player not found.");
return;
}
if(player != null) {
CommandHandler.sendMessage(null, String.format("Player [%s:%s] has kicked player [%s:%s]", player.getAccount().getPlayerId(), player.getAccount().getUsername(), target, targetPlayer.getAccount().getUsername()));
}
CommandHandler.sendMessage(player, String.format("Kicking player [%s:%s]", target, targetPlayer.getAccount().getUsername()));
targetPlayer.getSession().close();
}
}
@Command(label = "stop", usage = "stop", description = "Stops the server", permission = "server.stop")
public static class StopCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
Grasscutter.getLogger().info("Server shutting down...");
for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
p.dropMessage("Server shutting down...");
}
System.exit(1);
}
}
@Command(label = "broadcast", aliases = {"b"},
usage = "broadcast <message>", description = "Sends a message to all the players", permission = "server.broadcast")
public static class BroadcastCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
if(args.size() < 1) {
CommandHandler.sendMessage(null, "Usage: broadcast <message>"); return;
}
String message = String.join(" ", args.subList(0, args.size()));
for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
p.dropMessage(message);
}
CommandHandler.sendMessage(null, "Message sent.");
}
@Override
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 1) {
CommandHandler.sendMessage(player, "Usage: broadcast <message>"); return;
}
String message = String.join(" ", args.subList(0, args.size()));
for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
p.dropMessage(message);
}
CommandHandler.sendMessage(player, "Message sent.");
}
}
@Command(label = "sendmessage", aliases = {"sendmsg", "msg"},
usage = "Usage: sendmessage <player> <message>")
usage = "sendmessage <player> <message>", description = "Sends a message to a player")
public static class SendMessageCommand implements CommandHandler {
@Override
@ -81,8 +162,8 @@ public final class ServerCommands {
}
@Command(label = "account",
usage = "Usage: account <create|delete> <username> [uid]",
execution = Command.Execution.CONSOLE)
usage = "account <create|delete> <username> [uid]",
description = "Modify user accounts", execution = Command.Execution.CONSOLE)
public static class AccountCommand implements CommandHandler {
@Override
@ -126,8 +207,8 @@ public final class ServerCommands {
}
@Command(label = "permission",
usage = "Usage: permission <add|remove> <username> <permission>",
execution = Command.Execution.CONSOLE)
usage = "permission <add|remove> <username> <permission>",
description = "Grants or removes a permission for a user", permission = "*")
public static class PermissionCommand implements CommandHandler {
@Override
@ -166,50 +247,93 @@ public final class ServerCommands {
}
@Command(label = "help",
usage = "Usage: help [command]")
usage = "help [command]", description = "Sends the help message or shows information about a specified command")
public static class HelpCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
List<CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = handlers.stream()
.map(handler -> handler.getClass().getAnnotation(Command.class))
.collect(Collectors.toList());
if(args.size() < 1) {
StringBuilder builder = new StringBuilder("Available commands:\n");
annotations.forEach(annotation -> builder.append(annotation.usage()).append("\n"));
CommandHandler.sendMessage(null, builder.toString());
} else {
String command = args.get(0);
CommandHandler handler = CommandMap.getInstance().getHandler(command);
if(handler == null) {
CommandHandler.sendMessage(null, "Command not found."); return;
}
Command annotation = handler.getClass().getAnnotation(Command.class);
CommandHandler.sendMessage(null, annotation.usage());
}
this.execute(null, args);
}
@Override
public void execute(GenshinPlayer player, List<String> args) {
List<CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = handlers.stream()
.map(handler -> handler.getClass().getAnnotation(Command.class))
.collect(Collectors.toList());
if(args.size() < 1) {
annotations.forEach(annotation -> player.dropMessage(annotation.usage()));
HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = new ArrayList<Command>();
for(String key : handlers.keySet()) {
Command annotation = handlers.get(key).getClass().getAnnotation(Command.class);
if(!Arrays.asList(annotation.aliases()).contains(key)) {
if(player != null && annotation.permission() != "" && !player.getAccount().hasPermission(annotation.permission())) continue;
annotations.add(annotation);
}
}
SendAllHelpMessage(player, annotations);
} else {
String command = args.get(0);
CommandHandler handler = CommandMap.getInstance().getHandler(command);
StringBuilder builder = new StringBuilder(player == null ? "\nHelp - " : "Help - ").append(command).append(": \n");
if(handler == null) {
CommandHandler.sendMessage(player, "Command not found."); return;
builder.append("No command found.");
} else {
Command annotation = handler.getClass().getAnnotation(Command.class);
builder.append(" ").append(annotation.description()).append("\n");
builder.append(" Usage: ").append(annotation.usage());
if(annotation.aliases().length >= 1) {
builder.append("\n").append(" Aliases: ");
for (String alias : annotation.aliases()) {
builder.append(alias).append(" ");
}
}
if(player != null && annotation.permission() != "" && !player.getAccount().hasPermission(annotation.permission())) {
builder.append("\n Warning: You do not have permission to run this command.");
}
}
Command annotation = handler.getClass().getAnnotation(Command.class);
CommandHandler.sendMessage(player, annotation.usage());
CommandHandler.sendMessage(player, builder.toString());
}
}
void SendAllHelpMessage(GenshinPlayer player, List<Command> annotations) {
if(player == null) {
StringBuilder builder = new StringBuilder("\nAvailable commands:\n");
annotations.forEach(annotation -> {
if (annotation.execution() != (player == null ? Command.Execution.PLAYER : Command.Execution.CONSOLE)) {
builder.append(annotation.label()).append("\n");
builder.append(" ").append(annotation.description()).append("\n");
builder.append(" Usage: ").append(annotation.usage());
if (annotation.aliases().length >= 1) {
builder.append("\n").append(" Aliases: ");
for (String alias : annotation.aliases()) {
builder.append(alias).append(" ");
}
}
builder.append("\n");
}
});
CommandHandler.sendMessage(null, builder.toString());
} else {
CommandHandler.sendMessage(player, "Available commands:");
annotations.forEach(annotation -> {
if (annotation.execution() != (player == null ? Command.Execution.PLAYER : Command.Execution.CONSOLE)) {
StringBuilder builder = new StringBuilder(annotation.label()).append("\n");
builder.append(" ").append(annotation.description()).append("\n");
builder.append(" Usage: ").append(annotation.usage());
if (annotation.aliases().length >= 1) {
builder.append("\n").append(" Aliases: ");
for (String alias : annotation.aliases()) {
builder.append(alias).append(" ");
}
}
CommandHandler.sendMessage(player, builder.toString());
}
});
}
}
}

View File

@ -106,6 +106,10 @@ public class Account {
this.permissions.add(permission); return true;
}
public boolean hasPermission(String permission) {
return this.permissions.contains(permission) || this.permissions.contains("*") ? true : false;
}
public boolean removePermission(String permission) {
return this.permissions.remove(permission);
}