Plugin.java 2.97 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;

KingRainbow44's avatar
KingRainbow44 committed
7
import java.io.File;
8
9
10
import java.io.InputStream;
import java.net.URLClassLoader;

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

    /**
KingRainbow44's avatar
KingRainbow44 committed
22
23
24
     * This method is reflected into.
     * 
     * Set plugin variables.
KingRainbow44's avatar
KingRainbow44 committed
25
26
     * @param identifier The plugin's identifier.
     */
27
    private void initializePlugin(PluginIdentifier identifier, URLClassLoader classLoader) {
KingRainbow44's avatar
KingRainbow44 committed
28
29
30
31
32
33
34
35
36
37
38
39
40
        if(this.identifier != null) {
            Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt.");
            return;
        }
        
        this.identifier = identifier;
        this.classLoader = classLoader;
        this.dataFolder = new File(Grasscutter.getConfig().PLUGINS_FOLDER, identifier.name);
        
        if(!this.dataFolder.exists() && !this.dataFolder.mkdirs()) {
            Grasscutter.getLogger().warn("Failed to create plugin data folder for " + this.identifier.name);
            return;
        }
KingRainbow44's avatar
KingRainbow44 committed
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
67
68
69
70
71
72
73
74
75
76
    }

    /**
     * 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() {
77
78
79
80
81
82
83
84
85
86
87
88
        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);
    }

KingRainbow44's avatar
KingRainbow44 committed
89
90
91
92
93
94
95
96
    /**
     * Returns a directory where plugins can store data files.
     * @return A directory on the file system.
     */
    public final File getDataFolder() {
        return this.dataFolder;
    }

97
98
99
100
101
102
    /**
     * Returns the server hook.
     * @return A server hook singleton.
     */
    public final ServerHook getHandle() {
        return this.server;
KingRainbow44's avatar
KingRainbow44 committed
103
104
105
106
107
108
109
110
111
    }
    
    /* 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() { }
}