CoopCommand.java 2.37 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;

方块君's avatar
方块君 committed
12
@Command(label = "coop", usage = "coop [host UID]", permission = "server.coop", 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
		if (targetPlayer == null) {
18
			CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
19
20
21
22
23
24
			return;
		}

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