Plugin.java 2.42 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
package emu.grasscutter.plugin;

import emu.grasscutter.Grasscutter;
4
import emu.grasscutter.plugin.api.ServerHook;
KingRainbow44's avatar
KingRainbow44 committed
5
6
import emu.grasscutter.server.game.GameServer;

7
8
9
import java.io.InputStream;
import java.net.URLClassLoader;

KingRainbow44's avatar
KingRainbow44 committed
10
11
12
13
/**
 * The base class for all plugins to extend.
 */
public abstract class Plugin {
14
15
    private final ServerHook server = ServerHook.getInstance();
    
KingRainbow44's avatar
KingRainbow44 committed
16
    private PluginIdentifier identifier;
17
    private URLClassLoader classLoader;
KingRainbow44's avatar
KingRainbow44 committed
18
19

    /**
KingRainbow44's avatar
KingRainbow44 committed
20
21
22
     * This method is reflected into.
     * 
     * Set plugin variables.
KingRainbow44's avatar
KingRainbow44 committed
23
24
     * @param identifier The plugin's identifier.
     */
25
    private void initializePlugin(PluginIdentifier identifier, URLClassLoader classLoader) {
KingRainbow44's avatar
KingRainbow44 committed
26
27
        if(this.identifier == null)
            this.identifier = identifier;
28
29
        if(this.classLoader == null)
            this.classLoader = classLoader;
KingRainbow44's avatar
KingRainbow44 committed
30
        else Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt.");
KingRainbow44's avatar
KingRainbow44 committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    }

    /**
     * The plugin's identifier instance.
     * @return An instance of {@link PluginIdentifier}.
     */
    public final PluginIdentifier getIdentifier(){
        return this.identifier;
    }

    /**
     * Get the plugin's name.
     */
    public final String getName() {
        return this.identifier.name;
    }

    /**
     * Get the plugin's description.
     */
    public final String getDescription() {
        return this.identifier.description;
    }

    /**
     * Get the plugin's version.
     */
    public final String getVersion() {
        return this.identifier.version;
    }

    /**
     * Returns the server that initialized the plugin.
     * @return A server instance.
     */
    public final GameServer getServer() {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        return this.server.getGameServer();
    }

    /**
     * Returns an input stream for a resource in the JAR file.
     * @param resourceName The name of the resource.
     * @return An input stream.
     */
    public final InputStream getResource(String resourceName) {
        return this.classLoader.getResourceAsStream(resourceName);
    }

    /**
     * Returns the server hook.
     * @return A server hook singleton.
     */
    public final ServerHook getHandle() {
        return this.server;
KingRainbow44's avatar
KingRainbow44 committed
85
86
87
88
89
90
91
92
93
    }
    
    /* Called when the plugin is first loaded. */
    public void onLoad() { }
    /* Called after (most of) the server enables. */
    public void onEnable() { }
    /* Called before the server disables. */
    public void onDisable() { }
}