Commit ea0f8fd5 authored by Magix's avatar Magix Committed by GitHub
Browse files

Overhaul commands (not the system)

Added admin commands, refactored help command, and created command descriptions + more
parents 14da6c47 71cdd2c8
...@@ -7,9 +7,11 @@ import java.lang.annotation.RetentionPolicy; ...@@ -7,9 +7,11 @@ import java.lang.annotation.RetentionPolicy;
public @interface Command { public @interface Command {
String label() default ""; 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; Execution execution() default Execution.ALL;
......
...@@ -68,10 +68,12 @@ public final class CommandMap { ...@@ -68,10 +68,12 @@ public final class CommandMap {
* Returns a list of all registered commands. * Returns a list of all registered commands.
* @return All command handlers as a list. * @return All command handlers as a list.
*/ */
public List<CommandHandler> getHandlers() { public List<CommandHandler> getHandlersAsList() {
return new LinkedList<>(this.commands.values()); return new LinkedList<>(this.commands.values());
} }
public HashMap<String, CommandHandler> getHandlers() { return new LinkedHashMap<>(this.commands); }
/** /**
* Returns a handler by label/alias. * Returns a handler by label/alias.
* @param label The command label. * @param label The command label.
...@@ -111,8 +113,7 @@ public final class CommandMap { ...@@ -111,8 +113,7 @@ public final class CommandMap {
if(player != null) { if(player != null) {
String permissionNode = this.annotations.get(label).permission(); String permissionNode = this.annotations.get(label).permission();
Account account = player.getAccount(); Account account = player.getAccount();
List<String> permissions = account.getPermissions(); if(permissionNode != "" && !account.hasPermission(permissionNode)) {
if(!permissions.contains("*") && !permissions.contains(permissionNode)) {
CommandHandler.sendMessage(player, "You do not have permission to run this command."); return; CommandHandler.sendMessage(player, "You do not have permission to run this command."); return;
} }
} }
......
...@@ -31,7 +31,7 @@ import java.util.List; ...@@ -31,7 +31,7 @@ import java.util.List;
*/ */
public final class PlayerCommands { public final class PlayerCommands {
@Command(label = "give", aliases = {"g", "item", "giveitem"}, @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 { public static class GiveCommand implements CommandHandler {
@Override @Override
...@@ -148,8 +148,8 @@ public final class PlayerCommands { ...@@ -148,8 +148,8 @@ public final class PlayerCommands {
} }
@Command(label = "drop", aliases = {"d", "dropitem"}, @Command(label = "drop", aliases = {"d", "dropitem"},
usage = "Usage: drop <itemId|itemName> [amount]", usage = "drop <itemId|itemName> [amount]",
execution = Command.Execution.PLAYER) execution = Command.Execution.PLAYER, description = "Drops an item near you", permission = "server.drop")
public static class DropCommand implements CommandHandler { public static class DropCommand implements CommandHandler {
@Override @Override
...@@ -185,8 +185,8 @@ public final class PlayerCommands { ...@@ -185,8 +185,8 @@ public final class PlayerCommands {
} }
} }
@Command(label = "givechar", aliases = {"givec"}, @Command(label = "givechar", aliases = { "givec" }, usage = "givechar <playerId> <avatarId> [level]",
usage = "Usage: givechar <player|avatarId> [level|avatarId] [level]") description = "Gives the player a specified character", permission = "player.givechar")
public static class GiveCharCommand implements CommandHandler { public static class GiveCharCommand implements CommandHandler {
@Override public void execute(GenshinPlayer player, List<String> args) { @Override public void execute(GenshinPlayer player, List<String> args) {
int target, avatarId, level = 1, ascension = 1; int target, avatarId, level = 1, ascension = 1;
...@@ -305,7 +305,7 @@ public final class PlayerCommands { ...@@ -305,7 +305,7 @@ public final class PlayerCommands {
} }
@Command(label = "spawn", execution = Command.Execution.PLAYER, @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 { public static class SpawnCommand implements CommandHandler {
@Override @Override
...@@ -338,7 +338,7 @@ public final class PlayerCommands { ...@@ -338,7 +338,7 @@ public final class PlayerCommands {
} }
@Command(label = "killall", @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 { public static class KillAllCommand implements CommandHandler {
@Override @Override
...@@ -383,7 +383,8 @@ public final class PlayerCommands { ...@@ -383,7 +383,8 @@ public final class PlayerCommands {
} }
@Command(label = "resetconst", aliases = {"resetconstellation"}, @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 { public static class ResetConstellationCommand implements CommandHandler {
@Override @Override
...@@ -412,7 +413,7 @@ public final class PlayerCommands { ...@@ -412,7 +413,7 @@ public final class PlayerCommands {
} }
@Command(label = "godmode", @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 { public static class GodModeCommand implements CommandHandler {
@Override @Override
...@@ -423,7 +424,8 @@ public final class PlayerCommands { ...@@ -423,7 +424,8 @@ public final class PlayerCommands {
} }
@Command(label = "sethealth", aliases = {"sethp"}, @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 { public static class SetHealthCommand implements CommandHandler {
@Override @Override
...@@ -447,8 +449,9 @@ public final class PlayerCommands { ...@@ -447,8 +449,9 @@ public final class PlayerCommands {
} }
} }
@Command(label = "setworldlevel", aliases = {"setworldlvl"}, @Command(label = "setworldlevel", aliases = {"setworldlvl"}, usage = "setworldlevel <level>",
usage = "Usage: setworldlevel <level>", execution = Command.Execution.PLAYER) description = "Sets your world level (Relog to see proper effects)", permission = "player.setworldlevel",
execution = Command.Execution.PLAYER)
public static class SetWorldLevelCommand implements CommandHandler { public static class SetWorldLevelCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer player, List<String> args) { public void execute(GenshinPlayer player, List<String> args) {
...@@ -471,7 +474,8 @@ public final class PlayerCommands { ...@@ -471,7 +474,8 @@ public final class PlayerCommands {
} }
@Command(label = "clearartifacts", aliases = {"clearart"}, @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 { public static class ClearArtifactsCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer player, List<String> args) { public void execute(GenshinPlayer player, List<String> args) {
...@@ -485,7 +489,7 @@ public final class PlayerCommands { ...@@ -485,7 +489,7 @@ public final class PlayerCommands {
} }
@Command(label = "changescene", aliases = {"scene"}, @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 { public static class ChangeSceneCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer player, List<String> args) { public void execute(GenshinPlayer player, List<String> args) {
...@@ -505,4 +509,31 @@ public final class PlayerCommands { ...@@ -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.");
}
}
}
} }
package emu.grasscutter.commands; package emu.grasscutter.commands;
import com.mongodb.internal.connection.CommandHelper;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account; import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer; 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.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -12,7 +17,7 @@ import java.util.stream.Collectors; ...@@ -12,7 +17,7 @@ import java.util.stream.Collectors;
* A container for server-related commands. * A container for server-related commands.
*/ */
public final class ServerCommands { 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 { public static class ReloadCommand implements CommandHandler {
@Override @Override
...@@ -25,12 +30,88 @@ public final class ServerCommands { ...@@ -25,12 +30,88 @@ public final class ServerCommands {
@Override @Override
public void execute(GenshinPlayer player, List<String> args) { public void execute(GenshinPlayer player, List<String> args) {
CommandHandler.sendMessage(player, "Reloading config.");
this.execute(args); 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"}, @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 { public static class SendMessageCommand implements CommandHandler {
@Override @Override
...@@ -81,8 +162,8 @@ public final class ServerCommands { ...@@ -81,8 +162,8 @@ public final class ServerCommands {
} }
@Command(label = "account", @Command(label = "account",
usage = "Usage: account <create|delete> <username> [uid]", usage = "account <create|delete> <username> [uid]",
execution = Command.Execution.CONSOLE) description = "Modify user accounts", execution = Command.Execution.CONSOLE)
public static class AccountCommand implements CommandHandler { public static class AccountCommand implements CommandHandler {
@Override @Override
...@@ -126,8 +207,8 @@ public final class ServerCommands { ...@@ -126,8 +207,8 @@ public final class ServerCommands {
} }
@Command(label = "permission", @Command(label = "permission",
usage = "Usage: permission <add|remove> <username> <permission>", usage = "permission <add|remove> <username> <permission>",
execution = Command.Execution.CONSOLE) description = "Grants or removes a permission for a user", permission = "*")
public static class PermissionCommand implements CommandHandler { public static class PermissionCommand implements CommandHandler {
@Override @Override
...@@ -166,50 +247,93 @@ public final class ServerCommands { ...@@ -166,50 +247,93 @@ public final class ServerCommands {
} }
@Command(label = "help", @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 { public static class HelpCommand implements CommandHandler {
@Override @Override
public void execute(List<String> args) { public void execute(List<String> args) {
List<CommandHandler> handlers = CommandMap.getInstance().getHandlers(); this.execute(null, args);
List<Command> annotations = handlers.stream() }
.map(handler -> handler.getClass().getAnnotation(Command.class))
.collect(Collectors.toList());
@Override
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 1) { if(args.size() < 1) {
StringBuilder builder = new StringBuilder("Available commands:\n"); HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
annotations.forEach(annotation -> builder.append(annotation.usage()).append("\n")); List<Command> annotations = new ArrayList<Command>();
CommandHandler.sendMessage(null, builder.toString()); 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 { } else {
String command = args.get(0); String command = args.get(0);
CommandHandler handler = CommandMap.getInstance().getHandler(command); CommandHandler handler = CommandMap.getInstance().getHandler(command);
StringBuilder builder = new StringBuilder(player == null ? "\nHelp - " : "Help - ").append(command).append(": \n");
if(handler == null) { if(handler == null) {
CommandHandler.sendMessage(null, "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, builder.toString());
CommandHandler.sendMessage(null, annotation.usage());
} }
} }
@Override void SendAllHelpMessage(GenshinPlayer player, List<Command> annotations) {
public void execute(GenshinPlayer player, List<String> args) { if(player == null) {
List<CommandHandler> handlers = CommandMap.getInstance().getHandlers(); StringBuilder builder = new StringBuilder("\nAvailable commands:\n");
List<Command> annotations = handlers.stream() annotations.forEach(annotation -> {
.map(handler -> handler.getClass().getAnnotation(Command.class)) if (annotation.execution() != (player == null ? Command.Execution.PLAYER : Command.Execution.CONSOLE)) {
.collect(Collectors.toList()); 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(" ");
}
}
if(args.size() < 1) { builder.append("\n");
annotations.forEach(annotation -> player.dropMessage(annotation.usage())); }
});
CommandHandler.sendMessage(null, builder.toString());
} else { } else {
String command = args.get(0); CommandHandler.sendMessage(player, "Available commands:");
CommandHandler handler = CommandMap.getInstance().getHandler(command); annotations.forEach(annotation -> {
if(handler == null) { if (annotation.execution() != (player == null ? Command.Execution.PLAYER : Command.Execution.CONSOLE)) {
CommandHandler.sendMessage(player, "Command not found."); return; 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(" ");
}
} }
Command annotation = handler.getClass().getAnnotation(Command.class); CommandHandler.sendMessage(player, builder.toString());
CommandHandler.sendMessage(player, annotation.usage()); }
});
} }
} }
} }
......
...@@ -106,6 +106,10 @@ public class Account { ...@@ -106,6 +106,10 @@ public class Account {
this.permissions.add(permission); return true; 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) { public boolean removePermission(String permission) {
return this.permissions.remove(permission); return this.permissions.remove(permission);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment