CoopCommand.java 2.11 KB
Newer Older
Kengxxiao's avatar
Kengxxiao committed
1
2
package emu.grasscutter.command.commands;

方块君's avatar
方块君 committed
3
import emu.grasscutter.Grasscutter;
Kengxxiao's avatar
Kengxxiao committed
4
5
6
7
8
9
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;

import java.util.List;

10
@Command(label = "coop", usage = {"[<host UID>]"}, permission = "server.coop", permissionTargeted = "server.coop.others")
KingRainbow44's avatar
KingRainbow44 committed
11
public final class CoopCommand implements CommandHandler {
12

Kengxxiao's avatar
Kengxxiao committed
13
    @Override
AnimeGitB's avatar
AnimeGitB committed
14
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
15
16
17
        Player host = sender;
        switch (args.size()) {
            case 0:  // Summon target to self
18
19
                if (sender == null) { // Console doesn't have a self to summon to
                    sendUsageMessage(sender);
AnimeGitB's avatar
AnimeGitB committed
20
                    return;
21
                }
AnimeGitB's avatar
AnimeGitB committed
22
23
24
                break;
            case 1:  // Summon target to argument
                try {
AnimeGitB's avatar
AnimeGitB committed
25
26
                    int hostId = Integer.parseInt(args.get(0));
                    host = Grasscutter.getGameServer().getPlayerByUid(hostId);
AnimeGitB's avatar
AnimeGitB committed
27
                    if (host == null) {
28
                        CommandHandler.sendTranslatedMessage(sender, "commands.execution.player_offline_error");
AnimeGitB's avatar
AnimeGitB committed
29
30
31
                        return;
                    }
                    break;
AnimeGitB's avatar
AnimeGitB committed
32
                } catch (NumberFormatException ignored) {
33
                    CommandHandler.sendTranslatedMessage(sender, "commands.generic.invalid.uid");
AnimeGitB's avatar
AnimeGitB committed
34
35
36
                    return;
                }
            default:
37
                sendUsageMessage(sender);
AnimeGitB's avatar
AnimeGitB committed
38
                return;
Kengxxiao's avatar
Kengxxiao committed
39
        }
github-actions's avatar
github-actions committed
40

AnimeGitB's avatar
AnimeGitB committed
41
        // There's no target==host check but this just places them in multiplayer in their own world which seems fine.
AnimeGitB's avatar
AnimeGitB committed
42
        if (targetPlayer.isInMultiplayer()) {
43
            targetPlayer.getServer().getMultiplayerSystem().leaveCoop(targetPlayer);
Kengxxiao's avatar
Kengxxiao committed
44
        }
45
46
        host.getServer().getMultiplayerSystem().applyEnterMp(targetPlayer, host.getUid());
        targetPlayer.getServer().getMultiplayerSystem().applyEnterMpReply(host, targetPlayer.getUid(), true);
47
        CommandHandler.sendTranslatedMessage(sender, "commands.coop.success", targetPlayer.getNickname(), host.getNickname());
Kengxxiao's avatar
Kengxxiao committed
48
49
    }
}