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

3
4
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem;
5
6
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
KingRainbow44's avatar
KingRainbow44 committed
7
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
8
import emu.grasscutter.server.game.GameServer;
9
10
import emu.grasscutter.server.http.HttpServer;
import emu.grasscutter.server.http.Router;
KingRainbow44's avatar
KingRainbow44 committed
11
12
13
14
15
16
17
18
19

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;
20
    private final GameServer gameServer;
21
    private final HttpServer httpServer;
KingRainbow44's avatar
KingRainbow44 committed
22
23
24
25
26
27
28
29
30
31
32

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

    /**
     * Hooks into a server.
33
     * @param gameServer The game server to hook into.
34
     * @param httpServer The HTTP server to hook into.
KingRainbow44's avatar
KingRainbow44 committed
35
     */
36
    public ServerHook(GameServer gameServer, HttpServer httpServer) {
37
        this.gameServer = gameServer;
38
        this.httpServer = httpServer;
KingRainbow44's avatar
KingRainbow44 committed
39

KingRainbow44's avatar
KingRainbow44 committed
40
41
42
        instance = this;
    }

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

    /**
51
     * @return The HTTP server.
52
     */
53
54
    public HttpServer getHttpServer() {
        return this.httpServer;
55
    }
KingRainbow44's avatar
KingRainbow44 committed
56

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

    /**
     * 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);
    }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

    /**
     * Adds a router using an instance of a class.
     * @param router A router instance.
     */
    public void addRouter(Router router) {
        this.addRouter(router.getClass());
    }

    /**
     * Adds a router using a class.
     * @param router The class of the router.
     */
    public void addRouter(Class<? extends Router> router) {
        this.httpServer.addRouter(router);
    }

    /**
     * Sets the server's authentication system.
     * @param authSystem An instance of the authentication system.
     */
    public void setAuthSystem(AuthenticationSystem authSystem) {
        Grasscutter.setAuthenticationSystem(authSystem);
    }
KingRainbow44's avatar
KingRainbow44 committed
100
}