ServerHook.java 2.13 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.plugin.api;

3
4
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
KingRainbow44's avatar
KingRainbow44 committed
5
import emu.grasscutter.game.player.Player;
6
import emu.grasscutter.server.dispatch.DispatchServer;
KingRainbow44's avatar
KingRainbow44 committed
7
8
9
10
11
12
13
14
15
16
import emu.grasscutter.server.game.GameServer;

import java.util.LinkedList;
import java.util.List;

/**
 * Hooks into the {@link GameServer} class, adding convenient ways to do certain things.
 */
public final class ServerHook {
    private static ServerHook instance;
17
18
    private final GameServer gameServer;
    private final DispatchServer dispatchServer;
KingRainbow44's avatar
KingRainbow44 committed
19
20
21
22
23
24
25
26
27
28
29

    /**
     * Gets the server hook instance.
     * @return A {@link ServerHook} singleton.
     */
    public static ServerHook getInstance() {
        return instance;
    }

    /**
     * Hooks into a server.
30
31
     * @param gameServer The game server to hook into.
     * @param dispatchServer The dispatch server to hook into.
KingRainbow44's avatar
KingRainbow44 committed
32
     */
33
34
35
    public ServerHook(GameServer gameServer, DispatchServer dispatchServer) {
        this.gameServer = gameServer;
        this.dispatchServer = dispatchServer;
KingRainbow44's avatar
KingRainbow44 committed
36

KingRainbow44's avatar
KingRainbow44 committed
37
38
39
        instance = this;
    }

40
41
42
43
44
45
46
47
48
49
50
51
52
    /**
     * @return The game server.
     */
    public GameServer getGameServer() {
        return this.gameServer;
    }

    /**
     * @return The dispatch server.
     */
    public DispatchServer getDispatchServer() {
        return this.dispatchServer;
    }
KingRainbow44's avatar
KingRainbow44 committed
53

KingRainbow44's avatar
KingRainbow44 committed
54
55
56
57
    /**
     * Gets all online players.
     * @return Players connected to the server.
     */
KingRainbow44's avatar
KingRainbow44 committed
58
    public List<Player> getOnlinePlayers() {
59
        return new LinkedList<>(this.gameServer.getPlayers().values());
KingRainbow44's avatar
KingRainbow44 committed
60
    }
61
62
63
64
65
66
67
68
69
70
71
72

    /**
     * Registers a command to the {@link emu.grasscutter.command.CommandMap}.
     * @param handler The command handler.
     */
    public void registerCommand(CommandHandler handler) {
        Class<? extends CommandHandler> clazz = handler.getClass();
        if(!clazz.isAnnotationPresent(Command.class))
            throw new IllegalArgumentException("Command handler must be annotated with @Command.");
        Command commandData = clazz.getAnnotation(Command.class);
        this.gameServer.getCommandMap().registerCommand(commandData.label(), handler);
    }
KingRainbow44's avatar
KingRainbow44 committed
73
}