Commit 63cb0a81 authored by Jaida Wu's avatar Jaida Wu
Browse files

Rewrite commands


Signed-off-by: default avatarJaida Wu <mlgmxyysd@meowcat.org>
parent 087cd680
package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import java.util.List;
@Command(label = "say", usage = "say <player> <message>", description = "Sends a message to a player as the server",
aliases = {"sendservmsg", "sendservermessage", "sendmessage"}, permission = "server.sendmessage")
public class SendMessage implements CommandHandler {
@Override
public void onCommand(GenshinPlayer sender, 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(sender, "Player not found.");
return;
}
CommandHandler.sendMessage(targetPlayer, message);
CommandHandler.sendMessage(sender, "Message sent.");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, "Invalid player ID.");
}
}
}
\ No newline at end of file
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
import java.util.List;
@Command(label = "sethealth", usage = "sethealth <hp>",
description = "Sets your health to the specified value", aliases = {"sethp"}, permission = "player.sethealth")
public class SetHealth implements CommandHandler {
@Override
public void onCommand(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
return; // TODO: set player's health from console or other players
}
if (args.size() < 1) {
CommandHandler.sendMessage(null, "Usage: sethealth <hp>");
return;
}
try {
int health = Integer.parseInt(args.get(0));
EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity();
if (entity == null) {
return;
}
entity.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, health);
entity.getWorld().broadcastPacket(new PacketEntityFightPropUpdateNotify(entity, FightProperty.FIGHT_PROP_CUR_HP));
sender.dropMessage("Health set to " + health + ".");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid health value.");
}
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.props.PlayerProperty;
import java.util.List;
@Command(label = "setworldlevel", usage = "setworldlevel <level>",
description = "Sets your world level (Relog to see proper effects)",
aliases = {"setworldlvl"}, permission = "player.setworldlevel")
public class SetWorldLevel implements CommandHandler {
@Override
public void onCommand(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
return; // TODO: set player's world level from console or other players
}
if (args.size() < 1) {
CommandHandler.sendMessage(sender, "Usage: setworldlevel <level>");
return;
}
try {
int level = Integer.parseInt(args.get(0));
// Set in both world and player props
sender.getWorld().setWorldLevel(level);
sender.setProperty(PlayerProperty.PROP_PLAYER_WORLD_LEVEL, level);
sender.dropMessage("World level set to " + level + ".");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid world level.");
}
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.utils.Position;
import java.util.List;
@Command(label = "spawn", usage = "spawn <entityId|entityName> [level] [amount]",
description = "Spawns an entity near you", permission = "server.spawn")
public class Spawn implements CommandHandler {
@Override
public void onCommand(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
return;
}
if (args.size() < 1) {
CommandHandler.sendMessage(sender, "Usage: spawn <entityId|entityName> [amount]");
return;
}
try {
int entity = Integer.parseInt(args.get(0));
int level = args.size() > 1 ? Integer.parseInt(args.get(1)) : 1;
int amount = args.size() > 2 ? Integer.parseInt(args.get(2)) : 1;
MonsterData entityData = GenshinData.getMonsterDataMap().get(entity);
if (entityData == null) {
CommandHandler.sendMessage(sender, "Invalid entity id.");
return;
}
float range = (5f + (.1f * amount));
for (int i = 0; i < amount; i++) {
Position pos = sender.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityMonster monster = new EntityMonster(sender.getScene(), entityData, pos, level);
sender.getScene().addEntity(monster);
}
CommandHandler.sendMessage(sender, String.format("Spawned %s of %s.", amount, entity));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, "Invalid item or player ID.");
}
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import java.util.List;
@Command(label = "stop", usage = "stop",
description = "Stops the server", permission = "server.stop")
public class Stop implements CommandHandler {
@Override
public void onCommand(GenshinPlayer sender, List<String> args) {
CommandHandler.sendMessage(null, "Server shutting down...");
for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
CommandHandler.sendMessage(p, "Server shutting down...");
}
System.exit(1);
}
}
package emu.grasscutter.commands;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Command {
String label() default "";
String usage() default "No usage specified";
String description() default "No description specified";
String[] aliases() default {};
Execution execution() default Execution.ALL;
String permission() default "";
enum Execution {
ALL,
CONSOLE,
PLAYER
}
}
package emu.grasscutter.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.AvatarSkillDepotData;
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.GenshinScene;
import emu.grasscutter.game.World;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.entity.EntityItem;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.game.inventory.GenshinItem;
import emu.grasscutter.game.inventory.Inventory;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
import emu.grasscutter.server.packet.send.PacketItemAddHintNotify;
import emu.grasscutter.utils.Position;
import java.util.LinkedList;
import java.util.List;
/**
* A container for player-related commands.
*/
public final class PlayerCommands {
@Command(label = "give", aliases = {"g", "item", "giveitem"},
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
public void execute(GenshinPlayer player, List<String> args) {
int target, item, amount = 1;
switch(args.size()) {
default:
CommandHandler.sendMessage(player, "Usage: give <player> <itemId|itemName> [amount]");
return;
case 1:
try {
item = Integer.parseInt(args.get(0));
target = player.getAccount().getPlayerId();
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(player, "Invalid item id.");
return;
}
break;
case 2:
try {
target = Integer.parseInt(args.get(0));
if(Grasscutter.getGameServer().getPlayerByUid(target) == null) {
target = player.getUid(); amount = Integer.parseInt(args.get(1));
item = Integer.parseInt(args.get(0));
} else {
item = Integer.parseInt(args.get(1));
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(player, "Invalid item or player ID.");
return;
}
break;
case 3:
try {
target = Integer.parseInt(args.get(0));
if(Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(player, "Invalid player ID."); return;
}
item = Integer.parseInt(args.get(1));
amount = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(player, "Invalid item or player ID.");
return;
}
break;
}
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(player, "Player not found."); return;
}
ItemData itemData = GenshinData.getItemDataMap().get(item);
if(itemData == null) {
CommandHandler.sendMessage(player, "Invalid item id."); return;
}
this.item(targetPlayer, itemData, amount);
}
/**
* give [player] [itemId|itemName] [amount]
*/
@Override public void execute(List<String> args) {
if(args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: give <player> <itemId|itemName> [amount]");
return;
}
try {
int target = Integer.parseInt(args.get(0));
int item = Integer.parseInt(args.get(1));
int amount = 1; if(args.size() > 2) amount = Integer.parseInt(args.get(2));
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(null, "Player not found."); return;
}
ItemData itemData = GenshinData.getItemDataMap().get(item);
if(itemData == null) {
CommandHandler.sendMessage(null, "Invalid item id."); return;
}
this.item(targetPlayer, itemData, amount);
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid item or player ID.");
}
}
private void item(GenshinPlayer player, ItemData itemData, int amount) {
GenshinItem genshinItem = new GenshinItem(itemData);
if(itemData.isEquip()) {
List<GenshinItem> items = new LinkedList<>();
for(int i = 0; i < amount; i++) {
items.add(genshinItem);
} player.getInventory().addItems(items);
player.sendPacket(new PacketItemAddHintNotify(items, ActionReason.SubfieldDrop));
} else {
genshinItem.setCount(amount);
player.getInventory().addItem(genshinItem);
player.sendPacket(new PacketItemAddHintNotify(genshinItem, ActionReason.SubfieldDrop));
}
}
}
@Command(label = "drop", aliases = {"d", "dropitem"},
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
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 1) {
CommandHandler.sendMessage(player, "Usage: drop <itemId|itemName> [amount]");
return;
}
try {
int item = Integer.parseInt(args.get(0));
int amount = 1; if(args.size() > 1) amount = Integer.parseInt(args.get(1));
ItemData itemData = GenshinData.getItemDataMap().get(item);
if(itemData == null) {
CommandHandler.sendMessage(player, "Invalid item id."); return;
}
if (itemData.isEquip()) {
float range = (5f + (.1f * amount));
for (int i = 0; i < amount; i++) {
Position pos = player.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityItem entity = new EntityItem(player.getScene(), player, itemData, pos, 1);
player.getScene().addEntity(entity);
}
} else {
EntityItem entity = new EntityItem(player.getScene(), player, itemData, player.getPos().clone().addY(3f), amount);
player.getScene().addEntity(entity);
}
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(player, "Invalid item or player ID.");
}
}
}
@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;
if(args.size() < 1) {
CommandHandler.sendMessage(player, "Usage: givechar <player> <avatarId> [level]");
return;
}
switch(args.size()) {
default:
CommandHandler.sendMessage(player, "Usage: givechar <player> <avatarId> [level]");
return;
case 2:
try {
target = Integer.parseInt(args.get(0));
if(Grasscutter.getGameServer().getPlayerByUid(target) == null) {
target = player.getUid();
level = Integer.parseInt(args.get(1));
avatarId = Integer.parseInt(args.get(0));
} else {
avatarId = Integer.parseInt(args.get(1));
}
} catch (NumberFormatException ignored) {
// TODO: Parse from avatar name using GM Handbook.
CommandHandler.sendMessage(player, "Invalid avatar or player ID.");
return;
}
break;
case 3:
try {
target = Integer.parseInt(args.get(0));
if(Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(player, "Invalid player ID."); return;
}
avatarId = Integer.parseInt(args.get(1));
level = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
// TODO: Parse from avatar name using GM Handbook.
CommandHandler.sendMessage(player, "Invalid avatar or player ID.");
return;
}
break;
}
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(player, "Player not found."); return;
}
AvatarData avatarData = GenshinData.getAvatarDataMap().get(avatarId);
if(avatarData == null) {
CommandHandler.sendMessage(player, "Invalid avatar id."); return;
}
// Calculate ascension level.
if (level <= 40) {
ascension = (int) Math.ceil(level / 20f);
} else {
ascension = (int) Math.ceil(level / 10f) - 3;
}
GenshinAvatar avatar = new GenshinAvatar(avatarId);
avatar.setLevel(level);
avatar.setPromoteLevel(ascension);
// This will handle stats and talents
avatar.recalcStats();
targetPlayer.addAvatar(avatar);
}
@Override
public void execute(List<String> args) {
if(args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: givechar <player> <itemId|itemName> [amount]");
return;
}
try {
int target = Integer.parseInt(args.get(0));
int avatarID = Integer.parseInt(args.get(1));
int level = 1; if(args.size() > 2) level = Integer.parseInt(args.get(2));
int ascension;
GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
if(targetPlayer == null) {
CommandHandler.sendMessage(null, "Player not found."); return;
}
AvatarData avatarData = GenshinData.getAvatarDataMap().get(avatarID);
if(avatarData == null) {
CommandHandler.sendMessage(null, "Invalid avatar id."); return;
}
// Calculate ascension level.
if (level <= 40) {
ascension = (int) Math.ceil(level / 20f);
} else {
ascension = (int) Math.ceil(level / 10f) - 3;
}
GenshinAvatar avatar = new GenshinAvatar(avatarID);
avatar.setLevel(level);
avatar.setPromoteLevel(ascension);
// This will handle stats and talents
avatar.recalcStats();
targetPlayer.addAvatar(avatar);
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid item or player ID.");
}
}
}
@Command(label = "spawn", execution = Command.Execution.PLAYER,
usage = "spawn <entityId|entityName> [level] [amount]", description = "Spawns an entity near you", permission = "server.spawn")
public static class SpawnCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 1) {
CommandHandler.sendMessage(null, "Usage: spawn <entityId|entityName> [amount]");
return;
}
try {
int entity = Integer.parseInt(args.get(0));
int level = 1; if(args.size() > 1) level = Integer.parseInt(args.get(1));
int amount = 1; if(args.size() > 2) amount = Integer.parseInt(args.get(2));
MonsterData entityData = GenshinData.getMonsterDataMap().get(entity);
if(entityData == null) {
CommandHandler.sendMessage(null, "Invalid entity id."); return;
}
float range = (5f + (.1f * amount));
for (int i = 0; i < amount; i++) {
Position pos = player.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityMonster monster = new EntityMonster(player.getScene(), entityData, pos, level);
player.getScene().addEntity(monster);
}
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid item or player ID.");
}
}
}
@Command(label = "killall",
usage = "killall [playerUid] [sceneId]", description = "Kill all entities", permission = "server.killall")
public static class KillAllCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
GenshinScene scene = player.getScene();
scene.getEntities().values().stream()
.filter(entity -> entity instanceof EntityMonster)
.forEach(entity -> scene.killEntity(entity, 0));
CommandHandler.sendMessage(null, "Killing all monsters in scene " + scene.getId());
}
@Override
public void execute(List<String> args) {
if(args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: killall [playerUid] [sceneId]"); return;
}
try {
int playerUid = Integer.parseInt(args.get(0));
int sceneId = Integer.parseInt(args.get(1));
GenshinPlayer player = Grasscutter.getGameServer().getPlayerByUid(playerUid);
if (player == null) {
CommandHandler.sendMessage(null, "Player not found or offline.");
return;
}
GenshinScene scene = player.getWorld().getSceneById(sceneId);
if (scene == null) {
CommandHandler.sendMessage(null, "Scene not found in player world");
return;
}
scene.getEntities().values().stream()
.filter(entity -> entity instanceof EntityMonster)
.forEach(entity -> scene.killEntity(entity, 0));
CommandHandler.sendMessage(null, "Killing all monsters in scene " + scene.getId());
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid arguments.");
}
}
}
@Command(label = "resetconst", aliases = {"resetconstellation"},
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
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() > 0 && args.get(0).equalsIgnoreCase("all")) {
player.getAvatars().forEach(this::resetConstellation);
player.dropMessage("Reset all avatars' constellations.");
} else {
EntityAvatar entity = player.getTeamManager().getCurrentAvatarEntity();
if(entity == null)
return;
GenshinAvatar avatar = entity.getAvatar();
this.resetConstellation(avatar);
player.dropMessage("Constellations for " + avatar.getAvatarData().getName() + " have been reset. Please relog to see changes.");
}
}
private void resetConstellation(GenshinAvatar avatar) {
avatar.getTalentIdList().clear();
avatar.setCoreProudSkillLevel(0);
avatar.recalcStats();
avatar.save();
}
}
@Command(label = "godmode",
usage = "godmode", execution = Command.Execution.PLAYER, description = "Prevents you from taking damage", permission = "player.godmode")
public static class GodModeCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer player, List<String> args) {
player.setGodmode(!player.inGodmode());
player.dropMessage("Godmode is now " + (player.inGodmode() ? "enabled" : "disabled") + ".");
}
}
@Command(label = "sethealth", aliases = {"sethp"},
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
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 1) {
CommandHandler.sendMessage(null, "Usage: sethealth <hp>"); return;
}
try {
int health = Integer.parseInt(args.get(0));
EntityAvatar entity = player.getTeamManager().getCurrentAvatarEntity();
if(entity == null)
return;
entity.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, health);
entity.getWorld().broadcastPacket(new PacketEntityFightPropUpdateNotify(entity, FightProperty.FIGHT_PROP_CUR_HP));
player.dropMessage("Health set to " + health + ".");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid health value.");
}
}
}
@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) {
if(args.size() < 1) {
CommandHandler.sendMessage(player, "Usage: setworldlevel <level>"); return;
}
try {
int level = Integer.parseInt(args.get(0));
// Set in both world and player props
player.getWorld().setWorldLevel(level);
player.setProperty(PlayerProperty.PROP_PLAYER_WORLD_LEVEL, level);
player.dropMessage("World level set to " + level + ".");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid world level.");
}
}
}
@Command(label = "clearartifacts", aliases = {"clearart"},
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) {
Inventory playerInventory = player.getInventory();
playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_RELIQUARY)
.filter(item -> item.getLevel() == 1 && item.getExp() == 0)
.filter(item -> !item.isLocked() && !item.isEquipped())
.forEach(item -> playerInventory.removeItem(item, item.getCount()));
}
}
@Command(label = "changescene", aliases = {"scene"},
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) {
if(args.size() < 1) {
CommandHandler.sendMessage(player, "Usage: changescene <scene id>"); return;
}
try {
int sceneId = Integer.parseInt(args.get(0));
boolean result = player.getWorld().transferPlayerToScene(player, sceneId, player.getPos());
if (!result) {
CommandHandler.sendMessage(null, "Scene does not exist or you are already in it");
}
} catch (Exception e) {
CommandHandler.sendMessage(player, "Usage: changescene <scene id>"); return;
}
}
}
@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;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import java.util.*;
/**
* A container for server-related commands.
*/
public final class ServerCommands {
@Command(label = "reload", usage = "reload", description = "Reload server config", permission = "server.reload")
public static class ReloadCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
Grasscutter.getLogger().info("Reloading config.");
Grasscutter.loadConfig();
Grasscutter.getDispatchServer().loadQueries();
Grasscutter.getLogger().info("Reload complete.");
}
@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));
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 = "sendmessage <player> <message>", description = "Sends a message to a player")
public static class SendMessageCommand implements CommandHandler {
@Override
public void execute(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.");
}
}
@Override
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 2) {
CommandHandler.sendMessage(player, "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(player, "Player not found."); return;
}
targetPlayer.sendMessage(player, message);
CommandHandler.sendMessage(player, "Message sent.");
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(player, "Invalid player ID.");
}
}
}
@Command(label = "account",
usage = "account <create|delete> <username> [uid]",
description = "Modify user accounts", execution = Command.Execution.CONSOLE)
public static class AccountCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
if(args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]"); return;
}
String action = args.get(0);
String username = args.get(1);
switch(action) {
default:
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]");
return;
case "create":
int uid = 0;
if(args.size() > 2) {
try {
uid = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid UID."); return;
}
}
Account account = DatabaseHelper.createAccountWithId(username, uid);
if(account == null) {
CommandHandler.sendMessage(null, "Account already exists."); return;
} else {
CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerId() + ".");
account.addPermission("*"); // Grant the player superuser permissions.
}
return;
case "delete":
if(DatabaseHelper.deleteAccount(username)) {
CommandHandler.sendMessage(null, "Account deleted."); return;
} else CommandHandler.sendMessage(null, "Account not found.");
return;
}
}
}
@Command(label = "permission",
usage = "permission <add|remove> <username> <permission>",
description = "Grants or removes a permission for a user", permission = "*")
public static class PermissionCommand implements CommandHandler {
@Override
public void execute(List<String> args) {
if(args.size() < 3) {
CommandHandler.sendMessage(null, "Usage: permission <add|remove> <username> <permission>"); return;
}
String action = args.get(0);
String username = args.get(1);
String permission = args.get(2);
Account account = Grasscutter.getGameServer().getAccountByName(username);
if(account == null) {
CommandHandler.sendMessage(null, "Account not found."); return;
}
switch(action) {
default:
CommandHandler.sendMessage(null, "Usage: permission <add|remove> <username> <permission>");
break;
case "add":
if(account.addPermission(permission)) {
CommandHandler.sendMessage(null, "Permission added.");
} else CommandHandler.sendMessage(null, "They already have this permission!");
break;
case "remove":
if(account.removePermission(permission)) {
CommandHandler.sendMessage(null, "Permission removed.");
} else CommandHandler.sendMessage(null, "They don't have this permission!");
break;
}
account.save();
}
}
@Command(label = "help",
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) {
this.execute(null, args);
}
@Override
public void execute(GenshinPlayer player, List<String> args) {
if(args.size() < 1) {
HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
List<Command> annotations = new ArrayList<>();
for(String key : handlers.keySet()) {
Command annotation = handlers.get(key).getClass().getAnnotation(Command.class);
if(!Arrays.asList(annotation.aliases()).contains(key)) {
if(player != null && !Objects.equals(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) {
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 && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission())) {
builder.append("\n Warning: You do not have permission to run this command.");
}
}
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() != Command.Execution.PLAYER) {
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() != 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());
}
});
}
}
}
}
package emu.grasscutter.game.managers;
import emu.grasscutter.commands.CommandMap;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.net.packet.GenshinPacket;
import emu.grasscutter.server.game.GameServer;
......
......@@ -6,7 +6,7 @@ import java.util.concurrent.ConcurrentHashMap;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.commands.CommandMap;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
......
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