CoopCommand.java 2.34 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
11
import static emu.grasscutter.utils.Language.translate;

tester233's avatar
tester233 committed
12
@Command(label = "coop", usage = "coop <playerId> <target playerId>", permission = "server.coop", permissionTargeted = "server.coop.others", description = "commands.coop.description")
KingRainbow44's avatar
KingRainbow44 committed
13
public final class CoopCommand implements CommandHandler {
14

Kengxxiao's avatar
Kengxxiao committed
15
    @Override
AnimeGitB's avatar
AnimeGitB committed
16
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
17
18
19
        Player host = sender;
        switch (args.size()) {
            case 0:  // Summon target to self
Secretboy's avatar
Secretboy committed
20
                CommandHandler.sendMessage(sender, translate(sender, "commands.coop.usage"));
21
                if (sender == null) // Console doesn't have a self to summon to
AnimeGitB's avatar
AnimeGitB committed
22
                    return;
AnimeGitB's avatar
AnimeGitB committed
23
24
25
                break;
            case 1:  // Summon target to argument
                try {
AnimeGitB's avatar
AnimeGitB committed
26
27
                    int hostId = Integer.parseInt(args.get(0));
                    host = Grasscutter.getGameServer().getPlayerByUid(hostId);
AnimeGitB's avatar
AnimeGitB committed
28
                    if (host == null) {
Secretboy's avatar
Secretboy committed
29
                        CommandHandler.sendMessage(sender, translate(sender, "commands.execution.player_offline_error"));
AnimeGitB's avatar
AnimeGitB committed
30
31
32
                        return;
                    }
                    break;
AnimeGitB's avatar
AnimeGitB committed
33
                } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
34
                    CommandHandler.sendMessage(sender, translate(sender, "commands.execution.uid_error"));
AnimeGitB's avatar
AnimeGitB committed
35
36
37
                    return;
                }
            default:
Secretboy's avatar
Secretboy committed
38
                CommandHandler.sendMessage(sender, translate(sender, "commands.coop.usage"));
AnimeGitB's avatar
AnimeGitB committed
39
                return;
Kengxxiao's avatar
Kengxxiao committed
40
        }
KingRainbow44's avatar
KingRainbow44 committed
41
        
AnimeGitB's avatar
AnimeGitB committed
42
        // 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
43
        if (targetPlayer.isInMultiplayer()) {
AnimeGitB's avatar
AnimeGitB committed
44
            targetPlayer.getServer().getMultiplayerManager().leaveCoop(targetPlayer);
Kengxxiao's avatar
Kengxxiao committed
45
        }
AnimeGitB's avatar
AnimeGitB committed
46
47
        host.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid());
        targetPlayer.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true);
Secretboy's avatar
Secretboy committed
48
        CommandHandler.sendMessage(sender, translate(sender, "commands.coop.success", targetPlayer.getNickname(), host.getNickname()));
Kengxxiao's avatar
Kengxxiao committed
49
50
    }
}